1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.project;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\r
17 import org.simantics.databoard.Bindings;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.Session;
\r
21 import org.simantics.db.VirtualGraph;
\r
22 import org.simantics.db.VirtualGraphContext;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.procedure.single.SingleSetSyncListener;
\r
25 import org.simantics.db.common.request.WriteRequest;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.layer0.adapter.RuntimeValuations;
\r
28 import org.simantics.db.layer0.realization.RealizationVirtualGraph;
\r
29 import org.simantics.db.layer0.realization.ResourceData;
\r
30 import org.simantics.db.layer0.util.RemoverUtil;
\r
31 import org.simantics.db.request.Read;
\r
32 import org.simantics.layer0.Layer0;
\r
33 import org.simantics.simulation.ontology.SimulationResource;
\r
34 import org.simantics.sysdyn.SysdynResource;
\r
35 import org.simantics.utils.datastructures.Pair;
\r
37 public class HistoryRealizationVirtualGraph extends RealizationVirtualGraph {
\r
39 public HistoryRealizationVirtualGraph(Session session, Resource model) throws DatabaseException {
\r
40 super(session, model, SysdynResource.getInstance(session).HistoryRealization);
\r
44 public void initialize(final VirtualGraphContext context, WriteGraph graph) throws DatabaseException {
\r
46 graph.asyncRequest(new Read<Collection<Resource>>() {
\r
49 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
\r
50 // System.out.println("Compute runs starts");
\r
51 HashSet<Resource> result = new HashSet<Resource>();
\r
52 Layer0 l0 = Layer0.getInstance(graph);
\r
53 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
54 for(Resource config : graph.getObjects(model, l0.ConsistsOf)) {
\r
55 if(graph.isInstanceOf(config, SIMU.Experiment)) {
\r
56 for(Resource run : graph.getObjects(config, l0.ConsistsOf)) {
\r
57 if(graph.isInstanceOf(run, SIMU.Run)) {
\r
66 }, new SingleSetSyncListener<Resource>() {
\r
69 public void add(ReadGraph graph, final Resource run) throws DatabaseException {
\r
71 final Layer0 l0 = Layer0.getInstance(graph);
\r
72 final String experimentName = graph.getPossibleRelatedValue(run, l0.HasName, Bindings.STRING);
\r
74 RuntimeValuations vs = graph.getService(RuntimeValuations.class);
\r
75 if(vs.supports(experimentName)) {
\r
77 graph.asyncRequest(new Read<Collection<Pair<String, Resource>>>() {
\r
80 public Collection<Pair<String, Resource>> perform(ReadGraph graph) throws DatabaseException {
\r
81 HashSet<Pair<String, Resource>> result = new HashSet<Pair<String, Resource>>();
\r
82 Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
\r
83 for(Resource part : graph.getObjects(configuration, l0.ConsistsOf)) {
\r
84 String partName = graph.getPossibleRelatedValue(part, l0.HasName, Bindings.STRING);
\r
85 result.add(new Pair<String, Resource>(partName, part));
\r
89 }, new SingleSetSyncListener<Pair<String, Resource>>() {
\r
92 public void add(ReadGraph graph, final Pair<String, Resource> part) throws DatabaseException {
\r
93 graph.getSession().asyncRequest(new WriteRequest(graph.getService(VirtualGraph.class)) {
\r
95 public void perform(WriteGraph graph) throws DatabaseException {
\r
96 int runNode = context.getIndex(run);
\r
97 if(part.first != null) {
\r
98 Resource partResource = graph.newResource();
\r
99 int partNode = context.getIndex(partResource);
\r
100 graph.claim(run, l0.ConsistsOf, null, partResource);
\r
101 // undiscovered.put(partNode, new ResourceData(runNode, part.second, valuation, part.first, experimentName, null));
\r
108 public void remove(ReadGraph graph, final Pair<String, Resource> part) throws DatabaseException {
\r
109 graph.getSession().asyncRequest(new WriteRequest(graph.getService(VirtualGraph.class)) {
\r
111 public void perform(WriteGraph graph) throws DatabaseException {
\r
112 Integer key = null;
\r
113 for(int k : undiscovered.keySet()) {
\r
114 ResourceData rd = undiscovered.get(k);
\r
115 // if (rd.name.equals(part.first) && rd.structureResource.equals(part.second)) {
\r
121 Resource r = context.getResource(key);
\r
122 RemoverUtil.remove(graph, r);
\r
123 undiscovered.remove(key);
\r
130 public boolean isDisposed() {
\r
141 public void exception(ReadGraph graph, Throwable throwable) {
\r
142 throwable.printStackTrace();
\r
146 public boolean isDisposed() {
\r