X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fvariable%2FVariables.java;h=218a268a464097916f666d44cf3c20f91115a166;hb=035118aa5f35c9e5acd1f34d22065055dfdee486;hp=5fb0636a08dec5e41a616b863c58bbb7fbbaa4df;hpb=277ddb17bc33a7c0b5c352f80bbc2605acaee50d;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java index 5fb0636a0..218a268a4 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java @@ -18,6 +18,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.simantics.databoard.Bindings; import org.simantics.databoard.Databoard; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.mutable.Variant; @@ -30,9 +31,11 @@ import org.simantics.db.Resource; 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.Logger; +import org.simantics.db.common.utils.CommonDBUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.InvalidVariableException; import org.simantics.db.layer0.exception.MissingVariableException; +import org.simantics.db.layer0.exception.MissingVariableValueException; import org.simantics.db.layer0.request.Model; import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable; import org.simantics.db.layer0.request.PossibleVariableIndexRoot; @@ -49,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) { @@ -150,13 +158,33 @@ 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 { String uri = graph.getPossibleURI(resource); - return uri != null ? getPossibleVariable(graph, uri) : null; + if (uri != null) + return getPossibleVariable(graph, uri); + Resource parent = CommonDBUtils.getPossibleOwner(graph, resource); + if (parent == null) + return null; + Variable possibleVariable = getPossibleVariable(graph, parent); + if (possibleVariable == null) + return null; + String possibleName = graph.getPossibleRelatedValue(resource, Layer0.getInstance(graph).HasName, Bindings.STRING); + if (possibleName == null) + possibleName = VariableUtils.unnamedResourceName(resource); + Variable possibleChild = possibleVariable.getPossibleChild(graph, possibleName); + if (possibleChild != null) + return possibleChild; + for (Variable v : possibleVariable.getChildren(graph)) { + Resource vr = v.getPossibleRepresents(graph); + if (vr != null && vr.equals(resource)) { + return v; + } + } + return null; } public static Variable getPossibleVariable(ReadGraph graph, String uri) throws DatabaseException { @@ -285,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); @@ -358,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); @@ -406,7 +434,8 @@ final public class Variables { public static Variable getContext(ReadGraph graph, Variable variable) throws DatabaseException { Variable context = getPossibleContext(graph, variable); - if(context == null) throw new DatabaseException("No context found for " + variable.getURI(graph)); + if (context == null) + throw new MissingVariableException("No context found for " + variable.getURI(graph), variable.getPossibleRepresents(graph)); else return context; } @@ -425,7 +454,7 @@ final public class Variables { public static String getRVI(ReadGraph graph, Variable variable) throws DatabaseException { Resource realizationResource = getRealization(graph, variable); if (realizationResource == null) - throw new DatabaseException("No realization found for " + variable.getURI(graph)); + throw new InvalidVariableException("No realization found for " + variable.getURI(graph)); return variable.getURI(graph).substring(graph.getURI(realizationResource).length()); } @@ -471,7 +500,8 @@ final public class Variables { public static Variable switchRealization(ReadGraph graph, Variable variable, Resource realization) throws DatabaseException { Resource current = getRealization(graph, variable); - if(current == null) throw new DatabaseException("No current realization found for variable"); + if (current == null) + throw new InvalidVariableException("No current realization found for variable"); return switchRealization(graph, variable, current, realization); } @@ -520,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); } @@ -545,13 +575,13 @@ final public class Variables { } public static String appendRVI(ReadGraph graph, String modelURI, String rvi, Resource configuration) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); String partName = graph.getPossibleRelatedValue(configuration, L0.HasName); - if(partName == null) throw new MissingVariableException("Can not append a child corresponding to " + configuration + " to rvi '" + rvi + "' since there is no name."); + if (partName == null) + throw new MissingVariableException("Can not append a child corresponding to " + configuration + " to rvi '" + + rvi + "' since there is no name.", configuration); String escaped = URIStringUtils.escape(partName); return rvi + "/" + escaped; - } public static boolean isValid(ReadGraph graph, Variable variable) { @@ -615,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 DatabaseException(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 children; @@ -671,52 +702,53 @@ final public class Variables { public static NodeStructure requestNodeStructure(ReadGraph graph, VariableNode node) throws DatabaseException { NodeStructure value = graph.syncRequest(new NodeStructureRequest(node)); - if (value == null) throw new DatabaseException("External data access error"); - 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 DatabaseException("External data access error", e); - } - - if (getter.exception != null) - throw new DatabaseException("External data access error", getter.exception); - if (getter.result == null) - throw new DatabaseException("External data access error"); - - return getter.result; - - } + 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.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; + } + } /** @@ -763,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; } } @@ -787,10 +821,9 @@ final public class Variables { try { result = NodeStructureRequest.get(n); } catch (NodeManagerException e) { - Logger.defaultLogError(e); exception = e; } - } + } }; @@ -798,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 getValue1, Function2 getValue2, - Function2 setValue2, Function3 setValue3, - Function1 getDatatype) { - return new SCLValueAccessor(getValue1, getValue2, setValue2, setValue3, getDatatype); - } + + public static ValueAccessor createValueAccessor(Function1 getValue1, Function2 getValue2, + Function2 setValue2, Function3 setValue3, + Function1 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);