]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Support some basic Vensim functions
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 Apr 2011 09:47:03 +0000 (09:47 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 Apr 2011 09:47:03 +0000 (09:47 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20586 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/VensimFunctions.pgraph [new file with mode: 0644]
org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/View.java

index 8f0c57d871e5ab8e8b6a338b2f2fa075a137ea7b..2456f494df1875f56f76afb46b2501ea1e8a9cbc 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
diff --git a/org.simantics.sysdyn.ontology/graph/VensimFunctions.pgraph b/org.simantics.sysdyn.ontology/graph/VensimFunctions.pgraph
new file mode 100644 (file)
index 0000000..18c1955
--- /dev/null
@@ -0,0 +1,199 @@
+\r
+L0 = <http://www.simantics.org/Layer0-1.0>\r
+SYSDYN = <http://www.simantics.org/Sysdyn-1.0>\r
+\r
+VF = <http://www.simantics.org/Sysdyn-1.0/Built-in Functions/Vensim Functions> : SYSDYN.SysdynModelicaFunctionLibrary\r
+\r
+/*\r
+VF.DELAYFIXED : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """DELAY FIXED( inputVar , dtime , init )\r
+    -> delay(inputVar, dtime)\r
+    init -value is ignored."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real inputVar;\r
+    input Real dtime;\r
+    input Real init;\r
+    input Real t = time;\r
+    output Real z;\r
+protected\r
+    parameter Real parameterDTime = dtime; \r
+algorithm\r
+    z := if time < parameterDTime then init else delay(inputVar, parameterDTime);"""\r
+*/\r
+    \r
+VF.IFTHENELSE : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """IF THEN ELSE( cond , ontrue , onfalse )\r
+    -> if cond then ontrue else onfalse"""\r
+    SYSDYN.HasModelicaFunctionCode """    input Boolean cond;\r
+    input Real ontrue;\r
+    input Real onfalse;\r
+    output Real z;\r
+algorithm\r
+    z := if cond then ontrue else onfalse;"""    \r
+    \r
+VF.MAX : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """MAX(a, b) \r
+    Returns the larger of a and b."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real a;\r
+    input Real b;\r
+    output Real z;\r
+algorithm\r
+    z := if a > b then a else b;"""       \r
+    \r
+VF.MIN : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """MIN(a, b) \r
+    Returns the smaller of a and b."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real a;\r
+    input Real b;\r
+    output Real z;\r
+algorithm\r
+    z := if a < b then a else b;"""        \r
+    \r
+VF.XIDZ : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """XIDZ(a, b, x)\r
+    x if divided by zero, a/b otherwise."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real a;\r
+    input Real b;\r
+    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := xidz(a, b, x);"""     \r
+    \r
+VF.ZIDZ : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """XIDZ(a, b)\r
+    Zero if divided by zero, a/b otherwise."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real a;\r
+    input Real b;\r
+    output Real z;\r
+algorithm\r
+    z := zidz(a, b);"""       \r
+    \r
+VF.ABS : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """ABS(x)\r
+    Returns the absolute value of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := abs(x);"""      \r
+    \r
+VF.SQRT : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """SQRT(x)\r
+    Returns the square root of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := sqrt(x);"""   \r
+        \r
+VF.MODULO : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """MODULO(a, b)\r
+    Returns the remainder when a is divided by b. """\r
+    SYSDYN.HasModelicaFunctionCode """    input Real a;\r
+    input Real b;\r
+    output Real z;\r
+algorithm\r
+    z := mod(a, b);"""       \r
+    \r
+VF.PULSE : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """PULSE(start, width)\r
+    Returns 1.0 starting at time start and lasting for interval width. 0.0 is returned at other times."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real start;\r
+    input Real width;\r
+    input Real t = time;\r
+    output Real z;    \r
+algorithm\r
+    z := if t >= start and (t - start) <= width then 1.0 else 0.0;"""    \r
+    \r
+VF.RAMP : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """RAMP(slope, startTime, endTime) \r
+    Returns 0 until the start time and then slopes upward until end time and then holds constant."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real slope;\r
+    input Real startTime;\r
+    input Real endTime;\r
+    input Real t = time;\r
+    output Real z;    \r
+algorithm\r
+    z := \r
+    if t > startTime then\r
+        if t < endTime then \r
+            slope * (t - startTime) \r
+        else\r
+            slope * (endTime - startTime)\r
+    else\r
+        0;"""        \r
+    \r
+VF.STEP : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """STEP(height, stepTime)\r
+    Returns 0.0 until the step time and then returns height."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real height;\r
+    input Real stepTime;\r
+    input Real t = time;\r
+    output Real z;\r
+algorithm\r
+    z := if t >= stepTime then height else 0.0;""" \r
+    \r
+/* Continuous built-in common mathematical functions */\r
+\r
+VF.EXP : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """\r
+    """\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := exp(x);"""\r
+    \r
+VF.SIN : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """SIN(x)\r
+    Returns the sine of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := sin(x);"""  \r
+            \r
+VF.SINH : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """SINH(x)\r
+    Returns the hyperbolic sine of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := sinh(x);"""  \r
+\r
+VF.COS : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """COS(x)\r
+    Returns the cosine of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := cos(x);"""  \r
+    \r
+VF.COSH : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """COSH(x)\r
+    Returns the hyperbolic cosine of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := cosh(x);"""  \r
+    \r
+VF.TAN : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """TAN(x)\r
+    Returns the tangent of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := tan(x);"""  \r
+    \r
+VF.TANH : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """TANH(x)\r
+    Returns the hyperbolic tangent of x."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := tanh(x);"""  \r
+    \r
+VF.LN : SYSDYN.SysdynModelicaFunction\r
+    L0.HasDescription """LN(x)\r
+    Returns the natural logarithm of x. \r
+    In modelica log(x) is the natural logarighm and log10(x) is base 10 logarighm."""\r
+    SYSDYN.HasModelicaFunctionCode """    input Real x;\r
+    output Real z;\r
+algorithm\r
+    z := log(x);"""  \r
+         
\ No newline at end of file
index 5a2dfc432e5049b7e46f4fda8bae98c7a43feffa..5c3e87a790ee6921c00564aca006851599e1025f 100644 (file)
@@ -14,6 +14,26 @@ public class SysdynResource {
     public final Resource AuxiliarySymbol;\r
     public final Resource BasicExperiment;\r
     public final Resource Built$in_Functions;\r
+    public final Resource Built$in_Functions_Vensim_Functions;\r
+    public final Resource Built$in_Functions_Vensim_Functions_ABS;\r
+    public final Resource Built$in_Functions_Vensim_Functions_COS;\r
+    public final Resource Built$in_Functions_Vensim_Functions_COSH;\r
+    public final Resource Built$in_Functions_Vensim_Functions_EXP;\r
+    public final Resource Built$in_Functions_Vensim_Functions_IFTHENELSE;\r
+    public final Resource Built$in_Functions_Vensim_Functions_LN;\r
+    public final Resource Built$in_Functions_Vensim_Functions_MAX;\r
+    public final Resource Built$in_Functions_Vensim_Functions_MIN;\r
+    public final Resource Built$in_Functions_Vensim_Functions_MODULO;\r
+    public final Resource Built$in_Functions_Vensim_Functions_PULSE;\r
+    public final Resource Built$in_Functions_Vensim_Functions_RAMP;\r
+    public final Resource Built$in_Functions_Vensim_Functions_SIN;\r
+    public final Resource Built$in_Functions_Vensim_Functions_SINH;\r
+    public final Resource Built$in_Functions_Vensim_Functions_SQRT;\r
+    public final Resource Built$in_Functions_Vensim_Functions_STEP;\r
+    public final Resource Built$in_Functions_Vensim_Functions_TAN;\r
+    public final Resource Built$in_Functions_Vensim_Functions_TANH;\r
+    public final Resource Built$in_Functions_Vensim_Functions_XIDZ;\r
+    public final Resource Built$in_Functions_Vensim_Functions_ZIDZ;\r
     public final Resource Built$in_Functions_interpolate;\r
     public final Resource Built$in_Functions_interpolateFull;\r
     public final Resource Built$in_Functions_xidz;\r
@@ -156,6 +176,26 @@ public class SysdynResource {
         public static final String AuxiliarySymbol = "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol";\r
         public static final String BasicExperiment = "http://www.simantics.org/Sysdyn-1.0/BasicExperiment";\r
         public static final String Built$in_Functions = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions";\r
+        public static final String Built$in_Functions_Vensim_Functions = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions";\r
+        public static final String Built$in_Functions_Vensim_Functions_ABS = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/ABS";\r
+        public static final String Built$in_Functions_Vensim_Functions_COS = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/COS";\r
+        public static final String Built$in_Functions_Vensim_Functions_COSH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/COSH";\r
+        public static final String Built$in_Functions_Vensim_Functions_EXP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/EXP";\r
+        public static final String Built$in_Functions_Vensim_Functions_IFTHENELSE = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/IFTHENELSE";\r
+        public static final String Built$in_Functions_Vensim_Functions_LN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/LN";\r
+        public static final String Built$in_Functions_Vensim_Functions_MAX = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MAX";\r
+        public static final String Built$in_Functions_Vensim_Functions_MIN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MIN";\r
+        public static final String Built$in_Functions_Vensim_Functions_MODULO = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MODULO";\r
+        public static final String Built$in_Functions_Vensim_Functions_PULSE = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/PULSE";\r
+        public static final String Built$in_Functions_Vensim_Functions_RAMP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/RAMP";\r
+        public static final String Built$in_Functions_Vensim_Functions_SIN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SIN";\r
+        public static final String Built$in_Functions_Vensim_Functions_SINH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SINH";\r
+        public static final String Built$in_Functions_Vensim_Functions_SQRT = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SQRT";\r
+        public static final String Built$in_Functions_Vensim_Functions_STEP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/STEP";\r
+        public static final String Built$in_Functions_Vensim_Functions_TAN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/TAN";\r
+        public static final String Built$in_Functions_Vensim_Functions_TANH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/TANH";\r
+        public static final String Built$in_Functions_Vensim_Functions_XIDZ = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/XIDZ";\r
+        public static final String Built$in_Functions_Vensim_Functions_ZIDZ = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/ZIDZ";\r
         public static final String Built$in_Functions_interpolate = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolate";\r
         public static final String Built$in_Functions_interpolateFull = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolateFull";\r
         public static final String Built$in_Functions_xidz = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/xidz";\r
@@ -308,6 +348,26 @@ public class SysdynResource {
         AuxiliarySymbol = getResourceOrNull(graph, URIs.AuxiliarySymbol);\r
         BasicExperiment = getResourceOrNull(graph, URIs.BasicExperiment);\r
         Built$in_Functions = getResourceOrNull(graph, URIs.Built$in_Functions);\r
+        Built$in_Functions_Vensim_Functions = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions);\r
+        Built$in_Functions_Vensim_Functions_ABS = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_ABS);\r
+        Built$in_Functions_Vensim_Functions_COS = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_COS);\r
+        Built$in_Functions_Vensim_Functions_COSH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_COSH);\r
+        Built$in_Functions_Vensim_Functions_EXP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_EXP);\r
+        Built$in_Functions_Vensim_Functions_IFTHENELSE = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_IFTHENELSE);\r
+        Built$in_Functions_Vensim_Functions_LN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_LN);\r
+        Built$in_Functions_Vensim_Functions_MAX = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MAX);\r
+        Built$in_Functions_Vensim_Functions_MIN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MIN);\r
+        Built$in_Functions_Vensim_Functions_MODULO = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MODULO);\r
+        Built$in_Functions_Vensim_Functions_PULSE = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_PULSE);\r
+        Built$in_Functions_Vensim_Functions_RAMP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_RAMP);\r
+        Built$in_Functions_Vensim_Functions_SIN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SIN);\r
+        Built$in_Functions_Vensim_Functions_SINH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SINH);\r
+        Built$in_Functions_Vensim_Functions_SQRT = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SQRT);\r
+        Built$in_Functions_Vensim_Functions_STEP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_STEP);\r
+        Built$in_Functions_Vensim_Functions_TAN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_TAN);\r
+        Built$in_Functions_Vensim_Functions_TANH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_TANH);\r
+        Built$in_Functions_Vensim_Functions_XIDZ = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_XIDZ);\r
+        Built$in_Functions_Vensim_Functions_ZIDZ = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_ZIDZ);\r
         Built$in_Functions_interpolate = getResourceOrNull(graph, URIs.Built$in_Functions_interpolate);\r
         Built$in_Functions_interpolateFull = getResourceOrNull(graph, URIs.Built$in_Functions_interpolateFull);\r
         Built$in_Functions_xidz = getResourceOrNull(graph, URIs.Built$in_Functions_xidz);\r
index 6bebe335f8494be4b9abe52fa8577ca4e04a78a1..cf4b0ce8fcce8c7065a4f7ebdd8275a8424e8e94 100644 (file)
@@ -13,8 +13,6 @@ package org.simantics.sysdyn.ui.browser.contributions;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
 \r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
 import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor;\r
@@ -23,7 +21,6 @@ import org.simantics.db.Resource;
 import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.layer0.Layer0;\r
-import org.simantics.scl.runtime.function.Function;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode;\r
 import org.simantics.sysdyn.ui.browser.nodes.FunctionNode;\r
@@ -32,7 +29,6 @@ import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder;
 \r
 public class FunctionLibraries  extends ViewpointContributor<FunctionsFolder> {\r
 \r
-    @SuppressWarnings("unchecked")\r
        @Override\r
     public Collection<?> getContribution(ReadGraph graph, FunctionsFolder functionsFolder)\r
     throws DatabaseException {\r
@@ -47,30 +43,19 @@ public class FunctionLibraries  extends ViewpointContributor<FunctionsFolder> {
                result.add(new FunctionNode(function));\r
         }\r
         \r
-        // Find function libraries in model and sysdyn ontology\r
-        Function index = graph.adapt(L0.Dependencies, Function.class);\r
-        Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)index.apply(graph, functionsFolder.data, "Types:SysdynModelicaFunctionLibrary");\r
-        HashSet<Resource> modelLibraries = new HashSet<Resource>();\r
-        for(Map<String, Object> r : results) {\r
-               Resource library = (Resource)r.get("Resource");\r
-               if(library == null)\r
-                       continue;\r
-               Resource root = graph.getPossibleObject(library, l0.PartOf);\r
-               while(!graph.isInstanceOf(root, sr.SysdynModel) && !graph.isInstanceOf(root, l0.Ontology)) {\r
-                       library = root;\r
-                       root = graph.getPossibleObject(library, l0.PartOf);\r
+        // Find model function libraries\r
+        for(Resource modelLibrary : graph.syncRequest(new ObjectsWithType(functionsFolder.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) {\r
+               new FunctionLibraryNode<Resource>(modelLibrary);\r
+        }       \r
+        \r
+        Resource sysdyn = graph.getPossibleResource("http://www.simantics.org/Sysdyn-1.0");\r
+        if(sysdyn != null) {\r
+               for(Resource library : graph.syncRequest(new ObjectsWithType(sysdyn, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) {\r
+                        result.add(new FunctionLibraryNode<Resource>(library));\r
                }\r
-               \r
-               // If the root is not a shared library (it is the model or sysdyn ontology)\r
-               if(graph.isInstanceOf(root, sr.SysdynModel))\r
-                       modelLibraries.add(library);\r
         }\r
         \r
         \r
-        for(Resource modelLibrary : modelLibraries) {\r
-               result.add(new FunctionLibraryNode<Resource>(modelLibrary));\r
-        }\r
-        \r
         result.add(new SharedFunctionsFolder(functionsFolder.data));\r
         \r
         return result;\r
index fd21b27ab9fe5f95628586f1e79529a1e6a08fc8..72c9ba739868664628f01d8c71b883a36c96c0f0 100644 (file)
@@ -26,20 +26,15 @@ import org.simantics.db.Builtins;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
-import org.simantics.db.VirtualGraph;\r
 import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.procedure.single.SingleSetSyncListener;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.common.request.Queries;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.common.utils.OrderedSetUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.GenericRelationIndex;\r
 import org.simantics.db.layer0.adapter.RuntimeValuations;\r
 import org.simantics.db.layer0.adapter.TrendVariable;\r
 import org.simantics.db.layer0.service.ActivationManager;\r
-import org.simantics.db.layer0.util.Simantics;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.db.service.GraphChangeListenerSupport;\r
 import org.simantics.db.service.LifecycleSupport;\r
@@ -161,7 +156,6 @@ public class SysdynProject extends AbstractProjectFeature {
                        if(g.isInstanceOf(dep, PROJ.NamespaceRequirement)) {\r
                                for(Resource req : g.getObjects(dep, PROJ.RequiresNamespace)) {\r
                                        String uri = g.getPossibleValue(req, Bindings.STRING);\r
-                                       System.err.println("dep uri=" + uri);\r
                                        if(uri != null) {\r
                                                Resource target = g.getResource(uri);\r
                                                if(target != null) {\r
@@ -232,7 +226,6 @@ public class SysdynProject extends AbstractProjectFeature {
 \r
 \r
 \r
-            final Layer0 l0 = Layer0.getInstance(session);\r
             ActivationManager activationManager = session.getService(ActivationManager.class);\r
             if (activationManager != null) {\r
                 activationManager.activate(session, projectResource);\r
@@ -240,46 +233,6 @@ public class SysdynProject extends AbstractProjectFeature {
 \r
 \r
             VirtualGraphSupport support = session.getService(VirtualGraphSupport.class);\r
-//            final VirtualGraph graph = session.getService(VirtualGraph.class);\r
-\r
-//            SysdynResource SYSDYN = SysdynResource.getInstance(session);\r
-//            session.asyncRequest(new ObjectsWithType(projectResource, l0.ConsistsOf, SYSDYN.SysdynModel), new SingleSetSyncListener<Resource>() {\r
-//\r
-//                @Override\r
-//                public void add(ReadGraph g, final Resource model) throws DatabaseException {\r
-//                     \r
-//                     GenericRelationIndex index = g.adapt(l0.DependenciesRelation, GenericRelationIndex.class);\r
-//                     index.trackAndIndex(g.getSession(), model);\r
-//                     \r
-//                    // FIXME:\r
-//                    Simantics.async(new Runnable() {\r
-//\r
-//                        @Override\r
-//                        public void run() {\r
-//                            try {\r
-//                                // This creates experiment realizations\r
-//                                graph.register(new HistoryRealizationVirtualGraph(session, model));\r
-//                                // This creates the BaseRealization\r
-//                                graph.register(new DefaultRealizationVirtualGraph(session, model));\r
-//                            } catch (DatabaseException e) {\r
-//                                e.printStackTrace();\r
-//                            }\r
-//                        }\r
-//\r
-//                    });\r
-//                }\r
-//\r
-//                @Override\r
-//                public void exception(ReadGraph graph, Throwable t) {\r
-//                    t.printStackTrace();\r
-//                }\r
-//\r
-//                @Override\r
-//                public boolean isDisposed() {\r
-//                    return false;\r
-//                }\r
-//\r
-//            });\r
 \r
             support.getWorkspacePersistent("experiments");\r
 \r
index 585601dd43939f853399820c0815cc57ab742e3c..ee40f1e8c8ab7e49d548872ebbeeb3d0f4ded4c3 100644 (file)
@@ -37,7 +37,7 @@ public class FunctionUtils {
                                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                                        SimulationResource simu = SimulationResource.getInstance(graph);\r
                                        \r
-                                       Set<Resource> parents = getParents(graph, model);\r
+                                       Set<Resource> parents = getParents(graph, model, true);\r
                                        for(Resource parent : parents) {\r
                                                if(graph.isInstanceOf(parent, sr.SysdynModel)) {\r
                                                        Resource configuration = graph.getPossibleObject(parent, simu.HasConfiguration);\r
@@ -59,7 +59,7 @@ public class FunctionUtils {
                return paths;\r
        }\r
        \r
-       private static Set<Resource> getParents(ReadGraph graph, SysdynModel model) throws DatabaseException{\r
+       private static Set<Resource> getParents(ReadGraph graph, SysdynModel model, boolean onlyWithContent) throws DatabaseException{\r
                HashSet<Resource> parents = new HashSet<Resource>();\r
 \r
                Layer0 l0 = Layer0.getInstance(graph);\r
@@ -69,18 +69,53 @@ public class FunctionUtils {
                \r
                parents.add(modelResource);\r
                \r
-               Collection<Resource> sharedOntologies = graph.syncRequest(new ObjectsWithType(\r
-                               modelResource, l0.IsLinkedTo, sr.SharedFunctionOntology));\r
-               parents.addAll(sharedOntologies);\r
+               for(Resource r : graph.getObjects(modelResource, l0.IsLinkedTo)) {\r
+                       if(graph.isInstanceOf(r, sr.SharedFunctionOntology)) {\r
+                               parents.add(r);\r
+                       } else {\r
+                               Collection<Resource> libraries = graph.syncRequest(new ObjectsWithType(\r
+                                               r, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary));\r
+                               if(!libraries.isEmpty())\r
+                                       parents.add(r);\r
+                       }\r
+               }\r
+               \r
+               if(onlyWithContent) {\r
+                       HashSet<Resource> contentParents = new HashSet<Resource>();\r
+                       for(Resource parent : parents) {\r
+                               if(hasContent(graph, parent))\r
+                                       contentParents.add(parent);\r
+                       }\r
+                       parents = contentParents;\r
+               }\r
                \r
                return parents;\r
                \r
        }\r
        \r
+       private static boolean hasContent(ReadGraph graph, Resource library) {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               try {\r
+                       if(!graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunction)).isEmpty())\r
+                               return true;\r
+                       else {\r
+                               for(Resource l : graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) {\r
+                                       boolean hasContent = hasContent(graph, l);\r
+                                       if(hasContent)\r
+                                               return true;\r
+                               }\r
+                       }\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return false;\r
+       }\r
+       \r
        public static void updateFunctionFilesForModel(ReadGraph graph, SysdynModel model) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Set<Resource> parents = getParents(graph, model);\r
+               Set<Resource> parents = getParents(graph, model, false);\r
                \r
                for(Resource parent : parents) {\r
                        if(graph.isInstanceOf(parent, sr.SysdynModel)) {\r
index 66f44e266aa9f0fc565fb85e3a62df46f44459e1..2f68f086a63cfa6fcb4dc39632766019023b36a5 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.sysdyn.mdlImport.mdlElements.Element;
 import org.simantics.sysdyn.mdlImport.mdlElements.EquivalenceSubscript;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Expression;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Flow;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Function;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Stock;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Subscript;\r
@@ -299,7 +300,7 @@ public class MdlParser {
                                name =(nameAndData[0].replace("\"", ""));\r
                                variable = getVariable(model, name);\r
                                if(variable == null) {\r
-                                       variable = new Auxiliary();\r
+                                       variable = new Function();\r
                                        variable.setName(name);\r
                                        model.addElement(variable);\r
                                }\r
index 06bde8e5b859422602765ca6d0d96bc6aa78b831..d6c644690a5aaa9d0e67f6a86d34b622181391b2 100644 (file)
@@ -36,11 +36,13 @@ public abstract class Connection implements IWriteableMDLObject {
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
                \r
-               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
-               \r
-               if(diagram == null)\r
+               Resource diagram = graph.getPossibleObject(configuration, mr.CompositeToDiagram);\r
+               Resource startElement = graph.getPossibleObject(start.getResource(), mr.ComponentToElement);\r
+               Resource endElement = graph.getPossibleObject(end.getResource(), mr.ComponentToElement);\r
+               if(diagram == null || startElement == null || endElement == null)\r
                        return null;\r
                \r
+               \r
                if(connectionType == null)\r
                        connectionType = sr.Dependency;\r
                \r
@@ -57,12 +59,10 @@ public abstract class Connection implements IWriteableMDLObject {
                \r
                \r
                // Build diagram connectors and connection\r
-               Resource startElement = graph.getSingleObject(start.getResource(), mr.ComponentToElement);\r
                Resource tailConnector = GraphUtils.create2(graph, \r
                                dr.Connector,\r
                                sr.HasTailTerminal, startElement);\r
                \r
-               Resource endElement = graph.getSingleObject(end.getResource(), mr.ComponentToElement);\r
                Resource headConnector = GraphUtils.create2(graph,\r
                                dr.Connector,\r
                                sr.HasHeadTerminal, endElement,\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java
new file mode 100644 (file)
index 0000000..faadc02
--- /dev/null
@@ -0,0 +1,61 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.mdlImport.ImportUtils;\r
+\r
+public class Function extends Variable {\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent, double xOffset,\r
+                       double yOffset) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
+               try {\r
+                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                       if(!graph.isInstanceOf(parent, sr.SysdynModel))\r
+                               return;\r
+                       Layer0 l0 = Layer0.getInstance(graph); \r
+                       \r
+                       Resource function = GraphUtils.create2(graph, \r
+                                       sr.SysdynModelicaFunction,\r
+                                       l0.HasName, ImportUtils.escapeName(this.getName()));\r
+                       \r
+                       if(comments != null && comments.length() > 0)\r
+                               graph.claimLiteral(function, l0.HasDescription, comments);\r
+                       \r
+                       if(expressions != null && expressions.get(0) != null) {\r
+                               StringBuilder sb = new StringBuilder();\r
+                               sb.append("    input Real a;\n");\r
+                               sb.append("    output Real result;\n");\r
+                               sb.append("algorithm\n");\r
+                               sb.append("    result := interpolate(a, " + expressions.get(0).getExpression() + ");");\r
+                               graph.claimLiteral(function, sr.HasModelicaFunctionCode, sb.toString());\r
+                       }\r
+\r
+                       graph.claim(parent, l0.ConsistsOf, function);\r
+                       resource = function;\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               \r
+       }\r
+\r
+\r
+}\r
index 011cb30bf6c59c5502e4dd76eb2b8d1d9337a784..98e00c954e6732694b149fc6879e2b493f028c30 100644 (file)
@@ -36,6 +36,7 @@ public class Model implements IWriteableMDLObject {
        \r
        private HashMap<String, Element> elementMap = new HashMap<String, Element>();\r
        private ArrayList<Subscript> subscripts = new ArrayList<Subscript>();\r
+       private ArrayList<Function> functions = new ArrayList<Function>();\r
        private ArrayList<Connection> connections = new ArrayList<Connection>();\r
        private ArrayList<View> views = new ArrayList<View>();\r
        private ArrayList<Element> unlocatedElements = new ArrayList<Element>();\r
@@ -43,6 +44,8 @@ public class Model implements IWriteableMDLObject {
        public void addElement(Element element) {\r
                if(element instanceof Subscript)\r
                        addSubscript((Subscript)element);\r
+               else if(element instanceof Function)\r
+                       addFunction((Function)element);\r
                else\r
                        unlocatedElements.add(element);\r
                if(element.getName() != null)\r
@@ -53,6 +56,10 @@ public class Model implements IWriteableMDLObject {
                subscripts.add(subscript);\r
        }\r
        \r
+       public void addFunction(Function function) {\r
+               functions.add(function);\r
+       }\r
+       \r
        public void addElement(View view, Element element) {\r
                if(element instanceof Subscript)\r
                        addSubscript((Subscript)element);\r
@@ -218,6 +225,10 @@ public class Model implements IWriteableMDLObject {
 \r
                        // Diagram creation copied from SysdynProject. \r
                        // Could the same code be reused? \r
+                       \r
+                       Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.0");\r
+                       Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0");\r
+\r
                        Resource model = GraphUtils.create2(\r
                                        graph,\r
                                        sr.SysdynModel,\r
@@ -225,7 +236,9 @@ public class Model implements IWriteableMDLObject {
                                        l0.HasName, getName(),\r
                                        l0.HasLabel, getName(),\r
                                        sr.HasStartTime, startTime,\r
-                                       sr.HasStopTime, endTime                 \r
+                                       sr.HasStopTime, endTime,\r
+                                       l0.IsLinkedTo, sysdyn,\r
+                                       l0.IsLinkedTo, layer0\r
                        );\r
 \r
                        Resource diagram = OrderedSetUtils.create(graph, sr.ConfigurationDiagram);\r
@@ -241,6 +254,7 @@ public class Model implements IWriteableMDLObject {
 \r
                        graph.claim(conf, mr.CompositeToDiagram, diagram);\r
                        graph.claim(model, simu.HasConfiguration, conf);\r
+                       graph.claim(model, l0.HasBaseRealization, conf);\r
 \r
                        Resource mapping = graph.newResource();\r
                        graph.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
@@ -290,6 +304,10 @@ public class Model implements IWriteableMDLObject {
                        for(Connection c : connections) {\r
                                c.write(graph, conf);\r
                        }\r
+                       \r
+                       for(Function f : functions) {\r
+                               f.write(graph, model, 0, 0);\r
+                       }\r
 \r
 \r
                } catch (DatabaseException e) {\r
index abf26b219e61cdde12229b7a302cf0863a8eb474..88fe41e63d16a8665c168283711324c84d1def1e 100644 (file)
@@ -35,6 +35,10 @@ public class View implements IWriteableMDLObject {
        }\r
        \r
        public void addElement(Element e) {\r
+               if(e instanceof Subscript ||\r
+                               e instanceof Function)\r
+                       return;\r
+               \r
                if(e.getX()<minX)\r
                        minX = e.getX();\r
                if(e.getX()>maxX)\r