-/*******************************************************************************\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.solver;\r
-\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.equation.EquationResources;\r
-\r
-\r
-public class Solver {\r
-\r
- private static Map<Resource,ExpressionSolver> solvers = new HashMap<Resource, ExpressionSolver>();\r
-\r
-\r
- public static void addSolver(Resource res, ExpressionSolver solver) {\r
- if (res == null || solver == null)\r
- throw new NullPointerException("Expression solver must be bound to valid resource");\r
- solvers.put(res, solver);\r
- }\r
-\r
- public void evaluate(ReadGraph g, Resource r) throws DatabaseException {\r
- if (g.isInstanceOf(r, EquationResources.equationResource.Expression)) {\r
- Collection<Resource> types = g.getTypes(r);\r
- ExpressionSolver solver = null;\r
-\r
- for (Resource res : types) {\r
- solver = solvers.get(res);\r
- if (solver != null)\r
- break;\r
- }\r
- solver.evaluate(this, g, r);\r
- } else {\r
- throw new IllegalArgumentException("Input is not an equation");\r
- }\r
- }\r
-\r
- Map<Resource,Object> valueMap = new HashMap<Resource,Object>();\r
-\r
- public Object getCachedValue(Resource res) {\r
- return valueMap.get(res);\r
- }\r
-\r
- public void setValue(Resource res, Object o) {\r
- valueMap.put(res, o);\r
- }\r
-\r
- public void clear() {\r
- valueMap.clear();\r
- }\r
-\r
- public double getDoubleValue(ReadGraph g, Resource d) throws DatabaseException {\r
- Object o = getValue(g, d);\r
- if (o instanceof double[])\r
- return ((double[])o)[0];\r
- if (o instanceof float[])\r
- return ((float[])o)[0];\r
- if (o instanceof int[])\r
- return ((int[])o)[0];\r
- if (o instanceof long[])\r
- return ((long[])o)[0];\r
- throw new RuntimeException("Cannot cast object to double " + o + " " + o.getClass());\r
- }\r
-\r
- public Object getValue(ReadGraph graph, Resource res) throws DatabaseException {\r
- Object o = getCachedValue(res);\r
- if (o == null) {\r
- o = graph.getValue(res);\r
- }\r
- return o;\r
- }\r
-\r
- public void pushToGraph(WriteGraph graph) throws DatabaseException {\r
- for (Resource r : valueMap.keySet()) {\r
- if (graph.isInstanceOf(r, EquationResources.builtins.Literal)) {\r
- Object value = valueMap.get(r);\r
- if (value instanceof boolean[] ||\r
- value instanceof byte[] ||\r
- value instanceof int[] ||\r
- value instanceof long[] ||\r
- value instanceof float[] ||\r
- value instanceof double[] ||\r
- value instanceof String[])\r
- graph.claimValue(r, value);\r
- else {\r
- throw new RuntimeException("Internal error, unknown datatype " + value.getClass());\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.solver;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.equation.EquationResources;
+
+
+public class Solver {
+
+ private static Map<Resource,ExpressionSolver> solvers = new HashMap<Resource, ExpressionSolver>();
+
+
+ public static void addSolver(Resource res, ExpressionSolver solver) {
+ if (res == null || solver == null)
+ throw new NullPointerException("Expression solver must be bound to valid resource");
+ solvers.put(res, solver);
+ }
+
+ public void evaluate(ReadGraph g, Resource r) throws DatabaseException {
+ if (g.isInstanceOf(r, EquationResources.equationResource.Expression)) {
+ Collection<Resource> types = g.getTypes(r);
+ ExpressionSolver solver = null;
+
+ for (Resource res : types) {
+ solver = solvers.get(res);
+ if (solver != null)
+ break;
+ }
+ solver.evaluate(this, g, r);
+ } else {
+ throw new IllegalArgumentException("Input is not an equation");
+ }
+ }
+
+ Map<Resource,Object> valueMap = new HashMap<Resource,Object>();
+
+ public Object getCachedValue(Resource res) {
+ return valueMap.get(res);
+ }
+
+ public void setValue(Resource res, Object o) {
+ valueMap.put(res, o);
+ }
+
+ public void clear() {
+ valueMap.clear();
+ }
+
+ public double getDoubleValue(ReadGraph g, Resource d) throws DatabaseException {
+ Object o = getValue(g, d);
+ if (o instanceof double[])
+ return ((double[])o)[0];
+ if (o instanceof float[])
+ return ((float[])o)[0];
+ if (o instanceof int[])
+ return ((int[])o)[0];
+ if (o instanceof long[])
+ return ((long[])o)[0];
+ throw new RuntimeException("Cannot cast object to double " + o + " " + o.getClass());
+ }
+
+ public Object getValue(ReadGraph graph, Resource res) throws DatabaseException {
+ Object o = getCachedValue(res);
+ if (o == null) {
+ o = graph.getValue(res);
+ }
+ return o;
+ }
+
+ public void pushToGraph(WriteGraph graph) throws DatabaseException {
+ for (Resource r : valueMap.keySet()) {
+ if (graph.isInstanceOf(r, EquationResources.builtins.Literal)) {
+ Object value = valueMap.get(r);
+ if (value instanceof boolean[] ||
+ value instanceof byte[] ||
+ value instanceof int[] ||
+ value instanceof long[] ||
+ value instanceof float[] ||
+ value instanceof double[] ||
+ value instanceof String[])
+ graph.claimValue(r, value);
+ else {
+ throw new RuntimeException("Internal error, unknown datatype " + value.getClass());
+ }
+ }
+ }
+ }
+
+}