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%2Fexperiment%2FExperimentUtil.java;h=d2fc9b560e7e9b54e1e3e4e81e7a5ea7acedec2d;hp=449afe59f3209d26e229b57c0da30bb4c05ccc95;hb=ca59190923cb45fbcde8d18d78c9c1418c1f0471;hpb=d0d3598ccf62e2222c319d78cd2b99950cdd6f78 diff --git a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java index 449afe59f..d2fc9b560 100644 --- a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java +++ b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java @@ -37,6 +37,10 @@ import org.simantics.simulation.project.IExperimentManager; */ public final class ExperimentUtil { + public static void refreshExperiment(ReadGraph graph, IExperiment experiment) { + experiment.refresh(graph); + } + public static void stepExperiment(IExperiment experiment, double duration) { if(experiment instanceof IDynamicExperiment) ((IDynamicExperiment)experiment).simulateDuration(duration); @@ -84,6 +88,31 @@ public final class ExperimentUtil { ((IDynamicExperiment)experiment).simulateDuration(duration); } + public static boolean canStepUntil(double endTime) { + IExperimentManager manager = + Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if (experiment instanceof IDynamicExperiment) { + IDynamicExperiment exp = (IDynamicExperiment) experiment; + double currentTime = exp.getSimulationTime(); + return currentTime < endTime; + } + return false; + } + + public static void stepUntil(double endTime) { + IExperimentManager manager = + Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if (experiment instanceof IDynamicExperiment) { + IDynamicExperiment exp = (IDynamicExperiment) experiment; + double currentTime = exp.getSimulationTime(); + if (currentTime < endTime) { + exp.simulateDuration(endTime - currentTime); + } + } + } + public static void simulate(boolean enabled) { IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); @@ -202,7 +231,20 @@ public final class ExperimentUtil { } } - private static Resource activateExperiment(WriteGraph graph, Resource experiment) throws DatabaseException { + public static Resource activateExperiment(WriteGraph graph, Resource experiment) throws DatabaseException { + + VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class); + return graph.syncRequest(new WriteResultRequest(vgs.getMemoryPersistent("experiments")) { + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return createExperimentRun(graph, experiment); + } + }); + + } + + public static Resource createExperimentRun(WriteGraph graph, Resource experiment) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); SimulationResource SIMU = SimulationResource.getInstance(graph); @@ -213,35 +255,35 @@ public final class ExperimentUtil { if (runTypes.size() != 1) throw new DatabaseException("No unique run type was found for experiment " + graph.getPossibleURI(experiment)); final Resource runType = runTypes.iterator().next(); + + return createExperimentRunWithType(graph, experiment, runType); + + } - VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class); - return graph.syncRequest(new WriteResultRequest(vgs.getMemoryPersistent("experiments")) { - @Override - public Resource perform(WriteGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - Resource run = graph.newResource(); - graph.claim(run, L0.InstanceOf, runType); - graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING); - graph.claim(experiment, L0.ConsistsOf, run); - - Resource activeRun = graph.syncRequest(new PossibleActiveExperiment(experiment)); - if (activeRun != null) { - graph.deny(activeRun, SIMU.IsActive, activeRun); - } - graph.claim(run, SIMU.IsActive, run); + public static Resource createExperimentRunWithType(WriteGraph graph, Resource experiment, Resource runType) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); - return run; - } - }); + Resource run = graph.newResource(); + graph.claim(run, L0.InstanceOf, runType); + graph.addLiteral(run, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(experiment, L0.ConsistsOf, run); + + activateRun(graph, run); + + return run; + } - private static void activateRun(WriteGraph graph, Resource run) throws DatabaseException { + public static void activateRun(WriteGraph graph, Resource run) throws DatabaseException { + SimulationResource SIMU = SimulationResource.getInstance(graph); Resource activeRun = graph.syncRequest(new PossibleActiveExperiment(run)); if (activeRun != null) { graph.deny(activeRun, SIMU.IsActive, activeRun); } graph.claim(run, SIMU.IsActive, run); + } }