From: Jussi Koskela Date: Tue, 12 Jun 2018 12:47:59 +0000 (+0300) Subject: New implementation of server state based on StandardNodeManager X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F32%2F1832%2F1;p=simantics%2Fsysdyn.git New implementation of server state based on StandardNodeManager gitlab #1 Change-Id: I209be4310d5e5f60a2817970a3cd8fd5b9d91f0e --- diff --git a/bundles/org.simantics.sysdyn/META-INF/MANIFEST.MF b/bundles/org.simantics.sysdyn/META-INF/MANIFEST.MF index 2083aad0..3e314909 100644 --- a/bundles/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java b/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java index 94c1d5f5..ebcd8699 100644 --- a/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java +++ b/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java @@ -1,418 +1,420 @@ -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.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 problem = new DataContainer(); - final DataContainer run = new DataContainer(); - 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 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 results = new ArrayList(); - - 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 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 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) { - - 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); - 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); - SCLRealm realm = SCLSessionManager.sclRealmById(session.getURI(graph) + "/__icstate__"); - realm.applyState(blob); - realm.refreshVariablesSync(); - } - } - } - - 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); - } - } - -} +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 problem = new DataContainer(); + final DataContainer run = new DataContainer(); + 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 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 results = new ArrayList(); + + 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 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 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); + } + } + +}