Introduce DataProvider. Allow predicates to access Supervisor.

DataProvider is an abstract way to associate any data with the
runtime object, such as Supervisor.
Predicate solvers that ought to be stateful may make use of
DataProvider to access the internal state.
This commit is contained in:
Fedor Isakov 2020-06-02 11:13:50 +02:00
parent 161beb6a4d
commit c5d9e798a3
3 changed files with 65 additions and 2 deletions

View File

@ -89,14 +89,14 @@ internal class ControllerImpl (
processing
override fun ask(invocation: PredicateInvocation): Boolean {
val solver = invocation.predicate().symbol().solver()
val solver = invocation.predicate().symbol().solver(supervisor)
val result = solver.ask(invocation)
trace.ask(result, invocation)
return result
}
override fun tell(invocation: PredicateInvocation) {
val solver = invocation.predicate().symbol().solver()
val solver = invocation.predicate().symbol().solver(supervisor)
trace.tell(invocation)
solver.tell(invocation)
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2014-2020 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 java.util.function.Supplier;
/**
* @author Fedor Isakov
*/
public interface DataProvider {
class Key<T> {
private final String name;
public Key(String name) {
this.name = name;
}
@Override
public int hashCode() {
return 17*name.hashCode();
}
@Override
public boolean equals(Object that) {
if (that == null) return false;
if (that.getClass() != Key.class) return false;
return this.name.equals(((Key) that).name);
}
@Override
public String toString() {
return name;
}
}
<T> T getData(Key<T> key);
<T> T getOrSetData(Key<T> key, Supplier<T> supplier);
<T> void setData(Key<T> key, T data);
}

View File

@ -18,6 +18,7 @@ package jetbrains.mps.logic.reactor.program;
import jetbrains.mps.logic.reactor.evaluation.Solver;
import jetbrains.mps.logic.reactor.evaluation.Supervisor;
/**
* A predicate symbol.
@ -32,6 +33,10 @@ public abstract class PredicateSymbol extends Symbol {
public abstract Solver solver();
public Solver solver(Supervisor supervisor) {
return solver();
}
@Override
public String toString() {
return id() + "()/" + arity();