package org.simantics.pythonlink;\r
\r
+import java.util.List;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.simantics.db.layer0.variable.NodeSupport;\r
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.VariableNode;\r
+import org.simantics.pythonlink.PythonContext.Listener;\r
+import org.simantics.pythonlink.variable.PythonNode;\r
+import org.simantics.pythonlink.variable.PythonNodeManager;\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.simulator.variable.NodeManager;\r
+import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
\r
public class Python {\r
private static final String PYTHON_CONTEXT = "Simantics/Python/Python";\r
return new PythonContext();\r
}\r
\r
+ public static Variable getPythonContextVariable(PythonContext context) {\r
+ NodeManager<PythonNode> nodeManager = new PythonNodeManager(context);\r
+ PythonNode root;\r
+ try {\r
+ root = nodeManager.getNode("/");\r
+ } catch (NodeManagerException e) {\r
+ // Should not happen\r
+ throw new RuntimeException("Getting root Python node failed");\r
+ }\r
+ \r
+ final NodeSupport<PythonNode> support = new NodeSupport<PythonNode>(nodeManager, 0, TimeUnit.NANOSECONDS);\r
+ \r
+ context.addListener(new Listener() {\r
+ @Override\r
+ public void updated(String variableName) {\r
+ try {\r
+ PythonNode root = nodeManager.getNode("/");\r
+ if (variableName != null) {\r
+ PythonNode node = nodeManager.getNode(variableName);\r
+ if (node != null) support.valueCache.removeListening(node);\r
+ support.structureCache.removeListening(root);\r
+ }\r
+ else {\r
+ List<PythonNode> props = nodeManager.getProperties(root);\r
+ for (PythonNode p : props)\r
+ support.valueCache.removeListening(p);\r
+ support.structureCache.removeListening(root);\r
+ }\r
+ \r
+ support.valueCache.clearExpired();\r
+ support.structureCache.clearExpired();\r
+ } catch (NodeManagerException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void closed() {\r
+ }\r
+ });\r
+ \r
+ return new StandardGraphChildVariable(null, new VariableNode<PythonNode>(support, root), null);\r
+ }\r
+ \r
@SuppressWarnings( { "unchecked", "rawtypes" } )\r
public static Object runPythonF(Function f) {\r
SCLContext sclContext = SCLContext.getCurrent();\r