--- /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.db.layer0.adapter.impl;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.uri.ResourceToURI;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Valuations;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class DefaultValuations implements Valuations {\r
+\r
+ final private Resource resource;\r
+ \r
+ public DefaultValuations(Resource module) {\r
+ this.resource = module;\r
+ }\r
+\r
+ public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {\r
+ int position = path.indexOf("/", startPosition);\r
+ String prefix = path.substring(0, position);\r
+// System.out.println("prefix=" + prefix);\r
+ Resource r = graph.getResource(prefix);\r
+ if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
+ return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length())); \r
+ } else {\r
+ return splitModelAndStructure(graph, path, position + 1);\r
+ }\r
+ }\r
+\r
+ public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {\r
+ Resource r = graph.getResource(path);\r
+ if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
+ return new Pair<String, String>(path, "");\r
+ } else {\r
+ return splitModelAndStructure(graph, path, 8);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment) throws DatabaseException {\r
+// System.out.println("getValue " + module);\r
+ String path = graph.syncRequest(new ResourceToURI(resource));\r
+// System.out.println("path=" + path);\r
+ Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);\r
+// System.out.println("mas=" + modelAndStructure);\r
+ // TODO: fixme please\r
+ try {\r
+ return graph.getResource(modelAndStructure.first + "/" + experiment + "/" + modelAndStructure.second);\r
+ } catch (DatabaseException e) {\r
+ try {\r
+ return graph.getResource(modelAndStructure.first + "/BaseRealization/" + modelAndStructure.second);\r
+ } catch (DatabaseException e2) {\r
+ return null;\r
+ }\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment, String suffix) throws DatabaseException {\r
+// System.out.println("getValue " + resource);\r
+ String path = graph.syncRequest(new ResourceToURI(resource));\r
+// System.out.println("path=" + path);\r
+ Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);\r
+// System.out.println("mas=" + modelAndStructure);\r
+ String prefix = modelAndStructure.second.isEmpty() ? "" : "/" + modelAndStructure.second;\r
+ String uri = modelAndStructure.first + "/" + experiment + prefix + (suffix.isEmpty() ? "" : "/" + suffix);\r
+// System.out.println("defaultValuations uri=" + uri);\r
+ return graph.getResource(uri);\r
+ }\r
+\r
+ \r
+}\r