]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java
Made spatial picking optimization optional for now
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / experiment / ExperimentUtil.java
1 package org.simantics.simulation.experiment;
2
3 import java.util.Collection;
4 import java.util.UUID;
5 import java.util.function.Consumer;
6
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.ui.progress.IProgressConstants2;
12 import org.simantics.DatabaseJob;
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.RequestProcessor;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
21 import org.simantics.db.common.request.ObjectsWithType;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.common.request.WriteResultRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.request.PossibleActiveExperiment;
26 import org.simantics.db.layer0.request.PossibleActiveRun;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.procedure.Procedure;
29 import org.simantics.db.service.VirtualGraphSupport;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.project.IProject;
32 import org.simantics.simulation.ontology.SimulationResource;
33 import org.simantics.simulation.project.IExperimentManager;
34
35 /**
36  * @author Tuukka Lehtonen
37  */
38 public final class ExperimentUtil {
39
40     public static void refreshExperiment(ReadGraph graph, IExperiment experiment) {
41         experiment.refresh(graph);
42     }
43
44     public static void stepExperiment(IExperiment experiment, double duration) {
45         if(experiment instanceof IDynamicExperiment)
46             ((IDynamicExperiment)experiment).simulateDuration(duration);
47     }
48
49     public static void simulateExperiment(IExperiment experiment, boolean enabled) {
50         if(experiment instanceof IDynamicExperiment)
51             ((IDynamicExperiment)experiment).simulate(enabled);
52     }
53
54     public static ExperimentState getExperimentState(ReadGraph graph, IExperiment experiment) throws DatabaseException {
55         return experiment.getState(graph);
56     }
57         
58     public static void disposeExperiment(final IExperiment experiment) {
59         
60         if(experiment instanceof IDynamicExperiment) {
61                 
62             ((IDynamicExperiment)experiment).shutdown(null);
63             
64             Session session = Simantics.getSession();
65                         VirtualGraphSupport vgs = session.getService(VirtualGraphSupport.class);
66                         session.asyncRequest(new WriteRequest(vgs.getMemoryPersistent("experiments")) {
67
68                                 @Override
69                                 public void perform(WriteGraph graph) throws DatabaseException {
70
71                                         SimulationResource SIMU = SimulationResource.getInstance(graph);
72                                         Resource activeRun = experiment.getResource();
73                                         graph.deny(activeRun, SIMU.IsActive, activeRun);
74
75                                 }
76
77                         });
78
79         }
80         
81     }
82
83     public static void step(double duration) {
84         IExperimentManager manager = 
85             Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
86         IExperiment experiment = manager.getActiveExperiment();
87         if(experiment instanceof IDynamicExperiment)
88             ((IDynamicExperiment)experiment).simulateDuration(duration);
89     }
90
91     public static void simulate(boolean enabled) {
92         IExperimentManager manager =
93             Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
94         IExperiment experiment = manager.getActiveExperiment();
95         if(experiment instanceof IDynamicExperiment)
96             ((IDynamicExperiment)experiment).simulate(enabled);
97     }
98
99     /**
100      * Synchronously shutdown active experiment.
101      * 
102      * @param project
103      */
104     public static void shutdownActiveExperiment(IProject project) {
105         shutdownActiveExperiment(project, null);
106     }
107
108     /**
109      * Synchronously shutdown active experiment.
110      * 
111      * @param project
112      */
113     public static void shutdownActiveExperiment(IProject project, IProgressMonitor monitor) {
114         IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
115         IExperiment experiment = manager.getActiveExperiment();
116         if (experiment != null)
117             experiment.shutdown(monitor);
118     }
119
120     /**
121      * If there is an active experiment, schedule a job for its shutdown.
122      * 
123      * @param project
124      */
125     public static void scheduleShutdownActiveExperiment(IProject project) {
126         scheduleShutdownActiveExperiment(project, null);
127     }
128
129     /**
130      * If there is an active experiment, schedule a job for its shutdown.
131      * 
132      * @param project
133      */
134     public static void scheduleShutdownActiveExperiment(IProject project, Consumer<IExperiment> callback) {
135         IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
136         final IExperiment experiment = manager.getActiveExperiment();
137         if (experiment != null) {
138             Job job = new DatabaseJob("Shutting down experiment") {
139                 @Override
140                 protected IStatus run(final IProgressMonitor monitor) {
141                     try {
142                         experiment.shutdown(monitor);
143                         return Status.OK_STATUS;
144                     } finally {
145                         monitor.done();
146                         if (callback != null)
147                             callback.accept(null);
148                     }
149                 }
150             };
151             job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);
152             job.setUser(true);
153             job.schedule();
154         } else {
155             if (callback != null)
156                 callback.accept(null);
157         }
158     }
159
160     public static Variable possibleActiveRunVariable(ReadGraph graph, Resource model) throws DatabaseException {
161         return graph.syncRequest(new PossibleActiveRun(model));
162     }
163
164     /**
165      * @param processor
166      * @param experiment
167      * @param asyncCallback
168      * @return
169      * @throws DatabaseException
170      */
171     public static Resource activateExperiment(RequestProcessor processor, Resource experiment, Procedure<Resource> asyncCallback) throws DatabaseException {
172         VirtualGraphSupport vgs = processor.getService(VirtualGraphSupport.class);
173         WriteResultRequest<Resource> w = new WriteResultRequest<Resource>(vgs.getMemoryPersistent("experiments")) {
174             @Override
175             public Resource perform(WriteGraph graph) throws DatabaseException {
176                 return activateExperiment(graph, experiment);
177             }
178         };
179         if (processor instanceof WriteGraph) {
180             return ((WriteGraph) processor).syncRequest(w);
181         } else {
182             if (asyncCallback == null)
183                 asyncCallback = new ProcedureAdapter<>();
184             processor.getSession().asyncRequest(w, asyncCallback);
185             return null;
186         }
187     }
188
189     /**
190      * @param processor
191      * @param run
192      * @throws DatabaseException
193      */
194     public static void activateRun(RequestProcessor processor, Resource run) throws DatabaseException {
195         VirtualGraphSupport vgs = processor.getService(VirtualGraphSupport.class);
196         WriteRequest w = new WriteRequest(vgs.getMemoryPersistent("experiments")) {
197             @Override
198             public void perform(WriteGraph graph) throws DatabaseException {
199                 ExperimentUtil.activateRun(graph, run);
200             }
201         };
202         if (processor instanceof WriteGraph) {
203             ((WriteGraph) processor).syncRequest(w);
204         } else {
205             processor.getSession().asyncRequest(w);
206         }
207     }
208
209     public static Resource activateExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {
210
211         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
212         return graph.syncRequest(new WriteResultRequest<Resource>(vgs.getMemoryPersistent("experiments")) {
213             @Override
214             public Resource perform(WriteGraph graph) throws DatabaseException {
215                 return createExperimentRun(graph, experiment);
216             }
217         });
218         
219     }
220
221     public static Resource createExperimentRun(WriteGraph graph, Resource experiment) throws DatabaseException {
222         
223         Layer0 L0 = Layer0.getInstance(graph);
224         SimulationResource SIMU = SimulationResource.getInstance(graph);
225
226         Resource experimentType = graph.getPossibleType(experiment, SIMU.Experiment);
227         if (experimentType == null)
228             throw new DatabaseException("No unique experiment type was found for experiment " + graph.getPossibleURI(experiment));
229         Collection<Resource> runTypes = graph.sync(new ObjectsWithType(experimentType, L0.ConsistsOf, SIMU.RunType));
230         if (runTypes.size() != 1)
231             throw new DatabaseException("No unique run type was found for experiment " + graph.getPossibleURI(experiment));
232         final Resource runType = runTypes.iterator().next();
233         
234         return createExperimentRunWithType(graph, experiment, runType);
235         
236     }
237
238     public static Resource createExperimentRunWithType(WriteGraph graph, Resource experiment, Resource runType) throws DatabaseException {
239         
240         Layer0 L0 = Layer0.getInstance(graph);
241
242         Resource run = graph.newResource();
243         graph.claim(run, L0.InstanceOf, runType);
244         graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
245         graph.claim(experiment, L0.ConsistsOf, run);
246
247         activateRun(graph, run);
248         
249         return run;
250         
251     }
252
253     public static void activateRun(WriteGraph graph, Resource run) throws DatabaseException {
254         
255         SimulationResource SIMU = SimulationResource.getInstance(graph);
256         Resource activeRun = graph.syncRequest(new PossibleActiveExperiment(run));
257         if (activeRun != null) {
258             graph.deny(activeRun, SIMU.IsActive, activeRun);
259         }
260         graph.claim(run, SIMU.IsActive, run);
261         
262     }
263
264 }