Skip to content

Commit

Permalink
SimSystem and SimTools update
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Herzog committed Mar 26, 2021
1 parent 9d9356b commit 12c73bb
Show file tree
Hide file tree
Showing 77 changed files with 968 additions and 255 deletions.
46 changes: 23 additions & 23 deletions SimSystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,57 @@
<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.11.0</version>
<version>1.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.31.1</version>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.34.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<version>5.6.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.odftoolkit/simple-odf -->
<dependency>
<groupId>org.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.9.0-RC1</version>
<groupId>org.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.9.0-RC1</version>
</dependency>
</dependencies>

Expand Down
72 changes: 71 additions & 1 deletion SimSystem/src/main/java/mathtools/MultiTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package mathtools;

import java.awt.Component;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -46,7 +47,7 @@
/**
* Diese Klasse ermöglichst das Laden und Speichern mehrerer Tabellen in einer Exceldatei
* @author Alexander Herzog
* @version 1.4
* @version 1.5
*/
public final class MultiTable {
/**
Expand Down Expand Up @@ -298,6 +299,75 @@ public boolean load(final File file) {
return load(file,Table.SaveMode.SAVEMODE_BYFILENAME);
}

/**
* Versucht eine Tabelle aus einem {@link ByteArrayInputStream} zu laden.<br>
* Unterstützt werden dabei Plain-Text-Formate und xlsx-Dateien.
* @param stream Eingabestream
* @return Liefert im Erfolgsfall <code>true</code> zurück
*/
private boolean loadByteArray(final ByteArrayInputStream stream) {
if (stream.available()<2) return false;

final byte[] start=new byte[2];
try {
if (stream.read(start)!=2) return false;
} catch (IOException e) {
return false;
}
stream.reset();

if (start[0]=='P' && start[1]=='K') {
/* xlsx */
try {
try (XSSFWorkbook wb=new XSSFWorkbook(stream)) {
if (wb.getNumberOfSheets()<1) return false;
for (int i=0;i<wb.getNumberOfSheets();i++) {
Table table=new Table();
table.loadFromSheet(wb.getSheetAt(i));
add(wb.getSheetName(i),table);
}
}
} catch (Exception e) {return false;}
return true;
} else {
/* Plain */
final int size=stream.available();
final byte[] data=new byte[size];
try {
if (stream.read(data)!=size) return false;
} catch (IOException e) {
return false;
}
final String tableTest=new String(data);
final Table table=new Table();
table.load(tableTest);
add("",table);
return true;
}
}

/**
* Versucht eine Tabelle aus einem Stream zu laden.<br>
* Unterstützt werden dabei Plain-Text-Formate und xlsx-Dateien.
* @param stream Eingabestream
* @return Liefert im Erfolgsfall <code>true</code> zurück
*/
public boolean loadStream(final InputStream stream) {
if (stream instanceof ByteArrayInputStream) return loadByteArray((ByteArrayInputStream)stream);

try {
final int size=stream.available();
final byte[] data=new byte[size];
if (stream.read(data)!=size) return false;

try (ByteArrayInputStream byteArray=new ByteArrayInputStream(data)) {
return loadByteArray(byteArray);
}
} catch (IOException e) {
return false;
}
}

/**
* Speichert den Inhalt der Arbeitsmappe in einer html-Datei.
* @param file Dateiname der Datei.
Expand Down
10 changes: 7 additions & 3 deletions SimSystem/src/main/java/mathtools/SimpleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class SimpleParser extends CalcSystemBase {
* Konstruktor der Klasse <code>SimpleParser</code>
*/
public SimpleParser() {
super();
}

/**
Expand Down Expand Up @@ -349,7 +348,12 @@ public double calc(double[] variableValues) throws MathCalcError {
return getConstValue();
}
if (root==null) throw new MathCalcError(this);
return root.calc(variableValues);
try {
return root.calc(variableValues);
} catch (Exception e) {
if (e instanceof MathCalcError) throw e;
throw new MathCalcError(e);
}
}

@Override
Expand All @@ -358,7 +362,7 @@ public double calcOrDefault(double[] variableValues, double fallbackValue) {
if (root==null) return fallbackValue;
try {
return root.calc(variableValues);
} catch (MathCalcError e) {
} catch (Exception e) {
return fallbackValue;
}
}
Expand Down
33 changes: 31 additions & 2 deletions SimSystem/src/main/java/mathtools/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ public List<String> getLine(final int index) {
public void setLine(final int index, final List<String> newLine) {
if (index<0) return;
while (index>=data.size()) data.add(new ArrayList<>());
final List<String> list=new ArrayList<>(newLine.size());
list.addAll(newLine);
final List<String> list=new ArrayList<>(newLine);
data.set(index,list);
}

Expand Down Expand Up @@ -2109,6 +2108,36 @@ public static int numberFromColumnName(final String colName) {
return col-1;
}

/**
* Wandelt eine Spaltenbezeichnung wie A in einen 0-basierenden wert um.
* @param colName Spaltenbezeichner A,B,...,Y,Z,AA,AB,...
* @return 0-basierter Index der Spalte oder -1 im Falle eines Fehlers
*/
public static int numberFromColumnNameIgnoreRowNumbers(String colName) {
if (colName==null) return -1;
colName=colName.trim();
if (colName.isEmpty()) return -1;
final char[] c=colName.toCharArray();
int i=0,col=0;
while (i<c.length) {
if (c[i]>='A' && c[i]<='Z') {
col*=26;
col+=(c[i]-'A')+1;
i++;
continue;
}
if (c[i]>='a' && c[i]<='z') {
col*=26;
col+=(c[i]-'a')+1;
i++;
continue;
}
break;
}
/* if (i!=c.length) return -1; - Zahlen am Ende werden ignoriert */
return col-1;
}

/**
* Wandelt eine Zellenbeschreibung wie A1 in ein Array aus Zeilen- und Spaltennummer (jeweils 0-basierend) um.
* @param cellID Bezeichner der Zelle
Expand Down
9 changes: 5 additions & 4 deletions SimSystem/src/main/java/mathtools/TableXLSXReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.binary.XSSFBSheetHandler.SheetContentsHandler;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
Expand Down Expand Up @@ -184,7 +183,8 @@ private void outputMissingRows(int number) {
@Override
public void startRow(int rowNum) {
endRow(-1);
outputMissingRows(rowNum-table.getSize(0));
final int missedRows=rowNum-table.getSize(0);
if (missedRows>0) outputMissingRows(missedRows);
row=new ArrayList<>();
}

Expand All @@ -200,8 +200,9 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen

if (cellReference==null) cellReference=new CellAddress(table.getSize(0),row.size()).formatAsString();

int thisCol=(new CellReference(cellReference)).getCol();
int missedCols=thisCol-row.size();
final int thisCol=Table.numberFromColumnNameIgnoreRowNumbers(cellReference);
/* Speicherintensiver: final int thisCol=new CellReference(cellReference).getCol(); */
final int missedCols=thisCol-row.size();
for (int i=0;i<missedCols;i++) row.add("");

row.add(formattedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public interface DistributionWithRandom {
* @param generator Generator für auf [0;1] gleichverteilte Zufallszahlen
* @return Zufallszahl gemäß der Verteilung
*/
public double random(final RandomGenerator generator);
double random(final RandomGenerator generator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ public enum LabelMode {
* @param saveAsImageButtons Bietet, wenn auf <code>true</code> gesetzt, die Möglichkeit, die Verteilung als Grafik zu speichern (statt sonst nur als Tabelle).
*/
public JDataDistributionEditPanel(DataDistributionImpl distribution, PlotMode plotType, boolean editable, double editStep, boolean saveAsImageButtons) {
super();
changeListener=new ArrayList<>();
if (distribution==null) distribution=new DataDistributionImpl(1,1);
this.distribution=distribution.clone();
Expand Down Expand Up @@ -771,7 +770,6 @@ private class DataPlotter extends JPanel implements MouseListener, MouseMotionLi
* Konstruktor der Klasse
*/
public DataPlotter() {
super();
addMouseListener(this);
addMouseMotionListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public class JDataLoader extends JDialog {
* @param numbersOnly Gibt an, ob Zahlen (<code>true</code>) oder beliebige Daten (<code>false</code>) verlangt werden.
*/
public JDataLoader(final Window owner, final MultiTable data, final int minValues, final int maxValues, final boolean numbersOnly) {
super(owner,Title,Dialog.ModalityType.APPLICATION_MODAL);
super(owner,Title,Dialog.ModalityType.DOCUMENT_MODAL);

this.owner=owner;
this.data=data;
Expand Down Expand Up @@ -409,7 +409,6 @@ private class ImportTableModel extends AbstractTableModel {
* @param table Anzuzeigende Tabelle
*/
public ImportTableModel(final Table table) {
super();
this.table=table;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class JDistributionEditorDialog extends JDialog {
* @param imageSize Gibt die Größe des Bildes beim Speichern an.
*/
public JDistributionEditorDialog(final Window owner, final AbstractRealDistribution distribution, final double maxXValue, final int plotType, final boolean allowDistributionTypeChange, final boolean allowOk, final int imageSize) {
super(owner,JDistributionEditorPanel.DialogTitle,Dialog.ModalityType.APPLICATION_MODAL);
super(owner,JDistributionEditorPanel.DialogTitle,Dialog.ModalityType.DOCUMENT_MODAL);
addWindowListener(new WindowAdapter() {@Override
public void windowClosing(WindowEvent event) {setVisible(false);}});
setResizable(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
/**
* Diese Klasse kapselt einen Verteilungseditor als Panel.
* @author Alexander Herzog
* @version 2.0
* @version 2.1
* @see JDistributionEditorDialog
*/
public class JDistributionEditorPanel extends JPanel {
Expand Down Expand Up @@ -284,11 +284,24 @@ private boolean isFullAccess() {
/**
* Auslesen der momentanen Verteilung
* @return Aktuelles Verteilungsobjekt
* @see #setDistribution(AbstractRealDistribution)
*/
public AbstractRealDistribution getDistribution() {
return distribution;
}

/**
* Einstellen einer neuen Verteilung
* @param distribution Neue Verteilung
* @see #getDistribution()
*/
public void setDistribution(final AbstractRealDistribution distribution) {
if (distribution==null) return;
this.distribution=distribution;

setDataFromDistribution();
}

/**
* Erstellt ein Panel mit Eingabefeldern
* @param name Name unter dem das Panel im {@link CardLayout} registriert werden soll
Expand Down
Loading

0 comments on commit 12c73bb

Please sign in to comment.