From: lempinen Date: Thu, 10 Jun 2010 13:11:41 +0000 (+0000) Subject: Create experiments and remove run files when removing model, experiment or result X-Git-Tag: simantics-1.2.0~224 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=067b9408dd9790e76933136e6535186b59118548;p=simantics%2Fsysdyn.git Create experiments and remove run files when removing model, experiment or result git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16111 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 402d10a1..40eb7ca1 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -251,6 +251,22 @@ label="Module" style="push"> + + + + + + + + @@ -385,6 +403,11 @@ id="org.simantics.sysdyn.ui.activateResult" name="Activate Result"> + + @@ -422,13 +445,29 @@ class="org.simantics.sysdyn.ui.handlers.UnlinkNodeHandler" commandId="org.simantics.sysdyn.ui.removeNode"> - - - - + + + + + + + + + + + + + + - - - - diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewExperimentNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewExperimentNodeHandler.java new file mode 100644 index 00000000..3b101a20 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewExperimentNodeHandler.java @@ -0,0 +1,47 @@ +package org.simantics.sysdyn.ui.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.db.Builtins; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; + +public class NewExperimentNodeHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + System.out.println("New Experiment"); + + ISelection sel = HandlerUtil.getCurrentSelection(event); + Resource[] resources = ResourceAdaptionUtils.toResources(sel); + if (resources.length != 1) + return null; + + final Resource model = resources[0]; + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) throws DatabaseException { + Builtins b = g.getBuiltins(); + Resource report = GraphUtils.create2(g, b.Report, b.HasDocumentation, "===Report==="); + @SuppressWarnings("unused") + Resource experiment = GraphUtils.create2(g, b.Experiment, + b.HasName, "Experiment", + b.HasLabel, "Experiment", + b.HasReportFactory, report, + b.PartOf, model); + } + }); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java index 307f1f20..52666750 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java @@ -11,6 +11,10 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.handlers; +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; @@ -26,6 +30,7 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.editor.SysdynDiagramEditor; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; @@ -54,20 +59,29 @@ public class UnlinkNodeHandler extends AbstractHandler { } private void deleteItem(final Resource[] resources) { + final ArrayList configurations = new ArrayList(); try { SimanticsUI.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException { - Builtins b = graph.getBuiltins(); - for (Resource r : resources) - graph.deny(r, b.PartOf); + SysdynResource sr = SysdynResource.getInstance(graph); + for (Resource r : resources) { + if (graph.isInstanceOf(r, sr.SysdynModel)) { + unlinkModel(graph, r); + configurations.add(graph.getSingleObject(r, graph.getBuiltins().HasConfiguration)); + } + else if (graph.isInstanceOf(r, graph.getBuiltins().Experiment)) + unlinkExperiment(graph, r); + else if (graph.isInstanceOf(r, sr.Result)) + unlinkResult(graph, r); + } } }); } catch (DatabaseException e) { ExceptionUtils.logAndShowError(e); } - for (Resource r : resources) { + for (Resource r : configurations) { IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences(); for(IEditorReference er : editorReferences) { if(er.getEditor(false) instanceof SysdynDiagramEditor) { @@ -81,4 +95,41 @@ public class UnlinkNodeHandler extends AbstractHandler { } + private void unlinkModel(WriteGraph graph, Resource model) throws DatabaseException { + Builtins b = graph.getBuiltins(); + + for(Resource r : graph.getObjects(model, b.ConsistsOf)) + if(graph.isInstanceOf(r, SysdynResource.getInstance(graph).Result)) + deleteResultFiles(graph, r); + + graph.deny(model, b.PartOf); + } + + private void unlinkExperiment(WriteGraph graph, Resource experiment) throws DatabaseException { + Builtins b = graph.getBuiltins(); + Collection results = graph.getObjects(experiment, SysdynResource.getInstance(graph).HasResult); + if(results != null) + for(Resource result : results) + unlinkResult(graph, result); + graph.deny(experiment, b.PartOf); + + } + + private void unlinkResult(WriteGraph graph, Resource result) throws DatabaseException { + Builtins b = graph.getBuiltins(); + deleteResultFiles(graph, result); + graph.deny(result, b.PartOf); + graph.deny(result, graph.getInverse(SysdynResource.getInstance(graph).HasResult)); + + } + + private void deleteResultFiles(WriteGraph graph, Resource result) throws DatabaseException { + String path; + path = graph.getPossibleRelatedValue(result, SysdynResource.getInstance(graph).HasResultFile); + if(path != null) { + File file = new File(path); + file.delete(); + } + } + } \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index c89d0a63..a2a2e88f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -47,6 +47,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { public void saveState() { // TODO Auto-generated method stub System.out.println("saveState"); + if(sysdynModel.getSimulationResult() == null) return; try { session.syncRequest(new WriteRequest() { @@ -62,6 +63,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { Resource res = GraphUtils.create2(graph, sr.Result, b.HasLabel, "Result x", b.HasName, "Result x", + b.PartOf, model, sr.HasResultFile, file.getAbsolutePath()); graph.claim(experiment, sr.HasResult, res); result.saveToFile(file); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java index e0783bb7..5b751fc1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -109,6 +109,7 @@ public class SysdynResult { } public void saveToFile(File file) { + if(recordingSession == null) return; try { Binding binding = Bindings.getBinding( recordingSession.getClass() ); Accessors.createFile(file, binding, recordingSession);