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