From 0bc14e4faf0b099d005a77fed1b26389347f3547 Mon Sep 17 00:00:00 2001 From: villberg Date: Sat, 18 Oct 2014 19:03:20 +0000 Subject: [PATCH] (refs #5420) Performance of variable name array resolution git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30452 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../adapter/ActiveDatasetsIndexVariable.java | 5 +- .../sysdyn/adapter/IndexVariable.java | 57 ++++++++++++++----- .../sysdyn/adapter/NamesIndexVariable.java | 3 +- .../sysdyn/adapter/TimesIndexVariable.java | 4 +- .../adapter/ValueIndexVariableBase.java | 6 +- .../sysdyn/adapter/ValuesIndexVariable.java | 4 +- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ActiveDatasetsIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ActiveDatasetsIndexVariable.java index 4b57b97d..3039cea7 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ActiveDatasetsIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ActiveDatasetsIndexVariable.java @@ -2,6 +2,7 @@ package org.simantics.sysdyn.adapter; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; @@ -26,8 +27,8 @@ public class ActiveDatasetsIndexVariable extends IndexVariable< ArrayList results = experiment.getActiveResults(); - ArrayList variableNamesWithIndexes = getVariableNamesWithIndexNumbers(); - ArrayList variableNames = getVariableNames(); + List variableNamesWithIndexes = getVariableNamesWithIndexNumbers(); + List variableNames = getVariableNames(); for(int i = 0; i < variableNamesWithIndexes.size(); i++) { for(SysdynResult result : results) { 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 bf71e39f..fa6b38b0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/IndexVariable.java @@ -1,7 +1,9 @@ package org.simantics.sysdyn.adapter; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import org.simantics.Simantics; import org.simantics.databoard.Bindings; @@ -38,7 +40,7 @@ public abstract class IndexVariable extends AbstractPropertyVariable { protected HashMap rvis; protected String indexes; protected VariableSubscriptionManager subscriptionManager; - protected Pair,ArrayList> variableNameArrays; + private Pair,List> variableNameArrays; public IndexVariable(ReadGraph graph, Variable parent, String indexes) throws DatabaseException { this.parent = parent; @@ -208,28 +210,53 @@ public abstract class IndexVariable extends AbstractPropertyVariable { } - private Pair,ArrayList> getVariableNameArrays() { + private Pair,List> getVariableNameArrays() { if(variableNameArrays == null) { - Pair,ArrayList> result = new Pair,ArrayList>(new ArrayList(), new ArrayList()); if(rvi != null && rvis != null) { if(rvi.indexOf('#') > 0) rvi = rvi.substring(0, rvi.indexOf('#')); + boolean regexp = false; String test = rvi; if(indexes != null && !indexes.isEmpty()) { - String regexIndexes = indexes.trim().replaceAll(":", "[a-zA-Z0-9_]*"); // Why was there was ".replace('.', ',')" ? - Removed by Teemu 2.9.2012 - test = rvi + "\\[" + regexIndexes + "\\]"; + regexp = indexes.contains(":"); + if(regexp) { + String regexIndexes = indexes.trim().replaceAll(":", "[a-zA-Z0-9_]*"); // Why was there was ".replace('.', ',')" ? - Removed by Teemu 2.9.2012 + test = rvi + "\\[" + regexIndexes + "\\]"; + } else { + test = rvi + "[" + indexes + "]"; + } } else { test = rvi + "(\\[.*\\])?"; + regexp = true; } - for(String k : rvis.keySet()) { - String value = rvis.get(k); - if(k.matches(test) || value.matches(test)) { - result.first.add(k.substring(1).replace("/", ".").replace("%20", "_")); - result.second.add(value.substring(1).replace("/", ".").replace("%20", " ")); + + Pair,List> result = null; + + if(regexp) { + result = new Pair,List>(new ArrayList(), new ArrayList()); + for(String k : rvis.keySet()) { + String value = rvis.get(k); + if(k.matches(test) || value.matches(test)) { + result.first.add(transform(k)); + result.second.add(transform(value)); + } + } + } else { + String value = rvis.get(test); + if(value != null) { + result = new Pair,List>(Collections.singletonList(transform(test)), Collections.singletonList(transform(value))); + } else { + for(String k : rvis.keySet()) { + value = rvis.get(k); + if(value.equals(test)) { + result = new Pair,List>(Collections.singletonList(transform(k)), Collections.singletonList(transform(test))); + break; + } + } } } @@ -239,17 +266,21 @@ public abstract class IndexVariable extends AbstractPropertyVariable { } - if(variableNameArrays == null) return new Pair,ArrayList>(new ArrayList(), new ArrayList()); + if(variableNameArrays == null) return new Pair,List>(new ArrayList(), new ArrayList()); return variableNameArrays; } - protected ArrayList getVariableNames() { + private String transform(String s) { + return s.substring(1).replace("/", ".").replace("%20", "_"); + } + + protected List getVariableNames() { return getVariableNameArrays().second; } - protected ArrayList getVariableNamesWithIndexNumbers() { + protected List getVariableNamesWithIndexNumbers() { return getVariableNameArrays().first; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/NamesIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/NamesIndexVariable.java index 7e32bbad..2ef5afa3 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/NamesIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/NamesIndexVariable.java @@ -1,6 +1,7 @@ package org.simantics.sysdyn.adapter; import java.util.ArrayList; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; @@ -16,7 +17,7 @@ public class NamesIndexVariable extends IndexVariable { @Override public String[] getValue() { - ArrayList names = getVariableNames(); + List names = getVariableNames(); return names.toArray(new String[names.size()]); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/TimesIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/TimesIndexVariable.java index 329e4cd2..57e2e125 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/TimesIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/TimesIndexVariable.java @@ -1,7 +1,7 @@ package org.simantics.sysdyn.adapter; -import java.util.ArrayList; import java.util.Collection; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; @@ -24,7 +24,7 @@ public class TimesIndexVariable extends IndexVariable { return new double[0][0]; Collection results = experiment.getActiveResults(); - ArrayList variableNames = getVariableNamesWithIndexNumbers(); + List variableNames = getVariableNamesWithIndexNumbers(); double[][] result = new double[variableNames.size()][]; for(int i = 0; i < variableNames.size(); i++) { 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 cbb948bc..53db9ef8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariableBase.java @@ -1,7 +1,7 @@ package org.simantics.sysdyn.adapter; -import java.util.ArrayList; import java.util.Collection; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -46,7 +46,7 @@ abstract public class ValueIndexVariableBase extends IndexVariable { Collection results = experiment.getActiveResults(); - ArrayList variableNames = getVariableNamesWithIndexNumbers(); + List variableNames = getVariableNamesWithIndexNumbers(); double[] result = new double[variableNames.size()]; for(int i = 0; i < variableNames.size(); i++) { @@ -101,7 +101,7 @@ abstract public class ValueIndexVariableBase extends IndexVariable { try { - ArrayList variableNames = getVariableNamesWithIndexNumbers(); + List variableNames = getVariableNamesWithIndexNumbers(); for(int i = 0; i < variableNames.size() && i < values.length; i++) { if(values[i] == Double.NaN) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java index d0eb2032..2ed53f3f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java @@ -1,7 +1,7 @@ package org.simantics.sysdyn.adapter; -import java.util.ArrayList; import java.util.Collection; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; @@ -20,7 +20,7 @@ public class ValuesIndexVariable extends IndexVariable { @Override public double[][] getValue() { - ArrayList variableNames = getVariableNamesWithIndexNumbers(); + List variableNames = getVariableNamesWithIndexNumbers(); double[][] result = new double[variableNames.size()][]; if (experiment != null) { -- 2.47.1