Fixed typo from build.p2.site org.apache.lucene4.queryparser definition
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / equations / TestEquationContext.java
1 package org.simantics.scl.runtime.equations;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collections;\r
5 import java.util.List;\r
6 \r
7 import org.simantics.scl.runtime.SCLContext;\r
8 import org.simantics.scl.runtime.function.Function;\r
9 import org.simantics.scl.runtime.tuple.Tuple0;\r
10 import org.simantics.scl.runtime.tuple.Tuple2;\r
11 \r
12 import gnu.trove.map.hash.THashMap;\r
13 import gnu.trove.procedure.TObjectObjectProcedure;\r
14 \r
15 public class TestEquationContext implements EquationContext {\r
16 \r
17     public static final boolean TRACE = true;\r
18     \r
19     THashMap<String, Object> values = new THashMap<String, Object>(); \r
20     THashMap<String, ArrayList<Function>> listenerMap = new THashMap<String, ArrayList<Function>>(); \r
21 \r
22     @Override\r
23     public void listenEquationVariable(String variableName, Function listener) {\r
24         if(TRACE)\r
25             System.out.println("listenEquationVariable(" + variableName + ", " + listener + ")");\r
26         if(values.containsKey(variableName)) {\r
27             Object value = values.get(variableName);\r
28             if(TRACE)\r
29                 System.out.println("    apply " + value);\r
30             listener.apply(value);\r
31         }\r
32         else {\r
33             if(TRACE)\r
34                 System.out.println("    add listener");\r
35             ArrayList<Function> listeners = listenerMap.get(variableName);\r
36             if(listeners == null) {\r
37                 listeners = new ArrayList<Function>();\r
38                 listenerMap.put(variableName, listeners);\r
39             }\r
40             listeners.add(listener);\r
41         }\r
42     }\r
43 \r
44     @Override\r
45     public void setEquationVariable(String variableName, Object value) {\r
46         if(TRACE)\r
47             System.out.println("setEquationVariable(" + variableName + ", " + value + ")");\r
48         if(values.containsKey(variableName))\r
49             throw new IllegalStateException("Value for " + variableName + " already defined (oldValue=" + values.get(variableName) + \r
50                     ", newValue=" + value + ").");\r
51         values.put(variableName, value);\r
52         ArrayList<Function> listeners = listenerMap.remove(variableName);\r
53         SCLContext context = SCLContext.getCurrent();\r
54         if(listeners != null) {\r
55             Object oldEquationContex = context.put("equation", this);\r
56             try {\r
57                 for(Function listener : listeners) {\r
58                     if(TRACE)\r
59                         System.out.println("    apply " + listener + " " + value);\r
60                     listener.apply(value);\r
61                 }\r
62             } finally {\r
63                 context.put("equation", oldEquationContex);\r
64             }\r
65         }\r
66     }\r
67     \r
68     public static List<Tuple2> solveEquations(Function f) {\r
69         TestEquationContext equationContext = new TestEquationContext();\r
70         SCLContext context = SCLContext.getCurrent();\r
71         Object oldEquationContext = context.put("equation", equationContext);\r
72         try {\r
73             f.apply(Tuple0.INSTANCE);\r
74         } finally {\r
75             context.put("equation", oldEquationContext);\r
76         }\r
77         ArrayList<Tuple2> result = new ArrayList<Tuple2>(equationContext.values.size());\r
78         equationContext.values.forEachEntry(new TObjectObjectProcedure<String, Object>() {\r
79             @Override\r
80             public boolean execute(String a, Object b) {\r
81                 result.add(new Tuple2(a, String.valueOf(b)));\r
82                 return true;\r
83             }\r
84         });\r
85         Collections.sort(result, (t1, t2) -> {\r
86             return ((String)t1.c0).compareTo((String)t2.c0);\r
87         });\r
88         return result;\r
89     }\r
90     \r
91     public THashMap<String, Object> getValues() {\r
92         return values;\r
93     }\r
94 \r
95 }\r