+ /**
+ * 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);
+ }
+ });
+ }
+