1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.profile.impl;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.LinkedList;
17 import java.util.Stack;
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;
39 public class MappedPropertyStyleEvaluator extends DepthFirstAdapter
42 public static class ApplicationException extends Exception {
44 private static final long serialVersionUID = 1L;
46 public ApplicationException(String message) {
52 public interface Function {
53 Object evaluate(Collection<Object> args) throws ApplicationException;
56 final ReadGraph graph;
58 final Resource component;
60 public MappedPropertyStyleEvaluator(ReadGraph graph, Resource element) throws DatabaseException {
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);
69 Stack<Object> stack = new Stack<Object>();
70 HashMap<String, Function> builtins = new HashMap<String, Function>();
72 public Object getResult() {
79 public void outAConstantValue(AConstantValue node)
81 // System.out.println("eval constant " + node);
82 stack.push(Double.valueOf(node.toString()));
86 public void outAStringValue(AStringValue node)
88 // System.out.println("eval string " + node.toString());
89 String value = node.toString();
90 stack.push(value.substring(1, value.length()-2).trim());
94 public void outAVariablePrimary(AVariablePrimary node)
99 Valuations vs = graph.adapt(component, Valuations.class);
101 stack.push("No Variables for mapped component.");
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() + "'");
110 Object value = graph.getValue(valueResource);
113 } catch (DatabaseException e) {
115 stack.push(e.toString());
122 public void outAAddressValue(AAddressValue node)
124 stack.push('&' + node.getRange().toString());
128 public void outARangeValue(ARangeValue node)
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);
143 public void outAPlusExpression(APlusExpression node)
146 Object o2 = stack.pop();
147 Object o1 = stack.pop();
149 // System.out.println("plus " + o1 + " " + o2);
151 if(o1 instanceof String) {
152 stack.push(o1.toString() + o2.toString());
154 double d1 = extractValue(o1);
155 double d2 = extractValue(o2);
163 public void outAMultMultiplicative(AMultMultiplicative node)
165 Object o1 = stack.pop();
166 Object o2 = stack.pop();
167 double d1 = extractValue(o1);
168 double d2 = extractValue(o2);
170 // System.out.println("mult " + d1 + " " + d2);
174 public void outADivMultiplicative(ADivMultiplicative node)
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);
182 // System.out.println("div " + d1 + " " + d2);
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());
193 public void outAFunctionPrimary(AFunctionPrimary node)
195 String functionName = node.getFunc().getText().replace("(", "");
196 //System.out.println(hashCode() + " eval " + functionName);
198 Function function = builtins.get(functionName);
199 if(function != null) {
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());
210 Object result = function.evaluate(args);
212 } catch (ApplicationException e) {