From ea8c2542d3a553db28eb9bcba01646691ab9b74c Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 28 Nov 2012 09:24:26 +0000 Subject: [PATCH] Removed spreadsheet remover and improved sheet handling (refs #3473) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26473 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/modelica/ModelicaManager.java | 78 ++++++------------- .../ResourceSelectionProcessor.java | 10 ++- .../sysdyn/manager/SysdynResult.java | 2 +- .../sysdyn/modelica/ModelicaWriter.java | 8 +- .../sysdyn/representation/Sheet.java | 30 +++---- 5 files changed, 54 insertions(+), 74 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index b55a46c0..b62398e3 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -13,10 +13,10 @@ package org.simantics.modelica; import java.io.File; import java.io.FileNotFoundException; +import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; -import java.io.RandomAccessFile; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; @@ -471,19 +471,29 @@ public class ModelicaManager { Process p = runOMC(simulationLocation.fullModelDir, null, parameters); p.waitFor(); - // Find the last line of the full description. This will be the name of the created simulation files - String lastLine = readLastLine(simulationLocation.fullModel.getAbsolutePath()); - - if(lastLine != null && lastLine.startsWith("end ")) { - String lastClass = lastLine.substring(4, lastLine.length() -1); // e.g. "end xidz;" -> "xidz" - lastClass = lastClass + "_init.xml"; - File initFile = new File(simulationLocation.fullModelDir, lastClass); - if(initFile != null && initFile.isFile()) { - // Replace original init contents with the new contents - replaceInitFileContents(simulationLocation.initFile.getAbsolutePath(), initFile.getAbsolutePath(), experimentParameters); + // Find the new init file + FilenameFilter initFilter = new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + if(name.endsWith("_init.xml")) + return true; + else + return false; } + }; + + File initFile = null; + for(File f : simulationLocation.fullModelDir.listFiles(initFilter)) { + initFile = f; + break; } - + + if(initFile != null && initFile.isFile()) { + // Replace original init contents with the new contents + replaceInitFileContents(simulationLocation.initFile.getAbsolutePath(), initFile.getAbsolutePath(), experimentParameters); + } + } catch (ModelicaException e) { e.printStackTrace(); } catch (InterruptedException e) { @@ -568,48 +578,4 @@ public class ModelicaManager { } } - /** - * utility for reading the list line of a file - * @param fileName - * @return - */ - private static String readLastLine( String fileName ) { - try { - File file = new File( fileName ); - RandomAccessFile fileHandler = new RandomAccessFile( file, "r" ); - long fileLength = file.length() - 1; - StringBuilder sb = new StringBuilder(); - - for( long filePointer = fileLength; filePointer != -1; filePointer-- ) { - fileHandler.seek( filePointer ); - int readByte = fileHandler.readByte(); - - if( readByte == 0xA ) { - if( filePointer == fileLength ) { - continue; - } else { - break; - } - } else if( readByte == 0xD ) { - if( filePointer == fileLength - 1 ) { - continue; - } else { - break; - } - } - - sb.append( ( char ) readByte ); - } - - String lastLine = sb.reverse().toString(); - - fileHandler.close(); - return lastLine; - } catch( java.io.FileNotFoundException e ) { - e.printStackTrace(); - } catch( java.io.IOException e ) { - e.printStackTrace(); - } - return null; - } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index 31e0c5bd..1d5c8616 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -125,7 +125,7 @@ public class ResourceSelectionProcessor implements SelectionProcessor sheetNames = new HashSet(); for(Sheet sheet : getSpreadSheets(configurations)) sheetNames.add(sheet.getName()); @@ -81,9 +84,10 @@ public class ModelicaWriter { b.append("end " + modelName + ";\n\n"); - b.append(getGlobalSpreadSheets(configurations)); - + // Insert spreadsheets at this point. + b.insert(spreadsheetlocation, getGlobalSpreadSheets(configurations)); + return b.toString(); } 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 daefd69c..a2deef14 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -52,22 +52,24 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { @UpdateMethod public boolean updateCells(ReadGraph g, Resource r) throws DatabaseException { - this.resource = r; - - g.getObjects(r, Layer0.getInstance(g).ConsistsOf); - - Variable v = g.adapt(r, Variable.class); cells.clear(); usedRanges.clear(); - for(Variable child : v.getChildren(g)) { - String name = child.getName(g); - try { - SpreadsheetUtils.decodeCellAbsolute(name); - Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT); - cells.put(name, value.getValue()); - } catch (CellParseException e) { - } catch (MissingVariableException e) { - System.out.println("missing content for: " + name); + this.resource = null; + + if(g.hasStatement(r)) { + this.resource = r; + g.getObjects(r, Layer0.getInstance(g).ConsistsOf); + Variable v = g.adapt(r, Variable.class); + for(Variable child : v.getChildren(g)) { + String name = child.getName(g); + try { + SpreadsheetUtils.decodeCellAbsolute(name); + Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT); + cells.put(name, value.getValue()); + } catch (CellParseException e) { + } catch (MissingVariableException e) { + System.out.println("missing content for: " + name); + } } } return true; -- 2.47.1