]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
New implementation of server state based on StandardNodeManager 32/1832/1
authorJussi Koskela <jussi.koskela@semantum.fi>
Tue, 12 Jun 2018 12:47:59 +0000 (15:47 +0300)
committerJussi Koskela <jussi.koskela@semantum.fi>
Tue, 12 Jun 2018 12:47:59 +0000 (15:47 +0300)
gitlab #1

Change-Id: I209be4310d5e5f60a2817970a3cd8fd5b9d91f0e

bundles/org.simantics.sysdyn/META-INF/MANIFEST.MF
bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java

index 2083aad03060a778818d65ac241e3c779023e39d..3e3149098ad75a4a24a4d371db3c18d04e53a4d8 100644 (file)
@@ -33,7 +33,8 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0",
  org.jfree.jcommon;bundle-version="1.0.16",
  org.simantics.spreadsheet.graph;bundle-version="1.1.0",
  fi.semantum.sysdyn.solver;bundle-version="0.1.0",
- org.simantics.fileimport
+ org.simantics.fileimport,
+ org.simantics.document.server
 Export-Package: org.simantics.sysdyn,
  org.simantics.sysdyn.adapter,
  org.simantics.sysdyn.elementaryCycles,
index 94c1d5f53aa2ccaac6132f2d0b55320563d97e8a..ebcd86998134fb5434b33ed997f96d0bd9432e7f 100644 (file)
-package org.simantics.sysdyn.manager;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.List;\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.NullProgressMonitor;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.simantics.Simantics;\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.WriteGraph;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;\r
-import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
-import org.simantics.db.layer0.util.Layer0Utils;\r
-import org.simantics.db.layer0.util.RemoverUtil;\r
-import org.simantics.db.layer0.variable.RVI;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-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
-import org.simantics.simulation.experiment.IDynamicExperiment;\r
-import org.simantics.simulation.experiment.IExperiment;\r
-import org.simantics.simulation.model.ExperimentLoadingFailed;\r
-import org.simantics.simulation.project.IExperimentActivationListener;\r
-import org.simantics.simulation.project.IExperimentManager;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.utils.DataContainer;\r
-\r
-public class SysdynExperiments {\r
-\r
-       public static void setPublishResults(ReadGraph graph, Variable variable, boolean value) throws DatabaseException {\r
-\r
-        final IProject project = Simantics.getProject();\r
-        if (project == null) return;\r
-\r
-        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
-        if(experimentManager == null) return;\r
-        \r
-        IExperiment exp = experimentManager.getExperiment(variable.getName(graph));\r
-        if(exp instanceof OldSysdynExperiment) {\r
-               OldSysdynExperiment experiment = (OldSysdynExperiment)exp;\r
-               experiment.setPublishResults(value);\r
-        }\r
-\r
-       }\r
-       \r
-    public static String activateExperiment(IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) {\r
-       \r
-       if(monitor == null) monitor = new NullProgressMonitor();\r
-       \r
-        final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);\r
-\r
-//        SysdynExperimentManagerListener.listenManager(manager);\r
-        IExperiment[] experiments = manager.getExperiments();\r
-        SubMonitor shutdownMon = mon.newChild(10000);\r
-        int workPerExperiment;\r
-        if (experiments.length > 0)\r
-               workPerExperiment = 10000 / experiments.length;\r
-        else\r
-               workPerExperiment = 10000;\r
-        for(IExperiment e : experiments)\r
-            if(e.getState() != ExperimentState.DISPOSED)\r
-                e.shutdown(shutdownMon.newChild(workPerExperiment));\r
-        mon.setWorkRemaining(90000);\r
-\r
-        final Semaphore activated = new Semaphore(0);\r
-        final DataContainer<Throwable> problem = new DataContainer<Throwable>();\r
-        final DataContainer<IExperiment> run = new DataContainer<IExperiment>();\r
-        manager.startExperiment(experimentResource, new IExperimentActivationListener() {\r
-\r
-            @Override\r
-            public void onExperimentActivated(final IExperiment experiment) {\r
-//                MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));\r
-               run.set(experiment);\r
-                activated.release();\r
-            }\r
-            @Override\r
-            public void onFailure(Throwable e) {\r
-                problem.set(e);\r
-                activated.release();\r
-            }\r
-            @Override\r
-            public void onMessage(IStatus message) {\r
-//                MessageService.getDefault().log(message);\r
-            }\r
-            @Override\r
-            public IProgressMonitor getProgressMonitor() {\r
-                return mon;\r
-            }\r
-        }, true);\r
-        try {\r
-            activated.acquire();\r
-            //System.err.println("foobar");\r
-            //System.err.println(manager.getExperiment(run.get().getIdentifier()));\r
-            Throwable t = problem.get();\r
-            if (t != null) {\r
-                if (t instanceof ExperimentLoadingFailed) {\r
-//                    ErrorLogger.defaultLogError(t);\r
-//                    ShowMessage.showError("Experiment Activation Failed", t.getMessage());\r
-                } else {\r
-//                    ExceptionUtils.logAndShowError(t);\r
-                }\r
-            }\r
-\r
-            return run.get().getIdentifier();\r
-            //return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Experiment activation failed, see exception for details.", problem.get());\r
-        } catch (InterruptedException e) {\r
-            return null;\r
-        }\r
-    }\r
-       \r
-    \r
-    public static String activateExperiment(Resource experiment) {\r
-\r
-//     Resource experiment = Layer0Utils.getPossibleChild(graph, model, name);\r
-//     if( experiment == null) return false;\r
-\r
-//     SimulationResource SIMU = SimulationResource.getInstance(graph);\r
-//        if (!graph.isInstanceOf(experiment, SIMU.Experiment)) return false;\r
-        \r
-        final IProject project = Simantics.getProject();\r
-        if (project == null) return null;\r
\r
-        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
-        if(experimentManager == null) return null;\r
-        \r
-       return SysdynExperiments.activateExperiment(null, project, experimentManager, experiment);\r
-               \r
-       }\r
-    \r
-    public static void run(String experimentId) {\r
-\r
-        final IProject project = Simantics.getProject();\r
-        if (project == null) return;\r
\r
-        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
-        if(experimentManager == null) return;\r
-        \r
-        IExperiment experiment = experimentManager.getExperiment(experimentId);\r
-        if(experiment instanceof IDynamicExperiment)\r
-               ((IDynamicExperiment)experiment).simulate(true);\r
-        \r
-    }\r
-    \r
-    public static List<Tuple2> experimentResult(String experimentId, String variableName) {\r
-\r
-               final IProject project = Simantics.getProject();\r
-               if (project == null) return null;\r
-\r
-               final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
-               if(experimentManager == null) return null;\r
-\r
-               IExperiment experiment = experimentManager.getExperiment(experimentId);\r
-               \r
-               if (!(experiment instanceof SysdynExperiment))\r
-                       return null;\r
-               \r
-               SysdynDataSet dataset = ((SysdynExperiment)experiment).getCurrentResult().getDataSet(variableName);\r
-               \r
-               if (dataset == null) {\r
-                       return null;\r
-               }\r
-               \r
-               List<Tuple2> results = new ArrayList<Tuple2>();\r
-               \r
-               for (int i = 0; i < dataset.times.length; i++) {\r
-                       results.add(new Tuple2(dataset.times[i], dataset.values[i]));\r
-               }\r
-               \r
-               return results;\r
-       }\r
-\r
-       private static void processChild(ReadGraph graph, Variable child, PartialIC IC) throws DatabaseException {\r
-               \r
-               StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-               \r
-               for(Variable c : child.getChildren(graph)) {\r
-                       Resource type = c.getPossibleType(graph);\r
-                       if(type == null) continue;\r
-                       if(graph.isInheritedFrom(type, STR.Component))\r
-                               processChild(graph, c, IC);\r
-               }\r
-               Variable values = child.getPossibleProperty(graph, "values");\r
-               if(values == null) return;\r
-                       \r
-               Resource represents = child.getPossibleRepresents(graph);\r
-               if(represents == null) return;\r
-                       \r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
-               \r
-               Collection<Resource> exprs = graph.syncRequest(new ObjectsWithType(represents, L0.ConsistsOf, SYSDYN.ParameterExpression));\r
-               if(exprs.size() == 0) {\r
-                       if(graph.hasStatement(represents, SYSDYN.Variable_isHeadOf)) return;\r
-               }\r
-                       \r
-               Variable v = values.getPossibleProperty(graph, "");\r
-               if(v == null) return;\r
-               double[][] vs = v.getValue(graph);\r
-               Variable value = child.getPossibleProperty(graph, "value");\r
-               if(value == null) return;\r
-               for(int i=0;i<vs.length;i++) {\r
-                       double[] ds = vs[i];\r
-                       if(ds == null) continue;\r
-                       Variable target = value.getPossibleProperty(graph, "$" + i);\r
-                       if(target != null && ds.length > 0) {\r
-                               RVI rvi = target.getRVI(graph);\r
-                               double initial = ds[0];\r
-//                             System.err.println("rvi=" + rvi + " value=" + initial);\r
-                               IC.add(rvi, Variant.ofInstance(initial));\r
-                       }\r
-               }\r
-       }\r
-       \r
-       public static Resource saveIC(WriteGraph graph, Variable input) throws DatabaseException {\r
-           Layer0 L0 = Layer0.getInstance(graph);\r
-           Resource model = Variables.getModel(graph, input);\r
-           String name = NameUtils.findFreshName(graph, "IC", model, L0.ConsistsOf);\r
-           return saveIC(graph, input, name);\r
-       }\r
-\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
-       SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
-       \r
-       if(child != null) {\r
-               if(graph.isInstanceOf(child, SYSDYN.InitialCondition)) {\r
-                       RemoverUtil.remove(graph, child);\r
-               } else {\r
-                       return null;\r
-               }\r
-       }\r
-       \r
-               PartialIC IC = new PartialIC();\r
-               processChild(graph, input, IC);\r
-               \r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               ModelingResources MOD = ModelingResources.getInstance(graph);\r
-               Resource ic = graph.newResource();\r
-               graph.claim(ic, L0.InstanceOf, SYSDYN.InitialCondition);\r
-               graph.addLiteral(ic, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
-               graph.addLiteral(ic, SYSDYN.InitialCondition_HasInitialValues, SYSDYN.InitialCondition_HasInitialValues_Inverse, MOD.PartialIC, IC, PartialIC.BINDING);\r
-               graph.claim(model, L0.ConsistsOf, ic);\r
-\r
-               Resource represents = input.getPossibleRepresents(graph);\r
-               if(represents != null) {\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
-               return ic;\r
-       \r
-    }\r
-\r
-    public static void assignIC(WriteGraph graph, Variable experiment, String name) throws DatabaseException {\r
-        graph.markUndoPoint();\r
-\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
-\r
-       final Resource run = experiment.getPossibleRepresents(graph);\r
-       if(run == null) return;\r
-       Resource exp = graph.getPossibleObject(run, L0.PartOf);\r
-       if(exp == null) return;\r
-\r
-       Resource model = Variables.getModel(graph, experiment);\r
-       Resource ic = Layer0Utils.getPossibleChild(graph, model, name);\r
-       if(ic == null) return;\r
-       \r
-       if(!graph.isInstanceOf(ic, SYSDYN.InitialCondition)) return;\r
-       \r
-               graph.deny(exp, SYSDYN.Experiment_ic);\r
-               graph.claim(exp, SYSDYN.Experiment_ic, ic);\r
-       Layer0Utils.addCommentMetadata(graph, "Assigned Initial Condition " + graph.getRelatedValue2(ic, L0.HasName, Bindings.STRING) + " to experiment " + graph.getRelatedValue2(exp, L0.HasName, Bindings.STRING));\r
-       \r
-       Resource oldState = Layer0Utils.getPossibleChild(graph, run, "__simupedia__");\r
-       if(oldState != null) {\r
-               graph.deny(run, L0.ConsistsOf, oldState);\r
-       }\r
-       \r
-       final Resource storedState = Layer0Utils.getPossibleChild(graph, ic, "__simupedia__");\r
-       if(storedState != null) {\r
-               VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
-               graph.syncRequest(new WriteRequest(vgs.getMemoryPersistent("experiments")) {\r
-\r
-                               @Override\r
-                               public void perform(WriteGraph graph) throws DatabaseException {\r
-                                   Layer0Utils.copyTo(graph, run, storedState, null, new DefaultCopyHandler(storedState), new DefaultPasteHandler(run));\r
-                               }\r
-                       \r
-               });\r
-       }\r
-       \r
-    }\r
-    \r
-    public static void deassignIC(WriteGraph graph, Variable experiment) throws DatabaseException {\r
-\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
-\r
-       Resource run = experiment.getPossibleRepresents(graph);\r
-       if(run == null) return;\r
-       Resource exp = graph.getPossibleObject(run, L0.PartOf);\r
-       if(exp == null) return;\r
-       \r
-               graph.deny(exp, SYSDYN.Experiment_ic);\r
-       \r
-    }\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
-\r
-       Resource run = experiment.getPossibleRepresents(graph);\r
-       if(run == null) return;\r
-       Resource exp = graph.getPossibleObject(run, L0.PartOf);\r
-       if(exp == null) return;\r
-\r
-       Resource model = Variables.getModel(graph, experiment);\r
-       Resource ic = Layer0Utils.getPossibleChild(graph, model, name);\r
-       if(ic == null) return;\r
-       \r
-       if(!graph.isInstanceOf(ic, SYSDYN.InitialCondition)) return;\r
-       \r
-               Variable base = Variables.getVariable(graph, run);\r
-               \r
-               SysdynExperiments.setPublishResults(graph, base, false);\r
-               \r
-               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
-    }\r
-    \r
-    public static void deleteIC(WriteGraph graph, Variable input, String name) throws DatabaseException {\r
-        graph.markUndoPoint();\r
-        Resource model = Variables.getModel(graph, input);\r
-        Resource child = Layer0Utils.getPossibleChild(graph, model, name);\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
-\r
-        if((child != null) && graph.isInstanceOf(child, SYSDYN.InitialCondition)) {\r
-            // is it really necessary to fetch the name from the graph and not just use the provided name?\r
-            String actualName = graph.getRelatedValue2(child, L0.HasName, Bindings.STRING);\r
-            RemoverUtil.remove(graph, child);\r
-            Layer0Utils.addCommentMetadata(graph, "Deleted Initial Condition " + actualName);\r
-        }\r
-    }\r
-    \r
-}\r
+package org.simantics.sysdyn.manager;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Semaphore;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.db.layer0.variable.RVI;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.document.server.state.StateNodeManager;
+import org.simantics.document.server.state.StateRealm;
+import org.simantics.document.server.state.StateSessionManager;
+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;
+import org.simantics.simulation.experiment.IDynamicExperiment;
+import org.simantics.simulation.experiment.IExperiment;
+import org.simantics.simulation.model.ExperimentLoadingFailed;
+import org.simantics.simulation.project.IExperimentActivationListener;
+import org.simantics.simulation.project.IExperimentManager;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.sysdyn.SysdynResource;
+import org.simantics.utils.DataContainer;
+
+public class SysdynExperiments {
+
+       public static void setPublishResults(ReadGraph graph, Variable variable, boolean value) throws DatabaseException {
+
+        final IProject project = Simantics.getProject();
+        if (project == null) return;
+
+        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+        if(experimentManager == null) return;
+        
+        IExperiment exp = experimentManager.getExperiment(variable.getName(graph));
+        if(exp instanceof OldSysdynExperiment) {
+               OldSysdynExperiment experiment = (OldSysdynExperiment)exp;
+               experiment.setPublishResults(value);
+        }
+
+       }
+       
+    public static String activateExperiment(IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) {
+       
+       if(monitor == null) monitor = new NullProgressMonitor();
+       
+        final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);
+
+//        SysdynExperimentManagerListener.listenManager(manager);
+        IExperiment[] experiments = manager.getExperiments();
+        SubMonitor shutdownMon = mon.newChild(10000);
+        int workPerExperiment;
+        if (experiments.length > 0)
+               workPerExperiment = 10000 / experiments.length;
+        else
+               workPerExperiment = 10000;
+        for(IExperiment e : experiments)
+            if(e.getState() != ExperimentState.DISPOSED)
+                e.shutdown(shutdownMon.newChild(workPerExperiment));
+        mon.setWorkRemaining(90000);
+
+        final Semaphore activated = new Semaphore(0);
+        final DataContainer<Throwable> problem = new DataContainer<Throwable>();
+        final DataContainer<IExperiment> run = new DataContainer<IExperiment>();
+        manager.startExperiment(experimentResource, new IExperimentActivationListener() {
+
+            @Override
+            public void onExperimentActivated(final IExperiment experiment) {
+//                MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));
+               run.set(experiment);
+                activated.release();
+            }
+            @Override
+            public void onFailure(Throwable e) {
+                problem.set(e);
+                activated.release();
+            }
+            @Override
+            public void onMessage(IStatus message) {
+//                MessageService.getDefault().log(message);
+            }
+            @Override
+            public IProgressMonitor getProgressMonitor() {
+                return mon;
+            }
+        }, true);
+        try {
+            activated.acquire();
+            //System.err.println("foobar");
+            //System.err.println(manager.getExperiment(run.get().getIdentifier()));
+            Throwable t = problem.get();
+            if (t != null) {
+                if (t instanceof ExperimentLoadingFailed) {
+//                    ErrorLogger.defaultLogError(t);
+//                    ShowMessage.showError("Experiment Activation Failed", t.getMessage());
+                } else {
+//                    ExceptionUtils.logAndShowError(t);
+                }
+            }
+
+            return run.get().getIdentifier();
+            //return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Experiment activation failed, see exception for details.", problem.get());
+        } catch (InterruptedException e) {
+            return null;
+        }
+    }
+       
+    
+    public static String activateExperiment(Resource experiment) {
+
+//     Resource experiment = Layer0Utils.getPossibleChild(graph, model, name);
+//     if( experiment == null) return false;
+
+//     SimulationResource SIMU = SimulationResource.getInstance(graph);
+//        if (!graph.isInstanceOf(experiment, SIMU.Experiment)) return false;
+        
+        final IProject project = Simantics.getProject();
+        if (project == null) return null;
+        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+        if(experimentManager == null) return null;
+        
+       return SysdynExperiments.activateExperiment(null, project, experimentManager, experiment);
+               
+       }
+    
+    public static void run(String experimentId) {
+
+        final IProject project = Simantics.getProject();
+        if (project == null) return;
+        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+        if(experimentManager == null) return;
+        
+        IExperiment experiment = experimentManager.getExperiment(experimentId);
+        if(experiment instanceof IDynamicExperiment)
+               ((IDynamicExperiment)experiment).simulate(true);
+        
+    }
+    
+    public static List<Tuple2> experimentResult(String experimentId, String variableName) {
+
+               final IProject project = Simantics.getProject();
+               if (project == null) return null;
+
+               final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+               if(experimentManager == null) return null;
+
+               IExperiment experiment = experimentManager.getExperiment(experimentId);
+               
+               if (!(experiment instanceof SysdynExperiment))
+                       return null;
+               
+               SysdynDataSet dataset = ((SysdynExperiment)experiment).getCurrentResult().getDataSet(variableName);
+               
+               if (dataset == null) {
+                       return null;
+               }
+               
+               List<Tuple2> results = new ArrayList<Tuple2>();
+               
+               for (int i = 0; i < dataset.times.length; i++) {
+                       results.add(new Tuple2(dataset.times[i], dataset.values[i]));
+               }
+               
+               return results;
+       }
+
+       private static void processChild(ReadGraph graph, Variable child, PartialIC IC) throws DatabaseException {
+               
+               StructuralResource2 STR = StructuralResource2.getInstance(graph);
+               
+               for(Variable c : child.getChildren(graph)) {
+                       Resource type = c.getPossibleType(graph);
+                       if(type == null) continue;
+                       if(graph.isInheritedFrom(type, STR.Component))
+                               processChild(graph, c, IC);
+               }
+               Variable values = child.getPossibleProperty(graph, "values");
+               if(values == null) return;
+                       
+               Resource represents = child.getPossibleRepresents(graph);
+               if(represents == null) return;
+                       
+               Layer0 L0 = Layer0.getInstance(graph);
+               SysdynResource SYSDYN = SysdynResource.getInstance(graph);
+               
+               Collection<Resource> exprs = graph.syncRequest(new ObjectsWithType(represents, L0.ConsistsOf, SYSDYN.ParameterExpression));
+               if(exprs.size() == 0) {
+                       if(graph.hasStatement(represents, SYSDYN.Variable_isHeadOf)) return;
+               }
+                       
+               Variable v = values.getPossibleProperty(graph, "");
+               if(v == null) return;
+               double[][] vs = v.getValue(graph);
+               Variable value = child.getPossibleProperty(graph, "value");
+               if(value == null) return;
+               for(int i=0;i<vs.length;i++) {
+                       double[] ds = vs[i];
+                       if(ds == null) continue;
+                       Variable target = value.getPossibleProperty(graph, "$" + i);
+                       if(target != null && ds.length > 0) {
+                               RVI rvi = target.getRVI(graph);
+                               double initial = ds[0];
+//                             System.err.println("rvi=" + rvi + " value=" + initial);
+                               IC.add(rvi, Variant.ofInstance(initial));
+                       }
+               }
+       }
+       
+       public static Resource saveIC(WriteGraph graph, Variable input) throws DatabaseException {
+           Layer0 L0 = Layer0.getInstance(graph);
+           Resource model = Variables.getModel(graph, input);
+           String name = NameUtils.findFreshName(graph, "IC", model, L0.ConsistsOf);
+           return saveIC(graph, input, name);
+       }
+
+       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);
+       SysdynResource SYSDYN = SysdynResource.getInstance(graph);
+       
+       if(child != null) {
+               if(graph.isInstanceOf(child, SYSDYN.InitialCondition)) {
+                       RemoverUtil.remove(graph, child);
+               } else {
+                       return null;
+               }
+       }
+       
+               PartialIC IC = new PartialIC();
+               processChild(graph, input, IC);
+               
+               Layer0 L0 = Layer0.getInstance(graph);
+               ModelingResources MOD = ModelingResources.getInstance(graph);
+               Resource ic = graph.newResource();
+               graph.claim(ic, L0.InstanceOf, SYSDYN.InitialCondition);
+               graph.addLiteral(ic, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
+               graph.addLiteral(ic, SYSDYN.InitialCondition_HasInitialValues, SYSDYN.InitialCondition_HasInitialValues_Inverse, MOD.PartialIC, IC, PartialIC.BINDING);
+               graph.claim(model, L0.ConsistsOf, ic);
+
+               Resource represents = input.getPossibleRepresents(graph);
+               if(represents != null) {
+                       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) {
+                       
+                       StateRealm realm = (StateRealm) StateSessionManager.getInstance().getOrCreateRealm(graph, session.getURI(graph)+"/__icstate__");
+                       byte[] values = ((StateNodeManager)realm.getNodeManager()).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);
+               return ic;
+       
+    }
+
+    public static void assignIC(WriteGraph graph, Variable experiment, String name) throws DatabaseException {
+        graph.markUndoPoint();
+
+               Layer0 L0 = Layer0.getInstance(graph);
+               SysdynResource SYSDYN = SysdynResource.getInstance(graph);
+
+       final Resource run = experiment.getPossibleRepresents(graph);
+       if(run == null) return;
+       Resource exp = graph.getPossibleObject(run, L0.PartOf);
+       if(exp == null) return;
+
+       Resource model = Variables.getModel(graph, experiment);
+       Resource ic = Layer0Utils.getPossibleChild(graph, model, name);
+       if(ic == null) return;
+       
+       if(!graph.isInstanceOf(ic, SYSDYN.InitialCondition)) return;
+       
+               graph.deny(exp, SYSDYN.Experiment_ic);
+               graph.claim(exp, SYSDYN.Experiment_ic, ic);
+       Layer0Utils.addCommentMetadata(graph, "Assigned Initial Condition " + graph.getRelatedValue2(ic, L0.HasName, Bindings.STRING) + " to experiment " + graph.getRelatedValue2(exp, L0.HasName, Bindings.STRING));
+       
+       Resource oldState = Layer0Utils.getPossibleChild(graph, run, "__simupedia__");
+       if(oldState != null) {
+               graph.deny(run, L0.ConsistsOf, oldState);
+       }
+       
+       final Resource storedState = Layer0Utils.getPossibleChild(graph, ic, "__simupedia__");
+       if(storedState != null) {
+               VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
+               graph.syncRequest(new WriteRequest(vgs.getMemoryPersistent("experiments")) {
+
+                               @Override
+                               public void perform(WriteGraph graph) throws DatabaseException {
+                                   Layer0Utils.copyTo(graph, run, storedState, null, new DefaultCopyHandler(storedState), new DefaultPasteHandler(run));
+                               }
+                       
+               });
+       }
+       
+    }
+    
+    public static void deassignIC(WriteGraph graph, Variable experiment) throws DatabaseException {
+
+               Layer0 L0 = Layer0.getInstance(graph);
+               SysdynResource SYSDYN = SysdynResource.getInstance(graph);
+
+       Resource run = experiment.getPossibleRepresents(graph);
+       if(run == null) return;
+       Resource exp = graph.getPossibleObject(run, L0.PartOf);
+       if(exp == null) return;
+       
+               graph.deny(exp, SYSDYN.Experiment_ic);
+       
+    }
+
+    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);
+
+       Resource run = experiment.getPossibleRepresents(graph);
+       if(run == null) return;
+       Resource exp = graph.getPossibleObject(run, L0.PartOf);
+       if(exp == null) return;
+
+       Resource model = Variables.getModel(graph, experiment);
+       Resource ic = Layer0Utils.getPossibleChild(graph, model, name);
+       if(ic == null) return;
+       
+       if(!graph.isInstanceOf(ic, SYSDYN.InitialCondition)) return;
+       
+               Variable base = Variables.getVariable(graph, run);
+               
+               SysdynExperiments.setPublishResults(graph, base, false);
+               
+               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);
+                                       StateRealm realm = (StateRealm) StateSessionManager.getInstance().getOrCreateRealm(graph, session.getURI(graph)+"/__icstate__");
+                                       ((StateNodeManager)realm.getNodeManager()).deserialize(blob);
+                               }
+                       }
+               }
+               
+               SysdynExperiments.setPublishResults(graph, base, true);
+
+       
+    }
+    
+    public static void deleteIC(WriteGraph graph, Variable input, String name) throws DatabaseException {
+        graph.markUndoPoint();
+        Resource model = Variables.getModel(graph, input);
+        Resource child = Layer0Utils.getPossibleChild(graph, model, name);
+        Layer0 L0 = Layer0.getInstance(graph);
+        SysdynResource SYSDYN = SysdynResource.getInstance(graph);
+
+        if((child != null) && graph.isInstanceOf(child, SYSDYN.InitialCondition)) {
+            // is it really necessary to fetch the name from the graph and not just use the provided name?
+            String actualName = graph.getRelatedValue2(child, L0.HasName, Bindings.STRING);
+            RemoverUtil.remove(graph, child);
+            Layer0Utils.addCommentMetadata(graph, "Deleted Initial Condition " + actualName);
+        }
+    }
+    
+}