]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java
Support pending values in NodeManager
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / Variables.java
index 341a496a4ff2778a3e9972cb2c0c998dbe9f6114..218a268a464097916f666d44cf3c20f91115a166 100644 (file)
@@ -32,7 +32,6 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.request.TernaryRead;
 import org.simantics.db.common.utils.CommonDBUtils;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.exception.InvalidVariableException;
 import org.simantics.db.layer0.exception.MissingVariableException;
@@ -53,14 +52,19 @@ import org.simantics.scl.runtime.function.Function1;
 import org.simantics.scl.runtime.function.Function2;
 import org.simantics.scl.runtime.function.Function3;
 import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.simulator.variable.NodeManager;
 import org.simantics.simulator.variable.exceptions.NodeManagerException;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.map.hash.TObjectIntHashMap;
 
 final public class Variables {
 
-    public static final Variant PENDING_NODE_VALUE = new Variant();
+    private static final Logger LOGGER = LoggerFactory.getLogger(Variables.class);
+
+    public static final Variant PENDING_NODE_VALUE = NodeManager.PENDING_NODE_VALUE;
 
     public static final NodeStructure PENDING_NODE_STRUCTURE = new NodeStructure(Collections.emptyMap(), Collections.emptyMap()) {
         public boolean equals(Object object) {
@@ -154,8 +158,8 @@ final public class Variables {
 
     @Deprecated
     public final static String[] builtins = {
-        TYPE, RESOURCE, URI
-        //, SERIALISED
+            TYPE, RESOURCE, URI
+            //, SERIALISED
     };
 
     public static Variable getPossibleVariable(ReadGraph graph, Resource resource) throws DatabaseException {
@@ -309,15 +313,15 @@ final public class Variables {
     public static Variable getRootVariable(ReadGraph graph) throws DatabaseException {
         return graph.adapt(graph.getRootLibrary(), Variable.class);
     }
-    
+
     public static Resource getPossibleIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
-       return graph.syncRequest(new PossibleVariableIndexRoot(variable));
+        return graph.syncRequest(new PossibleVariableIndexRoot(variable));
     }
-    
+
     public static Resource getIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
-       return graph.syncRequest(new VariableIndexRoot(variable));
+        return graph.syncRequest(new VariableIndexRoot(variable));
     }
-    
+
     public static Resource getModel(ReadGraph graph, Variable variable) throws DatabaseException {
         String URI = variable.getURI(graph);
         return VariablesImpl.getFirst(graph, SimulationResource.getInstance(graph).Model, URI, 8);
@@ -382,7 +386,7 @@ final public class Variables {
         Variable context = getConfigurationContext(graph, resource);
         return context.browse(graph, RVI);
     }
-    
+
     public static Variable getConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
         Variable context = getConfigurationContext(graph, variable);
         RVI rvi = variable.getRVI(graph);
@@ -546,12 +550,12 @@ final public class Variables {
     }
 
     public static Variable toPossibleConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
-       
+
         Resource represents = variable.getPossibleRepresents(graph);
         if(represents == null) return null;
         Resource config = getPossibleConfigurationContextResource(graph, represents);
         if(config == null) return null;
-       return switchPossibleContext(graph, variable, config);
+        return switchPossibleContext(graph, variable, config);
 
     }
 
@@ -641,25 +645,26 @@ final public class Variables {
     }
 
     public static Variant requestNodeValue(ReadGraph graph, VariableNode<?> node, final Binding binding) throws DatabaseException {
-       Variant value = graph.syncRequest(new NodeValueRequest(node, binding));
-       if(PENDING_NODE_VALUE == value && graph.getSynchronous()) {
-               // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
-               
-                       ValueGetter getter = new ValueGetter(node, binding);
-                       try {
-                               node.support.manager.getRealm().syncExec(getter);
-                       } catch (InterruptedException e) {
-                               Logger.defaultLogError(e);
-                       }
-                       
-                       if (getter.exception != null)
-                               throw new MissingVariableValueException("No value for node " + node, getter.exception);
-                       
-                       return getter.result;
-       }
+        Variant value = graph.syncRequest(new NodeValueRequest(node, binding));
+        if(PENDING_NODE_VALUE == value && graph.getSynchronous()) {
+            // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
+
+            ValueGetter getter = new ValueGetter(node, binding);
+            try {
+                node.support.manager.getRealm().syncExec(getter);
+            } catch (InterruptedException e) {
+                LOGGER.error("Error while getting node value", e);
+                throw new InvalidVariableException("External data access error " + String.valueOf(node), e);
+            }
+
+            if (getter.exception != null)
+                throw new MissingVariableValueException("No value for node " + node, getter.exception);
+
+            return getter.result;
+        }
         return value; 
     }
-    
+
     public static class NodeStructure {
         // Immutable but wrapped with Collections.unmodifiableMap as an optimization
         public final Map<String,Object> children;
@@ -699,51 +704,51 @@ final public class Variables {
         NodeStructure value = graph.syncRequest(new NodeStructureRequest(node));
         if (value == null)
             throw new InvalidVariableException("External data access error " + String.valueOf(node));
-       if(PENDING_NODE_STRUCTURE == value && graph.getSynchronous()) {
-               // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
-               
-                       StructureGetter getter = new StructureGetter(node);
-                       try {
-                               node.support.manager.getRealm().syncExec(getter);
-                       } catch (InterruptedException e) {
-                               Logger.defaultLogError(e);
-                               throw new InvalidVariableException("External data access error " + String.valueOf(node), e);
-                       }
-                       
-                       if (getter.exception != null)
-                               throw new InvalidVariableException("External data access error " + String.valueOf(node), getter.exception);
-                       if (getter.result == null)
-                               throw new InvalidVariableException("External data access error " + String.valueOf(node));
-                       
-                       return getter.result;
-               
-       }
+        if(PENDING_NODE_STRUCTURE == value && graph.getSynchronous()) {
+            // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
+
+            StructureGetter getter = new StructureGetter(node);
+            try {
+                node.support.manager.getRealm().syncExec(getter);
+            } catch (InterruptedException e) {
+                LOGGER.error("Error while getting node structure", e);
+                throw new InvalidVariableException("External data access error " + String.valueOf(node), e);
+            }
+
+            if (getter.exception != null)
+                throw new InvalidVariableException("External data access error " + String.valueOf(node), getter.exception);
+            if (getter.result == null)
+                throw new InvalidVariableException("External data access error " + String.valueOf(node));
+
+            return getter.result;
+
+        }
         return value; 
 
     }
-    
+
     public static String getPossibleUnit(ReadGraph graph, Variable variable) throws DatabaseException {
-       
-       try {
-               
-               Resource predicate = variable.getPossiblePredicateResource(graph);
-               if(predicate != null) {
-                       PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
-                       if(info.definedUnit != null) return info.definedUnit;
-               }
-               
-               Variant variant = variable.getVariantValue(graph);
-               Binding binding = variant.getBinding();
-               if(binding == null) return null;
-               Datatype dt = binding.type();
-               if(!(dt instanceof NumberType)) return null;
-               NumberType nt = (NumberType)dt;
-               return nt.getUnit();
-               
-       } catch (DatabaseException e) {
-               return null;
-       }
-       
+
+        try {
+
+            Resource predicate = variable.getPossiblePredicateResource(graph);
+            if(predicate != null) {
+                PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                if(info.definedUnit != null) return info.definedUnit;
+            }
+
+            Variant variant = variable.getVariantValue(graph);
+            Binding binding = variant.getBinding();
+            if(binding == null) return null;
+            Datatype dt = binding.type();
+            if(!(dt instanceof NumberType)) return null;
+            NumberType nt = (NumberType)dt;
+            return nt.getUnit();
+
+        } catch (DatabaseException e) {
+            return null;
+        }
+
     }
 
     /**
@@ -790,8 +795,10 @@ final public class Variables {
                     result = new Variant(binding, n.support.manager.getValue(n.node, binding));
                 else
                     result = n.support.manager.getValue(n.node);
+            } catch (NodeManagerException e) {
+                exception = e;
             } catch (Exception e) {
-                Logger.defaultLogError(e);
+                LOGGER.error("Error while getting node value", e);
                 exception = e;
             }
         }
@@ -814,10 +821,9 @@ final public class Variables {
             try {
                 result = NodeStructureRequest.get(n);
             } catch (NodeManagerException e) {
-                Logger.defaultLogError(e);
                 exception = e;
             }
-        }
+         }
 
     };
 
@@ -825,12 +831,12 @@ final public class Variables {
         Variable v = Variables.getPossibleVariable(graph, entity);
         return v != null ? v.getPossibleProperty(graph, property) : null;
     }
-    
-       public static ValueAccessor createValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
-                       Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
-                       Function1<Variable, Datatype> getDatatype) {
-               return new SCLValueAccessor(getValue1, getValue2, setValue2, setValue3, getDatatype);
-       }
+
+    public static ValueAccessor createValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
+            Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
+            Function1<Variable, Datatype> getDatatype) {
+        return new SCLValueAccessor(getValue1, getValue2, setValue2, setValue3, getDatatype);
+    }
 
     public static void setRVIProperty(WriteGraph graph, Variable variable, RVI rvi) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);