X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.simulation%2Fsrc%2Forg%2Fsimantics%2Fsimulation%2Fproject%2FExperimentRuns.java;h=5edf6c856ca80a020e394afeab4ba53e0e883fb6;hp=23d6e386a2ef4f9bb7373088a2e0fc683150262e;hb=f212fa100ac8c0ce464a826794b3d1737b138b25;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.simulation/src/org/simantics/simulation/project/ExperimentRuns.java b/bundles/org.simantics.simulation/src/org/simantics/simulation/project/ExperimentRuns.java index 23d6e386a..5edf6c856 100644 --- a/bundles/org.simantics.simulation/src/org/simantics/simulation/project/ExperimentRuns.java +++ b/bundles/org.simantics.simulation/src/org/simantics/simulation/project/ExperimentRuns.java @@ -8,8 +8,8 @@ import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.VirtualGraph; import org.simantics.db.WriteGraph; -import org.simantics.db.common.CommentMetadata; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.service.VirtualGraphSupport; @@ -19,7 +19,6 @@ import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.experiment.IExperimentListener; import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.utils.datastructures.Callback; import org.simantics.utils.ui.ErrorLogger; /** @@ -80,53 +79,139 @@ public class ExperimentRuns { */ public static void createRun(final Session session, VirtualGraph vg, final Resource experimentResource, final IExperiment experiment, final String experimentRunTypeURI, - final IExperimentActivationListener listener, final Function2 external, final Consumer successCallback) + final IExperimentActivationListener listener, + final Function2 externalWrite, + final Consumer successCallback) + { + createRun(session, vg, + experimentResource, experiment, experimentRunTypeURI, + listener, externalWrite, successCallback, true); + } + + /** + * Create new experiment run in a selected virtual graph. + * + * @param session + * @param vg + * @param experimentResource + * @param experiment + * @param experimentRunTypeURI + * @param listener + * @param successCallback if non-null invoked with the created run resource + * as an argument, just before invoking + * listener.onExperimentActivated(experiment) + * @param attachDeactivationListener true to run for the created run-resource + * {@link #attachStateListener(Session, IExperiment, Resource)} + */ + public static void createRun( + Session session, + VirtualGraph vg, + Resource experimentResource, + IExperiment experiment, + String experimentRunTypeURI, + IExperimentActivationListener listener, + Function2 externalWrite, + Consumer successCallback, + boolean attachDeactivationListener) { - final AtomicReference _run = new AtomicReference(); + final AtomicReference run = new AtomicReference<>(); session.asyncRequest(new WriteRequest(vg) { @Override public void perform(WriteGraph graph) throws DatabaseException { // System.out.println("ExperimentActivator " + experimentResource + " " + experiment.getIdentifier()); + run.set( createRun(graph, experimentResource, experiment, experimentRunTypeURI, externalWrite) ); + } + }, e -> { + if (e != null) { + if (listener != null) + listener.onFailure(e); + else + ErrorLogger.defaultLogError(e); + } else { + if (attachDeactivationListener) + attachStateListener(session, experiment, run.get()); + if (successCallback != null) + successCallback.accept(run.get()); + if (listener != null) + listener.onExperimentActivated(experiment); + } + }); + } - final Layer0 L0 = Layer0.getInstance(graph); - final SimulationResource SIMU = SimulationResource.getInstance(graph); - - final Resource run = graph.newResource(); - String label = NameUtils.findFreshLabel(graph, "Experiment", experimentResource); - graph.claim(run, L0.InstanceOf, null, graph.getResource(experimentRunTypeURI)); - graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, experiment.getIdentifier(), Bindings.STRING); - graph.addLiteral(run, L0.HasLabel, L0.HasLabel_Inverse, L0.String, label, Bindings.STRING); - graph.addLiteral(run, SIMU.HasActivationTime, SIMU.HasActivationTime_Inverse, L0.Long, System.currentTimeMillis(), Bindings.LONG); - graph.claim(experimentResource, L0.ConsistsOf, L0.PartOf, run); - - // Mark the run active in the transient virtual graph. - VirtualGraph runtime = graph.getService(VirtualGraph.class); - graph.syncRequest(new WriteRequest(runtime) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - graph.claim(run, SIMU.IsActive, run); - if(external != null) - external.apply(graph, run); - } - }); - - _run.set(run); + /** + * Create new experiment run in a selected virtual graph. + * + * @param session + * @param vg + * @param experimentResource + * @param experiment + * @param experimentRunTypeURI + * @param listener + * @param successCallback if non-null invoked with the created run resource + * as an argument, just before invoking + * listener.onExperimentActivated(experiment) + */ + public static Resource createRun(WriteGraph graph, + VirtualGraph vg, + Resource experimentResource, + IExperiment experiment, + String experimentRunTypeURI, + Function2 externalWrite) + throws DatabaseException + { + return graph.syncRequest(new WriteResultRequest(vg) { + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return createRun(graph, experimentResource, experiment, experimentRunTypeURI, externalWrite); } - }, new Callback() { + }); + } + + public static Resource createRun(WriteGraph graph, + Resource experimentResource, + IExperiment experiment, + String experimentRunTypeURI, + Function2 externalWrite) + throws DatabaseException + { + Layer0 L0 = Layer0.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + + Resource run = graph.newResource(); + String label = NameUtils.findFreshLabel(graph, "Experiment", experimentResource); + graph.claim(run, L0.InstanceOf, null, graph.getResource(experimentRunTypeURI)); + graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, experiment.getIdentifier(), Bindings.STRING); + graph.addLiteral(run, L0.HasLabel, L0.HasLabel_Inverse, L0.String, label, Bindings.STRING); + graph.addLiteral(run, SIMU.HasActivationTime, SIMU.HasActivationTime_Inverse, L0.Long, System.currentTimeMillis(), Bindings.LONG); + graph.claim(experimentResource, L0.ConsistsOf, L0.PartOf, run); + + markRunActive(graph, run, externalWrite); + + return run; + } + + /** + * Mark the run active in the transient virtual graph. + * + * @param graph + * @param run + * @param externalWrite + * @throws DatabaseException + */ + private static void markRunActive( + WriteGraph graph, + Resource run, + Function2 externalWrite) + throws DatabaseException + { + SimulationResource SIMU = SimulationResource.getInstance(graph); + VirtualGraph runtime = graph.getService(VirtualGraph.class); + graph.syncRequest(new WriteRequest(runtime) { @Override - public void run(DatabaseException e) { - if (e != null) { - if (listener != null) - listener.onFailure(e); - else - ErrorLogger.defaultLogError(e); - } else { - attachStateListener(session, experiment, _run.get()); - if (successCallback != null) - successCallback.accept(_run.get()); - if (listener != null) - listener.onExperimentActivated(experiment); - } + public void perform(WriteGraph graph) throws DatabaseException { + graph.claim(run, SIMU.IsActive, run); + if(externalWrite != null) + externalWrite.apply(graph, run); } }); } @@ -149,16 +234,10 @@ public class ExperimentRuns { public void perform(WriteGraph graph) throws DatabaseException { SimulationResource SIMU = SimulationResource.getInstance(graph); graph.denyStatement(run, SIMU.IsActive, run); - - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add("Attaching state listener to track isActive for run")); - } - }, new Callback() { - @Override - public void run(DatabaseException e) { - if (e != null) - ErrorLogger.defaultLogError(e); } + }, e -> { + if (e != null) + ErrorLogger.defaultLogError(e); }); } }