1 package org.simantics.modeling;
3 import java.util.Stack;
5 import org.simantics.basicexpression.Expressions;
6 import org.simantics.basicexpression.analysis.DepthFirstAdapter;
7 import org.simantics.basicexpression.node.AConstantValue;
8 import org.simantics.basicexpression.node.ADivMultiplicative;
9 import org.simantics.basicexpression.node.AMultMultiplicative;
10 import org.simantics.basicexpression.node.APlusExpression;
11 import org.simantics.basicexpression.node.AVariablePrimary;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.structural.stubs.StructuralResource2;
19 import org.simantics.utils.datastructures.Pair;
20 import org.simantics.utils.datastructures.Triple;
22 public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
24 Stack<Object> stack = new Stack<Object>();
26 public Object getResult() {
27 if(stack.size() != 1) return null;
31 public void outAConstantValue(AConstantValue node) {
32 stack.push(Double.valueOf(node.toString()));
36 public void outAVariablePrimary(AVariablePrimary node) {
37 String value = node.toString().trim();
38 stack.push(Triple.make(1.0, 0.0, value));
41 @SuppressWarnings("unchecked")
42 public void outAPlusExpression(APlusExpression node) {
44 final Object o1 = stack.pop();
45 final Object o2 = stack.pop();
47 if(o1 instanceof Double && o2 instanceof Triple) {
48 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
49 stack.push(Triple.make(p.first, p.second + (Double)o1, p.third));
50 } else if (o2 instanceof Double && o1 instanceof Triple) {
51 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
52 stack.push(Triple.make(p.first, p.second + (Double)o2, p.third));
53 } else if (o2 instanceof Double && o1 instanceof Double) {
54 stack.push((Double)o1 + (Double)o2);
56 stack.push(Double.NaN);
61 @SuppressWarnings("unchecked")
62 public void outAMinusExpression(APlusExpression node) {
64 final Object o1 = stack.pop();
65 final Object o2 = stack.pop();
67 if(o1 instanceof Double && o2 instanceof Triple) {
68 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
69 stack.push(Triple.make(-p.first, (Double)o1 - p.second, p.third ));
70 } else if (o2 instanceof Double && o1 instanceof Triple) {
71 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
72 stack.push(Triple.make(p.first, p.second - (Double)o2, p.third));
73 } else if (o2 instanceof Double && o1 instanceof Double) {
74 stack.push((Double)o1 - (Double)o2);
76 stack.push(Double.NaN);
81 @SuppressWarnings("unchecked")
82 public void outAMultMultiplicative(AMultMultiplicative node) {
84 final Object o1 = stack.pop();
85 final Object o2 = stack.pop();
87 if(o1 instanceof Double && o2 instanceof Triple) {
88 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
89 stack.push(Triple.make(p.first * (Double)o1, p.second * (Double)o1, p.third));
90 } else if (o2 instanceof Double && o1 instanceof Triple) {
91 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
92 stack.push(Triple.make(p.first * (Double)o2, p.second * (Double)o2, p.third));
93 } else if (o2 instanceof Double && o1 instanceof Double) {
94 stack.push((Double)o1 * (Double)o2);
96 stack.push(Double.NaN);
101 @SuppressWarnings("unchecked")
102 public void outADivMultiplicative(ADivMultiplicative node) {
104 final Object o1 = stack.pop();
105 final Object o2 = stack.pop();
107 if(o1 instanceof Double && o2 instanceof Triple) {
108 stack.push(Double.NaN);
109 } else if (o2 instanceof Double && o1 instanceof Triple) {
110 Triple<Double, Double, String> p = (Triple<Double,Double, String>)o1;
111 stack.push(Triple.make(p.first / (Double)o2, p.second / (Double)o2, p.third));
112 } else if (o2 instanceof Double && o1 instanceof Double) {
113 stack.push((Double)o1 / (Double)o2);
115 stack.push(Double.NaN);
120 private static final String MAGIC = "_111_";
122 private static String replaced(String expression) {
123 return expression.replaceAll("\\.([A-Za-z])", MAGIC + "$1");
126 public static void invert(WriteGraph graph, Variable base, String expression, Object value) throws DatabaseException {
127 InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
128 Expressions.evaluate(replaced(expression), visitor);
129 Object pair = visitor.getResult();
130 if(pair == null) return;
131 if(pair instanceof Triple) {
132 @SuppressWarnings("unchecked")
133 Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
134 String key = data.third.replace(MAGIC, ".");
135 String path = getVariablePath(graph, base, key);
136 Variable targetVariable = base.browse(graph, path);
137 if(value instanceof Number) {
138 if(Math.abs(data.first) > 1e-9) {
139 Double inverted = (((Number)value).doubleValue() - data.second) / data.first;
140 targetVariable.setValue(graph, inverted);
142 } else if (value instanceof Boolean) {
143 // TODO: support 'not'
144 targetVariable.setValue(graph, value);
150 private static String getVariablePath(ReadGraph graph, Variable base, String key) throws DatabaseException {
151 StructuralResource2 STR = StructuralResource2.getInstance(graph);
152 Resource type = base.getPossibleType(graph);
155 boolean procedural = graph.isInstanceOf(type, STR.ProceduralComponentType);
156 Pair<String, Type> pair;
158 pair = graph.sync(new ProceduralSubstructureMapRequest(base)).get(key);
160 pair = ComponentTypeSubstructure.forType(graph, type).possibleTypedRVI(key);
169 * @param expression the expression to check for invertibility. An empty is expression is not invertible.
171 * @throws DatabaseException
172 * @throws NullPointerException for null expression
174 public static boolean isInvertible(ReadGraph graph, Variable base, String expression) throws DatabaseException {
175 if (expression == null)
176 throw new NullPointerException("null expression for variable " + base.getURI(graph));
177 if (expression.isEmpty())
180 Resource type = base.getPossibleType(graph);
181 if(type == null) return false;
183 InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
184 Expressions.evaluate(replaced(expression), visitor);
185 Object pair = visitor.getResult();
186 if(pair == null) return false;
187 if(pair instanceof Triple) {
188 @SuppressWarnings("unchecked")
189 Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
190 String key = data.third.replace(MAGIC,".");
191 return getVariablePath(graph, base, key) != null;
197 public static Variable possibleInvertibleExpressionReferencedProperty(ReadGraph graph, Variable base, String expression) throws DatabaseException {
198 if (base == null || expression == null || expression.isEmpty())
200 InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
201 //System.out.println("invert : " + expression + " -> " + replaced(expression) + " for " + base.getURI(graph));
202 Expressions.evaluate(replaced(expression), visitor);
203 Object pair = visitor.getResult();
206 if(pair instanceof Triple) {
207 @SuppressWarnings("unchecked")
208 Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
209 String key = data.third.replace(MAGIC,".");
210 String path = getVariablePath(graph, base, key);
213 Variable targetVariable = base.browsePossible(graph, path);
214 return targetVariable;