]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Create experiments and remove run files when removing model, experiment or result
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 10 Jun 2010 13:11:41 +0000 (13:11 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 10 Jun 2010 13:11:41 +0000 (13:11 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16111 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewExperimentNodeHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java

index 402d10a1ed41bcab896b92952e742ce348adf2fb..40eb7ca1819e36ff571d9e2b1e6e379f46cd30a0 100644 (file)
                   label="Module"\r
                   style="push">\r
             </command>\r
+            <command\r
+                  commandId="org.simantics.sysdyn.ui.newExperiment"\r
+                  id="org.simantics.sysdyn.ui.browser.newExperiment"\r
+                  label="Experiment"\r
+                  style="push">\r
+               <visibleWhen\r
+                     checkEnabled="true">\r
+                  <with\r
+                        variable="selection">\r
+                     <test\r
+                           args="org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder"\r
+                           property="org.simantics.sysdyn.ui.nodeClass">\r
+                     </test>\r
+                  </with>\r
+               </visibleWhen>\r
+            </command>\r
          </menu>\r
       </menuContribution>\r
       <menuContribution\r
          </state>\r
       </command>\r
       <command\r
+            defaultHandler="org.simantics.sysdyn.ui.handlers.NewModuleNodeHandler"\r
             id="org.simantics.sysdyn.ui.newModuleNode"\r
             name="New Module">\r
       </command>\r
       <command\r
+            defaultHandler="org.simantics.sysdyn.ui.handlers.NewModelHandler"\r
             id="org.simantics.sysdyn.ui.newModel"\r
             name="New Model">\r
       </command>\r
             id="org.simantics.sysdyn.ui.activateResult"\r
             name="Activate Result">\r
       </command>\r
+      <command\r
+            defaultHandler="org.simantics.sysdyn.ui.handlers.NewExperimentNodeHandler"\r
+            id="org.simantics.sysdyn.ui.newExperiment"\r
+            name="New Experiment">\r
+      </command>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.handlers">\r
             class="org.simantics.sysdyn.ui.handlers.UnlinkNodeHandler"\r
             commandId="org.simantics.sysdyn.ui.removeNode">\r
          <activeWhen>\r
-            <with\r
-                  variable="selection">\r
-               <test\r
-                     args="org.simantics.sysdyn.ui.browser.nodes.ModelNode"\r
-                     property="org.simantics.sysdyn.ui.nodeClass">\r
-               </test>\r
-            </with>\r
+            <or>\r
+               <with\r
+                     variable="selection">\r
+                  <test\r
+                        args="org.simantics.sysdyn.ui.browser.nodes.ExperimentNode"\r
+                        property="org.simantics.sysdyn.ui.nodeClass">\r
+                  </test>\r
+               </with>\r
+               <with\r
+                     variable="selection">\r
+                  <test\r
+                        args="org.simantics.sysdyn.ui.browser.nodes.ModelNode"\r
+                        property="org.simantics.sysdyn.ui.nodeClass">\r
+                  </test>\r
+               </with>\r
+               <with\r
+                     variable="selection">\r
+                  <test\r
+                        args="org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode"\r
+                        property="org.simantics.sysdyn.ui.nodeClass">\r
+                  </test>\r
+               </with>\r
+            </or>\r
          </activeWhen>\r
       </handler>\r
       <handler\r
             </with>\r
          </activeWhen>\r
       </handler>\r
-      <handler\r
-            class="org.simantics.sysdyn.ui.handlers.NewModuleNodeHandler"\r
-            commandId="org.simantics.sysdyn.ui.newModuleNode">\r
-      </handler>\r
-      <handler\r
-            class="org.simantics.sysdyn.ui.handlers.NewModelHandler"\r
-            commandId="org.simantics.sysdyn.ui.newModel">\r
-      </handler>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.cheatsheets.cheatSheetContent">\r
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 (file)
index 0000000..3b101a2
--- /dev/null
@@ -0,0 +1,47 @@
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+public class NewExperimentNodeHandler extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        System.out.println("New Experiment");\r
+        \r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        Resource[] resources = ResourceAdaptionUtils.toResources(sel);\r
+        if (resources.length != 1)\r
+            return null;\r
+        \r
+        final Resource model = resources[0];\r
+        \r
+        SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+            \r
+            @Override\r
+            public void perform(WriteGraph g) throws DatabaseException {\r
+                Builtins b = g.getBuiltins();\r
+                Resource report = GraphUtils.create2(g, b.Report,  b.HasDocumentation, "===Report===");\r
+                @SuppressWarnings("unused")\r
+                Resource experiment = GraphUtils.create2(g, b.Experiment,\r
+                        b.HasName, "Experiment",\r
+                        b.HasLabel, "Experiment",\r
+                        b.HasReportFactory, report,\r
+                        b.PartOf, model);\r
+            }\r
+        });\r
+        return null;\r
+    }\r
+\r
+}\r
index 307f1f205d74df9002ae6200be24c1cfd18525d1..52666750714e3ead447bbb818c215b1644672377 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.handlers;\r
 \r
+import java.io.File;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
@@ -26,6 +30,7 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.CancelTransactionException;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.editor.SysdynDiagramEditor;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
@@ -54,20 +59,29 @@ public class UnlinkNodeHandler extends AbstractHandler {
     }\r
 \r
     private void deleteItem(final Resource[] resources) {\r
+        final ArrayList<Resource> configurations = new ArrayList<Resource>();\r
         try {\r
             SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
                 @Override\r
                 public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException {\r
-                    Builtins b = graph.getBuiltins();\r
-                    for (Resource r : resources)\r
-                        graph.deny(r, b.PartOf);\r
+                    SysdynResource sr = SysdynResource.getInstance(graph);\r
+                    for (Resource r : resources) {\r
+                        if (graph.isInstanceOf(r, sr.SysdynModel)) {\r
+                            unlinkModel(graph, r);\r
+                            configurations.add(graph.getSingleObject(r, graph.getBuiltins().HasConfiguration));\r
+                        }\r
+                        else if (graph.isInstanceOf(r, graph.getBuiltins().Experiment))\r
+                            unlinkExperiment(graph, r);\r
+                        else if (graph.isInstanceOf(r, sr.Result))\r
+                            unlinkResult(graph, r);\r
+                    }\r
                 }\r
             });\r
         } catch (DatabaseException e) {\r
             ExceptionUtils.logAndShowError(e);\r
         }\r
 \r
-        for (Resource r : resources) {\r
+        for (Resource r : configurations) {\r
             IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();\r
             for(IEditorReference er : editorReferences) {\r
                 if(er.getEditor(false) instanceof SysdynDiagramEditor) {\r
@@ -81,4 +95,41 @@ public class UnlinkNodeHandler extends AbstractHandler {
 \r
     }\r
 \r
+    private void unlinkModel(WriteGraph graph, Resource model) throws DatabaseException {\r
+        Builtins b = graph.getBuiltins();\r
+\r
+        for(Resource r : graph.getObjects(model, b.ConsistsOf))\r
+            if(graph.isInstanceOf(r, SysdynResource.getInstance(graph).Result))\r
+                deleteResultFiles(graph, r);\r
+\r
+        graph.deny(model, b.PartOf);\r
+    }\r
+\r
+    private void unlinkExperiment(WriteGraph graph, Resource experiment) throws DatabaseException  {\r
+        Builtins b = graph.getBuiltins();\r
+        Collection<Resource> results = graph.getObjects(experiment, SysdynResource.getInstance(graph).HasResult);\r
+        if(results != null)\r
+            for(Resource result : results) \r
+                unlinkResult(graph, result);\r
+        graph.deny(experiment, b.PartOf);\r
+\r
+    }\r
+\r
+    private void unlinkResult(WriteGraph graph, Resource result) throws DatabaseException  {\r
+        Builtins b = graph.getBuiltins();\r
+        deleteResultFiles(graph, result);\r
+        graph.deny(result, b.PartOf);\r
+        graph.deny(result, graph.getInverse(SysdynResource.getInstance(graph).HasResult));\r
+\r
+    }\r
+\r
+    private void deleteResultFiles(WriteGraph graph, Resource result) throws DatabaseException  {\r
+        String path;\r
+        path = graph.getPossibleRelatedValue(result, SysdynResource.getInstance(graph).HasResultFile);\r
+        if(path != null) {\r
+            File file = new File(path);\r
+            file.delete();\r
+        }\r
+    }\r
+\r
 }
\ No newline at end of file
index c89d0a63c6ffe5d4889e33a81ac9c7a8c9c720bf..a2a2e88f8d05c4a07dc34dcd7c193e2c04d7f5fd 100644 (file)
@@ -47,6 +47,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
     public void saveState() {\r
         // TODO Auto-generated method stub\r
         System.out.println("saveState");\r
+        if(sysdynModel.getSimulationResult() == null) return;\r
 \r
         try {\r
             session.syncRequest(new WriteRequest() {\r
@@ -62,6 +63,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
                     Resource res = GraphUtils.create2(graph, sr.Result,\r
                             b.HasLabel, "Result x",\r
                             b.HasName, "Result x",\r
+                            b.PartOf, model,\r
                             sr.HasResultFile, file.getAbsolutePath());\r
                     graph.claim(experiment, sr.HasResult, res);\r
                     result.saveToFile(file);\r
index e0783bb7322e072eccd058359918d1e7492dce02..5b751fc1cd78f3e8d32cdf05f676a1a54e13f2c6 100644 (file)
@@ -109,6 +109,7 @@ public class SysdynResult {
     }\r
 \r
     public void saveToFile(File file) {\r
+        if(recordingSession == null) return;\r
         try {\r
             Binding binding = Bindings.getBinding( recordingSession.getClass() );\r
             Accessors.createFile(file, binding, recordingSession);\r