--- /dev/null
+/*******************************************************************************\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