]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java
Fix handling of property variables with no predicate resource.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardGraphPropertyVariable.java
index 13977a0e5a4397be7e2a063c75fe9b77aba9aa7c..c9214e398c69577da69642536c8b932e67e5bc69 100644 (file)
@@ -11,31 +11,38 @@ import org.simantics.databoard.accessor.reference.ChildReference;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
 import org.simantics.databoard.type.Datatype;
+import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.validation.L0Validations;
 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.exception.VariableException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.layer0.request.PropertyInfo;
 import org.simantics.db.layer0.request.PropertyInfoRequest;
+import org.simantics.db.layer0.scl.SCLDatabaseException;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.layer0.Layer0;
 import org.simantics.utils.Development;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
-       protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
-                       false, Collections.<String> emptySet(), null, null, null, null, null, null,
-                       Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
-                       null, false);
+    private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
+
+    protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null, true,
+            false, false, Collections.<String> emptySet(), null, null, null, null, null, null,
+            Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
+            null, false);
 
        final public Variable parent;
        final public Resource parentResource;
@@ -94,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);
        }
 
@@ -117,10 +128,14 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        public <T> T getValue(ReadGraph graph) throws DatabaseException {
                
                if(Development.DEVELOPMENT) {
-                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                       if(error != null) {
-                               Logger.defaultLogError(new ValidationException(error));
-                               //throw new ValidationException(error);
+                       if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                           LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
+                               }
                        }
                }
                
@@ -136,18 +151,24 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                        return getValue(graph);         
 
                if(Development.DEVELOPMENT) {
-                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                       if(error != null) {
-                               Logger.defaultLogError(new ValidationException(error));
-                               throw new ValidationException(error);
+                       if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
+                               }
                        }
                }
         
                try {
                        
-                       return (T)getValueAccessor(graph).getValue(graph, this, binding);
-               } catch (PendingVariableException e) {
-                   throw e;
+                       return (T) getValueAccessor(graph).getValue(graph, this, binding);
+               } catch (SCLDatabaseException e) { // these can be thrown when compiling e.g. derived properties
+                       throw e;
+               } catch (MissingVariableValueException | PendingVariableException e) {
+                       throw e;
                } catch (Throwable t) {
                        throw new MissingVariableValueException(t);
                }
@@ -157,15 +178,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        @Override
        public Resource getRepresents(ReadGraph graph) throws DatabaseException {
                if(represents == null)
-                       throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
+                       throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
                return represents;
-//             return graph.getSingleObject(parentResource, property.predicate);
        }
 
        @Override
        public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
-//        if(parentResource == null) return null;
-//             return graph.getPossibleObject(parentResource, property.predicate);
                return represents;
        }
 
@@ -173,10 +191,14 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
                
                if(Development.DEVELOPMENT) {
-                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                       if(error != null) {
-                               Logger.defaultLogError(new ValidationException(error));
-                               //throw new ValidationException(error);
+                       if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
+                               }
                        }
                }
                
@@ -188,10 +210,14 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        public void setValue(WriteGraph graph, Object value) throws DatabaseException {
                
                if(Development.DEVELOPMENT) {
-                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                       if(error != null) {
-                               Logger.defaultLogError(new ValidationException(error));
-                               throw new ValidationException(error);
+                       if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
+                               }
                        }
                }
                
@@ -211,16 +237,6 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
        @Override
        public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
-//<<<<<<< .mine
-//             
-//     Binding binding = getPossibleDefaultBinding(graph);
-//        if(binding != null) return binding.type();
-//
-//             Variant vt = getVariantValue(graph);
-//             binding = vt.getBinding();
-//             if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
-//             return binding.type();
-//=======
                Datatype type;
                try {                   
                        type = getValueAccessor(graph).getDatatype(graph, this);
@@ -231,13 +247,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                if (type == null) {
                        String uri = this.getPossibleURI(graph);
                        if (uri != null)
-                               throw new DatabaseException("No data type for " + uri);
+                               throw new DatatypeNotFoundException("No data type for " + uri);
                        else
-                               throw new DatabaseException("No data type for " + this.getIdentifier());
+                               throw new DatatypeNotFoundException("No data type for " + this.getIdentifier());
                }
                
                return type;
-//>>>>>>> .r30794
                
        }
        
@@ -304,8 +319,8 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                        return valueMap.getVariable(graph, this, name);
                } catch (DatabaseException e) {
                        return null;
-               } catch (Throwable t) {
-                       System.err.println("err: " + getURI(graph) + "  # " + name);
+               } catch (Exception t) {
+                       LOGGER.error("getPossibleDomainProperty is implemented incorrectly, but returns null on Exception for backward compatibility. URI="+getURI(graph)+", name="+name+".", t);
                        return null;
                }
                
@@ -322,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;
        }
        
@@ -401,7 +420,9 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
        protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
                if(represents == null) return All.standardPropertyDomainChildren;
-               return graph.getPossibleRelatedValue2(represents, Layer0.getInstance(graph).domainChildren, this);
+           Resource domainChildren = Layer0.getInstance(graph).domainChildren;
+               return graph.getPossibleRelatedValue2(represents, domainChildren, 
+                               new StandardGraphPropertyVariable(graph, this, domainChildren));
        }
 
        protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {