]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/expression/BasicExpression.java
Allow client-logic in component type interface property name editing
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / expression / BasicExpression.java
1 package org.simantics.modeling.ui.expression;
2
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;
14
15 public class BasicExpression implements Expression, EquationExpression {
16
17         final private Resource expression;
18         
19         public BasicExpression(Resource expression) {
20                 this.expression = expression;
21         }
22         
23         @Override
24         public <T> T evaluate(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {
25                 return evaluate(graph, property);
26         }
27
28         @SuppressWarnings("unchecked")
29     @Override
30         public <T> T evaluate(ReadGraph graph, Variable property) throws DatabaseException {
31         
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();
36                 
37         }
38
39         @Override
40         public <T> void invert(WriteGraph graph, Variable property, T value, Binding binding) throws DatabaseException {
41                 
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);
55                         }
56                 }
57                 
58         }
59
60         @Override
61         public boolean isInvertible(ReadGraph graph, Variable property) throws DatabaseException {
62                 
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;
68                 else return false;
69                 
70         }
71
72 }