Fix handling of property variables with no predicate resource. 95/3995/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 16 Mar 2020 19:59:06 +0000 (21:59 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 16 Mar 2020 20:03:12 +0000 (22:03 +0200)
gitlab #498

Change-Id: Iacbfdddca8d143b12950dd5166a835eefc44d270

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/function/All.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NoPredicateResourceException.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java

index 4e775e08a22e12597044218f138aef3cfcaf6637..f52148c2a6b2306d9c8d4b6c0ce9e31cb5e89efe 100644 (file)
@@ -139,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);
                                                        }
                                                }
@@ -201,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);
                                                        }
                                                }
@@ -1164,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;
@@ -1313,7 +1316,7 @@ public class All {
                        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), e);
                } catch (DoesNotContainValueException e) {
@@ -1334,9 +1337,10 @@ public class All {
 
                if (variable.parentResource == null)
                        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), e);
                } catch (DoesNotContainValueException e) {
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NoPredicateResourceException.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NoPredicateResourceException.java
new file mode 100644 (file)
index 0000000..a1b7463
--- /dev/null
@@ -0,0 +1,34 @@
+package org.simantics.db.layer0.variable;
+
+import org.simantics.db.Resource;
+import org.simantics.db.exception.AssumptionException;
+
+public class NoPredicateResourceException extends AssumptionException {
+
+       private static final long serialVersionUID = -374051341908276908L;
+
+       public NoPredicateResourceException(Throwable cause) {
+               super(cause);
+       }
+
+       public NoPredicateResourceException(String message, Throwable cause, Resource... rs) {
+               super(message, cause, rs);
+       }
+
+       public NoPredicateResourceException(String message, Resource... resources) {
+               super(message, resources);
+       }
+
+       public NoPredicateResourceException(String message, Throwable cause) {
+               super(message, cause);
+       }
+
+       public NoPredicateResourceException(String message, int... args) {
+               super(message, args);
+       }
+
+       public NoPredicateResourceException(String message) {
+               super(message);
+       }
+
+}
index b7eaf075edea1c1e051981662954629f70673cf1..c9214e398c69577da69642536c8b932e67e5bc69 100644 (file)
@@ -21,6 +21,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.DatatypeNotFoundException;
 import org.simantics.db.exception.ValidationException;
 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.function.All;
@@ -100,11 +101,15 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
        @Override
        public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       return null;
                return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
        }
 
        @Override
        public String getLabel(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new NoPredicateResourceException("No predicate resource for property " + getName(graph));
                return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
        }
 
@@ -124,10 +129,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                   LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                           LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -145,10 +152,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -183,10 +192,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -200,10 +211,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -324,11 +337,15 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        
        @Override
        public Variable getPredicate(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new MissingVariableException("No predicate for property " + getName(graph));
                return Variables.getVariable(graph, graph.getURI(property.predicate));
        }
        
        @Override
        public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new NoPredicateResourceException("No predicate for property " + getName(graph));
                return property.predicate;
        }