Commit fddbac91 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

fix path problem in DataProcessingView

parent b39e6733
...@@ -18,6 +18,9 @@ import javax.swing.*; ...@@ -18,6 +18,9 @@ import javax.swing.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ProjectViewModel { public class ProjectViewModel {
...@@ -29,7 +32,8 @@ public class ProjectViewModel { ...@@ -29,7 +32,8 @@ public class ProjectViewModel {
private final OutputFileTableModel outputTableModel; private final OutputFileTableModel outputTableModel;
private final VadereScenarioTableModel scenarioTableModel; private final VadereScenarioTableModel scenarioTableModel;
private String currentProjectPath; private String currentProjectPath;
private Thread refreshOutputThread; private ExecutorService refreshOutputExecutor;
// these are also part of the model, because only they know the current selected row // these are also part of the model, because only they know the current selected row
private VTable scenarioTable; private VTable scenarioTable;
...@@ -45,7 +49,7 @@ public class ProjectViewModel { ...@@ -45,7 +49,7 @@ public class ProjectViewModel {
this.outputRefreshListeners = new LinkedList<>(); this.outputRefreshListeners = new LinkedList<>();
this.projectChangeListeners = new LinkedList<>(); this.projectChangeListeners = new LinkedList<>();
this.project = null; this.project = null;
this.refreshOutputThread = null; this.refreshOutputExecutor = Executors.newSingleThreadExecutor();
} }
public void deleteOutputFiles(final int[] rows) throws IOException { public void deleteOutputFiles(final int[] rows) throws IOException {
...@@ -123,17 +127,7 @@ public class ProjectViewModel { ...@@ -123,17 +127,7 @@ public class ProjectViewModel {
} }
public void refreshOutputTable() { public void refreshOutputTable() {
if (refreshOutputThread != null && refreshOutputThread.isAlive()) { refreshOutputExecutor.execute(new OutputRefresher());
try {
logger.info("wait for output refresh to be finished, before restart again");
refreshOutputThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
logger.error(e.getLocalizedMessage());
}
}
refreshOutputThread = new Thread(new OutputRefresher());
refreshOutputThread.start();
} }
public void addScenario(final Scenario scenario) { public void addScenario(final Scenario scenario) {
......
...@@ -492,9 +492,9 @@ class DataProcessingView extends JPanel implements IJsonView { ...@@ -492,9 +492,9 @@ class DataProcessingView extends JPanel implements IJsonView {
c.gridx = 1; c.gridx = 1;
c.gridy = 0; c.gridy = 0;
JTextField nameField = new JTextField(outputFile.getFileName()); JTextField nameField = new JTextField(outputFile.toString());
nameField.addActionListener(ae -> { nameField.addActionListener(ae -> {
String oldName = outputFile.getFileName(); String oldName = outputFile.toString();
String newName = nameField.getText(); String newName = nameField.getText();
if (!oldName.equals(newName)) { if (!oldName.equals(newName)) {
String msg = ""; String msg = "";
...@@ -505,7 +505,7 @@ class DataProcessingView extends JPanel implements IJsonView { ...@@ -505,7 +505,7 @@ class DataProcessingView extends JPanel implements IJsonView {
msg = "File name is already in use"; msg = "File name is already in use";
} }
if (msg.isEmpty()) { if (msg.isEmpty()) {
outputFile.setFileName(newName); outputFile.setRelativeFileName(newName);
outputFilesTable.repaint(); outputFilesTable.repaint();
refreshGUI(); refreshGUI();
} else { } else {
......
...@@ -74,9 +74,11 @@ public class ScenarioRun implements Runnable { ...@@ -74,9 +74,11 @@ public class ScenarioRun implements Runnable {
// prepare processors and simulation data writer // prepare processors and simulation data writer
processorManager = dataProcessingJsonManager.createProcessorManager(mainModel); processorManager = dataProcessingJsonManager.createProcessorManager(mainModel);
createAndSetOutputDirectory(); // Only create output directory and write .scenario file if there is any output.
if(!processorManager.isEmpty()) {
scenario.saveToOutputPath(outputPath); createAndSetOutputDirectory();
scenario.saveToOutputPath(outputPath);
}
sealAllAttributes(); sealAllAttributes();
......
...@@ -90,7 +90,7 @@ public class DataProcessingJsonManager { ...@@ -90,7 +90,7 @@ public class DataProcessingJsonManager {
private OutputFile<?> instantiateOutputFile(final OutputFileStore fileStore) { private OutputFile<?> instantiateOutputFile(final OutputFileStore fileStore) {
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType()); OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setFileName(fileStore.getFilename()); file.setAbsoluteFileName(fileStore.getFilename());
file.setProcessorIds(fileStore.getProcessors()); file.setProcessorIds(fileStore.getProcessors());
file.setSeparator(fileStore.getSeparator()); file.setSeparator(fileStore.getSeparator());
return file; return file;
......
...@@ -69,7 +69,7 @@ public class ProcessorManager { ...@@ -69,7 +69,7 @@ public class ProcessorManager {
} }
public void setOutputPath(String directory) { public void setOutputPath(String directory) {
this.outputFiles.forEach(file -> file.setFileName(Paths.get(directory, String.format("%s", new File(file.getFileName()).getName())).toString())); this.outputFiles.forEach(file -> file.setAbsoluteFileName(Paths.get(directory, String.format("%s", new File(file.getFileName()).getName())).toString()));
} }
public void writeOutput() { public void writeOutput() {
...@@ -80,6 +80,14 @@ public class ProcessorManager { ...@@ -80,6 +80,14 @@ public class ProcessorManager {
return this.jsonManager.serializeToNode(); return this.jsonManager.serializeToNode();
} }
/**
* Returns true if there is no output to write, otherwise false.
* @return
*/
public boolean isEmpty() {
return processorMap.isEmpty();
}
public void sealAllAttributes() { public void sealAllAttributes() {
processorMap.values().forEach(p -> p.sealAttributes()); processorMap.values().forEach(p -> p.sealAttributes());
} }
......
...@@ -4,10 +4,8 @@ import org.vadere.simulator.projects.dataprocessing.ProcessorManager; ...@@ -4,10 +4,8 @@ import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.DataKey; import org.vadere.simulator.projects.dataprocessing.datakey.DataKey;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor; import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor;
import java.io.FileWriter; import java.io.*;
import java.io.IOException; import java.nio.file.Path;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -42,10 +40,14 @@ public abstract class OutputFile<K extends DataKey<K>> { ...@@ -42,10 +40,14 @@ public abstract class OutputFile<K extends DataKey<K>> {
this.dataProcessors = new ArrayList<>(); this.dataProcessors = new ArrayList<>();
} }
public void setFileName(final String fileName) { public void setAbsoluteFileName(final String fileName) {
this.fileName = fileName; this.fileName = fileName;
} }
public void setRelativeFileName(final String fileName) {
this.fileName = new File(this.fileName).getParentFile().toPath().resolve(fileName).toString();
}
public void setProcessorIds(final List<Integer> processorIds) { public void setProcessorIds(final List<Integer> processorIds) {
this.processorIds = processorIds; this.processorIds = processorIds;
this.dataProcessors.clear(); this.dataProcessors.clear();
...@@ -78,6 +80,10 @@ public abstract class OutputFile<K extends DataKey<K>> { ...@@ -78,6 +80,10 @@ public abstract class OutputFile<K extends DataKey<K>> {
} }
} }
public boolean isEmpty() {
return this.dataProcessors.isEmpty();
}
private List<String> getFieldHeaders() { private List<String> getFieldHeaders() {
return composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders())); return composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
} }
...@@ -125,6 +131,6 @@ public abstract class OutputFile<K extends DataKey<K>> { ...@@ -125,6 +131,6 @@ public abstract class OutputFile<K extends DataKey<K>> {
@Override @Override
public String toString() { public String toString() {
return fileName; return new File(fileName).getName();
} }
} }
...@@ -47,8 +47,7 @@ public abstract class IOOutput { ...@@ -47,8 +47,7 @@ public abstract class IOOutput {
* Returns all valid output directories of the project. * Returns all valid output directories of the project.
*/ */
public static List<File> listAllOutputDirs(final VadereProject project) { public static List<File> listAllOutputDirs(final VadereProject project) {
return listAllDirs(project).stream().filter(f -> isValidOutputDirectory(project, f)) return listAllDirs(project).stream().filter(f -> isValidOutputDirectory(project, f)).collect(Collectors.toList());
.collect(Collectors.toList());
} }
/** /**
...@@ -59,20 +58,27 @@ public abstract class IOOutput { ...@@ -59,20 +58,27 @@ public abstract class IOOutput {
.forEach(dir -> cleanDirectory(project, dir)); .forEach(dir -> cleanDirectory(project, dir));
} }
public static Map<Step, List<Agent>> readTrajectories(final VadereProject project, public static Map<Step, List<Agent>> readTrajectories(final VadereProject project, final Scenario scenario, final String directoryName) throws IOException {
final Scenario scenario, final String directoryName) throws IOException { TrajectoryReader reader = new TrajectoryReader(getPathToOutputFile(project, directoryName, IOUtils.TRAJECTORY_FILE_EXTENSION), scenario);
TrajectoryReader reader = new TrajectoryReader(
getPathToOutputFile(project, directoryName, IOUtils.TRAJECTORY_FILE_EXTENSION), scenario);
return reader.readFile(); return reader.readFile();
} }
public static Map<Step, List<Agent>> readTrajectories(final Path trajectoryFilePath, public static Map<Step, List<Agent>> readTrajectories(final Path trajectoryFilePath, final Scenario scenario) throws IOException {
final Scenario scenario) throws IOException {
TrajectoryReader reader = new TrajectoryReader(trajectoryFilePath, scenario); TrajectoryReader reader = new TrajectoryReader(trajectoryFilePath, scenario);
Map<Step, List<Agent>> result = reader.readFile(); Map<Step, List<Agent>> result = reader.readFile();
return result; return result;
} }
private static Optional<Map<Step, List<Agent>>> readTrajectories(final VadereProject project, final File directory) {
try {
TrajectoryReader reader = new TrajectoryReader(getPathToOutputFile(project, directory.getName(), IOUtils.TRAJECTORY_FILE_EXTENSION));
return Optional.of(reader.readFile());
} catch (IOException | VadereClassNotFoundException e) {
logger.error("Error in output file " + directory.getName());
return Optional.empty();
}
}
public static Scenario readScenarioRunManager(final VadereProject project, final String directoryName) public static Scenario readScenarioRunManager(final VadereProject project, final String directoryName)
throws IOException { throws IOException {
String snapshotString = IOUtils String snapshotString = IOUtils
...@@ -190,7 +196,7 @@ public abstract class IOOutput { ...@@ -190,7 +196,7 @@ public abstract class IOOutput {
} }
private static boolean isValidOutputDirectory(final VadereProject project, final File directory) { private static boolean isValidOutputDirectory(final VadereProject project, final File directory) {
return readOutputFile(project, directory).isPresent(); return readOutputFile(project, directory).isPresent() && readTrajectories(project, directory).isPresent();
} }
private static boolean isMatchingOutputDirectory(final VadereProject project, final File directory, private static boolean isMatchingOutputDirectory(final VadereProject project, final File directory,
......
package org.vadere.simulator.projects.io; package org.vadere.simulator.projects.io;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.util.Pair;
import org.apache.log4j.LogManager; import org.apache.log4j.LogManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
...@@ -103,7 +102,7 @@ public class TrajectoryReader { ...@@ -103,7 +102,7 @@ public class TrajectoryReader {
return Files.lines(this.trajectoryFilePath) return Files.lines(this.trajectoryFilePath)
.skip(1) // Skip header line .skip(1) // Skip header line
.map(line -> line.split(" ")) .map(line -> line.split(SPLITTER))
.map(cells -> { .map(cells -> {
int step = Integer.parseInt(cells[stepIndex]); int step = Integer.parseInt(cells[stepIndex]);
int pedestrianId = Integer.parseInt(cells[pedIdIndex]); int pedestrianId = Integer.parseInt(cells[pedIdIndex]);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment