]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/userComponent/ComponentTypeCommands.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / userComponent / ComponentTypeCommands.java
index cf6690c311f0bdda988e695dc848d38cafd56390..74ae599e3797f6a06d9c07f2c9ed6bc7d4789843 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.modeling.userComponent;
 
  *******************************************************************************/
 package org.simantics.modeling.userComponent;
 
-import gnu.trove.map.hash.THashMap;
-
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -44,9 +42,15 @@ 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.selectionview.SelectionViewResources;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.structural2.utils.StructuralUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.map.hash.THashMap;
 
 public class ComponentTypeCommands {
 
 
 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);
     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);
@@ -248,7 +252,7 @@ public class ComponentTypeCommands {
 
         Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
 
         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;
         }
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }
@@ -262,7 +266,7 @@ public class ComponentTypeCommands {
 
        Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
 
        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;
         }
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }
@@ -325,29 +329,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) {
     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;
         }
 
                     " 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 +368,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) {
     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;
         }
                     " in " + NameUtils.getSafeName(graph, type) + ".");
             return;
         }
@@ -386,7 +394,7 @@ public class ComponentTypeCommands {
 
         Datatype newDatatype = TypeConversion.convertSCLTypeToDatatype(newSCLType);
         if(newDatatype == null) {
 
         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);
             return null;
         }
         Binding newBinding = Bindings.getBinding(newDatatype);
@@ -415,7 +423,7 @@ public class ComponentTypeCommands {
             Resource type, Resource relation, String newSCLType) throws DatabaseException {
         Resource object = getAssertedObject(g, type, relation);
         if(object == null) {
             Resource type, Resource relation, String newSCLType) 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;
         }
                     " in " + NameUtils.getSafeName(g, type) + ".");
             return;
         }