]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/expression/BasicExpressionVisitor.java
(refs #7561) Allow extra parameters to the application of type alias
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / expression / BasicExpressionVisitor.java
1 package org.simantics.modeling.ui.expression;
2
3 import java.util.HashMap;
4 import java.util.Stack;
5
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;
24
25 public class BasicExpressionVisitor extends DepthFirstAdapter {
26
27     public static final boolean DEBUG_APPLICATION = false;
28     
29         public static class ApplicationException extends Exception {
30
31                 private static final long serialVersionUID = 1L;
32
33                 public ApplicationException(String message) {
34                         super(message);
35                 }
36
37         }
38
39         final ReadGraph graph;
40         final Variable property;
41         
42         Stack<Object> stack = new Stack<Object>();
43
44         HashMap<String, Function> builtins = new HashMap<String, Function>();
45
46         public BasicExpressionVisitor(ReadGraph graph, Variable property) {
47
48                 this.graph = graph;
49                 this.property = property;
50                 
51         }
52
53         public Object getResult() {
54                 return stack.pop();
55         }
56
57         public void outAConstantValue(AConstantValue node) {
58                 // System.out.println("eval constant " + node);
59                 stack.push(Double.valueOf(node.toString()));
60         }
61
62         public void outAStringValue(AStringValue node) {
63
64                 try {
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) {
70                         stack.push(e);
71                 }
72                 
73         }
74
75         @Override
76         public void outARviValue(ARviValue node) {
77                 
78                 try {
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) {
89                         stack.push(e);
90                 }
91                 
92         }
93         
94         public void outAAddressValue(AAddressValue node) {
95                 stack.push('&' + node.getRange().toString());
96         }
97
98         public void outARangeValue(ARangeValue node) {
99         }
100
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);
106                 } else {
107                         return Double.NaN;
108                 }
109         }
110
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);
116                 stack.push(d1 + d2);
117                 // System.out.println("plus " + d1 + " " + d2);
118         }
119
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);
126                 stack.push(d1 - d2);
127         }
128
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);
134                 stack.push(d1 * d2);
135                 // System.out.println("mult " + d1 + " " + d2);
136         }
137         
138         @Override
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);
144                 stack.push(d2 / d1);
145         }
146
147         int countArguments(PArgList args) {
148                 if (args == null)
149                         return 0;
150                 if (args instanceof ASingleArgList)
151                         return 1;
152                 ASequenceArgList seq = (ASequenceArgList) args;
153                 return 1 + countArguments(seq.getArgList());
154         }
155
156         public void outAFunctionPrimary(AFunctionPrimary node) {
157         }
158
159 }