+++ /dev/null
-package org.simantics.scl.runtime.equations;\r
-\r
-import org.simantics.scl.runtime.SCLContext;\r
-import org.simantics.scl.runtime.function.Function;\r
-import org.simantics.scl.runtime.tuple.Tuple0;\r
-\r
-public interface EquationContext {\r
- void listenEquationVariable(String variableName, Function callback);\r
- void setEquationVariable(String variableName, Object value);\r
- \r
- public static Object applyEquationContext(Function f, EquationContext equationContext) {\r
- SCLContext context = SCLContext.getCurrent();\r
- Object oldEquationContext = context.put("equation", equationContext);\r
- try {\r
- return f.apply(Tuple0.INSTANCE);\r
- } finally {\r
- context.put("equation", oldEquationContext);\r
- }\r
- }\r
-}\r
+++ /dev/null
-package org.simantics.scl.runtime.equations;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.simantics.scl.runtime.SCLContext;\r
-import org.simantics.scl.runtime.function.Function;\r
-import org.simantics.scl.runtime.tuple.Tuple0;\r
-import org.simantics.scl.runtime.tuple.Tuple2;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.procedure.TObjectObjectProcedure;\r
-\r
-public class TestEquationContext implements EquationContext {\r
-\r
- public static final boolean TRACE = true;\r
- \r
- THashMap<String, Object> values = new THashMap<String, Object>(); \r
- THashMap<String, ArrayList<Function>> listenerMap = new THashMap<String, ArrayList<Function>>(); \r
-\r
- @Override\r
- public void listenEquationVariable(String variableName, Function listener) {\r
- if(TRACE)\r
- System.out.println("listenEquationVariable(" + variableName + ", " + listener + ")");\r
- if(values.containsKey(variableName)) {\r
- Object value = values.get(variableName);\r
- if(TRACE)\r
- System.out.println(" apply " + value);\r
- listener.apply(value);\r
- }\r
- else {\r
- if(TRACE)\r
- System.out.println(" add listener");\r
- ArrayList<Function> listeners = listenerMap.get(variableName);\r
- if(listeners == null) {\r
- listeners = new ArrayList<Function>();\r
- listenerMap.put(variableName, listeners);\r
- }\r
- listeners.add(listener);\r
- }\r
- }\r
-\r
- @Override\r
- public void setEquationVariable(String variableName, Object value) {\r
- if(TRACE)\r
- System.out.println("setEquationVariable(" + variableName + ", " + value + ")");\r
- if(values.containsKey(variableName))\r
- throw new IllegalStateException("Value for " + variableName + " already defined (oldValue=" + values.get(variableName) + \r
- ", newValue=" + value + ").");\r
- values.put(variableName, value);\r
- ArrayList<Function> listeners = listenerMap.remove(variableName);\r
- SCLContext context = SCLContext.getCurrent();\r
- if(listeners != null) {\r
- Object oldEquationContex = context.put("equation", this);\r
- try {\r
- for(Function listener : listeners) {\r
- if(TRACE)\r
- System.out.println(" apply " + listener + " " + value);\r
- listener.apply(value);\r
- }\r
- } finally {\r
- context.put("equation", oldEquationContex);\r
- }\r
- }\r
- }\r
- \r
- public static List<Tuple2> solveEquations(Function f) {\r
- TestEquationContext equationContext = new TestEquationContext();\r
- SCLContext context = SCLContext.getCurrent();\r
- Object oldEquationContext = context.put("equation", equationContext);\r
- try {\r
- f.apply(Tuple0.INSTANCE);\r
- } finally {\r
- context.put("equation", oldEquationContext);\r
- }\r
- ArrayList<Tuple2> result = new ArrayList<Tuple2>(equationContext.values.size());\r
- equationContext.values.forEachEntry(new TObjectObjectProcedure<String, Object>() {\r
- @Override\r
- public boolean execute(String a, Object b) {\r
- result.add(new Tuple2(a, String.valueOf(b)));\r
- return true;\r
- }\r
- });\r
- Collections.sort(result, (t1, t2) -> {\r
- return ((String)t1.c0).compareTo((String)t2.c0);\r
- });\r
- return result;\r
- }\r
- \r
- public THashMap<String, Object> getValues() {\r
- return values;\r
- }\r
-\r
-}\r