1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.uri.ResourceToURI;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.Valuations;
19 import org.simantics.simulation.ontology.SimulationResource;
20 import org.simantics.utils.datastructures.Pair;
22 public class DefaultValuations implements Valuations {
24 final private Resource resource;
26 public DefaultValuations(Resource module) {
27 this.resource = module;
30 public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {
31 int position = path.indexOf("/", startPosition);
32 String prefix = path.substring(0, position);
33 // System.out.println("prefix=" + prefix);
34 Resource r = graph.getResource(prefix);
35 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {
36 return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length()));
38 return splitModelAndStructure(graph, path, position + 1);
42 public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {
43 Resource r = graph.getResource(path);
44 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {
45 return new Pair<String, String>(path, "");
47 return splitModelAndStructure(graph, path, 8);
52 public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment) throws DatabaseException {
53 // System.out.println("getValue " + module);
54 String path = graph.syncRequest(new ResourceToURI(resource));
55 // System.out.println("path=" + path);
56 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);
57 // System.out.println("mas=" + modelAndStructure);
60 return graph.getResource(modelAndStructure.first + "/" + experiment + "/" + modelAndStructure.second);
61 } catch (DatabaseException e) {
63 return graph.getResource(modelAndStructure.first + "/BaseRealization/" + modelAndStructure.second);
64 } catch (DatabaseException e2) {
71 public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment, String suffix) throws DatabaseException {
72 // System.out.println("getValue " + resource);
73 String path = graph.syncRequest(new ResourceToURI(resource));
74 // System.out.println("path=" + path);
75 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);
76 // System.out.println("mas=" + modelAndStructure);
77 String prefix = modelAndStructure.second.isEmpty() ? "" : "/" + modelAndStructure.second;
78 String uri = modelAndStructure.first + "/" + experiment + prefix + (suffix.isEmpty() ? "" : "/" + suffix);
79 // System.out.println("defaultValuations uri=" + uri);
80 return graph.getResource(uri);