Planning session implementation. Removed Bridge module

This commit is contained in:
Fedor Isakov 2015-12-03 12:15:34 +01:00
parent db3fc2b069
commit 80b2cd5e4c
9 changed files with 209 additions and 163 deletions

View File

@ -3,7 +3,6 @@
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/API/API.iml" filepath="$PROJECT_DIR$/API/API.iml" />
<module fileurl="file://$PROJECT_DIR$/Bridge/Bridge.iml" filepath="$PROJECT_DIR$/Bridge/Bridge.iml" />
<module fileurl="file://$PROJECT_DIR$/Core/Core.iml" filepath="$PROJECT_DIR$/Core/Core.iml" />
<module fileurl="file://$PROJECT_DIR$/Test/Test.iml" filepath="$PROJECT_DIR$/Test/Test.iml" />
</modules>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="API" />
<orderEntry type="library" scope="RUNTIME" name="org.jetbrains.kotlin:kotlin-runtime:1.0.0-beta-2423" level="project" />
<orderEntry type="library" name="com.intellij:annotations:12.0" level="project" />
</component>
</module>

View File

@ -1,39 +0,0 @@
package jetbrains.mps.logic.reactor.handler;
import jetbrains.mps.logic.reactor.constraint.ComputingTracer;
import jetbrains.mps.logic.reactor.rule.Rule;
import org.jetbrains.annotations.NotNull;
/**
* @author Fedor Isakov
*/
public abstract class RuleHandler {
public static Factory with() {
return ourCompanion.createFactory();
}
public interface Factory {
RuleHandler newHandler();
}
public abstract void setRules(@NotNull Iterable<? extends Rule> rules);
@NotNull
public abstract Iterable<? extends Rule> rules();
/**
* SPI
*/
protected interface Companion {
Factory createFactory();
}
protected static void setOurCompanion(@NotNull Companion companion) {
if (ourCompanion != null) throw new IllegalStateException("ourCompanion has already been set");
ourCompanion = companion;
}
private static Companion ourCompanion = null;
}

View File

@ -8,7 +8,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="API" exported="" />
<orderEntry type="module" module-name="Bridge" />
<orderEntry type="library" scope="RUNTIME" name="org.jetbrains.kotlin:kotlin-runtime:1.0.0-beta-2423" level="project" />
<orderEntry type="library" name="com.intellij:annotations:12.0" level="project" />
</component>

View File

@ -0,0 +1,122 @@
package jetbrains.mps.logicl.reactor.core
/**
* @author Fedor Isakov
*/
import jetbrains.mps.logic.reactor.constraint.*
import jetbrains.mps.logic.reactor.program.PlanningSession
import jetbrains.mps.logic.reactor.rule.InvalidConstraintException
import jetbrains.mps.logic.reactor.rule.Rule
import java.util.*
import java.util.Collections.*
class ReactorPlanningSession(val name: String) : PlanningSession() {
class FactoryImpl : PlanningSession.Factory {
override fun create(name: String): PlanningSession = ReactorPlanningSession(name)
}
private val myRules = ArrayList<Rule>()
private val myRegistry = ConstraintRegistry()
override fun name(): String = name
override fun constraintSymbols(): Iterable<ConstraintSymbol> =
myRegistry.constraintSymbols()
override fun constraintArgumentTypes(constraintSymbol: ConstraintSymbol): List<Class<*>> =
myRegistry.constraintArgTypes(constraintSymbol)
override fun predicateSymbols(): Iterable<PredicateSymbol> =
myRegistry.predicateSymbols()
override fun solverClass(predicateSymbol: PredicateSymbol): Class<out Queryable>? =
myRegistry.solverClass(predicateSymbol)
override fun addRule(rule: Rule) {
myRegistry.update(rule)
myRules.add(rule)
}
override fun addRules(rules: Collection<Rule>) {
for (r in rules) {
myRegistry.update(r)
}
myRules.addAll(rules)
}
override fun rules(): Iterable<Rule> = unmodifiableCollection(myRules)
companion object {
val ourFactory = FactoryImpl()
fun init() {
setFactory(ourFactory)
}
fun deinit() {
clearFactory(ourFactory)
}
}
}
private class ConstraintRegistry {
private val myConstraintArgTypes = HashMap<ConstraintSymbol, List<Class<*>>>()
private val myPredicateSolvers = HashMap<PredicateSymbol, Class<out Queryable>>()
fun update(rule: Rule) {
for(item in rule.all()) {
checkValid(item)
introduce(item)
}
}
fun checkValid(item: AndItem) {
if (item.symbol().arity() != item.arguments().size) {
throw InvalidConstraintException("arity mismatch: ${item.symbol()}")
}
for (arg in item.arguments()) {
if (arg == null) {
throw InvalidConstraintException("argument is null: ${item.symbol()}")
}
}
when(item) {
is Constraint -> {
if (myConstraintArgTypes.containsKey(item.symbol())) {
if (!item.argumentTypes().equals(myConstraintArgTypes.get(item.symbol()))) {
throw InvalidConstraintException("argument types mismatch: ${item.symbol()}")
}
}
}
is Predicate -> {
// nothing
}
else -> throw InvalidConstraintException("unknown item: ${item}")
}
}
fun introduce(item: AndItem) {
when(item) {
is Constraint -> myConstraintArgTypes.put(item.symbol(), item.argumentTypes())
is Predicate -> myPredicateSolvers.put(item.symbol(), item.solverClass())
else -> throw InvalidConstraintException("unknown item: ${item}")
}
}
fun constraintSymbols(): Iterable<ConstraintSymbol> =
unmodifiableSet(myConstraintArgTypes.keys)
fun constraintArgTypes(symbol: ConstraintSymbol): List<Class<*>> =
unmodifiableList(myConstraintArgTypes.getOrDefault(symbol, emptyList()))
fun predicateSymbols(): Iterable<PredicateSymbol> =
unmodifiableSet(myPredicateSolvers.keys)
fun solverClass(symbol: PredicateSymbol): Class<out Queryable> =
myPredicateSolvers.getOrDefault(symbol, throw NoSuchElementException())
}

View File

@ -1,37 +0,0 @@
package jetbrains.mps.logicl.reactor.core
/**
* @author Fedor Isakov
*/
import jetbrains.mps.logic.reactor.handler.RuleHandler
import jetbrains.mps.logic.reactor.rule.Rule
import java.util.*
class FactoryImpl : RuleHandler.Factory {
override fun newHandler(): RuleHandler = RuleHandlerImpl()
}
class RuleHandlerImpl : RuleHandler() {
val myRules = ArrayList<Rule>()
override fun setRules(rules: Iterable<Rule>) {
for (rule in rules) {
myRules.add(rule)
}
}
override fun rules(): MutableIterable<Rule> {
return myRules
}
companion object {
fun init() {
setOurCompanion (object : RuleHandler.Companion {
override fun createFactory(): Factory = FactoryImpl()
})
}
}
}

View File

@ -10,7 +10,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit:junit:4.12" level="project" />
<orderEntry type="module" module-name="Core" scope="TEST" />
<orderEntry type="module" module-name="Bridge" scope="TEST" />
<orderEntry type="library" scope="RUNTIME" name="org.jetbrains.kotlin:kotlin-runtime:1.0.0-beta-2423" level="project" />
<orderEntry type="library" name="org.jetbrains.kotlin:kotlin-stdlib:1.0.0-beta-2423" level="project" />
<orderEntry type="library" name="com.intellij:annotations:12.0" level="project" />

View File

@ -0,0 +1,87 @@
import jetbrains.mps.logic.reactor.program.PlanningSession
import jetbrains.mps.logic.reactor.rule.InvalidConstraintException
import jetbrains.mps.logic.reactor.rule.InvalidRuleException
import jetbrains.mps.logicl.reactor.core.ReactorPlanningSession
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import java.util.*
import kotlin.reflect.KClass
import kotlin.test.assertEquals
import kotlin.test.expect
/**
* @author Fedor Isakov
*/
class Simple {
companion object {
@BeforeClass @JvmStatic fun setup() {
ReactorPlanningSession.init();
}
}
@Before fun beforeTest() {
session = PlanningSession.newSession("test")
}
lateinit var session:PlanningSession
@Test fun empty() {
session.addRules(ArrayList())
}
@Test(expected = InvalidRuleException::class)
fun emptyBody() {
session.addRules(arrayListOf(
rule("foo",
headKept(
constraint("bar")
))
))
assertEquals(session.rules().count(), 1)
}
@Test
fun replace() {
session.addRules(arrayListOf(
rule("foo",
headReplaced(
constraint("bar")
),
body(
constraint("baz")
)),
rule("qux",
headReplaced(
constraint("bar")
),
headKept(
constraint("quux")
),
body(
constraint("blah")
))
))
assertEquals(session.rules().count(), 2)
}
@Test(expected = InvalidConstraintException::class)
fun fail() {
session.addRules(arrayListOf(
rule("foo",
headReplaced(
constraint("bar", 1)
),
body(
constraint("bar", "1")
))
))
}
}

View File

@ -1,70 +0,0 @@
import jetbrains.mps.logic.reactor.handler.RuleHandler
import jetbrains.mps.logic.reactor.rule.InvalidRuleException
import jetbrains.mps.logicl.reactor.core.RuleHandlerImpl
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import java.util.*
import kotlin.reflect.KClass
import kotlin.test.assertEquals
import kotlin.test.expect
/**
* @author Fedor Isakov
*/
class Simple {
companion object {
@BeforeClass @JvmStatic fun setup() {
RuleHandlerImpl.init()
}
}
@Before fun beforeTest() {
handler = RuleHandler.with().newHandler()
}
lateinit var handler:RuleHandler
@Test fun empty() {
handler.setRules(ArrayList())
}
@Test(expected = InvalidRuleException::class)
fun emptyBody() {
handler.setRules(arrayListOf(
rule("foo",
headKept(
constraint("bar")
))
))
}
@Test
fun replace() {
handler.setRules(arrayListOf(
rule("foo",
headReplaced(
constraint("bar")
),
body(
constraint("baz")
))
))
val sb = StringBuilder()
handler.rules().forEach {
sb.append(it.tag()).append(": ")
it.all().joinTo(sb, ", ")
sb.append(";")
}
assertEquals("foo", sb.toString())
}
}