]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
New SD Model handler
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 31 Aug 2011 06:05:51 +0000 (06:05 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 31 Aug 2011 06:05:51 +0000 (06:05 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21973 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/icons/chart_organisation_add.png [new file with mode: 0644]
org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java

diff --git a/org.simantics.sysdyn.ui/icons/chart_organisation_add.png b/org.simantics.sysdyn.ui/icons/chart_organisation_add.png
new file mode 100644 (file)
index 0000000..f0dba4a
Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/chart_organisation_add.png differ
index 0fe3695bfee24a9320b1653a12215f9034a19e79..4d985f8909c9973c7190c7131ce2db52fc4fb14a 100644 (file)
       <menuContribution\r
             locationURI="popup:#ExternalFunctionFileBrowser?after=wbStart">\r
       </menuContribution>\r
+      <menuContribution\r
+            allPopups="false"\r
+            locationURI="menu:#FileNewMenu">\r
+         <command\r
+               commandId="org.simantics.sysdyn.ui.newModel"\r
+               icon="icons/chart_organisation_add.png"\r
+               label="SD Model"\r
+               style="push">\r
+         </command>\r
+      </menuContribution>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.commands">\r
             id="org.simantics.sysdyn.ui.newSpreadSheet"\r
             name="New SpreadSheet">\r
       </command>\r
+      <command\r
+            id="org.simantics.sysdyn.ui.newModel"\r
+            name="New System Dynamics Model">\r
+      </command>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.handlers">\r
             class="org.simantics.sysdyn.ui.handlers.NewSpreadSheetHandler"\r
             commandId="org.simantics.sysdyn.ui.newSpreadSheet">\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.simantics.browsing.ui.common.viewpointContributionBinding">\r
                value="plugin_customization.ini"/>\r
          <property\r
                name="windowImages"\r
-               value="icons/sysdyn.png,icons/sysdyn32.png">\r
+               value="icons/sysdyn.png">\r
          </property>\r
       </product>\r
    </extension>\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java
new file mode 100644 (file)
index 0000000..90778d9
--- /dev/null
@@ -0,0 +1,45 @@
+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.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\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.sysdyn.ui.Activator;\r
+import org.simantics.sysdyn.ui.utils.ModelUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class NewModelHandler extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        \r
+        \r
+        Job job = new Job("Creating System Dynamics Model") {\r
+            @Override\r
+            protected IStatus run(IProgressMonitor monitor) {\r
+                try {\r
+                    SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+                        @Override\r
+                        public void perform(WriteGraph graph) throws DatabaseException {\r
+                            ModelUtils.createModel(graph);\r
+                        }\r
+                    });\r
+                    return Status.OK_STATUS;\r
+                } catch (DatabaseException e) {\r
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);\r
+                }\r
+            }\r
+        };\r
+        job.setUser(true);\r
+        job.schedule();\r
+\r
+        return null;\r
+    }\r
+\r
+}\r
index 023783d3ae22b34198fba0f0823971065e0f9883..b5c30574efae3cda3541f1fc21cb15d18711de9d 100644 (file)
 package org.simantics.sysdyn.ui.utils;\r
 \r
+import java.util.UUID;\r
+\r
+import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Template;\r
+import org.simantics.document.DocumentResource;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.project.ontology.ProjectResource;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
 import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.ArrayMap;\r
 \r
 public class ModelUtils {\r
 \r
-       /**\r
-        * Returns the SysdynModel where the given variable is located\r
-        * \r
-        * @param graph ReadGraph\r
-        * @param variable Variable whose model is wanted\r
-        * @return SysdynModel where variable is located\r
-        * @throws DatabaseException\r
-        */\r
-       public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException  {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
-               Resource configuration = graph.getPossibleObject(variable, l0.PartOf);\r
-               if(configuration == null)\r
-                       return null;\r
-               SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
-               SysdynModel model = sdm.getModel(graph, configuration);\r
-               try {\r
-                       model.update(graph);\r
-               } catch (DatabaseException e1) {\r
-                       e1.printStackTrace();\r
-               }\r
-               return model;\r
-       }\r
+    /**\r
+     * Returns the SysdynModel where the given variable is located\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param variable Variable whose model is wanted\r
+     * @return SysdynModel where variable is located\r
+     * @throws DatabaseException\r
+     */\r
+    public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException  {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        Resource configuration = graph.getPossibleObject(variable, l0.PartOf);\r
+        if(configuration == null)\r
+            return null;\r
+        SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+        SysdynModel model = sdm.getModel(graph, configuration);\r
+        try {\r
+            model.update(graph);\r
+        } catch (DatabaseException e1) {\r
+            e1.printStackTrace();\r
+        }\r
+        return model;\r
+    }\r
+\r
+    public static void createModel(WriteGraph graph) {\r
+        createModelAt(graph, SimanticsUI.getProject().get());\r
+    }\r
+\r
+    public static void createModelAt(WriteGraph g, Resource library) {\r
+        try {\r
+            Layer0 l0 = Layer0.getInstance(g);\r
+            Layer0X L0X = Layer0X.getInstance(g);\r
+            SysdynResource sr = SysdynResource.getInstance(g);\r
+            SpreadsheetResource SHEET = SpreadsheetResource.getInstance(g);\r
+            ModelingUtils mu = new ModelingUtils(g);\r
+            DocumentResource DOC = DocumentResource.getInstance(g);\r
+\r
+            String modelName = NameUtils.findFreshName(g, "Model", library, l0.ConsistsOf, "%s%d");\r
+\r
+            Resource model = g.newResource();\r
+            g.claimLiteral(model, l0.HasName, modelName);\r
+            g.claimLiteral(model, l0.HasLabel, modelName);\r
+            g.claim(model, l0.InstanceOf, sr.SysdynModel);\r
+            g.claim(model, l0.PartOf, SimanticsUI.getProject().get());\r
+\r
+            Resource conf = GraphUtils.create2(g,\r
+                    sr.Configuration,\r
+                    l0.PartOf, model,\r
+                    L0X.IsBaseRealizationOf, model,\r
+                    l0.HasName, modelName\r
+                    );\r
+\r
+            Resource diagram = g.newResource();\r
+            g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,\r
+                    ArrayMap\r
+                    .keys("", "diagram", "name")\r
+                    .values(conf, diagram, "Diagrammi")\r
+                    );\r
+\r
+            g.claim(model, mu.SIMU.HasConfiguration, conf);\r
+\r
+            Resource book = g.newResource();\r
+            g.claim(book, l0.InstanceOf, null, SHEET.Book);\r
+            g.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING);\r
+            g.claim(conf, l0.ConsistsOf, l0.PartOf, book);\r
+\r
+            SheetUtils.createSheet(g, book, "Sheet1", new String[] { }, new int[] { 50 });\r
+\r
+\r
+\r
+            ModelingResources mr = ModelingResources.getInstance(g);\r
+            // Remove default mapping and add sysdyn mapping\r
+            for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) {\r
+                if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {\r
+                    g.deny(diagram, L0X.HasTrigger, trigger);\r
+                }\r
+            }\r
+            Resource mapping = g.newResource();\r
+            g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
+            g.claim(diagram, L0X.HasTrigger, mapping);\r
+\r
+            Resource report = GraphUtils.create2(g, DOC.Report,  DOC.HasDocumentation, "===Report===");\r
+\r
+            GraphUtils.create2(g, mu.SIMU.Experiment,\r
+                    l0.HasName, "Experiment",\r
+                    l0.HasLabel, "Experiment",\r
+                    DOC.HasReportFactory, report,\r
+                    l0.PartOf, model);\r
+\r
+            ProjectResource PROJ = ProjectResource.getInstance(g);\r
+            for(Resource dep : g.getObjects(library, l0.IsLinkedTo)) {\r
+                if(g.isInstanceOf(dep, PROJ.NamespaceRequirement)) {\r
+                    for(Resource req : g.getObjects(dep, PROJ.RequiresNamespace)) {\r
+                        String uri = g.getPossibleValue(req, Bindings.STRING);\r
+                        if(uri != null) {\r
+                            Resource target = g.getResource(uri);\r
+                            if(target != null) {\r
+                                g.claim(model, l0.IsLinkedTo, null, target);\r
+                            }\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+        } catch (DatabaseException e ) {\r
+\r
+        }\r
+    }\r
 }\r
index dcf0887a1bd705558d33a9b6a3b8214644b389e9..f0030c8c2ed235c7432244f97b19c685876d6319 100644 (file)
@@ -93,10 +93,10 @@ public class StockExpression extends Expression {
             }\r
 \r
         } else {\r
-            for(Valve valve : incoming)\r
-                b.append("\n        + ").append(valve.getName() + range);\r
             for(Valve valve : outgoing)\r
                 b.append("\n        - ").append(valve.getName() + range);\r
+            for(Valve valve : incoming)\r
+                b.append("\n        + ").append(valve.getName() + range);\r
         }\r
         b.append(";\n");\r
         return b.toString();\r