]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/Variables.java
Possible variable browse without URI
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / Variables.java
index 06050adf7417f275f2329ca90f1543ec8b161f38..341a496a4ff2778a3e9972cb2c0c998dbe9f6114 100644 (file)
@@ -18,6 +18,8 @@ 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;
 import org.simantics.databoard.type.Datatype;
@@ -26,11 +28,15 @@ import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
 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.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;
@@ -154,7 +160,27 @@ final public class Variables {
 
     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 {
@@ -404,7 +430,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;
     }
 
@@ -423,7 +450,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()); 
     }
 
@@ -469,7 +496,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);
     }
 
@@ -543,13 +571,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) {
@@ -625,7 +653,7 @@ final public class Variables {
                        }
                        
                        if (getter.exception != null)
-                               throw new DatabaseException(getter.exception);
+                               throw new MissingVariableValueException("No value for node " + node, getter.exception);
                        
                        return getter.result;
        }
@@ -669,7 +697,8 @@ 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 (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.
                
@@ -678,13 +707,13 @@ final public class Variables {
                                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;
                
@@ -803,5 +832,13 @@ final public class Variables {
                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);
+        Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
+        Resource predicate = variable.getPredicateResource(graph);
+        Resource subject = variable.getParent(graph).getRepresents(graph);
+        graph.deny(subject, predicate);
+        graph.claimLiteral(subject, predicate, L0.RVI, rvi, rviBinding);
+    }
+
 }