From de4d1d60f4ba3e6ff7d66811254c99311d408f54 Mon Sep 17 00:00:00 2001 From: villberg Date: Wed, 23 Sep 2015 12:50:49 +0000 Subject: [PATCH] (refs #6042) Support IC state in Simupedia API git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31707 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn/scl/Simantics/Sysdyn.scl | 3 + .../sysdyn/manager/SysdynExperiments.java | 67 ++++++++++++++----- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl b/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl index 3f28e4f9..f5028ee0 100644 --- a/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl +++ b/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl @@ -104,9 +104,12 @@ importJava "org.simantics.sysdyn.manager.SysdynExperiments" where experimentResult :: String -> String -> [(Double,Double)] saveIC :: Variable -> String -> Maybe Resource + // session, name + saveIC2 :: Variable -> Variable -> String -> Maybe Resource assignIC :: Variable -> String -> () deassignIC :: Variable -> () applyIC :: Variable -> String -> () + applyIC2 :: Variable -> Variable -> String -> () deleteIC :: Variable -> String -> () setPublishResults :: Variable -> Boolean -> () diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java index 7040f9ac..aa9744ee 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java @@ -30,6 +30,8 @@ import org.simantics.db.service.VirtualGraphSupport; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.PartialIC; +import org.simantics.modeling.scl.SCLRealm; +import org.simantics.modeling.scl.SCLSessionManager; import org.simantics.project.IProject; import org.simantics.scl.runtime.tuple.Tuple2; import org.simantics.simulation.experiment.ExperimentState; @@ -235,7 +237,12 @@ public class SysdynExperiments { return saveIC(graph, input, name); } - public static Resource saveIC(WriteGraph graph, Variable input, String name) throws DatabaseException { + public static Resource saveIC(WriteGraph graph, Variable input, String name) throws DatabaseException { + return saveIC2(graph, null, input, name); + } + + public static Resource saveIC2(WriteGraph graph, Variable session, Variable input, String name) throws DatabaseException { + graph.markUndoPoint(); Resource model = Variables.getModel(graph, input); Resource child = Layer0Utils.getPossibleChild(graph, model, name); @@ -262,23 +269,30 @@ public class SysdynExperiments { Resource represents = input.getPossibleRepresents(graph); if(represents != null) { - Resource state = Layer0Utils.getPossibleChild(graph, represents, "__simupedia__"); - Layer0Utils.copyTo(graph, ic, state, null, new DefaultCopyHandler(state) { - - @Override - protected boolean ignoreVirtualResources() { - return false; - } - - }, new DefaultPasteHandler(ic)); - -// String id = input.getName(graph); -// SCLRealm realm = SCLSessionManager.sclRealmById(id); -// if(realm != null) { -// byte[] blob = realm.serialize(); -// //graph.addLiteral(ic, SYSDYN.InitialCondition_HasInitialValues, SYSDYN.InitialCondition_HasInitialValues_Inverse, MOD.PartialIC, IC, PartialIC.BINDING); -// } - + Resource state = Layer0Utils.getPossibleChild(graph, represents, "__simupedia__"); + if(state != null) { + Layer0Utils.copyTo(graph, ic, state, null, new DefaultCopyHandler(state) { + + @Override + protected boolean ignoreVirtualResources() { + return false; + } + + }, new DefaultPasteHandler(ic)); + } + } + + if(session != null) { + + SCLRealm realm = SCLSessionManager.sclRealmById(session.getURI(graph) + "/__icstate__"); + byte[] values = realm.serialize(); + + Resource sclState = graph.newResource(); + graph.claim(sclState, L0.InstanceOf, MOD.SCLState); + graph.claimLiteral(sclState, MOD.SCLState_identifier, MOD.SCLState_identifier_Inverse, L0.String, "__icstate__", Bindings.STRING); + graph.claimLiteral(sclState, MOD.SCLState_blob, MOD.SCLState_blob_Inverse, L0.ByteArray, values, Bindings.BYTE_ARRAY); + graph.claim(ic, MOD.InitialCondition_HasSCLState, sclState); + } Layer0Utils.addCommentMetadata(graph, "Saved new Initial Condition " + name + " " + ic); @@ -342,6 +356,10 @@ public class SysdynExperiments { } public static void applyIC(WriteGraph graph, Variable experiment, String name) throws DatabaseException { + applyIC2(graph, null, experiment, name); + } + + public static void applyIC2(WriteGraph graph, Variable session, Variable experiment, String name) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); SysdynResource SYSDYN = SysdynResource.getInstance(graph); @@ -364,6 +382,19 @@ public class SysdynExperiments { PartialIC data = graph.getPossibleRelatedValue(ic, SYSDYN.InitialCondition_HasInitialValues, PartialIC.BINDING); data.apply(graph, base); + if(session != null) { + ModelingResources MOD = ModelingResources.getInstance(graph); + for(Resource sclState : graph.getObjects(ic, MOD.InitialCondition_HasSCLState)) { + String identifier = graph.getRelatedValue(sclState, MOD.SCLState_identifier); + if("__icstate__".equals(identifier)) { + byte[] blob = graph.getRelatedValue(sclState, MOD.SCLState_blob); + SCLRealm realm = SCLSessionManager.sclRealmById(session.getURI(graph) + "/__icstate__"); + realm.applyState(blob); + realm.refreshVariablesSync(); + } + } + } + SysdynExperiments.setPublishResults(graph, base, true); -- 2.47.1