]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
possibleObjectForType utility 51/3151/4
authorAntti Villberg <antti.villberg@semantum.fi>
Sun, 25 Aug 2019 10:38:56 +0000 (13:38 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sun, 25 Aug 2019 21:02:50 +0000 (21:02 +0000)
gitlab #362

Change-Id: I3c88a860df44be625b8f63cbacdd906ea79264e9

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfoRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java

index f3753ce80bfc3949c84914c234c14f76520c29e4..4d208b4571d14faaac8772dc30e3e0df241d1ef9 100644 (file)
@@ -1,3 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
 package org.simantics.db.layer0.request;
 
 import java.util.Collection;
@@ -20,6 +31,7 @@ public class PropertyInfo {
        public final Resource predicate;
        public final String name;
        public final boolean isHasProperty;
+       public final boolean isFunctional;
        public final Set<String> classifications;
        public final VariableBuilder builder;
        public final Resource literalRange;
@@ -30,9 +42,10 @@ public class PropertyInfo {
        public final Map<String,Pair<Resource, ChildReference>> subliteralPredicates;
        public final ValueAccessor valueAccessor;
        public final boolean hasEnumerationRange;
-       public PropertyInfo(Resource predicate, String name, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) {
+       public PropertyInfo(Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) {
                this.predicate = predicate;
                this.name = name;
+               this.isFunctional = isFunctional;
                this.isHasProperty = isHasProperty;
                this.classifications = classifications;
                this.builder = builder;
@@ -45,7 +58,7 @@ public class PropertyInfo {
                this.valueAccessor = valueAccessor;
                this.hasEnumerationRange = hasEnumerationRange;
        }
-       public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException {
+       public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException {
 
                Layer0 L0 = Layer0.getInstance(graph);
                if(literalRange != null) {
@@ -58,7 +71,7 @@ public class PropertyInfo {
                
                Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
                
-               return new PropertyInfo(predicate, name, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
+               return new PropertyInfo(predicate, name, isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
 
        }
        public boolean hasClassification(String classification) {
@@ -75,6 +88,8 @@ public class PropertyInfo {
                .append(requiredValueType)
                .append(", predicate=")
                .append(predicate)
+               .append(", isFunctional=")
+               .append(isFunctional)
                .append(", isHasProperty=")
                .append(isHasProperty)
                .append(", hasEnumerationRange=")
index 2a181674c49a3cdf06f0317bcb382c5bbe002221..90e63e29f639c10cc7d4ec04caf053c0909116e6 100644 (file)
@@ -41,6 +41,8 @@ final public class PropertyInfoRequest extends ResourceRead<PropertyInfo> {
                String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
                if(name != null) name = name.intern();
                
+               boolean isFunctional = graph.isInstanceOf(resource, L0.FunctionalRelation);
+               
                Set<String> classifications = graph.sync(new ClassificationsRequest(graph.getPrincipalTypes(resource)));
                VariableBuilder<?> variableBuilder = graph.getPossibleAdapter(resource, VariableBuilder.class);
                
@@ -88,13 +90,13 @@ final public class PropertyInfoRequest extends ResourceRead<PropertyInfo> {
                                                }
                                        }
                                        
-                                       return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, map, accessor, hasEnumerationRange);
+                                       return PropertyInfo.make(graph, resource, name, isFunctional, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, map, accessor, hasEnumerationRange);
                                        
                                }
                        }
                }
                
-               return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, Collections.<String,Pair<Resource,ChildReference>>emptyMap(), accessor, hasEnumerationRange);
+               return PropertyInfo.make(graph, resource, name, isFunctional, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, Collections.<String,Pair<Resource,ChildReference>>emptyMap(), accessor, hasEnumerationRange);
                
        }
        
index 610c8c8bd42d8735868cf1f80e4603ae5e63b7d8..830f7a6bb74776ece6a99b35eb3b9b51999cc10a 100644 (file)
@@ -71,7 +71,6 @@ import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.request.DelayedWriteRequest;
 import org.simantics.db.common.request.ObjectsWithType;
-import org.simantics.db.common.request.PossibleChild;
 import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.common.utils.CommonDBUtils;
@@ -116,9 +115,7 @@ import org.simantics.graph.representation.PrettyPrintTG;
 import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.layer0.Layer0;
 import org.simantics.operation.Layer0X;
-import org.simantics.scl.compiler.environment.Environments;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
-import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.function.Function;
@@ -1382,6 +1379,41 @@ public class Layer0Utils {
        return result;
     }
 
+    public static Resource possibleObjectForType(ReadGraph graph, Resource type, Resource relation) throws DatabaseException {
+        PropertyInfo pi = graph.syncRequest(new PropertyInfoRequest(relation));
+        return possibleObjectForType(graph, type, relation, pi.isFunctional);
+    }
+
+    public static Resource possibleObjectForType(ReadGraph graph, Resource type, Resource relation, boolean functional) throws DatabaseException {
+        if(functional) {
+            Layer0 L0 = Layer0.getInstance(graph);
+            Resource result = graph.getPossibleObject(type, relation);
+            if(result != null)
+                return result;
+            for(Resource su : graph.getObjects(L0.Inherits, type)) {
+                Resource r = possibleObjectForType(graph, su, relation, functional);
+                if(r != null) {
+                    if(result != null)
+                        return null;
+                    result = r;
+                }
+            }
+            return result;
+        } else {
+            Set<Resource> found = objectsForTypeNonFunctional(graph, type, relation, new HashSet<>());
+            return found.size() == 1 ? found.iterator().next() : null;
+        }
+    }
+
+    private static Set<Resource> objectsForTypeNonFunctional(ReadGraph graph, Resource type, Resource relation, Set<Resource> found) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        found.addAll(graph.getObjects(type, relation));
+        for(Resource su : graph.getObjects(L0.Inherits, type)) {
+            objectsForTypeNonFunctional(graph, su, relation, found);
+        }
+        return found;
+    }
+
     public static Resource getPossiblePredicateByNameFromType(ReadGraph graph, Resource type, String name) throws DatabaseException {
        Map<String,Resource> domain = getDomainOf(graph, type);
        return domain.get(name); 
index ed58556c7e570814bd4ce25e5473a6821f2c33bc..7927614cd37f9779292ee4ace98e5fd4ba7f00f0 100644 (file)
@@ -38,7 +38,7 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
     private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
     
        protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
-                       false, Collections.<String> emptySet(), null, null, null, null, null, null,
+                       false, false, Collections.<String> emptySet(), null, null, null, null, null, null,
                        Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
                        null, false);