From e07f608b04a60d6fd213dc8805cd1755142df6e1 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Thu, 3 Dec 2015 21:26:56 +0100 Subject: [PATCH] Implementing EvaluationSession --- ..._kotlin_kotlin_runtime_1_0_0_beta_2423.xml | 6 +- reactor/Core/Core.iml | 2 +- .../reactor/core/ReactorEvaluationSession.kt | 86 +++++++++++++++++++ .../reactor/core/ReactorPlanningSession.kt | 18 ++-- .../reactor/predicate/ReactorSessionSolver.kt | 23 +++++ reactor/Test/test/TestPlanningSession.kt | 5 +- 6 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorEvaluationSession.kt rename reactor/Core/src/jetbrains/mps/{logicl => logic}/reactor/core/ReactorPlanningSession.kt (85%) create mode 100644 reactor/Core/src/jetbrains/mps/logic/reactor/predicate/ReactorSessionSolver.kt diff --git a/reactor/.idea/libraries/org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_2423.xml b/reactor/.idea/libraries/org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_2423.xml index c9dc2521..46203338 100644 --- a/reactor/.idea/libraries/org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_2423.xml +++ b/reactor/.idea/libraries/org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_2423.xml @@ -2,9 +2,11 @@ - + - + + + \ No newline at end of file diff --git a/reactor/Core/Core.iml b/reactor/Core/Core.iml index c8c1f6cf..37a1f7f3 100644 --- a/reactor/Core/Core.iml +++ b/reactor/Core/Core.iml @@ -8,7 +8,7 @@ - + diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorEvaluationSession.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorEvaluationSession.kt new file mode 100644 index 00000000..4611e0d5 --- /dev/null +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorEvaluationSession.kt @@ -0,0 +1,86 @@ +package jetbrains.mps.logic.reactor.core + +import jetbrains.mps.logic.reactor.constraint.* +import jetbrains.mps.logic.reactor.predicate.ReactorSessionSolver +import jetbrains.mps.logic.reactor.program.EvaluationSession +import java.util.* + +/** + * @author Fedor Isakov + */ + + +private class ReactorEvaluationSession(val sessionSolver: SessionSolver) : EvaluationSession() { + + class Config : EvaluationSession.Config() { + + val myPredicateSymbols = ArrayList() + val myParameters = HashMap() + var myComputingTracer : ComputingTracer? = null + + override fun withPredicates(vararg predicateSymbols: PredicateSymbol): EvaluationSession.Config { + myPredicateSymbols.addAll(Arrays.asList(* predicateSymbols)) + return this + } + + override fun withTrace(computingTracer: ComputingTracer?): EvaluationSession.Config { + myComputingTracer = computingTracer + return this + } + + override fun withParam(key: String, param: Any?): EvaluationSession.Config { + myParameters.put(key, param) + return this + } + + override fun start(): EvaluationSession { + var session = ourBackend.ourSession.get() + if (session != null) throw IllegalStateException("session already active") + + val predicateSymbols = myPredicateSymbols.toArray(arrayOfNulls(myPredicateSymbols.size)) + val sessionSolver = ReactorSessionSolver() + sessionSolver.init(myComputingTracer, * predicateSymbols) + + session = ReactorEvaluationSession(sessionSolver) + ourBackend.ourSession.set(session) + return session + } + } + + override fun sessionSolver(): SessionSolver? { + throw UnsupportedOperationException() + } + + override fun constraintSymbols(): MutableIterable? { + throw UnsupportedOperationException() + } + + override fun constraintOccurrences(): MutableIterable? { + throw UnsupportedOperationException() + } + + override fun constraintOccurrences(symbol: ConstraintSymbol?): MutableIterable? { + throw UnsupportedOperationException() + } + + private class Backend : EvaluationSession.Backend { + + val ourSession = ThreadLocal() + + override fun current(): EvaluationSession = ourSession.get() ?: throw IllegalStateException("no session") + + override fun createConfig(): EvaluationSession.Config = Config() + } + + companion object { + private val ourBackend = Backend() + + fun init() { + setBackend(ourBackend) + } + + fun deinit () { + clearBackend(ourBackend) + } + } +} \ No newline at end of file diff --git a/reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorPlanningSession.kt similarity index 85% rename from reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt rename to reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorPlanningSession.kt index 51a0f530..9b676497 100644 --- a/reactor/Core/src/jetbrains/mps/logicl/reactor/core/ReactorPlanningSession.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/ReactorPlanningSession.kt @@ -1,4 +1,4 @@ -package jetbrains.mps.logicl.reactor.core +package jetbrains.mps.logic.reactor.core /** * @author Fedor Isakov @@ -12,15 +12,16 @@ import java.util.* import java.util.Collections.* -class ReactorPlanningSession(val name: String) : PlanningSession() { +class ReactorPlanningSession(val name: String, val sessionSolver: SessionSolver) : PlanningSession() { - class FactoryImpl : PlanningSession.Factory { - override fun create(name: String): PlanningSession = ReactorPlanningSession(name) + class FactoryImpl : Factory { + override fun create(name: String, sessionSolver: SessionSolver): PlanningSession = + ReactorPlanningSession(name, sessionSolver) } private val myRules = ArrayList() - private val myRegistry = ConstraintRegistry() + private val myRegistry = ConstraintRegistry(sessionSolver) override fun name(): String = name @@ -61,9 +62,10 @@ class ReactorPlanningSession(val name: String) : PlanningSession() { clearFactory(ourFactory) } } + } -private class ConstraintRegistry { +private class ConstraintRegistry(val sessionSolver: SessionSolver) { private val myConstraintArgTypes = HashMap>>() @@ -93,7 +95,7 @@ private class ConstraintRegistry { } } } - is Predicate -> { + is Predicate -> { // nothing } else -> throw InvalidConstraintException("unknown item: ${item}") @@ -103,7 +105,7 @@ private class ConstraintRegistry { fun introduce(item: AndItem) { when(item) { is Constraint -> myConstraintArgTypes.put(item.symbol(), item.argumentTypes()) - is Predicate -> myPredicateSolvers.put(item.symbol(), item.solverClass()) + is Predicate -> myPredicateSolvers.put(item.symbol(), sessionSolver.solverClass(item.symbol())) else -> throw InvalidConstraintException("unknown item: ${item}") } } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/predicate/ReactorSessionSolver.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/predicate/ReactorSessionSolver.kt new file mode 100644 index 00000000..61e3d66f --- /dev/null +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/predicate/ReactorSessionSolver.kt @@ -0,0 +1,23 @@ +package jetbrains.mps.logic.reactor.predicate + +import jetbrains.mps.logic.reactor.constraint.* + +/** + * @author Fedor Isakov + */ + +class ReactorSessionSolver : SessionSolver() { + + override fun solverClass(predicateSymbol: PredicateSymbol?): Class? { + throw UnsupportedOperationException() + } + + override fun registerSymbol(predicateSymbol: PredicateSymbol, computingTracer: ComputingTracer?) { + when (predicateSymbol) { + + } + + throw UnsupportedOperationException() + } + +} diff --git a/reactor/Test/test/TestPlanningSession.kt b/reactor/Test/test/TestPlanningSession.kt index 3cf5be05..d82a4995 100644 --- a/reactor/Test/test/TestPlanningSession.kt +++ b/reactor/Test/test/TestPlanningSession.kt @@ -1,7 +1,7 @@ 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 jetbrains.mps.logic.reactor.core.ReactorPlanningSession import org.junit.Before import org.junit.BeforeClass import org.junit.Test @@ -23,12 +23,11 @@ class Simple { } @Before fun beforeTest() { - session = PlanningSession.newSession("test") + session = PlanningSession.newSession("test", null) } lateinit var session:PlanningSession - @Test fun empty() { session.addRules(ArrayList()) }