From 80b2cd5e4c4ca34ba5915be244aba02c51842223 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Thu, 3 Dec 2015 12:15:34 +0100 Subject: [PATCH] Planning session implementation. Removed Bridge module --- reactor/.idea/modules.xml | 1 - reactor/Bridge/Bridge.iml | 14 -- .../logic/reactor/handler/RuleHandler.java | 39 ------ reactor/Core/Core.iml | 1 - .../reactor/core/ReactorPlanningSession.kt | 122 ++++++++++++++++++ .../mps/logicl/reactor/core/RuleHandler.kt | 37 ------ reactor/Test/Test.iml | 1 - reactor/Test/test/TestPlanningSession.kt | 87 +++++++++++++ reactor/Test/test/TestRuleHandler.kt | 70 ---------- 9 files changed, 209 insertions(+), 163 deletions(-) delete mode 100644 reactor/Bridge/Bridge.iml delete mode 100644 reactor/Bridge/src/jetbrains/mps/logic/reactor/handler/RuleHandler.java create mode 100644 reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt delete mode 100644 reactor/Core/src/jetbrains/mps/logicl/reactor/core/RuleHandler.kt create mode 100644 reactor/Test/test/TestPlanningSession.kt delete mode 100644 reactor/Test/test/TestRuleHandler.kt diff --git a/reactor/.idea/modules.xml b/reactor/.idea/modules.xml index a129b638..8a083989 100644 --- a/reactor/.idea/modules.xml +++ b/reactor/.idea/modules.xml @@ -3,7 +3,6 @@ - diff --git a/reactor/Bridge/Bridge.iml b/reactor/Bridge/Bridge.iml deleted file mode 100644 index a95a8f8a..00000000 --- a/reactor/Bridge/Bridge.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/reactor/Bridge/src/jetbrains/mps/logic/reactor/handler/RuleHandler.java b/reactor/Bridge/src/jetbrains/mps/logic/reactor/handler/RuleHandler.java deleted file mode 100644 index 8029cf48..00000000 --- a/reactor/Bridge/src/jetbrains/mps/logic/reactor/handler/RuleHandler.java +++ /dev/null @@ -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 rules); - - @NotNull - public abstract Iterable 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; -} - diff --git a/reactor/Core/Core.iml b/reactor/Core/Core.iml index a71db188..b7e80927 100644 --- a/reactor/Core/Core.iml +++ b/reactor/Core/Core.iml @@ -8,7 +8,6 @@ - diff --git a/reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt b/reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt new file mode 100644 index 00000000..51a0f530 --- /dev/null +++ b/reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt @@ -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() + + private val myRegistry = ConstraintRegistry() + + override fun name(): String = name + + override fun constraintSymbols(): Iterable = + myRegistry.constraintSymbols() + + override fun constraintArgumentTypes(constraintSymbol: ConstraintSymbol): List> = + myRegistry.constraintArgTypes(constraintSymbol) + + override fun predicateSymbols(): Iterable = + myRegistry.predicateSymbols() + + override fun solverClass(predicateSymbol: PredicateSymbol): Class? = + myRegistry.solverClass(predicateSymbol) + + override fun addRule(rule: Rule) { + myRegistry.update(rule) + myRules.add(rule) + } + + override fun addRules(rules: Collection) { + for (r in rules) { + myRegistry.update(r) + } + myRules.addAll(rules) + } + + override fun rules(): Iterable = unmodifiableCollection(myRules) + + companion object { + val ourFactory = FactoryImpl() + + fun init() { + setFactory(ourFactory) + } + + fun deinit() { + clearFactory(ourFactory) + } + } +} + +private class ConstraintRegistry { + + private val myConstraintArgTypes = HashMap>>() + + private val myPredicateSolvers = HashMap>() + + 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 = + unmodifiableSet(myConstraintArgTypes.keys) + + fun constraintArgTypes(symbol: ConstraintSymbol): List> = + unmodifiableList(myConstraintArgTypes.getOrDefault(symbol, emptyList())) + + fun predicateSymbols(): Iterable = + unmodifiableSet(myPredicateSolvers.keys) + + fun solverClass(symbol: PredicateSymbol): Class = + myPredicateSolvers.getOrDefault(symbol, throw NoSuchElementException()) +} diff --git a/reactor/Core/src/jetbrains/mps/logicl/reactor/core/RuleHandler.kt b/reactor/Core/src/jetbrains/mps/logicl/reactor/core/RuleHandler.kt deleted file mode 100644 index 584e5048..00000000 --- a/reactor/Core/src/jetbrains/mps/logicl/reactor/core/RuleHandler.kt +++ /dev/null @@ -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() - - override fun setRules(rules: Iterable) { - for (rule in rules) { - myRules.add(rule) - } - } - - override fun rules(): MutableIterable { - return myRules - } - - companion object { - fun init() { - setOurCompanion (object : RuleHandler.Companion { - override fun createFactory(): Factory = FactoryImpl() - }) - } - } -} - diff --git a/reactor/Test/Test.iml b/reactor/Test/Test.iml index 9812f8e6..6aee143e 100644 --- a/reactor/Test/Test.iml +++ b/reactor/Test/Test.iml @@ -10,7 +10,6 @@ - diff --git a/reactor/Test/test/TestPlanningSession.kt b/reactor/Test/test/TestPlanningSession.kt new file mode 100644 index 00000000..3cf5be05 --- /dev/null +++ b/reactor/Test/test/TestPlanningSession.kt @@ -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") + )) + )) + } +} + + + + diff --git a/reactor/Test/test/TestRuleHandler.kt b/reactor/Test/test/TestRuleHandler.kt deleted file mode 100644 index f38f5677..00000000 --- a/reactor/Test/test/TestRuleHandler.kt +++ /dev/null @@ -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()) - } -} - - - -