From 90484178ca69b795ccdfab055c139da700a54c09 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Wed, 3 Sep 2014 06:54:00 +0000 Subject: [PATCH] refs #5261 Optimizations in sysdyn Variables, LastValueIndexing and SysdynGameExperimentBase git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30190 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/adapter/IndexVariable.java | 2 +- .../adapter/ValueIndexVariableBase.java | 25 ++++++++-- .../sysdyn/adapter/VariableRVIUtils.java | 46 +++++++++++-------- .../manager/SysdynGameExperimentBase.java | 22 ++++++++- .../sysdyn/utils/DocumentationUtils.java | 28 +++++++---- 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java index 606b9f33..0b03a5b4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java @@ -199,7 +199,7 @@ public abstract class IndexVariable extends AbstractPropertyVariable { this.subscriptionManager = getSubscriptionManager(); rvi = Variables.getPossibleRVI(graph, this); if(rvi != null) - rvis = VariableRVIUtils.getActiveRVIs(graph, parent.getParent(graph)); + rvis = graph.syncRequest(new VariableRVIUtils.GetActiveRVIs(parent.getParent(graph))); //VariableRVIUtils.getActiveRVIs(graph, parent.getParent(graph)); } private Pair,ArrayList> getVariableNameArrays() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java index 0b308f70..cbb948bc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java @@ -14,6 +14,7 @@ import org.simantics.simulation.experiment.ExperimentState; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.manager.SysdynGameExperimentBase; +import org.simantics.sysdyn.manager.SysdynGameExperimentBase.Subscription; import org.simantics.sysdyn.manager.SysdynResult; import org.simantics.sysdyn.solver.ISolver; @@ -22,31 +23,45 @@ abstract public class ValueIndexVariableBase extends IndexVariable { public ValueIndexVariableBase(ReadGraph graph, Variable parent, String indexes) throws DatabaseException { super(graph, parent, indexes); + ensureInformationAvailable(graph); + if(experiment instanceof SysdynGameExperimentBase) { + s = ((SysdynGameExperimentBase) experiment).subscribe(getVariableNamesWithIndexNumbers()); + } } public static double[] UNRESOLVED = new double[0]; + public Subscription s; + public double[] getValueFull() { + if(experiment == null) return UNRESOLVED; + if(experiment instanceof SysdynGameExperimentBase) { + double[] arr = new double[s.indexes.length]; + s.getValues(arr); + return arr; + } + + Collection results = experiment.getActiveResults(); ArrayList variableNames = getVariableNamesWithIndexNumbers(); double[] result = new double[variableNames.size()]; for(int i = 0; i < variableNames.size(); i++) { for(SysdynResult r : results) { - if(experiment instanceof SysdynGameExperimentBase) { - Double d = ((SysdynGameExperimentBase)experiment).getCurrentValue(variableNames.get(i)); - result[i] = d != null ? d : 0; - } else { +// if(experiment instanceof SysdynGameExperimentBase) { +// Double d = ((SysdynGameExperimentBase)experiment).getCurrentValue(variableNames.get(i)); +// result[i] = d != null ? d : 0; +// } else { SysdynDataSet ds = r.getDataSet(variableNames.get(i)); if(ds != null && ds.values != null && ds.values.length > 0) { result[i] = ds.values[ds.values.length-1]; } else { return UNRESOLVED; } - } +// } } } return result; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java index 0f24cc58..7b6a68bc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java @@ -22,6 +22,7 @@ import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.VariableRead; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; @@ -158,30 +159,39 @@ public class VariableRVIUtils { * @throws DatabaseException */ public static HashMap getActiveRVIs(ReadGraph graph, Variable variable) throws DatabaseException { - HashMap rvis = new LinkedHashMap(); - - SysdynResource sr = SysdynResource.getInstance(graph); + return graph.syncRequest(new GetActiveRVIs(variable)); + } + + public static class GetActiveRVIs extends VariableRead> { - String rvi = Variables.getRVI(graph, variable); + public GetActiveRVIs(Variable variable) { + super(variable); + } - Resource r = variable.getRepresents(graph); + @Override + public HashMap perform(ReadGraph graph) throws DatabaseException { + HashMap rvis = new LinkedHashMap(); - Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList); - if(arrayIndexes == null) { - // If variable is single-dimensional, use the same rvi - rvis.put(rvi, rvi); - } else { - // If variable is multidimensional, get all indexes that are active and add them to rvis-map - List arrayIndexList = ListUtils.toList(graph, arrayIndexes); - resolveActiveArrayIndexes(graph, variable, arrayIndexList); + SysdynResource sr = SysdynResource.getInstance(graph); + String rvi = Variables.getRVI(graph, variable); + Resource r = variable.getRepresents(graph); - if(arrayIndexList.size() > 0) - traverseIndexes(graph, rvi, rvis, arrayIndexList); - else + Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList); + if(arrayIndexes == null) { + // If variable is single-dimensional, use the same rvi rvis.put(rvi, rvi); - } + } else { + // If variable is multidimensional, get all indexes that are active and add them to rvis-map + List arrayIndexList = ListUtils.toList(graph, arrayIndexes); + resolveActiveArrayIndexes(graph, variable, arrayIndexList); - return rvis; + if(arrayIndexList.size() > 0) + traverseIndexes(graph, rvi, rvis, arrayIndexList); + else + rvis.put(rvi, rvi); + } + return rvis; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java index 421e75d2..c4de04a1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java @@ -14,6 +14,7 @@ package org.simantics.sysdyn.manager; import gnu.trove.list.array.TDoubleArrayList; import gnu.trove.map.hash.THashMap; +import java.util.Collection; import java.util.HashMap; import org.eclipse.core.runtime.IProgressMonitor; @@ -47,7 +48,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { THashMap results; - protected String[] subscription; + public String[] subscription; public static double DEFAULT_STEP_DURATION = 1.0; public static double DEFAULT_STEP_LENGTH = 0.1; @@ -82,6 +83,25 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { this.savePer = interval; } + public static class Subscription { + public SysdynGameExperimentBase exp; + public int[] indexes; + public void getValues(double[] data) { + for(int i=0;i names) { + Subscription result = new Subscription(); + int[] indexes = new int[names.size()]; + int index = 0; + for(String name : names) + indexes[index++] = subscriptionIndexes.get(name); + result.exp = this; + result.indexes = indexes; + return result; + } + public Double getCurrentValue(String name) { if(subscriptionIndexes != null && name != null) { Integer index = subscriptionIndexes.get(name); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java index 966f9808..b8b06837 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -16,6 +16,7 @@ import org.simantics.databoard.Bindings; import org.simantics.databoard.util.Base64; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.BinaryRead; import org.simantics.db.common.request.ObjectsWithSupertype; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; @@ -617,16 +618,27 @@ public class DocumentationUtils { } - public static org.simantics.db.layer0.variable.Variable lastValueIndexed(ReadGraph graph, org.simantics.db.layer0.variable.Variable run, String path) throws DatabaseException { - - if(run == null) return null; + public static class LastValueIndexed extends BinaryRead { - Pair nameAndIndices = splitToNameAndIndices(path); + public LastValueIndexed(Resource resource, String path) { + super(resource, path); + } - org.simantics.db.layer0.variable.Variable v = run.browsePossible(graph, "/" + nameAndIndices.first + "#value#" + nameAndIndices.second); - if(v == null) throw new MissingVariableException("No variable for SysDyn reference path: " + path); - - return v; + @Override + public org.simantics.db.layer0.variable.Variable perform(ReadGraph graph) throws DatabaseException { + + org.simantics.db.layer0.variable.Variable run = Variables.getVariable(graph, parameter); + Pair nameAndIndices = splitToNameAndIndices(parameter2); + org.simantics.db.layer0.variable.Variable v = run.browsePossible(graph, "/" + nameAndIndices.first + "#value#" + nameAndIndices.second); + if(v == null) throw new MissingVariableException("No variable for SysDyn reference path: " + parameter2); + return v; + } + } + + + public static org.simantics.db.layer0.variable.Variable lastValueIndexed(ReadGraph graph, org.simantics.db.layer0.variable.Variable run, String path) throws DatabaseException { + if(run == null) return null; + return graph.syncRequest(new LastValueIndexed(run.getRepresents(graph), path)); } public static org.simantics.db.layer0.variable.Variable equation(ReadGraph graph, org.simantics.db.layer0.variable.Variable input, String path) throws DatabaseException { -- 2.47.1