X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.pythonlink%2Fsrc%2Forg%2Fsimantics%2Fpythonlink%2FPython.java;h=9d00c447a1dc81b67a8349ca7f9129506ef4702f;hb=15304b7c3168ef89c47dcfcfb4cf34c72000a75f;hp=af5c9e287b424090137d83f721fd278c5e01758f;hpb=52bef206878a4384b43494243dd39813b2bdf5ad;p=simantics%2Fpython.git diff --git a/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java index af5c9e2..9d00c44 100644 --- a/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java +++ b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java @@ -1,8 +1,20 @@ package org.simantics.pythonlink; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.simantics.db.layer0.variable.NodeSupport; +import org.simantics.db.layer0.variable.StandardGraphChildVariable; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.VariableNode; +import org.simantics.pythonlink.PythonContext.Listener; +import org.simantics.pythonlink.variable.PythonNode; +import org.simantics.pythonlink.variable.PythonNodeManager; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function; import org.simantics.scl.runtime.tuple.Tuple0; +import org.simantics.simulator.variable.NodeManager; +import org.simantics.simulator.variable.exceptions.NodeManagerException; public class Python { private static final String PYTHON_CONTEXT = "Simantics/Python/Python"; @@ -11,8 +23,52 @@ public class Python { return new PythonContext(); } + public static Variable getPythonContextVariable(PythonContext context) { + NodeManager nodeManager = new PythonNodeManager(context); + PythonNode root; + try { + root = nodeManager.getNode("/"); + } catch (NodeManagerException e) { + // Should not happen + throw new RuntimeException("Getting root Python node failed"); + } + + final NodeSupport support = new NodeSupport(nodeManager, 0, TimeUnit.NANOSECONDS); + + context.addListener(new Listener() { + @Override + public void updated(String variableName) { + try { + PythonNode root = nodeManager.getNode("/"); + if (variableName != null) { + PythonNode node = nodeManager.getNode(variableName); + if (node != null) support.valueCache.removeListening(node); + support.structureCache.removeListening(root); + } + else { + List props = nodeManager.getProperties(root); + for (PythonNode p : props) + support.valueCache.removeListening(p); + support.structureCache.removeListening(root); + } + + support.valueCache.clearExpired(); + support.structureCache.clearExpired(); + } catch (NodeManagerException e) { + e.printStackTrace(); + } + } + + @Override + public void closed() { + } + }); + + return new StandardGraphChildVariable(null, new VariableNode(support, root), null); + } + @SuppressWarnings( { "unchecked", "rawtypes" } ) - public static Object runPython(Function f) { + public static Object runPythonF(Function f) { SCLContext sclContext = SCLContext.getCurrent(); Object oldContext = sclContext.get(PYTHON_CONTEXT); try (PythonContext newContext = openPythonContext()) { @@ -25,7 +81,7 @@ public class Python { } @SuppressWarnings( { "unchecked", "rawtypes" } ) - public static Object runWithPythonContext(PythonContext context, Function f) { + public static Object runWithPythonContextF(PythonContext context, Function f) { SCLContext sclContext = SCLContext.getCurrent(); Object oldContext = sclContext.get(PYTHON_CONTEXT); try {