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.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;
+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;
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 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 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) {
}
if (getter.exception != null)
- throw new DatabaseException(getter.exception);
+ throw new MissingVariableValueException("No value for node " + node, getter.exception);
return getter.result;
}
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 (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.
node.support.manager.getRealm().syncExec(getter);
} catch (InterruptedException e) {
Logger.defaultLogError(e);
- throw new DatabaseException("External data access error", e);
+ throw new InvalidVariableException("External data access error " + String.valueOf(node), e);
}
if (getter.exception != null)
- throw new DatabaseException("External data access error", getter.exception);
+ throw new InvalidVariableException("External data access error " + String.valueOf(node), getter.exception);
if (getter.result == null)
- throw new DatabaseException("External data access error");
+ throw new InvalidVariableException("External data access error " + String.valueOf(node));
return getter.result;