From 3abbf7022e41b167e1b9e3954547527d69de0acc Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 26 Nov 2012 12:07:29 +0000 Subject: [PATCH] Moved calculating ranges to a different place and changed it to use synchronized query. (fixes #3874) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26444 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/representation/Sheet.java | 121 +++++++++--------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java index c6725a21..daefd69c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -74,40 +75,32 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { public String getStringRepresentation() { final StringBuilder clazz = new StringBuilder(); - final HashSet possibleRanges = new HashSet(); - clazz.append("class " + name + "_class\n"); - // Write all doubles that have been used in expressions - final HashSet usedCells = new HashSet(); - for(String key : usedRanges) { - if(cells.containsKey(key)) { - usedCells.add(key); - Object value = cells.get(key); - if(value instanceof String || value instanceof MutableString) { - try { - Double d = Double.parseDouble(value.toString()); - value = d; - } catch (NumberFormatException e) { - } - } - - if(value instanceof Double) { - Double d = (Double)value; - clazz.append(" parameter Real " + key + " = " + d + " /* Actual value read from init file */;\n"); - } - } else { - possibleRanges.add(key); - } - } - - // Get cell ranges. Add cell names that have not been used individually to usedCells set - // and use those names in ranges try { Simantics.getSession().syncRequest(new ReadRequest() { - + @Override public void run(ReadGraph graph) throws DatabaseException { + HashSet possibleRanges = new HashSet(); + + // Write all doubles that have been used in expressions + final HashSet usedCells = new HashSet(); + for(String key : usedRanges) { + if(cells.containsKey(key)) { + addRange(key, usedCells, clazz); + } else { + for(String range : getChildRanges(graph, key)) { + if(cells.containsKey(range)) + addRange(range, usedCells, clazz); + } + possibleRanges.add(key); + } + } + + // Get cell ranges. Add cell names that have not been used individually to usedCells set + // and use those names in ranges + Variable v = graph.adapt(resource, Variable.class); for(String possibleRange : possibleRanges) { String[] parts = possibleRange.split(":"); @@ -155,11 +148,53 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { } catch (DatabaseException e) { e.printStackTrace(); } - + clazz.append("end " + name + "_class;\n"); // clazz.append(" " + name + "_class " + name + ";\n"); return clazz.toString(); } + + private void addRange(String key, HashSet usedCells, StringBuilder clazz) { + usedCells.add(key); + Object value = cells.get(key); + if(value instanceof String || value instanceof MutableString) { + try { + Double d = Double.parseDouble(value.toString()); + value = d; + } catch (NumberFormatException e) { + } + } + + if(value instanceof Double) { + Double d = (Double)value; + clazz.append(" parameter Real " + key + " = " + d + " /* Actual value read from init file */;\n"); + } + } + + private ArrayList getChildRanges(ReadGraph graph, String range) throws DatabaseException { + ArrayList ranges = new ArrayList(); + if(range.contains(":")) { + // Get cell ranges. Add cell names that have not been used individually to usedCells set + // and use those names in ranges + Variable v = graph.adapt(resource, Variable.class); + String[] parts = range.split(":"); + if(parts.length != 2 || !cells.containsKey(parts[0]) || !cells.containsKey(parts[1])) + return ranges; + Variable rangeVariable = v.getChild(graph, range); + if(rangeVariable == null) + return ranges; + String[][] rangeCells = rangeVariable.getPropertyValue(graph, SheetVariables.RANGE_CELL_NAMES); + if(rangeCells == null) + return ranges; + + for(int i = 0; i < rangeCells.length; i++) { + for(int j = 0; j < rangeCells[i].length; j++) { + ranges.add(rangeCells[i][j]); + } + } + } + return ranges; + } public HashMap getCells() { @@ -180,34 +215,6 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { */ public void use(final String usedRange) { usedRanges.add(usedRange); - - - if(usedRange.contains(":")) { - // Get cell ranges. Add cell names that have not been used individually to usedCells set - // and use those names in ranges - Simantics.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - Variable v = graph.adapt(resource, Variable.class); - String[] parts = usedRange.split(":"); - if(parts.length != 2 || !cells.containsKey(parts[0]) || !cells.containsKey(parts[1])) - return; - Variable range = v.getChild(graph, usedRange); - if(range == null) - return; - String[][] rangeCells = range.getPropertyValue(graph, SheetVariables.RANGE_CELL_NAMES); - if(rangeCells == null) - return; - - for(int i = 0; i < rangeCells.length; i++) { - for(int j = 0; j < rangeCells[i].length; j++) { - usedRanges.add(rangeCells[i][j]); - } - } - } - }); - } } -- 2.47.1