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;
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;
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) {
@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 {
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);
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);
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;
}
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());
}
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);
}
}
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);
}
}
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) {
}
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<String,Object> children;
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;
+ }
+
}
/**
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;
}
}
try {
result = NodeStructureRequest.get(n);
} catch (NodeManagerException e) {
- Logger.defaultLogError(e);
exception = e;
}
- }
+ }
};
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);