]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/function/All.java
Fix handling of property variables with no predicate resource.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / function / All.java
index 08ed3edf2650873f316c8ceb1228bcf89e1afee1..f52148c2a6b2306d9c8d4b6c0ce9e31cb5e89efe 100644 (file)
@@ -38,14 +38,17 @@ import org.simantics.db.common.utils.Functions;
 import org.simantics.db.common.utils.ListUtils;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.common.utils.NearestOwnerFinder;
 import org.simantics.db.common.validation.L0Validations;
+import org.simantics.db.exception.AdaptionException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.DoesNotContainValueException;
 import org.simantics.db.exception.NoSingleResultException;
 import org.simantics.db.exception.RuntimeDatabaseException;
+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.exception.PendingVariableException;
-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;
@@ -136,7 +139,7 @@ public class All {
                                                return graph.getValue2(object, variable);
                                        } else {
                                                for (Pair<PropertyInfo, Resource> assertion : assertions.values()) {
-                                                       if (assertion.first.predicate.equals(variable.property.predicate)) {
+                                                       if (assertion.first.predicate.equals(variable.getPossiblePredicateResource(graph))) {
                                                                return graph.getValue2(assertion.second, variable);
                                                        }
                                                }
@@ -167,7 +170,7 @@ public class All {
                 if(value == null) throw new MissingVariableValueException(variable.getPossibleURI(graph));
                 return value.getValue(binding);
             } catch (AdaptException e) {
-                throw new DatabaseException(e);
+                throw new AdaptionException("Could not get value for " + context.getURI(graph), e);
             }
         }
                
@@ -198,7 +201,7 @@ public class All {
                                                return graph.getValue2(object, variable, binding);
                                        } else {
                                                for (Pair<PropertyInfo, Resource> assertion : assertions.values()) {
-                                                       if (assertion.first.predicate.equals(variable.property.predicate)) {
+                                                       if (assertion.first.predicate.equals(variable.getPossiblePredicateResource(graph))) {
                                                                return graph.getValue2(assertion.second, variable, binding);
                                                        }
                                                }
@@ -267,7 +270,7 @@ public class All {
                try {
                        modifier.apply(graph, context, value, Bindings.getBinding(value.getClass()));
                } catch (BindingConstructionException e) {
-                       throw new DatabaseException(e);
+                       throw new org.simantics.db.exception.BindingException("",e);
                }
 
        }
@@ -567,9 +570,9 @@ public class All {
             // Process graph properties
             for(Resource predicate : predicates) {
                
-                       PropertyInfo info = graph.isImmutable(predicate) ?
-                                       graph.syncRequest(new PropertyInfoRequest(predicate), TransientCacheAsyncListener.<PropertyInfo>instance()) :
-                                               graph.syncRequest(new PropertyInfoRequest(predicate));
+                       PropertyInfo info = //graph.isImmutable(predicate) ?
+                                       graph.syncRequest(new PropertyInfoRequest(predicate), TransientCacheAsyncListener.<PropertyInfo>instance());// :
+                                               //graph.syncRequest(new PropertyInfoRequest(predicate));
 
                        if(!info.isHasProperty) continue;
                                        
@@ -608,6 +611,8 @@ public class All {
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
                        Variable parent = context.getParent(graph);
                        Resource container = parent.getPossibleRepresents(graph);
+                       if(container == null)
+                               return null;
                        Map<String,Resource> methods = graph.syncRequest(new UnescapedMethodMapOfResource(container));
                        Resource predicate = methods.get(name);
                        if(predicate != null) {
@@ -623,6 +628,8 @@ public class All {
                public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
                        Variable parent = context.getParent(graph);
                        Resource container = parent.getPossibleRepresents(graph);
+                       if(container == null)
+                               return Collections.emptyMap();
                        Map<String,Resource> methods = graph.syncRequest(new UnescapedMethodMapOfResource(container));
                        for(Map.Entry<String, Resource> entry : methods.entrySet()) {
                                String name = entry.getKey();
@@ -985,7 +992,7 @@ public class All {
        
        public static PropertyInfo getPossiblePropertyInfoFromContext(ReadGraph graph, Variable variable, Resource context, String name) throws DatabaseException {
            if(context == null) return null;
-               Map<String, PropertyInfo> predicates = graph.syncRequest(new UnescapedPropertyMapOfResource(context));
+               Map<String, PropertyInfo> predicates = graph.syncRequest(new UnescapedPropertyMapOfResource(context), TransientCacheListener.instance());
                return predicates.get(name);
        }
 
@@ -1157,8 +1164,11 @@ public class All {
        if(property instanceof StandardGraphPropertyVariable) {
                StandardGraphPropertyVariable variable = (StandardGraphPropertyVariable)property;
                if (variable.parentResource != null) {
-                       Statement stm = graph.getPossibleStatement(variable.parentResource, variable.property.predicate);
-                       return stm != null && stm.isAsserted(variable.parentResource);
+                       Resource predicate = variable.getPossiblePredicateResource(graph);
+                       if (predicate != null) {
+                                       Statement stm = graph.getPossibleStatement(variable.parentResource, predicate);
+                                       return stm != null && stm.isAsserted(variable.parentResource);
+                       }
                        }
        }
        return Boolean.FALSE;
@@ -1303,14 +1313,14 @@ public class All {
                }
 
                if (variable.parentResource == null)
-                       throw new VariableException("Variable is not represented by any resource (URI=" + variable.getPossibleURI(graph) + ").");
+                       throw new InvalidVariableException("Variable is not represented by any resource (URI=" + variable.getPossibleURI(graph) + ").");
 
                try {
-                       return graph.getRelatedValue2(variable.parentResource, variable.property.predicate, variable);
+                       return graph.getRelatedValue2(variable.parentResource, variable.getPredicateResource(graph), variable);
                } catch (NoSingleResultException e) {
-                       throw new MissingVariableValueException(variable.getPossibleURI(graph));
+                       throw new MissingVariableValueException(variable.getPossibleURI(graph), e);
                } catch (DoesNotContainValueException e) {
-                       throw new MissingVariableValueException(variable.getPossibleURI(graph));
+                       throw new MissingVariableValueException(variable.getPossibleURI(graph), e);
                }
                
        }
@@ -1326,14 +1336,15 @@ public class All {
                }
 
                if (variable.parentResource == null)
-                       throw new VariableException("Variable is not represented by any resource (URI=" + variable.getPossibleURI(graph) + ").");
+                       throw new MissingVariableException("Variable is not represented by any resource (URI=" + variable.getPossibleURI(graph) + ").", context.getPossibleRepresents(graph));
+               
 
                try {
-                       return graph.getRelatedValue2(variable.parentResource, variable.property.predicate, variable);
+                       return graph.getRelatedValue2(variable.parentResource, variable.getPredicateResource(graph), variable);
                } catch (NoSingleResultException e) {
-                       throw new MissingVariableValueException(variable.getPossibleURI(graph));
+                       throw new MissingVariableValueException(variable.getPossibleURI(graph), e);
                } catch (DoesNotContainValueException e) {
-                       throw new MissingVariableValueException(variable.getPossibleURI(graph));
+                       throw new MissingVariableValueException(variable.getPossibleURI(graph), e);
                }
                
        }
@@ -1497,7 +1508,7 @@ public class All {
         ClusteringSupport cs = graph.getService(ClusteringSupport.class);
         if(cs.isClusterSet(resource) && !base.equals(resource)) return resource;
         
-        Resource nearest = CommonDBUtils.getNearestOwner(graph, Collections.singletonList(resource));
+        Resource nearest = NearestOwnerFinder.getNearestOwner(graph, resource);
         if(nearest == null) return null;
         
         return getPossibleNearestClusterSet(graph, base, nearest);