]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added more createRun utility functions to ExperimentRuns 13/1513/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 28 Feb 2018 11:08:16 +0000 (13:08 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 28 Feb 2018 11:41:36 +0000 (13:41 +0200)
The new functions directly take WriteGraph as input to allow clients to
create an experiment run within one write transaction and still work on
it afterwards.

refs #7790

Change-Id: I49c86df960ec9a42d7dfa3fdba95315398ec6f65
(cherry picked from commit 52f757730dc0355e187d36e745cb7b57375766ee)

bundles/org.simantics.simulation/src/org/simantics/simulation/project/ExperimentRuns.java

index c708153f22706365d3ba6638279cb8bbf88cf115..21fcf2c268847704ad00375f60dcbe525dca309b 100644 (file)
@@ -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;
@@ -79,37 +79,16 @@ 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<WriteGraph, Resource, Object> external, final Consumer<Resource> successCallback)
+            final IExperimentActivationListener listener,
+            final Function2<WriteGraph, Resource, Object> externalWrite,
+            final Consumer<Resource> successCallback)
     {
-        final AtomicReference<Resource> _run = new AtomicReference<Resource>();
+        final AtomicReference<Resource> run = new AtomicReference<>();
         session.asyncRequest(new WriteRequest(vg) {
             @Override
             public void perform(WriteGraph graph) throws DatabaseException {
 //                System.out.println("ExperimentActivator " + experimentResource + " " + experiment.getIdentifier());
-
-                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);
+                run.set( createRun(graph, experimentResource, experiment, experimentRunTypeURI, externalWrite) );
             }
         }, e -> {
             if (e != null) {
@@ -118,15 +97,93 @@ public class ExperimentRuns {
                 else
                     ErrorLogger.defaultLogError(e);
             } else {
-                attachStateListener(session, experiment, _run.get());
+                attachStateListener(session, experiment, run.get());
                 if (successCallback != null)
-                    successCallback.accept(_run.get());
+                    successCallback.accept(run.get());
                 if (listener != null)
                     listener.onExperimentActivated(experiment);
             }
         });
     }
 
+    /**
+     * 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<WriteGraph, Resource, Object> externalWrite)
+                    throws DatabaseException
+    {
+        return graph.syncRequest(new WriteResultRequest<Resource>(vg) {
+            @Override
+            public Resource perform(WriteGraph graph) throws DatabaseException {
+                return createRun(graph, experimentResource, experiment, experimentRunTypeURI, externalWrite);
+            }
+        });
+    }
+
+    public static Resource createRun(WriteGraph graph,
+            Resource experimentResource,
+            IExperiment experiment,
+            String experimentRunTypeURI,
+            Function2<WriteGraph, Resource, Object> 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<WriteGraph, Resource, Object> externalWrite)
+                    throws DatabaseException
+    {
+        SimulationResource SIMU = SimulationResource.getInstance(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(externalWrite != null)
+                    externalWrite.apply(graph, run);
+            }
+        });
+    }
+
     /**
      * Add listener for tracking run IsActive state in the graph.
      * 
@@ -145,9 +202,6 @@ 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"));
                         }
                     }, e -> {
                         if (e != null)