1 package org.simantics.pythonlink;
\r
3 import java.util.List;
\r
4 import java.util.concurrent.TimeUnit;
\r
6 import org.simantics.db.layer0.variable.NodeSupport;
\r
7 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
\r
8 import org.simantics.db.layer0.variable.Variable;
\r
9 import org.simantics.db.layer0.variable.VariableNode;
\r
10 import org.simantics.pythonlink.PythonContext.Listener;
\r
11 import org.simantics.pythonlink.variable.PythonNode;
\r
12 import org.simantics.pythonlink.variable.PythonNodeManager;
\r
13 import org.simantics.scl.runtime.SCLContext;
\r
14 import org.simantics.scl.runtime.function.Function;
\r
15 import org.simantics.scl.runtime.tuple.Tuple0;
\r
16 import org.simantics.simulator.variable.NodeManager;
\r
17 import org.simantics.simulator.variable.exceptions.NodeManagerException;
\r
19 public class Python {
\r
20 private static final String PYTHON_CONTEXT = "Simantics/Python/Python";
\r
22 public static PythonContext openPythonContext() {
\r
23 return new PythonContext();
\r
26 public static Variable getPythonContextVariable(PythonContext context) {
\r
27 NodeManager<PythonNode> nodeManager = new PythonNodeManager(context);
\r
30 root = nodeManager.getNode("/");
\r
31 } catch (NodeManagerException e) {
\r
32 // Should not happen
\r
33 throw new RuntimeException("Getting root Python node failed");
\r
36 final NodeSupport<PythonNode> support = new NodeSupport<PythonNode>(nodeManager, 0, TimeUnit.NANOSECONDS);
\r
38 context.addListener(new Listener() {
\r
40 public void updated(String variableName) {
\r
42 PythonNode root = nodeManager.getNode("/");
\r
43 if (variableName != null) {
\r
44 PythonNode node = nodeManager.getNode(variableName);
\r
45 if (node != null) support.valueCache.removeListening(node);
\r
46 support.structureCache.removeListening(root);
\r
49 List<PythonNode> props = nodeManager.getProperties(root);
\r
50 for (PythonNode p : props)
\r
51 support.valueCache.removeListening(p);
\r
52 support.structureCache.removeListening(root);
\r
55 support.valueCache.clearExpired();
\r
56 support.structureCache.clearExpired();
\r
57 } catch (NodeManagerException e) {
\r
58 e.printStackTrace();
\r
63 public void closed() {
\r
67 return new StandardGraphChildVariable(null, new VariableNode<PythonNode>(support, root), null);
\r
70 @SuppressWarnings( { "unchecked", "rawtypes" } )
\r
71 public static Object runPythonF(Function f) {
\r
72 SCLContext sclContext = SCLContext.getCurrent();
\r
73 Object oldContext = sclContext.get(PYTHON_CONTEXT);
\r
74 try (PythonContext newContext = openPythonContext()) {
\r
75 sclContext.put(PYTHON_CONTEXT, newContext);
\r
76 return f.apply(Tuple0.INSTANCE);
\r
79 sclContext.put(PYTHON_CONTEXT, oldContext);
\r
83 @SuppressWarnings( { "unchecked", "rawtypes" } )
\r
84 public static Object runWithPythonContextF(PythonContext context, Function f) {
\r
85 SCLContext sclContext = SCLContext.getCurrent();
\r
86 Object oldContext = sclContext.get(PYTHON_CONTEXT);
\r
88 sclContext.put(PYTHON_CONTEXT, context);
\r
89 return f.apply(Tuple0.INSTANCE);
\r
92 sclContext.put(PYTHON_CONTEXT, oldContext);
\r