]> gerrit.simantics Code Review - simantics/python.git/blob - org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java
Added support for dynamically typed data access and a Variable interface.
[simantics/python.git] / org.simantics.pythonlink / src / org / simantics / pythonlink / Python.java
1 package org.simantics.pythonlink;\r
2 \r
3 import java.util.List;\r
4 import java.util.concurrent.TimeUnit;\r
5 \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
18 \r
19 public class Python {\r
20     private static final String PYTHON_CONTEXT = "Simantics/Python/Python";\r
21 \r
22     public static PythonContext openPythonContext() {\r
23         return new PythonContext();\r
24     }\r
25     \r
26     public static Variable getPythonContextVariable(PythonContext context) {\r
27         NodeManager<PythonNode> nodeManager = new PythonNodeManager(context);\r
28         PythonNode root;\r
29                 try {\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
34                 }\r
35                 \r
36                 final NodeSupport<PythonNode> support = new NodeSupport<PythonNode>(nodeManager, 0, TimeUnit.NANOSECONDS);\r
37                 \r
38                 context.addListener(new Listener() {\r
39             @Override\r
40             public void updated(String variableName) {\r
41                 try {\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
47                     }\r
48                     else {\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
53                     }\r
54                     \r
55                     support.valueCache.clearExpired();\r
56                     support.structureCache.clearExpired();\r
57                 } catch (NodeManagerException e) {\r
58                     e.printStackTrace();\r
59                 }\r
60             }\r
61             \r
62             @Override\r
63             public void closed() {\r
64             }\r
65         });\r
66                 \r
67         return new StandardGraphChildVariable(null, new VariableNode<PythonNode>(support, root), null);\r
68     }\r
69     \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
77         }\r
78         finally {\r
79             sclContext.put(PYTHON_CONTEXT, oldContext);\r
80         }\r
81     }\r
82     \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
87         try {\r
88             sclContext.put(PYTHON_CONTEXT, context);\r
89             return f.apply(Tuple0.INSTANCE);\r
90         }\r
91         finally {\r
92             sclContext.put(PYTHON_CONTEXT, oldContext);\r
93         }\r
94     }    \r
95 }\r