]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.profile.impl;
13
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.LinkedList;
17 import java.util.Stack;
18
19 import org.simantics.basicexpression.analysis.DepthFirstAdapter;
20 import org.simantics.basicexpression.node.AAddressValue;
21 import org.simantics.basicexpression.node.AConstantValue;
22 import org.simantics.basicexpression.node.ADivMultiplicative;
23 import org.simantics.basicexpression.node.AFunctionPrimary;
24 import org.simantics.basicexpression.node.AMultMultiplicative;
25 import org.simantics.basicexpression.node.APlusExpression;
26 import org.simantics.basicexpression.node.ARangeValue;
27 import org.simantics.basicexpression.node.ASequenceArgList;
28 import org.simantics.basicexpression.node.ASingleArgList;
29 import org.simantics.basicexpression.node.AStringValue;
30 import org.simantics.basicexpression.node.AVariablePrimary;
31 import org.simantics.basicexpression.node.PArgList;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.adapter.Valuations;
36 import org.simantics.layer0.Layer0;
37 import org.simantics.scl.reflection.OntologyVersions;
38
39 public class MappedPropertyStyleEvaluator extends DepthFirstAdapter
40 {
41
42     public static class ApplicationException extends Exception {
43
44         private static final long serialVersionUID = 1L;
45
46         public ApplicationException(String message) {
47             super(message);
48         }
49
50     }
51
52     public interface Function {
53         Object evaluate(Collection<Object> args) throws ApplicationException;
54     }
55
56     final ReadGraph graph;
57     final Layer0 b;
58     final Resource component;
59
60     public MappedPropertyStyleEvaluator(ReadGraph graph, Resource element) throws DatabaseException {
61         this.graph = graph;
62         b = Layer0.getInstance(graph);
63         String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/ElementToComponent");
64         Resource toComponent = graph.getResource(uri);
65         component = graph.getPossibleObject(element, toComponent);
66     }
67
68
69     Stack<Object> stack = new Stack<Object>();
70     HashMap<String, Function> builtins = new HashMap<String, Function>();
71
72     public Object getResult() {
73         if (stack.isEmpty())
74             return null;
75         return stack.pop();
76     }
77
78     @Override
79     public void outAConstantValue(AConstantValue node)
80     {
81 //        System.out.println("eval constant " + node);
82         stack.push(Double.valueOf(node.toString()));
83     }
84
85     @Override
86     public void outAStringValue(AStringValue node)
87     {
88 //        System.out.println("eval string " + node.toString());
89         String value = node.toString();
90         stack.push(value.substring(1, value.length()-2).trim());
91     }
92
93     @Override
94     public void outAVariablePrimary(AVariablePrimary node)
95     {
96
97         try {
98
99             Valuations vs = graph.adapt(component, Valuations.class);
100             if(vs == null) {
101                 stack.push("No Variables for mapped component.");
102                 return;
103             }
104             Resource valueResource = vs.getValue(graph, null, "BaseRealization", node.toString().trim());
105             if(valueResource == null) {
106                 stack.push("Invalid value identifier based on  '" + node.toString().trim() + "'");
107                 return;
108             }
109
110             Object value = graph.getValue(valueResource);
111             stack.push(value);
112
113         } catch (DatabaseException e) {
114
115             stack.push(e.toString());
116
117         }
118
119     }
120
121     @Override
122     public void outAAddressValue(AAddressValue node)
123     {
124         stack.push('&' + node.getRange().toString());
125     }
126
127     @Override
128     public void outARangeValue(ARangeValue node)
129     {
130     }
131
132     private double extractValue(Object o) {
133         if(o instanceof Number) {
134             return ((Number)o).doubleValue();
135         } else if (o instanceof String) {
136             return Double.valueOf((String)o);
137         } else {
138             return Double.NaN;
139         }
140     }
141
142     @Override
143     public void outAPlusExpression(APlusExpression node)
144     {
145
146         Object o2 = stack.pop();
147         Object o1 = stack.pop();
148
149 //      System.out.println("plus " + o1 + " " + o2);
150
151         if(o1 instanceof String) {
152             stack.push(o1.toString() + o2.toString());
153         } else {
154             double d1 = extractValue(o1);
155             double d2 = extractValue(o2);
156             stack.push(d1+d2);
157         }
158
159
160     }
161
162     @Override
163     public void outAMultMultiplicative(AMultMultiplicative node)
164     {
165         Object o1 = stack.pop();
166         Object o2 = stack.pop();
167         double d1 = extractValue(o1);
168         double d2 = extractValue(o2);
169         stack.push(d1*d2);
170 //        System.out.println("mult " + d1 + " " + d2);
171     }
172
173     @Override
174     public void outADivMultiplicative(ADivMultiplicative node)
175     {
176         Object o2 = stack.pop();
177         Object o1 = stack.pop();
178         System.out.println("div " + o1 + " " + o2);
179         double d1 = extractValue(o1);
180         double d2 = extractValue(o2);
181         stack.push(d1/d2);
182 //        System.out.println("div " + d1 + " " + d2);
183     }
184
185     int countArguments(PArgList args) {
186         if(args == null) return 0;
187         if(args instanceof ASingleArgList) return 1;
188         ASequenceArgList seq = (ASequenceArgList)args;
189         return 1 + countArguments(seq.getArgList());
190     }
191
192     @Override
193     public void outAFunctionPrimary(AFunctionPrimary node)
194     {
195         String functionName = node.getFunc().getText().replace("(", "");
196         //System.out.println(hashCode() + " eval " + functionName);
197
198         Function function = builtins.get(functionName);
199         if(function != null) {
200
201             // TODO: fixme
202             LinkedList<Object> args = new LinkedList<Object>();
203             int argc = countArguments(node.getArgList());
204             //System.out.println(hashCode() + "Function takes " + args + " arguments stack has " + Arrays.toString(stack.toArray()));
205             for(int i=0;i<argc;i++) {
206                 args.addFirst(stack.pop());
207             }
208
209             try {
210                 Object result = function.evaluate(args);
211                 stack.push(result);
212             } catch (ApplicationException e) {
213                 e.printStackTrace();
214                 stack.push(null);
215             }
216
217
218             return;
219
220         }
221
222     }
223
224 }
225