]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
a70d2368a1fe4acf1c5ac932fc78970d657274fc
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.project;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.HashSet;\r
16 \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
36 \r
37 public class HistoryRealizationVirtualGraph extends RealizationVirtualGraph {\r
38 \r
39     public HistoryRealizationVirtualGraph(Session session, Resource model) throws DatabaseException {\r
40         super(session, model, SysdynResource.getInstance(session).HistoryRealization);\r
41     }\r
42 \r
43     @Override\r
44     public void initialize(final VirtualGraphContext context, WriteGraph graph) throws DatabaseException {\r
45 \r
46         graph.asyncRequest(new Read<Collection<Resource>>() {\r
47 \r
48             @Override\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
58                                 result.add(run);\r
59                             }\r
60                         }\r
61                     }\r
62                 }\r
63                 return result;\r
64             }\r
65 \r
66         }, new SingleSetSyncListener<Resource>() {\r
67 \r
68             @Override\r
69             public void add(ReadGraph graph, final Resource run) throws DatabaseException {\r
70 \r
71                 final Layer0 l0 = Layer0.getInstance(graph);\r
72                 final String experimentName = graph.getPossibleRelatedValue(run, l0.HasName, Bindings.STRING);\r
73 \r
74                 RuntimeValuations vs = graph.getService(RuntimeValuations.class);\r
75                 if(vs.supports(experimentName)) {\r
76 \r
77                     graph.asyncRequest(new Read<Collection<Pair<String, Resource>>>() {\r
78 \r
79                         @Override\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
86                             }\r
87                             return result;\r
88                         }\r
89                     }, new SingleSetSyncListener<Pair<String, Resource>>() {\r
90 \r
91                         @Override\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
94                                 @Override\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
102                                     }\r
103                                 }\r
104                             });\r
105                         }\r
106 \r
107                         @Override\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
110                                 @Override\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
116 //                                            key = k;\r
117 //                                            break;\r
118 //                                        }\r
119                                     }\r
120                                     if(key != null) {\r
121                                         Resource r = context.getResource(key);\r
122                                         RemoverUtil.remove(graph, r);\r
123                                         undiscovered.remove(key);\r
124                                     }\r
125                                 }\r
126                             });\r
127                         }\r
128 \r
129                         @Override\r
130                         public boolean isDisposed() {\r
131                             return false;\r
132                         }\r
133 \r
134                     });\r
135                     \r
136                 }\r
137 \r
138             }\r
139 \r
140             @Override\r
141             public void exception(ReadGraph graph, Throwable throwable) {\r
142                 throwable.printStackTrace();\r
143             }\r
144 \r
145             @Override\r
146             public boolean isDisposed() {\r
147                 return false;\r
148             }\r
149 \r
150         });\r
151 \r
152 \r
153     }\r
154 \r
155 \r
156 }\r