]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/function/All.java
Issues-view menu improvements & Variable-based issue context resolution
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / function / All.java
index 1c9331069ff13f28a56013cb42eaaa514bcd7f20..6a144165247732a36e1d47aa08240c5fbf9c0f86 100644 (file)
@@ -27,10 +27,10 @@ import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.issue.StandardIssue;
 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied2;
+import org.simantics.db.common.primitiverequest.PossibleResource;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.IsEnumeratedValue;
-import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.common.utils.CommonDBUtils;
 import org.simantics.db.common.utils.Functions;
@@ -47,6 +47,7 @@ import org.simantics.db.layer0.exception.VariableException;
 import org.simantics.db.layer0.request.PossibleURI;
 import org.simantics.db.layer0.request.PropertyInfo;
 import org.simantics.db.layer0.request.PropertyInfoRequest;
+import org.simantics.db.layer0.request.UnescapedAssertedPropertyMapOfResource;
 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
 import org.simantics.db.layer0.scl.CompileResourceValueRequest;
 import org.simantics.db.layer0.scl.CompileValueRequest;
@@ -56,6 +57,7 @@ import org.simantics.db.layer0.variable.AbstractVariable;
 import org.simantics.db.layer0.variable.ChildVariableMapRequest;
 import org.simantics.db.layer0.variable.ExternalSetValue;
 import org.simantics.db.layer0.variable.PropertyVariableMapRequest;
+import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable;
 import org.simantics.db.layer0.variable.StandardComposedProperty;
 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
@@ -118,10 +120,20 @@ public class All {
                        
             if (variable.isAsserted()) {
                                if (variable.parentResource != null) {
-                           Layer0 L0 = Layer0.getInstance(graph);
-                                       for(Resource assertion : graph.syncRequest(new ObjectsWithType(variable.parentResource, L0.Asserts, L0.Assertion))) {
-                                               if(variable.property.predicate.equals(graph.getSingleObject(assertion, L0.HasPredicate))) {
-                                                       return graph.getRelatedValue2(assertion, L0.HasObject, variable);
+                                       Map<String, Pair<PropertyInfo, Resource>> assertions = graph.syncRequest(
+                                                       new UnescapedAssertedPropertyMapOfResource(variable.parentResource),
+                                                       TransientCacheAsyncListener.instance());
+
+                                       // NOTE: This optimization assumes the property
+                                       // variable's representation is the asserted object.
+                                       Resource object = variable.getPossibleRepresents(graph);
+                                       if (object != null) {
+                                               return graph.getValue2(object, variable);
+                                       } else {
+                                               for (Pair<PropertyInfo, Resource> assertion : assertions.values()) {
+                                                       if (assertion.first.predicate.equals(variable.property.predicate)) {
+                                                               return graph.getValue2(assertion.second, variable);
+                                                       }
                                                }
                                        }
                                }
@@ -156,13 +168,12 @@ public class All {
                
        try {
                        
-               Layer0 L0 = Layer0.getInstance(graph);
-               
                if(variable.property.hasEnumerationRange) {
                Resource object = variable.getRepresents(graph);
                if(graph.sync(new IsEnumeratedValue(object))) {
+                       Layer0 L0 = Layer0.getInstance(graph);
                        if(graph.isInstanceOf(object, L0.Literal)) {
-                               return graph.getValue(object);
+                               return graph.getValue(object, binding);
                        } else {
                                return graph.getRelatedValue2(variable.getRepresents(graph), L0.HasLabel, binding);
                        }
@@ -171,11 +182,22 @@ public class All {
                        
                if (variable.isAsserted()) {
                        if (variable.parentResource != null) {
-                               for(Resource assertion : graph.syncRequest(new ObjectsWithType(variable.parentResource, L0.Asserts, L0.Assertion))) {
-                                       if(variable.property.predicate.equals(graph.getSingleObject(assertion, L0.HasPredicate))) {
-                                               return graph.getRelatedValue2(assertion, L0.HasObject, context);
-                                       }
-                               }
+                                       Map<String, Pair<PropertyInfo, Resource>> assertions = graph.syncRequest(
+                                                       new UnescapedAssertedPropertyMapOfResource(variable.parentResource),
+                                                       TransientCacheAsyncListener.instance());
+
+                                       // NOTE: This optimization assumes the property
+                                       // variable's representation is the asserted object.
+                                       Resource object = variable.getPossibleRepresents(graph);
+                                       if (object != null) {
+                                               return graph.getValue2(object, variable, binding);
+                                       } else {
+                                               for (Pair<PropertyInfo, Resource> assertion : assertions.values()) {
+                                                       if (assertion.first.predicate.equals(variable.property.predicate)) {
+                                                               return graph.getValue2(assertion.second, variable, binding);
+                                                       }
+                                               }
+                                       }
                        }
                }
                        
@@ -450,6 +472,33 @@ public class All {
             // Fallback: try to ask property resource uri from NodeManager
             return createStandardGraphPropertyVariable(graph, variable, propertyNode);
         }
+        // Final fallback: check types corresponding to
+        // node classification(s) and look for asserted
+        // properties from the URIs specified.
+        if (variable.node != null) {
+            try {
+                @SuppressWarnings("unchecked")
+                Set<String> classifications = variable.node.support.manager.getClassifications(variable.node.node);
+                if (!classifications.isEmpty()) {
+                    for (String uri : classifications) {
+                        Resource type = graph.syncRequest(
+                                new PossibleResource(uri),
+                                TransientCacheAsyncListener.instance());
+                        if (type == null)
+                            continue;
+                        Map<String, Pair<PropertyInfo, Resource>> pm = graph.syncRequest(
+                                new UnescapedAssertedPropertyMapOfResource(type),
+                                TransientCacheAsyncListener.instance());
+                        Pair<PropertyInfo, Resource> pi = pm.get(name);
+                        if (pi != null) {
+                            return new StandardAssertedGraphPropertyVariable(graph, context, null, type, pi.first.predicate, pi.second);
+                        }
+                    }
+                }
+            } catch(NodeManagerException e) {
+                throw new DatabaseException(e);
+            }
+        }
         return null;
     }