]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/impl/MappedPropertyStyleEvaluator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.profile / src / org / simantics / scenegraph / profile / impl / MappedPropertyStyleEvaluator.java
index 163f15044353d9c547a036844f16576d443d64e8..dbf35541808c0cdeb4c5023a750d000ce802126d 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.scenegraph.profile.impl;\r
-\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.LinkedList;\r
-import java.util.Stack;\r
-\r
-import org.simantics.basicexpression.analysis.DepthFirstAdapter;\r
-import org.simantics.basicexpression.node.AAddressValue;\r
-import org.simantics.basicexpression.node.AConstantValue;\r
-import org.simantics.basicexpression.node.ADivMultiplicative;\r
-import org.simantics.basicexpression.node.AFunctionPrimary;\r
-import org.simantics.basicexpression.node.AMultMultiplicative;\r
-import org.simantics.basicexpression.node.APlusExpression;\r
-import org.simantics.basicexpression.node.ARangeValue;\r
-import org.simantics.basicexpression.node.ASequenceArgList;\r
-import org.simantics.basicexpression.node.ASingleArgList;\r
-import org.simantics.basicexpression.node.AStringValue;\r
-import org.simantics.basicexpression.node.AVariablePrimary;\r
-import org.simantics.basicexpression.node.PArgList;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.Valuations;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scl.reflection.OntologyVersions;\r
-\r
-public class MappedPropertyStyleEvaluator extends DepthFirstAdapter\r
-{\r
-\r
-    public static class ApplicationException extends Exception {\r
-\r
-        private static final long serialVersionUID = 1L;\r
-\r
-        public ApplicationException(String message) {\r
-            super(message);\r
-        }\r
-\r
-    }\r
-\r
-    public interface Function {\r
-        Object evaluate(Collection<Object> args) throws ApplicationException;\r
-    }\r
-\r
-    final ReadGraph graph;\r
-    final Layer0 b;\r
-    final Resource component;\r
-\r
-    public MappedPropertyStyleEvaluator(ReadGraph graph, Resource element) throws DatabaseException {\r
-        this.graph = graph;\r
-        b = Layer0.getInstance(graph);\r
-       String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/ElementToComponent");\r
-        Resource toComponent = graph.getResource(uri);\r
-        component = graph.getPossibleObject(element, toComponent);\r
-    }\r
-\r
-\r
-    Stack<Object> stack = new Stack<Object>();\r
-    HashMap<String, Function> builtins = new HashMap<String, Function>();\r
-\r
-    public Object getResult() {\r
-        if (stack.isEmpty())\r
-            return null;\r
-        return stack.pop();\r
-    }\r
-\r
-    @Override\r
-    public void outAConstantValue(AConstantValue node)\r
-    {\r
-//        System.out.println("eval constant " + node);\r
-        stack.push(Double.valueOf(node.toString()));\r
-    }\r
-\r
-    @Override\r
-    public void outAStringValue(AStringValue node)\r
-    {\r
-//        System.out.println("eval string " + node.toString());\r
-        String value = node.toString();\r
-        stack.push(value.substring(1, value.length()-2).trim());\r
-    }\r
-\r
-    @Override\r
-    public void outAVariablePrimary(AVariablePrimary node)\r
-    {\r
-\r
-        try {\r
-\r
-            Valuations vs = graph.adapt(component, Valuations.class);\r
-            if(vs == null) {\r
-                stack.push("No Variables for mapped component.");\r
-                return;\r
-            }\r
-            Resource valueResource = vs.getValue(graph, null, "BaseRealization", node.toString().trim());\r
-            if(valueResource == null) {\r
-                stack.push("Invalid value identifier based on  '" + node.toString().trim() + "'");\r
-                return;\r
-            }\r
-\r
-            Object value = graph.getValue(valueResource);\r
-            stack.push(value);\r
-\r
-        } catch (DatabaseException e) {\r
-\r
-            stack.push(e.toString());\r
-\r
-        }\r
-\r
-    }\r
-\r
-    @Override\r
-    public void outAAddressValue(AAddressValue node)\r
-    {\r
-        stack.push('&' + node.getRange().toString());\r
-    }\r
-\r
-    @Override\r
-    public void outARangeValue(ARangeValue node)\r
-    {\r
-    }\r
-\r
-    private double extractValue(Object o) {\r
-        if(o instanceof Number) {\r
-            return ((Number)o).doubleValue();\r
-        } else if (o instanceof String) {\r
-            return Double.valueOf((String)o);\r
-        } else {\r
-            return Double.NaN;\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void outAPlusExpression(APlusExpression node)\r
-    {\r
-\r
-        Object o2 = stack.pop();\r
-        Object o1 = stack.pop();\r
-\r
-//     System.out.println("plus " + o1 + " " + o2);\r
-\r
-        if(o1 instanceof String) {\r
-            stack.push(o1.toString() + o2.toString());\r
-        } else {\r
-            double d1 = extractValue(o1);\r
-            double d2 = extractValue(o2);\r
-            stack.push(d1+d2);\r
-        }\r
-\r
-\r
-    }\r
-\r
-    @Override\r
-    public void outAMultMultiplicative(AMultMultiplicative node)\r
-    {\r
-        Object o1 = stack.pop();\r
-        Object o2 = stack.pop();\r
-        double d1 = extractValue(o1);\r
-        double d2 = extractValue(o2);\r
-        stack.push(d1*d2);\r
-//        System.out.println("mult " + d1 + " " + d2);\r
-    }\r
-\r
-    @Override\r
-    public void outADivMultiplicative(ADivMultiplicative node)\r
-    {\r
-        Object o2 = stack.pop();\r
-        Object o1 = stack.pop();\r
-        System.out.println("div " + o1 + " " + o2);\r
-        double d1 = extractValue(o1);\r
-        double d2 = extractValue(o2);\r
-        stack.push(d1/d2);\r
-//        System.out.println("div " + d1 + " " + d2);\r
-    }\r
-\r
-    int countArguments(PArgList args) {\r
-        if(args == null) return 0;\r
-        if(args instanceof ASingleArgList) return 1;\r
-        ASequenceArgList seq = (ASequenceArgList)args;\r
-        return 1 + countArguments(seq.getArgList());\r
-    }\r
-\r
-    @Override\r
-    public void outAFunctionPrimary(AFunctionPrimary node)\r
-    {\r
-        String functionName = node.getFunc().getText().replace("(", "");\r
-        //System.out.println(hashCode() + " eval " + functionName);\r
-\r
-        Function function = builtins.get(functionName);\r
-        if(function != null) {\r
-\r
-            // TODO: fixme\r
-            LinkedList<Object> args = new LinkedList<Object>();\r
-            int argc = countArguments(node.getArgList());\r
-            //System.out.println(hashCode() + "Function takes " + args + " arguments stack has " + Arrays.toString(stack.toArray()));\r
-            for(int i=0;i<argc;i++) {\r
-                args.addFirst(stack.pop());\r
-            }\r
-\r
-            try {\r
-                Object result = function.evaluate(args);\r
-                stack.push(result);\r
-            } catch (ApplicationException e) {\r
-                e.printStackTrace();\r
-                stack.push(null);\r
-            }\r
-\r
-\r
-            return;\r
-\r
-        }\r
-\r
-    }\r
-\r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.scenegraph.profile.impl;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Stack;
+
+import org.simantics.basicexpression.analysis.DepthFirstAdapter;
+import org.simantics.basicexpression.node.AAddressValue;
+import org.simantics.basicexpression.node.AConstantValue;
+import org.simantics.basicexpression.node.ADivMultiplicative;
+import org.simantics.basicexpression.node.AFunctionPrimary;
+import org.simantics.basicexpression.node.AMultMultiplicative;
+import org.simantics.basicexpression.node.APlusExpression;
+import org.simantics.basicexpression.node.ARangeValue;
+import org.simantics.basicexpression.node.ASequenceArgList;
+import org.simantics.basicexpression.node.ASingleArgList;
+import org.simantics.basicexpression.node.AStringValue;
+import org.simantics.basicexpression.node.AVariablePrimary;
+import org.simantics.basicexpression.node.PArgList;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.Valuations;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.reflection.OntologyVersions;
+
+public class MappedPropertyStyleEvaluator extends DepthFirstAdapter
+{
+
+    public static class ApplicationException extends Exception {
+
+        private static final long serialVersionUID = 1L;
+
+        public ApplicationException(String message) {
+            super(message);
+        }
+
+    }
+
+    public interface Function {
+        Object evaluate(Collection<Object> args) throws ApplicationException;
+    }
+
+    final ReadGraph graph;
+    final Layer0 b;
+    final Resource component;
+
+    public MappedPropertyStyleEvaluator(ReadGraph graph, Resource element) throws DatabaseException {
+        this.graph = graph;
+        b = Layer0.getInstance(graph);
+       String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/ElementToComponent");
+        Resource toComponent = graph.getResource(uri);
+        component = graph.getPossibleObject(element, toComponent);
+    }
+
+
+    Stack<Object> stack = new Stack<Object>();
+    HashMap<String, Function> builtins = new HashMap<String, Function>();
+
+    public Object getResult() {
+        if (stack.isEmpty())
+            return null;
+        return stack.pop();
+    }
+
+    @Override
+    public void outAConstantValue(AConstantValue node)
+    {
+//        System.out.println("eval constant " + node);
+        stack.push(Double.valueOf(node.toString()));
+    }
+
+    @Override
+    public void outAStringValue(AStringValue node)
+    {
+//        System.out.println("eval string " + node.toString());
+        String value = node.toString();
+        stack.push(value.substring(1, value.length()-2).trim());
+    }
+
+    @Override
+    public void outAVariablePrimary(AVariablePrimary node)
+    {
+
+        try {
+
+            Valuations vs = graph.adapt(component, Valuations.class);
+            if(vs == null) {
+                stack.push("No Variables for mapped component.");
+                return;
+            }
+            Resource valueResource = vs.getValue(graph, null, "BaseRealization", node.toString().trim());
+            if(valueResource == null) {
+                stack.push("Invalid value identifier based on  '" + node.toString().trim() + "'");
+                return;
+            }
+
+            Object value = graph.getValue(valueResource);
+            stack.push(value);
+
+        } catch (DatabaseException e) {
+
+            stack.push(e.toString());
+
+        }
+
+    }
+
+    @Override
+    public void outAAddressValue(AAddressValue node)
+    {
+        stack.push('&' + node.getRange().toString());
+    }
+
+    @Override
+    public void outARangeValue(ARangeValue node)
+    {
+    }
+
+    private double extractValue(Object o) {
+        if(o instanceof Number) {
+            return ((Number)o).doubleValue();
+        } else if (o instanceof String) {
+            return Double.valueOf((String)o);
+        } else {
+            return Double.NaN;
+        }
+    }
+
+    @Override
+    public void outAPlusExpression(APlusExpression node)
+    {
+
+        Object o2 = stack.pop();
+        Object o1 = stack.pop();
+
+//     System.out.println("plus " + o1 + " " + o2);
+
+        if(o1 instanceof String) {
+            stack.push(o1.toString() + o2.toString());
+        } else {
+            double d1 = extractValue(o1);
+            double d2 = extractValue(o2);
+            stack.push(d1+d2);
+        }
+
+
+    }
+
+    @Override
+    public void outAMultMultiplicative(AMultMultiplicative node)
+    {
+        Object o1 = stack.pop();
+        Object o2 = stack.pop();
+        double d1 = extractValue(o1);
+        double d2 = extractValue(o2);
+        stack.push(d1*d2);
+//        System.out.println("mult " + d1 + " " + d2);
+    }
+
+    @Override
+    public void outADivMultiplicative(ADivMultiplicative node)
+    {
+        Object o2 = stack.pop();
+        Object o1 = stack.pop();
+        System.out.println("div " + o1 + " " + o2);
+        double d1 = extractValue(o1);
+        double d2 = extractValue(o2);
+        stack.push(d1/d2);
+//        System.out.println("div " + d1 + " " + d2);
+    }
+
+    int countArguments(PArgList args) {
+        if(args == null) return 0;
+        if(args instanceof ASingleArgList) return 1;
+        ASequenceArgList seq = (ASequenceArgList)args;
+        return 1 + countArguments(seq.getArgList());
+    }
+
+    @Override
+    public void outAFunctionPrimary(AFunctionPrimary node)
+    {
+        String functionName = node.getFunc().getText().replace("(", "");
+        //System.out.println(hashCode() + " eval " + functionName);
+
+        Function function = builtins.get(functionName);
+        if(function != null) {
+
+            // TODO: fixme
+            LinkedList<Object> args = new LinkedList<Object>();
+            int argc = countArguments(node.getArgList());
+            //System.out.println(hashCode() + "Function takes " + args + " arguments stack has " + Arrays.toString(stack.toArray()));
+            for(int i=0;i<argc;i++) {
+                args.addFirst(stack.pop());
+            }
+
+            try {
+                Object result = function.evaluate(args);
+                stack.push(result);
+            } catch (ApplicationException e) {
+                e.printStackTrace();
+                stack.push(null);
+            }
+
+
+            return;
+
+        }
+
+    }
+
+}
+