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 stepExperiment(IExperiment experiment, double duration) {
41 if(experiment instanceof IDynamicExperiment)
42 ((IDynamicExperiment)experiment).simulateDuration(duration);
45 public static void simulateExperiment(IExperiment experiment, boolean enabled) {
46 if(experiment instanceof IDynamicExperiment)
47 ((IDynamicExperiment)experiment).simulate(enabled);
50 public static ExperimentState getExperimentState(ReadGraph graph, IExperiment experiment) throws DatabaseException {
51 return experiment.getState(graph);
54 public static void disposeExperiment(final IExperiment experiment) {
56 if(experiment instanceof IDynamicExperiment) {
58 ((IDynamicExperiment)experiment).shutdown(null);
60 Session session = Simantics.getSession();
61 VirtualGraphSupport vgs = session.getService(VirtualGraphSupport.class);
62 session.asyncRequest(new WriteRequest(vgs.getMemoryPersistent("experiments")) {
65 public void perform(WriteGraph graph) throws DatabaseException {
67 SimulationResource SIMU = SimulationResource.getInstance(graph);
68 Resource activeRun = experiment.getResource();
69 graph.deny(activeRun, SIMU.IsActive, activeRun);
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);
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);
96 * Synchronously shutdown active experiment.
100 public static void shutdownActiveExperiment(IProject project) {
101 shutdownActiveExperiment(project, null);
105 * Synchronously shutdown active experiment.
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);
117 * If there is an active experiment, schedule a job for its shutdown.
121 public static void scheduleShutdownActiveExperiment(IProject project) {
122 scheduleShutdownActiveExperiment(project, null);
126 * If there is an active experiment, schedule a job for its shutdown.
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") {
136 protected IStatus run(final IProgressMonitor monitor) {
138 experiment.shutdown(monitor);
139 return Status.OK_STATUS;
142 if (callback != null)
143 callback.accept(null);
147 job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);
151 callback.accept(null);
155 public static Variable possibleActiveRunVariable(ReadGraph graph, Resource model) throws DatabaseException {
156 return graph.syncRequest(new PossibleActiveRun(model));
162 * @param asyncCallback
164 * @throws DatabaseException
166 public static Resource activateExperiment(RequestProcessor processor, Resource experiment, Procedure<Resource> asyncCallback) throws DatabaseException {
167 VirtualGraphSupport vgs = processor.getService(VirtualGraphSupport.class);
168 WriteResultRequest<Resource> w = new WriteResultRequest<Resource>(vgs.getMemoryPersistent("experiments")) {
170 public Resource perform(WriteGraph graph) throws DatabaseException {
171 return activateExperiment(graph, experiment);
174 if (processor instanceof WriteGraph) {
175 return ((WriteGraph) processor).syncRequest(w);
177 if (asyncCallback == null)
178 asyncCallback = new ProcedureAdapter<>();
179 processor.getSession().asyncRequest(w, asyncCallback);
187 * @throws DatabaseException
189 public static void activateRun(RequestProcessor processor, Resource run) throws DatabaseException {
190 VirtualGraphSupport vgs = processor.getService(VirtualGraphSupport.class);
191 WriteRequest w = new WriteRequest(vgs.getMemoryPersistent("experiments")) {
193 public void perform(WriteGraph graph) throws DatabaseException {
194 ExperimentUtil.activateRun(graph, run);
197 if (processor instanceof WriteGraph) {
198 ((WriteGraph) processor).syncRequest(w);
200 processor.getSession().asyncRequest(w);
204 private static Resource activateExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {
205 Layer0 L0 = Layer0.getInstance(graph);
206 SimulationResource SIMU = SimulationResource.getInstance(graph);
208 Resource experimentType = graph.getPossibleType(experiment, SIMU.Experiment);
209 if (experimentType == null)
210 throw new DatabaseException("No unique experiment type was found for experiment " + graph.getPossibleURI(experiment));
211 Collection<Resource> runTypes = graph.sync(new ObjectsWithType(experimentType, L0.ConsistsOf, SIMU.RunType));
212 if (runTypes.size() != 1)
213 throw new DatabaseException("No unique run type was found for experiment " + graph.getPossibleURI(experiment));
214 final Resource runType = runTypes.iterator().next();
216 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
217 return graph.syncRequest(new WriteResultRequest<Resource>(vgs.getMemoryPersistent("experiments")) {
219 public Resource perform(WriteGraph graph) throws DatabaseException {
220 Layer0 L0 = Layer0.getInstance(graph);
221 Resource run = graph.newResource();
222 graph.claim(run, L0.InstanceOf, runType);
223 graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
224 graph.claim(experiment, L0.ConsistsOf, run);
226 Resource activeRun = graph.syncRequest(new PossibleActiveExperiment(experiment));
227 if (activeRun != null) {
228 graph.deny(activeRun, SIMU.IsActive, activeRun);
230 graph.claim(run, SIMU.IsActive, run);
237 private static void activateRun(WriteGraph graph, Resource run) throws DatabaseException {
238 SimulationResource SIMU = SimulationResource.getInstance(graph);
239 Resource activeRun = graph.syncRequest(new PossibleActiveExperiment(run));
240 if (activeRun != null) {
241 graph.deny(activeRun, SIMU.IsActive, activeRun);
243 graph.claim(run, SIMU.IsActive, run);