]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/StructuralRVIResolver.java
Merge "Log an error if there are two resources with the same GUID."
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / StructuralRVIResolver.java
index b2913f3203eccc550c799d3632282c2372fd7d6c..ec6b583617fcdd6c42af134845d829838944980a 100644 (file)
@@ -8,6 +8,7 @@ import org.simantics.databoard.binding.Binding;
 import org.simantics.datatypes.literal.GUID;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.adapter.Instances;
 import org.simantics.db.layer0.exception.MissingVariableException;
@@ -21,8 +22,11 @@ import org.simantics.db.layer0.variable.Variables;
 import org.simantics.layer0.Layer0;
 import org.simantics.simulation.ontology.SimulationResource;
 import org.simantics.structural.stubs.StructuralResource2;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class StructuralRVIResolver extends StandardRVIResolver {
+    private static final Logger LOGGER = LoggerFactory.getLogger(StructuralRVIResolver.class);
 
     protected boolean isRVIBase(ReadGraph graph, Variable variable) throws DatabaseException {
         if (Variables.isContext(graph, variable)) return true;
@@ -40,10 +44,12 @@ public class StructuralRVIResolver extends StandardRVIResolver {
             Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
             return RVI.empty(rviBinding);
         }
-        Variable base = variable.getParent(graph);
-        while(!isRVIBase(graph, base)) base = base.getParent(graph);
-        RVIPart part = getRVIPart(graph, variable);
-        return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
+        else {
+            Variable base = variable.getParent(graph);
+            while(!isRVIBase(graph, base)) base = base.getParent(graph);
+            RVIPart part = getRVIPart(graph, variable);
+            return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
+        }
     }
     
     @Override
@@ -53,15 +59,19 @@ public class StructuralRVIResolver extends StandardRVIResolver {
             Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
             return RVI.empty(rviBinding);
         }
-        Variable base = variable.getParent(graph);
-        if(base == null) return null;
-        while(!isRVIBase(graph, base)) {
-            base = base.getParent(graph);
+        else {
+            Variable base = variable.getParent(graph);
             if(base == null) return null;
+            while(!isRVIBase(graph, base)) {
+                base = base.getParent(graph);
+                if(base == null) return null;
+            }
+            RVIPart part = getRVIPart(graph, variable);
+            if(part == null) return null;
+            RVI baseRVI = base.getPossibleRVI(graph);
+            if(baseRVI == null) return null;
+            return new RVIBuilder(baseRVI).append(part).toRVI();
         }
-        RVIPart part = getRVIPart(graph, variable);
-        if(part == null) return null;
-        return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
     }
 
     protected boolean isPartOfComponentType(ReadGraph graph, Resource resource)
@@ -112,7 +122,21 @@ public class StructuralRVIResolver extends StandardRVIResolver {
                Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
                GUID guid = new GUID(part.mostSignificant, part.leastSignificant);
                Collection<Resource> queryResult = instances.find(graph, indexRoot, "GUID:"+guid.indexString());
-               if(queryResult.size() != 1) return null;
+               if(queryResult.size() != 1) {
+                   if(queryResult.size() > 1) {
+                       StringBuilder b = new StringBuilder();
+                       boolean first = true;
+                       for(Resource r : queryResult) {
+                           if(first)
+                               first = false;
+                           else
+                               b.append(", ");
+                           b.append(NameUtils.getURIOrSafeNameInternal(graph, r));
+                       }
+                       LOGGER.error("Found {} resources with the same GUID {}: {}.", queryResult.size(), guid, b);
+                   }
+                   return null;
+               }
                return getRVIPath(graph, variable, queryResult.iterator().next());
        }
        
@@ -121,16 +145,20 @@ public class StructuralRVIResolver extends StandardRVIResolver {
     @Override
     protected Variable resolveChild(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
         Collection<Resource> path = getRVIPath(graph, variable, resource);
-        if(path == null) throw new MissingVariableException("Didn't find a variable related to " + resource + ".", resource);
-        for(Resource r : path) variable = variable.browse(graph, r);
+        if (path == null)
+            throw new MissingVariableException("Didn't find a variable related to " + resource + ".", resource);
+        for (Resource r : path)
+            variable = variable.browse(graph, r);
         return variable;
     }
-    
+
     @Override
     protected Variable resolveChild(ReadGraph graph, Variable variable, GuidRVIPart part) throws DatabaseException {
         Collection<Resource> path = getRVIPath(graph, variable, part);
-        if(path == null) throw new MissingVariableException("Didn't find a variable related to " + part + ".");
-        for(Resource r : path) variable = variable.browse(graph, r);
+        if (path == null)
+            throw new MissingVariableException("Didn't find a variable related to " + part + ".", variable.getPossibleRepresents(graph));
+        for (Resource r : path)
+            variable = variable.browse(graph, r);
         return variable;
     }