Fixed InvertBasicExpressionVisitor.invert to keep inverted value type 86/1486/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 20 Feb 2018 22:26:16 +0000 (00:26 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 20 Feb 2018 22:26:16 +0000 (00:26 +0200)
Previously the code would convert all Number-values to Double instead of
passing the inverted value to Variable.setValue in the same type the
original non-inverted value was in.

refs #7777

Change-Id: I78771698d3e2222ede47969e3cceea2166c23a75

bundles/org.simantics.modeling/src/org/simantics/modeling/InvertBasicExpressionVisitor.java

index 11351f06b6a2cf64d05f5e5b2228e5781bd2f617..5bcce3ee415683f32ad4c54065f61ff5ffe8f764 100644 (file)
@@ -126,18 +126,19 @@ public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
     public static void invert(WriteGraph graph, Variable base, String expression, Object value) throws DatabaseException {
         InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
         Expressions.evaluate(replaced(expression), visitor);
-        Object pair = visitor.getResult();
-        if(pair == null) return;
-        if(pair instanceof Triple) {
+        Object result = visitor.getResult();
+        if(result == null) return;
+        if(result instanceof Triple) {
             @SuppressWarnings("unchecked")
-            Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
+            Triple<Double, Double, String> data = (Triple<Double, Double, String>)result;
             String key = data.third.replace(MAGIC, ".");
             String path = getVariablePath(graph, base, key);
             Variable targetVariable = base.browse(graph, path);
             if(value instanceof Number) {
                 if(Math.abs(data.first) > 1e-9) {
-                    Double inverted = (((Number)value).doubleValue() - data.second) / data.first;
-                    targetVariable.setValue(graph, inverted);
+                    double inverted = (double) (((Number)value).doubleValue() - data.second) / data.first;
+                    Object invertedValue = numericValueInType(inverted, value.getClass());
+                    targetVariable.setValue(graph, invertedValue);
                 }
             } else if (value instanceof Boolean) {
                 // TODO: support 'not'
@@ -146,7 +147,20 @@ public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
         }
         
     }
-    
+
+    private static Object numericValueInType(double value, Class<?> type) {
+        if (type == Integer.class) {
+            return (int) value;
+        } else if (type == Long.class) {
+            return (long) value;
+        } else if (type == Byte.class) {
+            return (byte) value;
+        } else if (type == Float.class) {
+            return (float) value;
+        }
+        return value;
+    }
+
     private static String getVariablePath(ReadGraph graph, Variable base, String key) throws DatabaseException {
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
         Resource type = base.getPossibleType(graph);