]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
modelica text viewer for system dynamics configuration, Expression + NormalExpression
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 19 Nov 2009 14:36:21 +0000 (14:36 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 19 Nov 2009 14:36:21 +0000 (14:36 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13148 ac1ea38d-2e2b-0410-8846-a27921b304fc

13 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/Connect.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java
org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java

index 8b793f4e048f27ae819966b54de6b816cc8fc5cd..e955187950d522bd0f0d4981be4ac19237e62af2 100644 (file)
@@ -81,7 +81,7 @@ public class Connect extends DragEventHandler {
                                                0.5 * (fromOrigo.getY() + toOrigo.getY())\r
                                                );\r
                                editor.getDiagram().addElement(new FlowElement(from, (ValveElement)newElement));\r
-                               editor.getDiagram().addElement(new FlowElement(to, (ValveElement)newElement));                          \r
+                               editor.getDiagram().addElement(new FlowElement((ValveElement)newElement, to));                          \r
                        }\r
                        else if(from instanceof ValveElement && to instanceof AuxiliaryElement)\r
                                newElement = new DependencyElement(to, from);\r
index 9a513a891ce9832be6fc1f1d1bc028d63e52d3e0..04474d5792fa25fb3f872d9c39b0f8e20d6a3491 100644 (file)
@@ -47,8 +47,16 @@ public class EquationView extends ViewPart implements ISelectionListener {
                         @Override\r
                         public void perform(WriteGraph g)\r
                                 throws DatabaseException {\r
+                            Builtins b = g.getBuiltins();\r
                             SysdynResource sr = SysdynResource.getInstance(g);\r
-                            g.claimValue(auxiliary, sr.HasValue, currentText);\r
+                            \r
+                            Resource expression = g.getPossibleObject(auxiliary, sr.HasExpression);\r
+                            if(expression == null) {\r
+                                expression = g.newResource();\r
+                                g.claim(expression, b.InstanceOf, sr.Expression);\r
+                                g.claim(auxiliary, sr.HasExpression, expression);\r
+                            }\r
+                            g.claimValue(expression, sr.HasText, currentText);\r
                         }\r
                         \r
                     });\r
@@ -83,7 +91,9 @@ public class EquationView extends ViewPart implements ISelectionListener {
             if(g.isInstanceOf(resource, sr.Auxiliary) || g.isInstanceOf(resource, sr.Valve)) {\r
                 auxiliary = resource;\r
                 final String name = g.getRelatedValue(resource, b.HasName);\r
-                final String value = g.getPossibleRelatedValue(resource, sr.HasValue);\r
+                Resource expression = g.getPossibleObject(resource, sr.HasExpression);     \r
+                final String value = expression == null ? null :\r
+                    (String)g.getPossibleRelatedValue(expression, sr.HasText);\r
                 text.getDisplay().asyncExec(new Runnable() {\r
 \r
                     @Override\r
@@ -101,7 +111,9 @@ public class EquationView extends ViewPart implements ISelectionListener {
             else if(g.isInstanceOf(resource, sr.Stock)) {\r
                 auxiliary = resource;\r
                 final String name = g.getRelatedValue(resource, b.HasName);\r
-                final String value = g.getPossibleRelatedValue(resource, sr.HasValue);\r
+                Resource expression = g.getPossibleObject(resource, sr.HasExpression);    \r
+                final String value = expression == null ? null :\r
+                    (String)g.getPossibleRelatedValue(expression, sr.HasText);\r
                 text.getDisplay().asyncExec(new Runnable() {\r
 \r
                     @Override\r
index 200a48065137a585397987bdb0ac017b6148ef77..542861aecd6b62a70d56f8ed0166d2c0e348742b 100644 (file)
@@ -17,6 +17,12 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.editors.text.TextEditor;\r
 import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.LoadRepresentation;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.workbench.ResourceEditorInput;\r
 \r
 \r
 public class SysdynModelicaEditor extends TextEditor {\r
@@ -38,7 +44,17 @@ public class SysdynModelicaEditor extends TextEditor {
             @Override\r
             protected IDocument createDocument(Object element)\r
                     throws CoreException {\r
-                return new Document((String)getEditorInput().getAdapter(String.class));\r
+                try {\r
+                    Configuration configuration =\r
+                        LoadRepresentation.load(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());\r
+                    ModelicaWriter writer = new ModelicaWriter();\r
+                    writer.write(configuration);\r
+                    \r
+                    return new Document(writer.toString());\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                    throw new CoreException(STATUS_ERROR);\r
+                }                \r
             }\r
 \r
             @Override\r
index 01448ff3128d4281db5cbdbb3f401c073ed2de8d..efa65edc45b894f2885ad184378ba7f0f42ba319 100644 (file)
@@ -22,16 +22,19 @@ public class SysdynResource {
     public final Resource Cloud;\r
     public final Resource Configuration;\r
     public final Resource Dependency;\r
+    public final Resource Expression;\r
     public final Resource Flow;\r
     public final Resource HasAngle;\r
+    public final Resource HasExpression;\r
     public final Resource HasHead;\r
     public final Resource HasTail;\r
-    public final Resource HasValue;\r
+    public final Resource HasText;\r
     public final Resource HasX;\r
     public final Resource HasY;\r
     public final Resource IndependentVariable;\r
     public final Resource IsHeadOf;\r
     public final Resource IsTailOf;\r
+    public final Resource NormalExpression;\r
     public final Resource Stock;\r
     public final Resource SysdynProject;\r
     public final Resource Valve;\r
@@ -42,16 +45,19 @@ public class SysdynResource {
         public static final String Cloud = "http://www.simantics.org/Sysdyn#Cloud";\r
         public static final String Configuration = "http://www.simantics.org/Sysdyn#Configuration";\r
         public static final String Dependency = "http://www.simantics.org/Sysdyn#Dependency";\r
+        public static final String Expression = "http://www.simantics.org/Sysdyn#Expression";\r
         public static final String Flow = "http://www.simantics.org/Sysdyn#Flow";\r
         public static final String HasAngle = "http://www.simantics.org/Sysdyn#HasAngle";\r
+        public static final String HasExpression = "http://www.simantics.org/Sysdyn#HasExpression";\r
         public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead";\r
         public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail";\r
-        public static final String HasValue = "http://www.simantics.org/Sysdyn#HasValue";\r
+        public static final String HasText = "http://www.simantics.org/Sysdyn#HasText";\r
         public static final String HasX = "http://www.simantics.org/Sysdyn#HasX";\r
         public static final String HasY = "http://www.simantics.org/Sysdyn#HasY";\r
         public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable";\r
         public static final String IsHeadOf = "http://www.simantics.org/Sysdyn#IsHeadOf";\r
         public static final String IsTailOf = "http://www.simantics.org/Sysdyn#IsTailOf";\r
+        public static final String NormalExpression = "http://www.simantics.org/Sysdyn#NormalExpression";\r
         public static final String Stock = "http://www.simantics.org/Sysdyn#Stock";\r
         public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject";\r
         public static final String Valve = "http://www.simantics.org/Sysdyn#Valve";\r
@@ -72,16 +78,19 @@ public class SysdynResource {
         Cloud = getResourceOrNull(graph, URIs.Cloud);\r
         Configuration = getResourceOrNull(graph, URIs.Configuration);\r
         Dependency = getResourceOrNull(graph, URIs.Dependency);\r
+        Expression = getResourceOrNull(graph, URIs.Expression);\r
         Flow = getResourceOrNull(graph, URIs.Flow);\r
         HasAngle = getResourceOrNull(graph, URIs.HasAngle);\r
+        HasExpression = getResourceOrNull(graph, URIs.HasExpression);\r
         HasHead = getResourceOrNull(graph, URIs.HasHead);\r
         HasTail = getResourceOrNull(graph, URIs.HasTail);\r
-        HasValue = getResourceOrNull(graph, URIs.HasValue);\r
+        HasText = getResourceOrNull(graph, URIs.HasText);\r
         HasX = getResourceOrNull(graph, URIs.HasX);\r
         HasY = getResourceOrNull(graph, URIs.HasY);\r
         IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable);\r
         IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf);\r
         IsTailOf = getResourceOrNull(graph, URIs.IsTailOf);\r
+        NormalExpression = getResourceOrNull(graph, URIs.NormalExpression);\r
         Stock = getResourceOrNull(graph, URIs.Stock);\r
         SysdynProject = getResourceOrNull(graph, URIs.SysdynProject);\r
         Valve = getResourceOrNull(graph, URIs.Valve);\r
index a63897e50e91c66b8691620711e8fa6a9cda3e23..23b1b8c11551ed37ecbe62252cda757e9ffa3f24 100644 (file)
@@ -5,41 +5,62 @@ import org.simantics.sysdyn.representation.Configuration;
 import org.simantics.sysdyn.representation.IElement;\r
 import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Valve;\r
+import org.simantics.sysdyn.representation.Variable;\r
 import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter;\r
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
 \r
 public class ModelicaWriter {\r
 \r
-    StringBuilder b;\r
+    StringBuilder b = new StringBuilder();\r
     \r
     IElementVisitorVoid componentWriter = new ElementVisitorVoidAdapter() {\r
         public void visit(Auxiliary auxiliary) {\r
             b.append("    Real ").append(escape(auxiliary.name)).append(";\n");\r
         }\r
-        public void visit(Stock stock) {\r
-            b.append("    Real ").append(escape(stock.name)).append(";\n");\r
-        }        \r
         public void visit(Valve valve) {\r
-            // TODO\r
+            b.append("    Real ").append(escape(valve.name)).append(";\n");\r
         }\r
+        public void visit(Stock stock) {\r
+            b.append("    Real ").append(escape(stock.name)).append(";\n");\r
+        }       \r
     };\r
     \r
     IElementVisitorVoid equationWriter = new ElementVisitorVoidAdapter() {\r
         public void visit(Auxiliary auxiliary) {\r
             b.append("    ").append(escape(auxiliary.name))\r
-                .append(" = ").append(auxiliary.value).append(";\n");\r
+                .append(" = ").append(auxiliary.expression.getText()).append(";\n");\r
         }\r
         public void visit(Stock stock) {\r
+            b.append("    der(")\r
+             .append(escape(stock.name))\r
+             .append(") =");\r
+            for(Variable flow : stock.incomingFlows)\r
+                b.append("\n        + ").append(escape(flow.name));\r
+            for(Variable flow : stock.outgoingFlows)\r
+                b.append("\n        - ").append(escape(flow.name));\r
+            b.append(";\n");\r
         }        \r
         public void visit(Valve valve) {\r
+            b.append("    ").append(escape(valve.name))\r
+                .append(" = ").append(valve.expression.getText()).append(";\n");\r
         }\r
     };\r
     \r
+    IElementVisitorVoid initialEquationWriter = new ElementVisitorVoidAdapter() {\r
+        public void visit(Stock stock) {\r
+            b.append("    ").append(escape(stock.name))\r
+             .append(" = ").append(stock.expression.getText()).append(";\n");\r
+        } \r
+    };\r
+    \r
     public void write(Configuration conf) {\r
         conf.update();\r
         b.append("model ").append(conf.name).append('\n');\r
         for(IElement element : conf.elements)\r
             element.accept(componentWriter);\r
+        b.append("initial equation\n");\r
+        for(IElement element : conf.elements)\r
+            element.accept(initialEquationWriter);\r
         b.append("equation\n");\r
         for(IElement element : conf.elements)\r
             element.accept(equationWriter);\r
@@ -47,7 +68,12 @@ public class ModelicaWriter {
     }\r
     \r
     public String escape(String name) {\r
-        return name;\r
+        return name.replace(' ', '_');\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return b.toString();\r
     }\r
     \r
 }\r
index 97cb4465fccd1b7cf522786c403442b3c4b9a8c9..f325b446f857c224fd23817ae6dd3470c83cf0bf 100644 (file)
@@ -5,9 +5,6 @@ import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;
 \r
 @GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
 public class Auxiliary extends Variable {\r
-    // TODO\r
-    public String value;\r
-\r
     @Override\r
     public void accept(IElementVisitorVoid v) {\r
         v.visit(this);        \r
index 86c640cd60786c9fb74dc69104846336ec252146..5cd87a8192e9df40966a8442c7bc7430654035ab 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.sysdyn.representation;
 import java.util.ArrayList;\r
 \r
 import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElements;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
 import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter;\r
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
@@ -13,7 +14,7 @@ public class Configuration {
     @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
     public String name = "Unnamed";\r
     \r
-    @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf")\r
+    @RelatedElements("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf")\r
     public ArrayList<IElement> elements = new ArrayList<IElement>();\r
     \r
     IElementVisitorVoid clearVisitor = new ElementVisitorVoidAdapter() {\r
index 631d2229729e669e06e10c6e0dd10a0b1a725e18..ff1eb648fa614b9a77db4ef4cba779000bb13cf9 100644 (file)
@@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType;
 import org.simantics.objmap.annotations.RelatedElement;\r
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
 \r
-@GraphType("http://www.simantics.org/Sysdyn#Cloud")\r
+@GraphType("http://www.simantics.org/Sysdyn#Dependency")\r
 public class Dependency implements IElement {\r
     @RelatedElement("http://www.simantics.org/Sysdyn#HasTail")\r
     public IElement tail;\r
index 6148dbfca2b66e072ab5ed2809726f8a69bb418d..c9a2aa6332ec4a48d2ee1f74fcf9b11bf93f737a 100644 (file)
@@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType;
 import org.simantics.objmap.annotations.RelatedElement;\r
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
 \r
-@GraphType("http://www.simantics.org/Sysdyn#Cloud")\r
+@GraphType("http://www.simantics.org/Sysdyn#Flow")\r
 public class Flow implements IElement {\r
     @RelatedElement("http://www.simantics.org/Sysdyn#HasTail")\r
     public IElement tail;\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java
new file mode 100644 (file)
index 0000000..d0e5ec4
--- /dev/null
@@ -0,0 +1,5 @@
+package org.simantics.sysdyn.representation;\r
+\r
+public interface IExpression {\r
+    String getText();\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java
new file mode 100644 (file)
index 0000000..d9d609f
--- /dev/null
@@ -0,0 +1,15 @@
+package org.simantics.sysdyn.representation;\r
+\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#NormalExpression")\r
+public class NormalExpression implements IExpression {\r
+    @RelatedValue("http://www.simantics.org/Sysdyn#HasText")\r
+    public String text;\r
+\r
+    @Override\r
+    public String getText() {\r
+        return text;\r
+    }\r
+}\r
index ba45a7c9e3a3731bd6b217f4bd47be5489e0a3c6..b36740102c79f28b3896d666aefd7d990640b803 100644 (file)
@@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType;
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
 \r
 @GraphType("http://www.simantics.org/Sysdyn#Valve")\r
-public class Valve implements IElement {\r
+public class Valve extends Variable {\r
     @Override\r
     public void accept(IElementVisitorVoid v) {\r
         v.visit(this);        \r
index 379edf5c910ce3ab490aaba64d5b0c27a03463c0..653c04ba3ddf53bccbd6e6cba8d1cd723f41ea1c 100644 (file)
@@ -1,8 +1,13 @@
 package org.simantics.sysdyn.representation;\r
 \r
+import org.simantics.objmap.annotations.RelatedElement;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
 \r
 public abstract class Variable implements IElement {\r
     @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
     public String name;\r
+    //@RelatedValue("http://www.simantics.org/Sysdyn#HasValue")\r
+    //public String value;\r
+    @RelatedElement("http://www.simantics.org/Sysdyn#HasExpression")\r
+    public IExpression expression;\r
 }\r