]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5261
authorjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 3 Sep 2014 06:54:00 +0000 (06:54 +0000)
committerjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 3 Sep 2014 06:54:00 +0000 (06:54 +0000)
Optimizations in sysdyn Variables, LastValueIndexing and SysdynGameExperimentBase

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30190 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java
org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java
org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java

index 606b9f332c84469adf3660c18d7b8c36c3d79f30..0b03a5b4ad08c4cf6f6362940c64175df8ecb902 100644 (file)
@@ -199,7 +199,7 @@ public abstract class IndexVariable<T> extends AbstractPropertyVariable {
         this.subscriptionManager = getSubscriptionManager();\r
                rvi =  Variables.getPossibleRVI(graph, this);\r
                if(rvi != null)\r
-                   rvis = VariableRVIUtils.getActiveRVIs(graph, parent.getParent(graph));      \r
+                   rvis = graph.syncRequest(new VariableRVIUtils.GetActiveRVIs(parent.getParent(graph))); //VariableRVIUtils.getActiveRVIs(graph, parent.getParent(graph));    \r
        }\r
        \r
        private Pair<ArrayList<String>,ArrayList<String>> getVariableNameArrays() {\r
index 0b308f70ef7564691982be8baa8b147cfb46651e..cbb948bca58ae4c2417d3eb64388bd5f85bd0a51 100644 (file)
@@ -14,6 +14,7 @@ import org.simantics.simulation.experiment.ExperimentState;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.manager.SysdynDataSet;\r
 import org.simantics.sysdyn.manager.SysdynGameExperimentBase;\r
+import org.simantics.sysdyn.manager.SysdynGameExperimentBase.Subscription;\r
 import org.simantics.sysdyn.manager.SysdynResult;\r
 import org.simantics.sysdyn.solver.ISolver;\r
 \r
@@ -22,31 +23,45 @@ abstract public class ValueIndexVariableBase<T> extends IndexVariable<T> {
        public ValueIndexVariableBase(ReadGraph graph, Variable parent, String indexes)\r
                        throws DatabaseException {\r
            super(graph, parent, indexes);\r
+           ensureInformationAvailable(graph);\r
+        if(experiment instanceof SysdynGameExperimentBase) {\r
+            s = ((SysdynGameExperimentBase) experiment).subscribe(getVariableNamesWithIndexNumbers());\r
+        }\r
        }\r
 \r
        public static double[] UNRESOLVED = new double[0];\r
        \r
+       public Subscription s;\r
+       \r
        public double[] getValueFull() {\r
+           \r
            if(experiment == null)\r
                return UNRESOLVED;\r
 \r
+        if(experiment instanceof SysdynGameExperimentBase) {\r
+            double[] arr = new double[s.indexes.length];\r
+            s.getValues(arr);\r
+            return arr;\r
+        }\r
+\r
+           \r
         Collection<SysdynResult> results = experiment.getActiveResults();\r
                ArrayList<String> variableNames = getVariableNamesWithIndexNumbers();\r
 \r
                double[] result = new double[variableNames.size()];\r
                for(int i = 0; i < variableNames.size(); i++) {\r
                    for(SysdynResult r : results) {            \r
-                       if(experiment instanceof SysdynGameExperimentBase) {\r
-                           Double d = ((SysdynGameExperimentBase)experiment).getCurrentValue(variableNames.get(i)); \r
-                           result[i] = d != null ? d : 0;\r
-                       } else {\r
+//                     if(experiment instanceof SysdynGameExperimentBase) {\r
+//                         Double d = ((SysdynGameExperimentBase)experiment).getCurrentValue(variableNames.get(i)); \r
+//                         result[i] = d != null ? d : 0;\r
+//                     } else {\r
                            SysdynDataSet ds = r.getDataSet(variableNames.get(i));\r
                            if(ds != null && ds.values != null && ds.values.length > 0) {\r
                                result[i] =  ds.values[ds.values.length-1];\r
                            } else {\r
                                return UNRESOLVED;\r
                            }\r
-                       }\r
+//                     }\r
                    }\r
                }\r
                return result;\r
index 0f24cc580059018bceec79914e3a727712a4f915..7b6a68bcd492a97c28f56770bd12aa1a71e8acb0 100644 (file)
@@ -22,6 +22,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.VariableRead;\r
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.db.layer0.variable.Variables;\r
 import org.simantics.layer0.Layer0;\r
@@ -158,30 +159,39 @@ public class VariableRVIUtils {
      * @throws DatabaseException\r
      */\r
     public static HashMap<String, String> getActiveRVIs(ReadGraph graph, Variable variable) throws DatabaseException {\r
-        HashMap<String, String> rvis = new LinkedHashMap<String, String>();\r
-\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        return graph.syncRequest(new GetActiveRVIs(variable));\r
+    }\r
+    \r
+    public static class GetActiveRVIs extends VariableRead<HashMap<String, String>> {\r
 \r
-        String rvi = Variables.getRVI(graph, variable);\r
+        public GetActiveRVIs(Variable variable) {\r
+            super(variable);\r
+        }\r
 \r
-        Resource r = variable.getRepresents(graph);\r
+        @Override\r
+        public HashMap<String, String> perform(ReadGraph graph) throws DatabaseException {\r
+            HashMap<String, String> rvis = new LinkedHashMap<String, String>();\r
 \r
-        Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList);\r
-        if(arrayIndexes == null) {\r
-            // If variable is single-dimensional, use the same rvi\r
-            rvis.put(rvi, rvi);\r
-        } else {\r
-            // If variable is multidimensional, get all indexes that are active and add them to rvis-map\r
-            List<Resource> arrayIndexList = ListUtils.toList(graph, arrayIndexes);\r
-            resolveActiveArrayIndexes(graph, variable, arrayIndexList);\r
+            SysdynResource sr = SysdynResource.getInstance(graph);\r
+            String rvi = Variables.getRVI(graph, variable);\r
+            Resource r = variable.getRepresents(graph);\r
 \r
-            if(arrayIndexList.size() > 0)\r
-                traverseIndexes(graph, rvi, rvis, arrayIndexList);\r
-            else\r
+            Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList);\r
+            if(arrayIndexes == null) {\r
+                // If variable is single-dimensional, use the same rvi\r
                 rvis.put(rvi, rvi);\r
-        }\r
+            } else {\r
+                // If variable is multidimensional, get all indexes that are active and add them to rvis-map\r
+                List<Resource> arrayIndexList = ListUtils.toList(graph, arrayIndexes);\r
+                resolveActiveArrayIndexes(graph, variable, arrayIndexList);\r
 \r
-        return rvis;\r
+                if(arrayIndexList.size() > 0)\r
+                    traverseIndexes(graph, rvi, rvis, arrayIndexList);\r
+                else\r
+                    rvis.put(rvi, rvi);\r
+            }\r
+            return rvis;\r
+        }\r
     }\r
 \r
     \r
index 421e75d26325f13f4dbd6ead3bbd04a41ca7e07d..c4de04a1da5b99e8e00a5d4e11b19b753b14c7c7 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.manager;
 import gnu.trove.list.array.TDoubleArrayList;\r
 import gnu.trove.map.hash.THashMap;\r
 \r
+import java.util.Collection;\r
 import java.util.HashMap;\r
 \r
 import org.eclipse.core.runtime.IProgressMonitor;\r
@@ -47,7 +48,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment {
        \r
        THashMap<String, TDoubleArrayList> results;\r
 \r
-       protected String[] subscription;\r
+       public String[] subscription;\r
        \r
        public static double DEFAULT_STEP_DURATION = 1.0;\r
        public static double DEFAULT_STEP_LENGTH        = 0.1;\r
@@ -82,6 +83,25 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment {
                this.savePer = interval;\r
        }\r
        \r
+       public static class Subscription {\r
+           public SysdynGameExperimentBase exp;\r
+           public int[] indexes;\r
+           public void getValues(double[] data) {\r
+               for(int i=0;i<indexes.length;i++) data[i] = exp.currentValues[indexes[i]];\r
+           }\r
+       }\r
+       \r
+       public Subscription subscribe(Collection<String> names) {\r
+           Subscription result = new Subscription();\r
+           int[] indexes = new int[names.size()];\r
+           int index = 0;\r
+           for(String name : names)\r
+               indexes[index++] = subscriptionIndexes.get(name);\r
+           result.exp = this;\r
+           result.indexes = indexes;\r
+           return result;\r
+       }\r
+       \r
     public Double getCurrentValue(String name) {\r
         if(subscriptionIndexes != null && name != null) {\r
             Integer index = subscriptionIndexes.get(name);\r
index 966f9808916e4685e9a96414bb0f145f9beaec1b..b8b06837bb56871ae0ae391a4f597a4af2fc4303 100644 (file)
@@ -16,6 +16,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.util.Base64;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.BinaryRead;\r
 import org.simantics.db.common.request.ObjectsWithSupertype;\r
 import org.simantics.db.common.request.UniqueRead;\r
 import org.simantics.db.common.request.WriteRequest;\r
@@ -617,16 +618,27 @@ public class DocumentationUtils {
 \r
     }\r
     \r
-    public static org.simantics.db.layer0.variable.Variable lastValueIndexed(ReadGraph graph, org.simantics.db.layer0.variable.Variable run, String path) throws DatabaseException {\r
-       \r
-       if(run == null) return null;\r
+    public static class LastValueIndexed extends BinaryRead<Resource, String, org.simantics.db.layer0.variable.Variable> {\r
 \r
-       Pair<String, String> nameAndIndices = splitToNameAndIndices(path);\r
+        public LastValueIndexed(Resource resource, String path) {\r
+            super(resource, path);\r
+        }\r
 \r
-       org.simantics.db.layer0.variable.Variable v = run.browsePossible(graph, "/" + nameAndIndices.first + "#value#" + nameAndIndices.second);\r
-       if(v == null) throw new MissingVariableException("No variable for SysDyn reference path: " + path);\r
-       \r
-       return v;\r
+        @Override\r
+        public org.simantics.db.layer0.variable.Variable perform(ReadGraph graph) throws DatabaseException {\r
+            \r
+            org.simantics.db.layer0.variable.Variable run = Variables.getVariable(graph, parameter);\r
+            Pair<String, String> nameAndIndices = splitToNameAndIndices(parameter2);\r
+            org.simantics.db.layer0.variable.Variable v = run.browsePossible(graph, "/" + nameAndIndices.first + "#value#" + nameAndIndices.second);\r
+            if(v == null) throw new MissingVariableException("No variable for SysDyn reference path: " + parameter2);\r
+            return v;\r
+        }\r
+    }\r
+    \r
+    \r
+    public static org.simantics.db.layer0.variable.Variable lastValueIndexed(ReadGraph graph, org.simantics.db.layer0.variable.Variable run, String path) throws DatabaseException {\r
+       if(run == null) return null;\r
+       return graph.syncRequest(new LastValueIndexed(run.getRepresents(graph), path));\r
     }    \r
     \r
     public static org.simantics.db.layer0.variable.Variable equation(ReadGraph graph, org.simantics.db.layer0.variable.Variable input, String path) throws DatabaseException {\r