]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.equation/src/org/simantics/equation/ext/ElementExpressionSolver.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.equation / src / org / simantics / equation / ext / ElementExpressionSolver.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.equation.ext;
13
14 import java.util.Iterator;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.utils.NameUtils;
19 import org.simantics.db.common.utils.OrderedSetUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.equation.EquationResources;
22 import org.simantics.equation.solver.ExpressionSolver;
23 import org.simantics.equation.solver.Solver;
24
25 public class ElementExpressionSolver implements ExpressionSolver {
26         
27         private Oper sum = new Sum();
28         private Oper sub = new Sub();
29         private Oper mul = new Mul();
30         private Oper div = new Div();
31         
32         @Override
33         public void evaluate(Solver solver, ReadGraph g, Resource t) throws DatabaseException {
34                 Iterator<Resource> it = OrderedSetUtils.iterator(g, t.get());
35                 double res = getNumber(solver, g, it.next());
36                 while (it.hasNext()) {
37                         Oper op = getOperator(g, it.next());
38                         double v = getNumber(solver, g, it.next());
39                         res = op.evaluate(res, v);
40                 }
41                 
42                 solver.setValue(g.getSingleObject(t, EquationResources.equationResource.HasTarget), new double[]{res});
43         }
44         
45         private double getNumber(Solver solver, ReadGraph g, Resource number) throws DatabaseException {
46                 // TODO : assumes that HasVariable points to Double while it can point to any Property
47                 return solver.getDoubleValue(g, g.getSingleObject(number, EquationResources.equationResource.HasVariable));
48         }
49         
50         private Oper getOperator(ReadGraph g, Resource operator) throws DatabaseException {
51                 if (operator.get().equals(EquationResources.equationResource.Addition))
52                         return sum;
53                 else if (operator.get().equals(EquationResources.equationResource.Subtraction))
54                         return sub;
55                 else if (operator.get().equals(EquationResources.equationResource.Multiplication))
56                         return mul;
57                 else if (operator.get().equals(EquationResources.equationResource.Division))
58                         return div;
59                 throw new RuntimeException("Unknown operation " + NameUtils.getSafeName(g, operator));
60                 
61         }
62         
63         private interface Oper {
64                 double evaluate(double d1, double d2);
65         }
66         
67         private class Sum implements Oper {
68                 @Override
69                 public double evaluate(double d1, double d2) {
70                         return d1 + d2;
71                 }
72         }
73         
74         private class Sub implements Oper {
75                 @Override
76                 public double evaluate(double d1, double d2) {
77                         return d1 - d2;
78                 }
79         }
80         
81         private class Mul implements Oper {
82                 @Override
83                 public double evaluate(double d1, double d2) {
84                         return d1 * d2;
85                 }
86         }
87         
88         private class Div implements Oper {
89                 @Override
90                 public double evaluate(double d1, double d2) {
91                         return d1 / d2;
92                 }
93         }
94         
95         
96
97 }