]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Use doubles from spreadsheets in variable equations: SheetName.Coordinate
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 11 Jul 2011 06:41:03 +0000 (06:41 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 11 Jul 2011 06:41:03 +0000 (06:41 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21456 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/Sysdyn_ModelBrowser_Viewpoint.pgraph
org.simantics.sysdyn/META-INF/MANIFEST.MF
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java

index b633d258ad179412f8208215847fb2d1527fdadd..e417d414a4bd042c9bd2862f422d6bdb255da6c8 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
index 18248d92a0a0b6678ef6efb3da3b59a6b9d51f0d..4925976cf575a68de2bf51f70aa69859b6c33c15 100644 (file)
@@ -36,6 +36,8 @@ MBC
         VP.ChildContribution.HasChildNodeType SBC.BuiltinFunctions\r
         VP.ChildContribution.HasRule _ : VP.ConstantChildRule\r
             VP.ConstantChildRule.HasChild BuiltinFunctions    \r
+    @VP.relationChildRule      SBC.BuiltinFunctions      L0.ConsistsOf      SYSDYN.SysdynModelicaFunctionLibrary    \r
+    @VP.relationChildRule      SBC.BuiltinFunctions      L0.ConsistsOf      SYSDYN.SysdynModelicaFunction            \r
     // Shared functions linked to a model    \r
     @VP.relationChildRuleWithFolder SBC.FunctionsFolder   L0.IsLinkedTo  SYSDYN.SharedFunctionOntology \r
         SBC.SharedFunctionsFolder : VP.ResourceNodeType\r
index f901e786b98eea95ef57dac5d2e0e5b51f16ce29..36314b58743bb2cc7afc01c7f27e18fc485dc5d8 100644 (file)
@@ -22,7 +22,9 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0",
  org.simantics.diagram;bundle-version="1.1.1",
  org.simantics.diagram.ontology;bundle-version="1.1.1",
  org.simantics.scl.runtime;bundle-version="0.1.3",
- org.simantics.db.layer0;bundle-version="1.1.0"
+ org.simantics.db.layer0;bundle-version="1.1.0",
+ org.simantics.spreadsheet.common;bundle-version="1.1.0",
+ org.simantics.spreadsheet;bundle-version="1.1.0"
 Export-Package: org.simantics.sysdyn,
  org.simantics.sysdyn.adapter,
  org.simantics.sysdyn.expressionParser,
index d88a02a50f5af3c53d57eb84f6e4bb462a92515b..484e1955e171b7388eaaaba81b7290606765a21a 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.modelica;
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 \r
+import org.simantics.sysdyn.representation.Book;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.Dependency;\r
 import org.simantics.sysdyn.representation.Enumeration;\r
@@ -54,6 +55,7 @@ public class ModelicaWriter {
         ArrayList<Dependency> inputDependencies = new ArrayList<Dependency>();\r
         ArrayList<Dependency> outputDependencies = new ArrayList<Dependency>();\r
         HashMap<String, ArrayList<Input>> moduleInputs = new HashMap<String, ArrayList<Input>>();\r
+        Book book = null;\r
 \r
         // Initialize lists\r
         for(IElement element : configuration.getElements()) {\r
@@ -80,6 +82,8 @@ public class ModelicaWriter {
                 } else if(dependency.getTail() instanceof Module){\r
                     inputDependencies.add(dependency);\r
                 }\r
+            } else if (element instanceof Book) {\r
+                book = (Book)element;\r
             }\r
         }\r
 \r
@@ -113,6 +117,11 @@ public class ModelicaWriter {
                 b.append(e.getDeclaration());\r
             }\r
         }\r
+        \r
+        if(book != null) {\r
+            b.append("// Spreadsheet definition\n");\r
+            b.append(book.getBook());\r
+        }\r
 \r
         boolean initialEquations = false;\r
         for(Stock stock : stocks) {\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java
new file mode 100644 (file)
index 0000000..17e1baa
--- /dev/null
@@ -0,0 +1,35 @@
+package org.simantics.sysdyn.representation;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.simantics.objmap.IMapping;\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElements;\r
+import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
+\r
+@GraphType("http://www.simantics.org/Spreadsheet-1.0/Book")\r
+public class Book implements IElement {\r
+    \r
+    @RelatedElements(\r
+            value = "http://www.simantics.org/Layer0-1.0/ConsistsOf",\r
+            composition = true)\r
+            private ArrayList<Sheet> sheets = new ArrayList<Sheet>();\r
+\r
+    @Override\r
+    public void accept(IElementVisitorVoid v) {\r
+        v.visit(this);                \r
+    }\r
+    \r
+    public List<Sheet> getSheets() {\r
+        return sheets;\r
+    }\r
+    \r
+    public String getBook() {\r
+        StringBuilder book = new StringBuilder();\r
+        for(Sheet sheet : sheets)\r
+            book.append(sheet.getCells());\r
+        return book.toString();\r
+    }\r
+\r
+}\r
index edc18884ac19cbca2373f27e7e1199ef5a992dda..912f2e9ba21f999544538e44822677a4f5c7e3f4 100644 (file)
@@ -20,6 +20,21 @@ import org.simantics.objmap.IMapping;
 import org.simantics.objmap.Mappings;\r
 \r
 public class LoadRepresentation {\r
+    \r
+    public static IMapping loadMappedMapping(Session session, final Resource configuration) throws DatabaseException {\r
+        return session.syncRequest(new Read<IMapping>() {\r
+\r
+            @Override\r
+            public IMapping perform(ReadGraph graph)\r
+                    throws DatabaseException {\r
+                SysdynSchema schema = new SysdynSchema(graph);\r
+                IMapping mapping = Mappings.createWithoutListening(schema);\r
+                mapping.map(graph, configuration);\r
+                return mapping;\r
+            }\r
+            \r
+        });\r
+    }\r
 \r
     public static Configuration loadConfiguration(Session session, final Resource configuration) throws DatabaseException {\r
         return session.syncRequest(new Read<Configuration>() {\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java
new file mode 100644 (file)
index 0000000..e33b0f5
--- /dev/null
@@ -0,0 +1,75 @@
+package org.simantics.sysdyn.representation;\r
+\r
+import java.util.HashMap;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.exception.MissingVariableException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.objmap.annotations.UpdateMethod;\r
+import org.simantics.spreadsheet.Range;\r
+import org.simantics.spreadsheet.SheetVariables;\r
+import org.simantics.spreadsheet.common.exception.CellParseException;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+\r
+@GraphType("http://www.simantics.org/Spreadsheet-1.0/Spreadsheet")\r
+public class Sheet {\r
+    \r
+    @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName")\r
+    protected String name;\r
+    \r
+    HashMap<String, Object> cells = new HashMap<String, Object>();\r
+\r
+    public String getSheet() {\r
+        return "Sheet";\r
+    }\r
+    \r
+    @UpdateMethod\r
+    public boolean updateCells(ReadGraph g, Resource r) throws DatabaseException {\r
+        System.out.println("updating cells");\r
+        Variable v = g.adapt(r, Variable.class);\r
+        cells.clear();\r
+        for(Variable child : v.browseChildren(g)) {\r
+            String name = child.getPropertyValue(g, Variables.NAME);\r
+            try {\r
+                Range range = SpreadsheetUtils.decodeCellAbsolute(name);\r
+                Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT);\r
+                cells.put(name, value.getValue());\r
+            } catch (CellParseException e) {\r
+            } catch (MissingVariableException e) {\r
+                System.out.println("missing content for: " + name);\r
+            }\r
+        }\r
+        return true;\r
+    }\r
+    \r
+    public String getCells() {\r
+        StringBuilder clazz = new StringBuilder();\r
+        \r
+        clazz.append("    class " + name + "_class\n        ");\r
+        \r
+        int counter = 0;\r
+        for(String key : cells.keySet()) {\r
+            Object value = cells.get(key);\r
+            if(value instanceof Double) {\r
+                Double d = (Double)value;\r
+                clazz.append("constant Real " + key + " = " + d + "; ");\r
+                counter++;\r
+                if(counter > 10) {\r
+                    counter = 0;\r
+                    clazz.append("\n        ");\r
+                }\r
+            }\r
+        }\r
+        clazz.append("\n    end " + name + "_class;\n");\r
+        clazz.append("    " + name + "_class " + name + ";\n");\r
+        return clazz.toString();\r
+    }\r
+    \r
+}\r
index 3614a667ca44ea76da723ce3c7873c988b0a2eb3..5b95bc9137f001622ea8f29eeb2c1975fe9a55c8 100644 (file)
@@ -51,6 +51,8 @@ public class SysdynSchema extends SimpleSchema {
             addLinkType(MappingSchemas.fromAnnotations(g, LookupExpression.class));\r
             addLinkType(MappingSchemas.fromAnnotations(g, Redeclaration.class));\r
             addLinkType(MappingSchemas.fromAnnotations(g, LibraryDummy.class));\r
+            addLinkType(MappingSchemas.fromAnnotations(g, Book.class));\r
+            addLinkType(MappingSchemas.fromAnnotations(g, Sheet.class));\r
         } catch (DatabaseException e) {\r
             e.printStackTrace();\r
         } catch (InstantiationException e) {\r
index a1693271c7448c8b19790fc9c6129afaa8b46ea9..e2bfc7a0405d47cb6f04cab47f102ec6557c9e79 100644 (file)
@@ -41,6 +41,8 @@ public class IndexUtils {
        \r
        \r
        public static String rangeToIndexes(Variable variable, String range) {\r
+           if(variable == null)\r
+               return range;\r
                StringBuilder sb = new StringBuilder();\r
                ArrayIndexes arrayIndexes = variable.getArrayIndexes();\r
                if(arrayIndexes == null || range == null)\r
index af988b5dc10a51f6059ec80dd1b5015a7981b432..4ee889c11ac0bdc3e194e34c22a73e5490b2cc43 100644 (file)
@@ -20,6 +20,7 @@ import org.simantics.sysdyn.representation.Flow;
 import org.simantics.sysdyn.representation.Input;\r
 import org.simantics.sysdyn.representation.LibraryDummy;\r
 import org.simantics.sysdyn.representation.Module;\r
+import org.simantics.sysdyn.representation.Book;\r
 import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Valve;\r
 \r
@@ -68,4 +69,8 @@ public class ElementVisitorVoidAdapter implements IElementVisitorVoid {
     @Override\r
     public void visit(LibraryDummy libraryDummy) {\r
     }\r
+    \r
+    @Override\r
+    public void visit(Book sheet) {\r
+    }\r
 }\r
index 9c7e55045d4b0a885d78871b982a616067159f29..89899f15987f74f4ea3f25fa57d1700d8f6b1c13 100644 (file)
@@ -20,6 +20,7 @@ import org.simantics.sysdyn.representation.Flow;
 import org.simantics.sysdyn.representation.Input;\r
 import org.simantics.sysdyn.representation.LibraryDummy;\r
 import org.simantics.sysdyn.representation.Module;\r
+import org.simantics.sysdyn.representation.Book;\r
 import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Valve;\r
 \r
@@ -36,4 +37,5 @@ public interface IElementVisitorVoid {
     void visit(Configuration configuration);\r
     void visit(Enumeration enumeration);\r
     void visit(LibraryDummy libraryDummy);\r
+    void visit(Book sheet);\r
 }\r