Move StoredValue storage into PrologEnvironment
StoredValue and values now stored in HashMap in PrologEnvironment instead of the hash manager in the prolog interpreter. PrologEnvironment also allows for child environments to copy its stored values. Change-Id: I2a51206d5dd948717d54c51620f1485f28daaa5d
This commit is contained in:
@@ -23,6 +23,8 @@ import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologMachineCopy;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Per-thread Prolog interpreter.
|
||||
@@ -45,6 +47,7 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
}
|
||||
|
||||
private final Injector injector;
|
||||
private final Map<StoredValue<Object>, Object> storedValues;
|
||||
|
||||
@Inject
|
||||
PrologEnvironment(Injector i, @Assisted PrologMachineCopy src) {
|
||||
@@ -52,6 +55,7 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
injector = i;
|
||||
setMaxArity(MAX_ARITY);
|
||||
setEnabled(EnumSet.allOf(Prolog.Feature.class), false);
|
||||
storedValues = new HashMap<StoredValue<Object>, Object>();
|
||||
}
|
||||
|
||||
/** Get the global Guice Injector that configured the environment. */
|
||||
@@ -67,7 +71,7 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
* @return the value; null if not stored.
|
||||
*/
|
||||
public <T> T get(StoredValue<T> sv) {
|
||||
return sv.getOrNull(engine);
|
||||
return (T) storedValues.get(sv);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +82,13 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
* @param obj the value to store under {@code sv}.
|
||||
*/
|
||||
public <T> void set(StoredValue<T> sv, T obj) {
|
||||
sv.set(engine, obj);
|
||||
storedValues.put((StoredValue<Object>) sv, obj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the stored values from another interpreter to this one.
|
||||
*/
|
||||
public void copyStoredValues(PrologEnvironment child) {
|
||||
storedValues.putAll(child.storedValues);
|
||||
}
|
||||
}
|
||||
@@ -48,12 +48,9 @@ public class StoredValue<T> {
|
||||
}
|
||||
|
||||
/** Look up the value in the engine, or return null. */
|
||||
@SuppressWarnings("unchecked")
|
||||
public T getOrNull(Prolog engine) {
|
||||
Term r = engine.getHashManager().get(key);
|
||||
return r != null && r.isJavaObject() ? (T) r.toJava() : null;
|
||||
return get((PrologEnvironment) engine.control);
|
||||
}
|
||||
|
||||
/** Get the value from the engine, or throw SystemException. */
|
||||
public T get(Prolog engine) {
|
||||
T r = getOrNull(engine);
|
||||
@@ -70,7 +67,7 @@ public class StoredValue<T> {
|
||||
}
|
||||
|
||||
public void set(Prolog engine, T obj) {
|
||||
engine.getHashManager().put(key, new JavaObjectTerm(obj));
|
||||
set((PrologEnvironment) engine.control, obj);
|
||||
}
|
||||
|
||||
/** Perform {@link #getOrNull(Prolog)} on the environment's interpreter. */
|
||||
@@ -82,4 +79,4 @@ public class StoredValue<T> {
|
||||
public void set(PrologEnvironment env, T obj) {
|
||||
env.set(this, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user