1 package org.simantics.simulation.experiment;
3 import java.util.Collection;
5 import java.util.function.Consumer;
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;
36 * @author Tuukka Lehtonen
38 public final class ExperimentUtil {
40 public static void refreshExperiment(ReadGraph graph, IExperiment experiment) {
41 experiment.refresh(graph);
44 public static void stepExperiment(IExperiment experiment, double duration) {
45 if(experiment instanceof IDynamicExperiment)
46 ((IDynamicExperiment)experiment).simulateDuration(duration);
49 public static void simulateExperiment(IExperiment experiment, boolean enabled) {
50 if(experiment instanceof IDynamicExperiment)
51 ((IDynamicExperiment)experiment).simulate(enabled);
54 public static ExperimentState getExperimentState(ReadGraph graph, IExperiment experiment) throws DatabaseException {
55 return experiment.getState(graph);
58 public static void disposeExperiment(final IExperiment experiment) {
60 if(experiment instanceof IDynamicExperiment) {
62 ((IDynamicExperiment)experiment).shutdown(null);
64 Session session = Simantics.getSession();
65 VirtualGraphSupport vgs = session.getService(VirtualGraphSupport.class);
66 session.asyncRequest(new WriteRequest(vgs.getMemoryPersistent("experiments")) {
69 public void perform(WriteGraph graph) throws DatabaseException {
71 SimulationResource SIMU = SimulationResource.getInstance(graph);
72 Resource activeRun = experiment.getResource();
73 graph.deny(activeRun, SIMU.IsActive, activeRun);
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);
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);
100 * Synchronously shutdown active experiment.
104 public static void shutdownActiveExperiment(IProject project) {
105 shutdownActiveExperiment(project, null);
109 * Synchronously shutdown active experiment.
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);
121 * If there is an active experiment, schedule a job for its shutdown.
125 public static void scheduleShutdownActiveExperiment(IProject project) {
126 scheduleShutdownActiveExperiment(project, null);
130 * If there is an active experiment, schedule a job for its shutdown.
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") {
140 protected IStatus run(final IProgressMonitor monitor) {
142 experiment.shutdown(monitor);
143 return Status.OK_STATUS;
146 if (callback != null)
147 callback.accept(null);
151 job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);
155 if (callback != null)
156 callback.accept(null);
160 public static Variable possibleActiveRunVariable(ReadGraph graph, Resource model) throws DatabaseException {
161 return graph.syncRequest(new PossibleActiveRun(model));
167 * @param asyncCallback
169 * @throws DatabaseException
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")) {
175 public Resource perform(WriteGraph graph) throws DatabaseException {
176 return activateExperiment(graph, experiment);
179 if (processor instanceof WriteGraph) {
180 return ((WriteGraph) processor).syncRequest(w);
182 if (asyncCallback == null)
183 asyncCallback = new ProcedureAdapter<>();
184 processor.getSession().asyncRequest(w, asyncCallback);
192 * @throws DatabaseException
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")) {
198 public void perform(WriteGraph graph) throws DatabaseException {
199 ExperimentUtil.activateRun(graph, run);
202 if (processor instanceof WriteGraph) {
203 ((WriteGraph) processor).syncRequest(w);
205 processor.getSession().asyncRequest(w);
209 public static Resource activateExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {
211 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
212 return graph.syncRequest(new WriteResultRequest<Resource>(vgs.getMemoryPersistent("experiments")) {
214 public Resource perform(WriteGraph graph) throws DatabaseException {
215 return createExperimentRun(graph, experiment);
221 public static Resource createExperimentRun(WriteGraph graph, Resource experiment) throws DatabaseException {
223 Layer0 L0 = Layer0.getInstance(graph);
224 SimulationResource SIMU = SimulationResource.getInstance(graph);
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();
234 return createExperimentRunWithType(graph, experiment, runType);
238 public static Resource createExperimentRunWithType(WriteGraph graph, Resource experiment, Resource runType) throws DatabaseException {
240 Layer0 L0 = Layer0.getInstance(graph);
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);
247 activateRun(graph, run);
253 public static void activateRun(WriteGraph graph, Resource run) throws DatabaseException {
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);
260 graph.claim(run, SIMU.IsActive, run);