From 7dcd683ba5074a942f51b72db696119a78d64964 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Fri, 26 Apr 2019 00:18:03 +0200 Subject: [PATCH] Enable EvaluationSession to have arbitrary parameters available at exec time. Drop two obsolete interfaces. --- .../logic/reactor/core/EvaluationSessionEx.kt | 7 +- .../core/internal/EvaluationSessionImpl.kt | 29 +++-- .../reactor/evaluation/EvaluationSession.java | 113 ++++++++++-------- .../reactor/evaluation/FailureHandler.java | 45 ------- .../reactor/evaluation/SessionSolver.java | 63 ---------- reactor/Test/test/TestController.kt | 2 +- 6 files changed, 83 insertions(+), 176 deletions(-) delete mode 100644 reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/FailureHandler.java delete mode 100644 reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionSolver.java diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/EvaluationSessionEx.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/EvaluationSessionEx.kt index 34f5d86b..04844523 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/EvaluationSessionEx.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/EvaluationSessionEx.kt @@ -19,7 +19,6 @@ package jetbrains.mps.logic.reactor.core import jetbrains.mps.logic.reactor.evaluation.EvaluationSession import jetbrains.mps.logic.reactor.evaluation.EvaluationTrace import jetbrains.mps.logic.reactor.evaluation.PredicateInvocation -import jetbrains.mps.logic.reactor.evaluation.SessionSolver import jetbrains.mps.logic.reactor.program.Program /** @@ -28,13 +27,15 @@ import jetbrains.mps.logic.reactor.program.Program */ abstract class EvaluationSessionEx(val program: Program, val trace: EvaluationTrace, - val sessionSolver: SessionSolver? = null) : EvaluationSession() { + val params: Map, *>?) : EvaluationSession() +{ abstract fun controller(): Controller override fun program(): Program = program - override fun sessionSolver(): SessionSolver = sessionSolver ?: SessionSolver() + @Suppress("UNCHECKED_CAST") + override fun parameter(key: ParameterKey): T? = params ?.get(key) as T override fun ask(invocation: PredicateInvocation): Boolean { val solver = invocation.predicate().symbol().solver() diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt index a25c79e0..3f81fbde 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt @@ -33,7 +33,7 @@ import com.github.andrewoma.dexx.collection.List as PList internal class EvaluationSessionImpl private constructor ( program: Program, trace: EvaluationTrace, - sessionSolver: SessionSolver? = null) : EvaluationSessionEx(program, trace, sessionSolver) + params: Map, *>?) : EvaluationSessionEx(program, trace, params) { lateinit var controller: ControllerImpl @@ -46,8 +46,11 @@ internal class EvaluationSessionImpl private constructor ( } private class Config(val program: Program) : EvaluationSession.Config() { - val parameters = HashMap() + + val parameters = HashMap, Any>() + var evaluationTrace: EvaluationTrace = EvaluationTrace.NULL + var storeView: StoreView? = null var feedbackHandler: EvaluationFeedbackHandler? = null @@ -68,28 +71,30 @@ internal class EvaluationSessionImpl private constructor ( } override fun withParam(key: String, param: Any): EvaluationSession.Config { - this.parameters.put(key, param) + this.parameters.put(ParameterKey.of(key, Any::class.java), param) return this } - override fun start(): EvaluationResult = start(SessionSolver()) + override fun withParameter(key: ParameterKey, value: T): EvaluationSession.Config { + this.parameters.put(key, value as Any) + return this + } - override fun start(sessionSolver: SessionSolver?): EvaluationResult { + override fun start(): EvaluationResult { var session = Backend.ourBackend.ourSession.get() if (session != null) throw IllegalStateException("session already active") - - sessionSolver?.init(evaluationTrace) - + @Suppress("UNCHECKED_CAST") - val durations = - parameters.get("profiling.data") as MutableMap? + val durations = parameters[ParameterKey.of("profiling.data", MutableMap::class.java)] + as MutableMap? val profiler = durations?.let { Profiler() } - session = EvaluationSessionImpl(program, evaluationTrace, sessionSolver) + session = EvaluationSessionImpl(program, evaluationTrace, parameters) Backend.ourBackend.ourSession.set(session) var failure: EvaluationFailure? = null try { - val state = session.launch(parameters["main"] as Constraint, profiler, storeView, feedbackHandler) + val main = parameters[ParameterKey.of("main", Constraint::class.java)] as Constraint + val state = session.launch(main, profiler, storeView, feedbackHandler) if (state is FAILED) { failure = state.failure } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java index ce542e39..6c01a66a 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java @@ -30,81 +30,90 @@ import jetbrains.mps.logic.reactor.program.Program; */ public abstract class EvaluationSession { - private static EvaluationSession.Backend ourBackend; - @SuppressWarnings("unchecked") public static S current(Class sessionClass) { return (S) current(); } public static EvaluationSession current() { - if (ourBackend == null) { - throw new IllegalStateException("no backend"); - } + if (ourBackend == null) throw new IllegalStateException("no backend"); return ourBackend.current(); } public static EvaluationSession.Config newSession(Program program) { - if (ourBackend == null) { - throw new IllegalStateException("no backend"); - } + if (ourBackend == null) throw new IllegalStateException("no backend"); return ourBackend.createConfig(program); } - protected static void setBackend(EvaluationSession.Backend backend) { - if (ourBackend != null) { - throw new IllegalStateException("backend already assigned"); - } - ourBackend = backend; - } - - protected static void clearBackend(EvaluationSession.Backend backend) { - if (ourBackend != backend) { - throw new IllegalStateException("illegal access"); - } - ourBackend = null; - } - - @Deprecated - public abstract SessionSolver sessionSolver(); - public abstract Program program(); public abstract boolean ask(PredicateInvocation invocation); public abstract void tell(PredicateInvocation invocation); - public interface Backend { + public abstract T parameter(ParameterKey key); + + public static class ParameterKey { + + private final String name; + + public static ParameterKey of(String name, Class klass) { + return new ParameterKey(name); + } + + private ParameterKey(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ParameterKey that = (ParameterKey) o; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } + } + + public static abstract class Config { + + public abstract Config withParameter(ParameterKey key, T value); + + public abstract Config withTrace(EvaluationTrace computingTracer); + + public abstract Config withStoreView(StoreView storeView); + + public abstract Config withFeedbackHandler(EvaluationFeedbackHandler handler); + + @Deprecated + public abstract Config withParam(String key, Object param); + + public abstract EvaluationResult start(); + + } + + protected static void setBackend(EvaluationSession.Backend backend) { + if (ourBackend != null) throw new IllegalStateException("backend already assigned"); + ourBackend = backend; + } + + protected static void clearBackend(EvaluationSession.Backend backend) { + if (ourBackend != backend) throw new IllegalStateException("illegal access"); + ourBackend = null; + } + + protected interface Backend { S current(); EvaluationSession.Config createConfig(Program program); } - - public static abstract class Config { - - public EvaluationSession.Config withTrace(EvaluationTrace computingTracer) { - return this; - } - - public EvaluationSession.Config withStoreView(StoreView storeView) { - return this; - } - - public EvaluationSession.Config withFeedbackHandler(EvaluationFeedbackHandler handler) { - return this; - } - - public EvaluationSession.Config withParam(String key, Object param) { - return this; - } - - @Deprecated - public abstract EvaluationResult start(SessionSolver sessionSolver); - - public abstract EvaluationResult start(); - - } - + + private static EvaluationSession.Backend ourBackend; } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/FailureHandler.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/FailureHandler.java deleted file mode 100644 index 5b321c6e..00000000 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/FailureHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2014-2019 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package jetbrains.mps.logic.reactor.evaluation; - -import jetbrains.mps.logic.reactor.program.Rule; - -/** - * @author Fedor Isakov - * @deprecated Use {@link EvaluationFeedbackHandler} instead. - */ -@Deprecated -public interface FailureHandler extends EvaluationFeedbackHandler { - - /** - * This method is called in order to decide what to do when a failure occurs during rule evaluation. - * Expected to return either the same failure (the default behaviour), a new failure with more details, - * or null. - * Any non-null returned value implies failure propagation up the evaluation stack. - * If null is returned, the failure is considered to have been handled. - */ - EvaluationFailure handleFailure(EvaluationFailure failure, Rule rule); - - @Override - default boolean handleFeedback(Rule rule, EvaluationFeedback feedback) { - // compatibility adapter - if (feedback instanceof EvaluationFailure) { - return handleFailure((EvaluationFailure) feedback, rule) == null; - } - return false; - } -} diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionSolver.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionSolver.java deleted file mode 100644 index 1ce8fb10..00000000 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionSolver.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2014-2019 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package jetbrains.mps.logic.reactor.evaluation; - - -/** - * // FIXME to be merged with EvaluationSession - * Is used to provide an interface for handlers and solvers working together in a single session. - * - * - * @author Fedor Isakov - * @deprecated Use EvalutionSession - */ -@Deprecated -public class SessionSolver implements Solver { - - private EvaluationTrace tracer = EvaluationTrace.NULL; - - public void init(EvaluationTrace evaluationTrace) { - tracer = evaluationTrace; - } - - /** - * @deprecated Use EvaluationSession - * @param invocation - * @return - */ - @Override - @Deprecated - public boolean ask(PredicateInvocation invocation) { - Solver solver = invocation.predicate().symbol().solver(); - boolean result = solver.ask(invocation); - tracer.ask(result, invocation); - return result; - } - - /** - * @deprecated Use EvaluationSession - * @param invocation - */ - @Override - @Deprecated - public void tell(PredicateInvocation invocation) { - Solver solver = invocation.predicate().symbol().solver(); - tracer.tell(invocation); - solver.tell(invocation); - } - -} diff --git a/reactor/Test/test/TestController.kt b/reactor/Test/test/TestController.kt index cb0e8a2f..31fbd8d6 100644 --- a/reactor/Test/test/TestController.kt +++ b/reactor/Test/test/TestController.kt @@ -34,7 +34,7 @@ class TestController { } private class MockSession(program: Program) : - EvaluationSessionEx(program, EvaluationTrace.NULL) { + EvaluationSessionEx(program, EvaluationTrace.NULL, params = mapOf, Any>()) { lateinit var controller: Controller override fun controller(): Controller = controller