]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/userComponent/ComponentTypeCommands.java
Merge changes I78c3a258,I7bf72f04
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / userComponent / ComponentTypeCommands.java
index cf6690c311f0bdda988e695dc848d38cafd56390..9cf93fac5ce83d9067b99d7ebbb7e0da6a272f63 100644 (file)
@@ -11,8 +11,7 @@
  *******************************************************************************/
 package org.simantics.modeling.userComponent;
 
-import gnu.trove.map.hash.THashMap;
-
+import java.util.Collections;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -29,11 +28,13 @@ import org.simantics.db.Resource;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.EnumerationMap;
+import org.simantics.db.common.request.IsEnumeratedValue;
 import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ServiceException;
-import org.simantics.db.layer0.request.ModelInstances;
+import org.simantics.db.layer0.QueryIndexUtils;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;
@@ -44,9 +45,16 @@ import org.simantics.scl.runtime.tuple.Tuple3;
 import org.simantics.selectionview.SelectionViewResources;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.structural2.utils.StructuralUtils;
+import org.simantics.utils.strings.AlphanumComparator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.map.hash.THashMap;
 
 public class ComponentTypeCommands {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ComponentTypeCommands.class);
+
     public static void applyCode(WriteGraph g, Resource componentType, String code) throws DatabaseException {
         StructuralResource2 STR = StructuralResource2.getInstance(g);
         g.claimLiteral(componentType, STR.ProceduralComponentType_code, code, Bindings.STRING);
@@ -195,6 +203,11 @@ public class ComponentTypeCommands {
 
     public static void setRequiredType(WriteGraph g, Resource componentType, Resource property,
             String requiredType) throws DatabaseException {
+        setRequiredType(g, componentType, property, requiredType, null);
+    }
+
+    public static void setRequiredType(WriteGraph g, Resource componentType, Resource property,
+            String requiredType, Resource possibleType) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(g);
         g.claimLiteral(property, L0.RequiresValueType, requiredType);
 
@@ -207,17 +220,34 @@ public class ComponentTypeCommands {
             }
         }
 
+        // We assert the range of the property only if we are given a dedicated graph value type
+        if(g.hasStatement(property, L0.HasRange))
+            g.deny(property, L0.HasRange);
+
+        if(possibleType != null) {
+            // We have a dedicated graph type for this SCL value type
+            if(g.hasStatement(possibleType, L0.Enumeration)) {
+                // This value type is an enumeration - let's constrain the range of this predicate to match the enumeration type only
+                g.claim(property, L0.HasRange, possibleType);
+            }
+        }
+
         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
         g.addMetadata(cm.add("Set required type "+ requiredType + " for component/annotation " + property));
     }
     
     public static void editType(WriteGraph graph, Resource componentType, Resource property, boolean convertDefaultValue, String newValue) throws DatabaseException {
-        ComponentTypeCommands.setRequiredType(graph, componentType, property, newValue);
+        editType(graph, componentType, property, convertDefaultValue, newValue, null);
+    }
+
+    public static void editType(WriteGraph graph, Resource componentType, Resource property, boolean convertDefaultValue, String newValue, Resource possibleType) throws DatabaseException {
+        ComponentTypeCommands.setRequiredType(graph, componentType, property, newValue, possibleType);
         if (convertDefaultValue) {
-            ComponentTypeCommands.convertDefaultValue(graph, componentType, property, newValue);
-            Map<String, Resource> instances = graph.sync(new ModelInstances(componentType, componentType));
-            for(Resource instance : instances.values()) {
-                ComponentTypeCommands.convertInstantiatedValue(graph, instance, property, newValue);
+            ComponentTypeCommands.convertDefaultValue(graph, componentType, property, newValue, possibleType);
+            for (Resource indexRoot : Layer0Utils.listIndexRoots(graph)) {
+                for(Resource instance : QueryIndexUtils.searchByTypeShallow(graph, indexRoot, componentType)) {
+                    ComponentTypeCommands.convertInstantiatedValue(graph, instance, property, newValue, componentType);
+                }
             }
         }
     }
@@ -248,7 +278,7 @@ public class ComponentTypeCommands {
 
         Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
-            System.err.println("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
+            LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }
@@ -262,15 +292,33 @@ public class ComponentTypeCommands {
 
        Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
-            System.err.println("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
+            LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }
+
+        Layer0 L0 = Layer0.getInstance(g);
+        Resource range = g.getPossibleObject(relation, L0.HasRange);
+        if (range != null) {
+            if(g.hasStatement(range, L0.Enumeration)) {
+                Map<String,Resource> values = g.syncRequest(new EnumerationMap(range));
+                Resource value = values.get(valueText);
+                if (value != null) {
+                    for(Resource assertion : g.getObjects(type, L0.Asserts)) {
+                        Resource p = g.getSingleObject(assertion, L0.HasPredicate);
+                        if (p.equals(relation)) {
+                            g.deny(assertion, L0.HasObject, object);
+                            g.claim(assertion, L0.HasObject, value);
+                        }
+                    }
+                }
+                return;
+            }
+        }
         
         if(valueText.length() > 0 && valueText.charAt(0) == '=') {
                
                String expression = valueText.substring(1);
-               Layer0 L0 = Layer0.getInstance(g);
                ModelingResources MOD = ModelingResources.getInstance(g);
                if(!g.isInstanceOf(object, MOD.SCLValue)) {
                        Resource assertion = g.getSingleObject(object, L0.HasObjectInverse);
@@ -284,7 +332,6 @@ public class ComponentTypeCommands {
             
         } else {
                
-               Layer0 L0 = Layer0.getInstance(g);
                ModelingResources MOD = ModelingResources.getInstance(g);
                if(g.isInstanceOf(object, MOD.SCLValue)) {
                        Resource assertion = g.getSingleObject(object, L0.HasObjectInverse);
@@ -325,29 +372,33 @@ public class ComponentTypeCommands {
     public static void setUnit(WriteGraph graph, Resource type, Resource relation, String unit) throws DatabaseException {
         Resource object = getAssertedObject(graph, type, relation);
         if (object == null) {
-            System.err.println("Didn't find assertion for " + NameUtils.getSafeName(graph, relation) + 
+            LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(graph, relation) + 
                     " in " + NameUtils.getSafeName(graph, type) + ".");
             return;
         }
 
-        Datatype dt = graph.getDataType(object);
-        if (dt instanceof NumberType) {
-            NumberType nt = (NumberType) dt;
-            Binding ntb = Bindings.getBindingUnchecked(Datatype.class);
-            nt.setUnit(unit);
-
-            Layer0 L0 = Layer0.getInstance(graph);
-            Layer0X L0X = Layer0X.getInstance(graph);
-            
-            String oldUnit = graph.getPossibleRelatedValue2(relation, L0X.HasUnit, Bindings.STRING);
+        Layer0 L0 = Layer0.getInstance(graph);
+        Layer0X L0X = Layer0X.getInstance(graph);
+        boolean hasRequiresDataType = graph.hasStatement(relation, L0X.RequiresDataType);
+        if (hasRequiresDataType) {
+            Datatype dt = graph.getDataType(object);
+            if (dt instanceof NumberType) {
+                NumberType nt = (NumberType) Bindings.DATATYPE.cloneUnchecked(dt);
+                nt.setUnit(unit);
+                graph.claimLiteral(object, L0.HasDataType, L0.DataType, nt, Bindings.DATATYPE);
+                graph.claimLiteral(relation, L0X.RequiresDataType, L0.DataType, nt, Bindings.DATATYPE);
+            }
+        }
 
-            graph.claimLiteral(object, L0.HasDataType, L0.DataType, nt, ntb);
-            graph.claimLiteral(relation, L0X.RequiresDataType, L0.DataType, nt, ntb);
-            graph.claimLiteral(relation, L0X.HasUnit, unit, Bindings.STRING);
-            
-            CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
-            graph.addMetadata(cm.add("Setted unit from " + oldUnit + " to " + unit + " for component/annotation " + type));
+        String oldUnit = graph.getPossibleRelatedValue2(relation, L0X.HasUnit, Bindings.STRING);
+        if (unit != null) {
+            graph.claimLiteral(relation, L0X.HasUnit, L0.String, unit, Bindings.STRING);
+        } else {
+            graph.denyValue(relation, L0X.HasUnit);
         }
+
+        CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+        graph.addMetadata(cm.add("Set unit from " + oldUnit + " to " + unit + " for component/annotation " + type + " property " + relation));
     }
 
     /**
@@ -360,7 +411,7 @@ public class ComponentTypeCommands {
     public static void setRange(WriteGraph graph, Resource type, Resource relation, String newRange) throws DatabaseException {
         Resource object = getAssertedObject(graph, type, relation);
         if (object == null) {
-            System.err.println("Didn't find assertion for " + NameUtils.getSafeName(graph, relation) + 
+            LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(graph, relation) + 
                     " in " + NameUtils.getSafeName(graph, type) + ".");
             return;
         }
@@ -386,7 +437,7 @@ public class ComponentTypeCommands {
 
         Datatype newDatatype = TypeConversion.convertSCLTypeToDatatype(newSCLType);
         if(newDatatype == null) {
-            System.err.println("Couldn't convert default value to <" + newSCLType + ">.");
+            LOGGER.warn("Couldn't convert default value to <" + newSCLType + ">.");
             return null;
         }
         Binding newBinding = Bindings.getBinding(newDatatype);
@@ -413,34 +464,95 @@ public class ComponentTypeCommands {
 
     public static void convertDefaultValue(WriteGraph g,
             Resource type, Resource relation, String newSCLType) throws DatabaseException {
+        convertDefaultValue(g, type, relation, newSCLType, null);
+    }
+
+    private static Resource findAssertionWithPO(ReadGraph graph, Resource possibleType, Resource predicate, Resource object) throws DatabaseException {
+           Layer0 L0 = Layer0.getInstance(graph);
+        for(Resource assertion : graph.getObjects(possibleType, L0.Asserts)) {
+            Resource p = graph.getSingleObject(assertion, L0.HasPredicate);
+            Resource o = graph.getSingleObject(assertion, L0.HasObject);
+            if(predicate.equals(p) && object.equals(o))
+                return assertion;
+        }
+        return null;
+    }
+
+    public static void convertDefaultValue(WriteGraph g,
+            Resource type, Resource relation, String newSCLType, Resource possibleType) throws DatabaseException {
         Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
-            System.err.println("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
+            LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(g, relation) + 
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }
 
+        Layer0 L0 = Layer0.getInstance(g);
+        if(possibleType != null) {
+            if(g.hasStatement(possibleType, L0.Enumeration)) {
+                if(!g.isInstanceOf(object, possibleType)) {
+                    Map<String, Resource> enumMap = g.syncRequest(new EnumerationMap(possibleType));
+                    String firstKey = Collections.min(enumMap.keySet(), AlphanumComparator.COMPARATOR);
+                    Resource defaultValue = enumMap.get(firstKey);
+
+                    if (defaultValue != null) {
+                        Resource assertion = findAssertionWithPO(g, type, relation, object);
+                        if(assertion != null) {
+                            g.deny(assertion, L0.HasObject);
+                            g.claim(assertion, L0.HasObject, defaultValue);
+                            return;
+                        } else {
+                            Layer0Utils.assert_(g, type, relation, defaultValue);
+                            return;
+                        }
+                    }
+                } else {
+                       return;
+                }
+            }
+        }
+
         Tuple tuple = getDatatypeValueAndBinding(g, object, newSCLType);
         if (tuple == null)
             return;
 
-        Layer0 L0 = Layer0.getInstance(g);
+        if(g.sync(new IsEnumeratedValue(object))) {
+            Resource assertion = findAssertionWithPO(g, type, relation, object);
+            object = g.newResource();
+            g.claim(object, L0.InstanceOf, L0.Literal);
+            if(assertion != null) {
+                g.deny(assertion, L0.HasObject);
+                g.claim(assertion, L0.HasObject, object);
+            }
+        }
+
         g.claimLiteral(object, L0.HasDataType, L0.DataType, tuple.get(0), Bindings.getBindingUnchecked(Datatype.class));
         g.claimLiteral(object, L0.HasValueType, g.<String>getRelatedValue(relation, L0.RequiresValueType, Bindings.STRING), Bindings.STRING);
         g.claimValue(object, tuple.get(1), (Binding)tuple.get(2));
 
     }
 
-    public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType)
+    public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType) throws DatabaseException {
+       convertInstantiatedValue(g, instance, relation, newSCLType, null);
+    }
+
+    public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType, Resource possibleType)
             throws DatabaseException {
 
         Statement stm = g.getPossibleStatement(instance, relation);
         if(stm != null && !stm.isAsserted(instance)) {
 
+            Layer0 L0 = Layer0.getInstance(g);
             Resource object = stm.getObject();
 
+            if(g.sync(new IsEnumeratedValue(object))) {
+                if(!g.isInstanceOf(object, possibleType)) {
+                    g.deny(instance, relation);
+                }
+                return;
+            }
+
             // We can only convert literals
-            Layer0 L0 = Layer0.getInstance(g);
             if(!g.isInstanceOf(object, L0.Literal)) return;
 
             Tuple tuple = getDatatypeValueAndBinding(g, object, newSCLType);