1 package org.simantics.modeling.ui.expression;
3 import java.util.HashMap;
4 import java.util.Stack;
6 import org.simantics.basicexpression.analysis.DepthFirstAdapter;
7 import org.simantics.basicexpression.node.AAddressValue;
8 import org.simantics.basicexpression.node.AConstantValue;
9 import org.simantics.basicexpression.node.ADivMultiplicative;
10 import org.simantics.basicexpression.node.AFunctionPrimary;
11 import org.simantics.basicexpression.node.AMinusExpression;
12 import org.simantics.basicexpression.node.AMultMultiplicative;
13 import org.simantics.basicexpression.node.APlusExpression;
14 import org.simantics.basicexpression.node.ARangeValue;
15 import org.simantics.basicexpression.node.ARviValue;
16 import org.simantics.basicexpression.node.ASequenceArgList;
17 import org.simantics.basicexpression.node.ASingleArgList;
18 import org.simantics.basicexpression.node.AStringValue;
19 import org.simantics.basicexpression.node.PArgList;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.adapter.Function;
23 import org.simantics.db.layer0.variable.Variable;
25 public class BasicExpressionVisitor extends DepthFirstAdapter {
27 public static final boolean DEBUG_APPLICATION = false;
29 public static class ApplicationException extends Exception {
31 private static final long serialVersionUID = 1L;
33 public ApplicationException(String message) {
39 final ReadGraph graph;
40 final Variable property;
42 Stack<Object> stack = new Stack<Object>();
44 HashMap<String, Function> builtins = new HashMap<String, Function>();
46 public BasicExpressionVisitor(ReadGraph graph, Variable property) {
49 this.property = property;
53 public Object getResult() {
57 public void outAConstantValue(AConstantValue node) {
58 // System.out.println("eval constant " + node);
59 stack.push(Double.valueOf(node.toString()));
62 public void outAStringValue(AStringValue node) {
65 String value = node.toString();
66 String rvi = value.substring(1, value.length() - 2).trim();
67 Variable relative = property.browse(graph, rvi);
68 stack.push(relative.getValue(graph));
69 } catch (DatabaseException e) {
76 public void outARviValue(ARviValue node) {
79 String value = node.toString();
80 String rvi = value.trim();
81 // System.out.println("property=" + property.getURI(graph) + " " + property);
82 // System.out.println("rvi=" + rvi);
83 Variable relative = property.browse(graph, rvi);
84 // System.out.println("relative=" + relative.getURI(graph));
85 Object valueObject = relative.getValue(graph);
86 // System.out.println("relative2=" + relative.getURI(graph) + " -> " + valueObject);
87 stack.push(valueObject);
88 } catch (DatabaseException e) {
94 public void outAAddressValue(AAddressValue node) {
95 stack.push('&' + node.getRange().toString());
98 public void outARangeValue(ARangeValue node) {
101 private double extractValue(Object o) {
102 if (o instanceof Number) {
103 return ((Number) o).doubleValue();
104 } else if (o instanceof String) {
105 return Double.valueOf((String) o);
111 public void outAPlusExpression(APlusExpression node) {
112 Object o1 = stack.pop();
113 Object o2 = stack.pop();
114 double d1 = extractValue(o1);
115 double d2 = extractValue(o2);
117 // System.out.println("plus " + d1 + " " + d2);
120 public void outAMinusExpression(AMinusExpression node) {
121 Object o2 = stack.pop();
122 Object o1 = stack.pop();
123 double d1 = extractValue(o1);
124 double d2 = extractValue(o2);
125 // System.out.println("minus " + d1 + " " + d2);
129 public void outAMultMultiplicative(AMultMultiplicative node) {
130 Object o1 = stack.pop();
131 Object o2 = stack.pop();
132 double d1 = extractValue(o1);
133 double d2 = extractValue(o2);
135 // System.out.println("mult " + d1 + " " + d2);
139 public void outADivMultiplicative(ADivMultiplicative node) {
140 Object o1 = stack.pop();
141 Object o2 = stack.pop();
142 double d1 = extractValue(o1);
143 double d2 = extractValue(o2);
147 int countArguments(PArgList args) {
150 if (args instanceof ASingleArgList)
152 ASequenceArgList seq = (ASequenceArgList) args;
153 return 1 + countArguments(seq.getArgList());
156 public void outAFunctionPrimary(AFunctionPrimary node) {