]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleSessionValuation.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PossibleSessionValuation.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.request;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.common.request.TernaryRead;
19 import org.simantics.db.exception.AdaptionException;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.ResourceNotFoundException;
22 import org.simantics.db.layer0.adapter.Valuations;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.scl.reflection.OntologyVersions;
25 import org.simantics.simulation.ontology.SimulationResource;
26
27 public class PossibleSessionValuation extends TernaryRead<Resource, String, String, Resource> {
28
29     public PossibleSessionValuation(Resource resource, String identifier) {
30         super(resource, identifier, "");
31     }
32
33     public PossibleSessionValuation(Resource resource, String identifier, String suffix) {
34         super(resource, identifier, suffix);
35     }
36
37     @Override
38     public Resource perform(ReadGraph graph) throws DatabaseException {
39         try {
40             Layer0 l0 = Layer0.getInstance(graph);
41             String valuationName = null;
42             Valuations valuations = graph.adapt(parameter, Valuations.class);
43
44             String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/CompositeToDiagram");
45             Resource map = graph.getResource(uri);
46
47             Resource diagram = graph.syncRequest(new PossibleDiagram(parameter));
48             if(diagram == null) {
49                 Resource d = parameter;
50                 while((d = graph.getPossibleObject(d, l0.PartOf)) != null) {
51                     diagram = graph.getPossibleObject(d, map);
52                     if(diagram != null) break;
53                 }
54             }
55
56             if(diagram != null) {
57                 SimulationResource SIMU = SimulationResource.getInstance(graph);
58                 for(Resource run : graph.getObjects(diagram, l0.ConsistsOf)) {
59                     String name = graph.getPossibleRelatedValue(run, l0.HasName, Bindings.STRING);
60                     if(parameter2.equals(name)) {
61                         valuationName = graph.getPossibleRelatedValue(run, SIMU.HasActiveExperiment);
62                         break;
63                     }
64                 }
65             }
66
67             Resource value = valuations.getValue(graph, null, valuationName, parameter3);
68             return graph.getSingleObject(value, l0.ConsistsOf);
69
70         } catch (ResourceNotFoundException e) {
71             if (DebugException.DEBUG)
72                 e.printStackTrace();
73             return null;
74         } catch (AdaptionException e) {
75             if (DebugException.DEBUG)
76                 e.printStackTrace();
77             return null;
78         }
79
80     }
81
82 }