]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
(refs #6042) Support IC state in Simupedia API
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 23 Sep 2015 12:50:49 +0000 (12:50 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 23 Sep 2015 12:50:49 +0000 (12:50 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31707 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/scl/Simantics/Sysdyn.scl
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java

index 3f28e4f96cdc72bedca11336d56133b35ca51982..f5028ee0982b2a9c914735bcce27c7c4e5cda5b0 100644 (file)
@@ -104,9 +104,12 @@ importJava "org.simantics.sysdyn.manager.SysdynExperiments" where
     experimentResult :: String -> String -> <Proc> [(Double,Double)]\r
     \r
     saveIC :: Variable -> String -> <WriteGraph> Maybe Resource\r
+    // session, name \r
+    saveIC2 :: Variable -> Variable -> String -> <WriteGraph> Maybe Resource\r
     assignIC :: Variable -> String -> <WriteGraph> ()\r
     deassignIC :: Variable -> <WriteGraph> ()\r
     applyIC :: Variable -> String -> <WriteGraph> ()\r
+    applyIC2 :: Variable -> Variable -> String -> <WriteGraph> ()\r
     deleteIC :: Variable -> String -> <WriteGraph> ()\r
 \r
     setPublishResults :: Variable -> Boolean -> <ReadGraph> ()\r
index 7040f9ace1c227d4afa1a8e09adbc87c53c2f7c9..aa9744ee967d990f429f5add3e169ba63d3593cc 100644 (file)
@@ -30,6 +30,8 @@ import org.simantics.db.service.VirtualGraphSupport;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.modeling.PartialIC;\r
+import org.simantics.modeling.scl.SCLRealm;\r
+import org.simantics.modeling.scl.SCLSessionManager;\r
 import org.simantics.project.IProject;\r
 import org.simantics.scl.runtime.tuple.Tuple2;\r
 import org.simantics.simulation.experiment.ExperimentState;\r
@@ -235,7 +237,12 @@ public class SysdynExperiments {
            return saveIC(graph, input, name);\r
        }\r
 \r
-    public static Resource saveIC(WriteGraph graph, Variable input, String name) throws DatabaseException {\r
+       public static Resource saveIC(WriteGraph graph, Variable input, String name) throws DatabaseException {\r
+               return saveIC2(graph, null, input, name);\r
+       }\r
+       \r
+    public static Resource saveIC2(WriteGraph graph, Variable session, Variable input, String name) throws DatabaseException {\r
+\r
         graph.markUndoPoint();\r
                Resource model = Variables.getModel(graph, input);\r
        Resource child = Layer0Utils.getPossibleChild(graph, model, name);\r
@@ -262,23 +269,30 @@ public class SysdynExperiments {
 \r
                Resource represents = input.getPossibleRepresents(graph);\r
                if(represents != null) {\r
-                       Resource state = Layer0Utils.getPossibleChild(graph, represents, "__simupedia__");   \r
-                   Layer0Utils.copyTo(graph, ic, state, null, new DefaultCopyHandler(state) {\r
-                       \r
-                       @Override\r
-                       protected boolean ignoreVirtualResources() {\r
-                               return false;\r
-                       }\r
-                       \r
-                   }, new DefaultPasteHandler(ic));\r
-                   \r
-//                 String id = input.getName(graph);\r
-//                 SCLRealm realm = SCLSessionManager.sclRealmById(id);\r
-//                 if(realm != null) {\r
-//                     byte[] blob = realm.serialize();\r
-//                             //graph.addLiteral(ic, SYSDYN.InitialCondition_HasInitialValues, SYSDYN.InitialCondition_HasInitialValues_Inverse, MOD.PartialIC, IC, PartialIC.BINDING);\r
-//                 }\r
-                   \r
+                       Resource state = Layer0Utils.getPossibleChild(graph, represents, "__simupedia__");\r
+                       if(state != null) {\r
+                           Layer0Utils.copyTo(graph, ic, state, null, new DefaultCopyHandler(state) {\r
+                               \r
+                               @Override\r
+                               protected boolean ignoreVirtualResources() {\r
+                                       return false;\r
+                               }\r
+                               \r
+                           }, new DefaultPasteHandler(ic));\r
+                       }\r
+               }\r
+               \r
+               if(session != null) {\r
+                       \r
+                       SCLRealm realm = SCLSessionManager.sclRealmById(session.getURI(graph) + "/__icstate__");\r
+                       byte[] values = realm.serialize();\r
+                       \r
+                       Resource sclState = graph.newResource();\r
+                       graph.claim(sclState, L0.InstanceOf, MOD.SCLState);\r
+                       graph.claimLiteral(sclState, MOD.SCLState_identifier, MOD.SCLState_identifier_Inverse, L0.String, "__icstate__", Bindings.STRING);\r
+                       graph.claimLiteral(sclState, MOD.SCLState_blob, MOD.SCLState_blob_Inverse, L0.ByteArray, values, Bindings.BYTE_ARRAY);\r
+                       graph.claim(ic, MOD.InitialCondition_HasSCLState, sclState);\r
+                       \r
                }\r
                \r
                Layer0Utils.addCommentMetadata(graph, "Saved new Initial Condition " + name + " " + ic);\r
@@ -342,6 +356,10 @@ public class SysdynExperiments {
     }\r
 \r
     public static void applyIC(WriteGraph graph, Variable experiment, String name) throws DatabaseException {\r
+       applyIC2(graph, null, experiment, name);\r
+    }\r
+\r
+    public static void applyIC2(WriteGraph graph, Variable session, Variable experiment, String name) throws DatabaseException {\r
 \r
                Layer0 L0 = Layer0.getInstance(graph);\r
                SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
@@ -364,6 +382,19 @@ public class SysdynExperiments {
                PartialIC data = graph.getPossibleRelatedValue(ic, SYSDYN.InitialCondition_HasInitialValues, PartialIC.BINDING);\r
                data.apply(graph, base);\r
                \r
+               if(session != null) {\r
+                       ModelingResources MOD = ModelingResources.getInstance(graph);\r
+                       for(Resource sclState : graph.getObjects(ic, MOD.InitialCondition_HasSCLState)) {\r
+                               String identifier = graph.getRelatedValue(sclState, MOD.SCLState_identifier);\r
+                               if("__icstate__".equals(identifier)) {\r
+                                       byte[] blob = graph.getRelatedValue(sclState, MOD.SCLState_blob);\r
+                                       SCLRealm realm = SCLSessionManager.sclRealmById(session.getURI(graph) + "/__icstate__");\r
+                                       realm.applyState(blob);\r
+                                       realm.refreshVariablesSync();\r
+                               }\r
+                       }\r
+               }\r
+               \r
                SysdynExperiments.setPublishResults(graph, base, true);\r
 \r
        \r