]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultValuations.java
Sync git svn branch with SVN repository r33389.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultValuations.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.adapter.impl;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.uri.ResourceToURI;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.layer0.adapter.Valuations;\r
19 import org.simantics.simulation.ontology.SimulationResource;\r
20 import org.simantics.utils.datastructures.Pair;\r
21 \r
22 public class DefaultValuations implements Valuations {\r
23 \r
24         final private Resource resource;\r
25         \r
26         public DefaultValuations(Resource module) {\r
27                 this.resource = module;\r
28         }\r
29 \r
30         public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {\r
31                 int position = path.indexOf("/", startPosition);\r
32                 String prefix = path.substring(0, position);\r
33 //              System.out.println("prefix=" + prefix);\r
34                 Resource r = graph.getResource(prefix);\r
35                 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
36                         return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length())); \r
37                 } else {\r
38                         return splitModelAndStructure(graph, path, position + 1);\r
39                 }\r
40         }\r
41 \r
42         public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {\r
43                 Resource r = graph.getResource(path);\r
44                 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
45                         return new Pair<String, String>(path, "");\r
46                 } else {\r
47                         return splitModelAndStructure(graph, path, 8);\r
48                 }\r
49         }\r
50         \r
51         @Override\r
52         public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment) throws DatabaseException {\r
53 //              System.out.println("getValue " + module);\r
54                 String path = graph.syncRequest(new ResourceToURI(resource));\r
55 //              System.out.println("path=" + path);\r
56                 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);\r
57 //              System.out.println("mas=" + modelAndStructure);\r
58                 // TODO: fixme please\r
59                 try {\r
60                         return graph.getResource(modelAndStructure.first + "/" + experiment + "/" + modelAndStructure.second);\r
61                 } catch (DatabaseException e) {\r
62                         try {\r
63                                 return graph.getResource(modelAndStructure.first + "/BaseRealization/" + modelAndStructure.second);\r
64                         } catch (DatabaseException e2) {\r
65                                 return null;\r
66                         }\r
67                 }\r
68         }\r
69 \r
70         @Override\r
71         public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment, String suffix) throws DatabaseException {\r
72 //              System.out.println("getValue " + resource);\r
73                 String path = graph.syncRequest(new ResourceToURI(resource));\r
74 //              System.out.println("path=" + path);\r
75                 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);\r
76 //              System.out.println("mas=" + modelAndStructure);\r
77                 String prefix = modelAndStructure.second.isEmpty() ? "" : "/" + modelAndStructure.second;\r
78                 String uri = modelAndStructure.first + "/" + experiment + prefix + (suffix.isEmpty() ? "" : "/" + suffix);\r
79 //              System.out.println("defaultValuations uri=" + uri);\r
80                 return graph.getResource(uri);\r
81         }\r
82 \r
83         \r
84 }\r