-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.equation.ext;\r
-\r
-import java.util.Iterator;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.equation.EquationResources;\r
-import org.simantics.equation.solver.ExpressionSolver;\r
-import org.simantics.equation.solver.Solver;\r
-\r
-public class ElementExpressionSolver implements ExpressionSolver {\r
- \r
- private Oper sum = new Sum();\r
- private Oper sub = new Sub();\r
- private Oper mul = new Mul();\r
- private Oper div = new Div();\r
- \r
- @Override\r
- public void evaluate(Solver solver, ReadGraph g, Resource t) throws DatabaseException {\r
- Iterator<Resource> it = OrderedSetUtils.iterator(g, t.get());\r
- double res = getNumber(solver, g, it.next());\r
- while (it.hasNext()) {\r
- Oper op = getOperator(g, it.next());\r
- double v = getNumber(solver, g, it.next());\r
- res = op.evaluate(res, v);\r
- }\r
- \r
- solver.setValue(g.getSingleObject(t, EquationResources.equationResource.HasTarget), new double[]{res});\r
- }\r
- \r
- private double getNumber(Solver solver, ReadGraph g, Resource number) throws DatabaseException {\r
- // TODO : assumes that HasVariable points to Double while it can point to any Property\r
- return solver.getDoubleValue(g, g.getSingleObject(number, EquationResources.equationResource.HasVariable));\r
- }\r
- \r
- private Oper getOperator(ReadGraph g, Resource operator) throws DatabaseException {\r
- if (operator.get().equals(EquationResources.equationResource.Addition))\r
- return sum;\r
- else if (operator.get().equals(EquationResources.equationResource.Subtraction))\r
- return sub;\r
- else if (operator.get().equals(EquationResources.equationResource.Multiplication))\r
- return mul;\r
- else if (operator.get().equals(EquationResources.equationResource.Division))\r
- return div;\r
- throw new RuntimeException("Unknown operation " + NameUtils.getSafeName(g, operator));\r
- \r
- }\r
- \r
- private interface Oper {\r
- double evaluate(double d1, double d2);\r
- }\r
- \r
- private class Sum implements Oper {\r
- @Override\r
- public double evaluate(double d1, double d2) {\r
- return d1 + d2;\r
- }\r
- }\r
- \r
- private class Sub implements Oper {\r
- @Override\r
- public double evaluate(double d1, double d2) {\r
- return d1 - d2;\r
- }\r
- }\r
- \r
- private class Mul implements Oper {\r
- @Override\r
- public double evaluate(double d1, double d2) {\r
- return d1 * d2;\r
- }\r
- }\r
- \r
- private class Div implements Oper {\r
- @Override\r
- public double evaluate(double d1, double d2) {\r
- return d1 / d2;\r
- }\r
- }\r
- \r
- \r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.equation.ext;
+
+import java.util.Iterator;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.common.utils.OrderedSetUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.equation.EquationResources;
+import org.simantics.equation.solver.ExpressionSolver;
+import org.simantics.equation.solver.Solver;
+
+public class ElementExpressionSolver implements ExpressionSolver {
+
+ private Oper sum = new Sum();
+ private Oper sub = new Sub();
+ private Oper mul = new Mul();
+ private Oper div = new Div();
+
+ @Override
+ public void evaluate(Solver solver, ReadGraph g, Resource t) throws DatabaseException {
+ Iterator<Resource> it = OrderedSetUtils.iterator(g, t.get());
+ double res = getNumber(solver, g, it.next());
+ while (it.hasNext()) {
+ Oper op = getOperator(g, it.next());
+ double v = getNumber(solver, g, it.next());
+ res = op.evaluate(res, v);
+ }
+
+ solver.setValue(g.getSingleObject(t, EquationResources.equationResource.HasTarget), new double[]{res});
+ }
+
+ private double getNumber(Solver solver, ReadGraph g, Resource number) throws DatabaseException {
+ // TODO : assumes that HasVariable points to Double while it can point to any Property
+ return solver.getDoubleValue(g, g.getSingleObject(number, EquationResources.equationResource.HasVariable));
+ }
+
+ private Oper getOperator(ReadGraph g, Resource operator) throws DatabaseException {
+ if (operator.get().equals(EquationResources.equationResource.Addition))
+ return sum;
+ else if (operator.get().equals(EquationResources.equationResource.Subtraction))
+ return sub;
+ else if (operator.get().equals(EquationResources.equationResource.Multiplication))
+ return mul;
+ else if (operator.get().equals(EquationResources.equationResource.Division))
+ return div;
+ throw new RuntimeException("Unknown operation " + NameUtils.getSafeName(g, operator));
+
+ }
+
+ private interface Oper {
+ double evaluate(double d1, double d2);
+ }
+
+ private class Sum implements Oper {
+ @Override
+ public double evaluate(double d1, double d2) {
+ return d1 + d2;
+ }
+ }
+
+ private class Sub implements Oper {
+ @Override
+ public double evaluate(double d1, double d2) {
+ return d1 - d2;
+ }
+ }
+
+ private class Mul implements Oper {
+ @Override
+ public double evaluate(double d1, double d2) {
+ return d1 * d2;
+ }
+ }
+
+ private class Div implements Oper {
+ @Override
+ public double evaluate(double d1, double d2) {
+ return d1 / d2;
+ }
+ }
+
+
+
+}