1 package org.simantics.modeling.ui.expression;
3 import org.simantics.basicexpression.Expressions;
4 import org.simantics.databoard.binding.Binding;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.EquationExpression;
11 import org.simantics.db.layer0.variable.Expression;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.utils.datastructures.Triple;
15 public class BasicExpression implements Expression, EquationExpression {
17 final private Resource expression;
19 public BasicExpression(Resource expression) {
20 this.expression = expression;
24 public <T> T evaluate(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {
25 return evaluate(graph, property);
28 @SuppressWarnings("unchecked")
30 public <T> T evaluate(ReadGraph graph, Variable property) throws DatabaseException {
32 String expressionString = graph.getValue(expression, Bindings.STRING);
33 BasicExpressionVisitor visitor = new BasicExpressionVisitor(graph, property);
34 Expressions.evaluate(expressionString, visitor);
35 return (T)visitor.getResult();
40 public <T> void invert(WriteGraph graph, Variable property, T value, Binding binding) throws DatabaseException {
42 String expressionString = graph.getValue(expression, Bindings.STRING);
43 InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
44 Expressions.evaluate(expressionString, visitor);
45 Object pair = visitor.getResult();
46 if(pair instanceof Triple) {
47 @SuppressWarnings("unchecked")
48 Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
49 if(Math.abs(data.first) > 1e-9) {
50 Double inverted = ((Double)value - data.second) / data.first;
51 String target = data.third;
52 Variable targetVariable = property.browse(graph, target);
53 // System.out.println("invert: " + target + " = " + property.browse(graph, target).getURI(graph));
54 targetVariable.setValue(graph, inverted, binding);
61 public boolean isInvertible(ReadGraph graph, Variable property) throws DatabaseException {
63 String expressionString = graph.getValue(expression, Bindings.STRING);
64 InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
65 Expressions.evaluate(expressionString, visitor);
66 Object value = visitor.getResult();
67 if(value instanceof Triple) return true;