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.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class ProjectViewModel {
......@@ -29,7 +32,8 @@ public class ProjectViewModel {
private final OutputFileTableModel outputTableModel;
private final VadereScenarioTableModel scenarioTableModel;
private String currentProjectPath;
private Thread refreshOutputThread;
private ExecutorService refreshOutputExecutor;
// these are also part of the model, because only they know the current selected row
private VTable scenarioTable;
......@@ -45,7 +49,7 @@ public class ProjectViewModel {
this.outputRefreshListeners = new LinkedList<>();
this.projectChangeListeners = new LinkedList<>();
this.project = null;
this.refreshOutputThread = null;
this.refreshOutputExecutor = Executors.newSingleThreadExecutor();
}
public void deleteOutputFiles(final int[] rows) throws IOException {
......@@ -123,17 +127,7 @@ public class ProjectViewModel {
}
public void refreshOutputTable() {
if (refreshOutputThread != null && refreshOutputThread.isAlive()) {
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();
refreshOutputExecutor.execute(new OutputRefresher());
}
public void addScenario(final Scenario scenario) {
......
......@@ -492,9 +492,9 @@ class DataProcessingView extends JPanel implements IJsonView {
c.gridx = 1;
c.gridy = 0;
JTextField nameField = new JTextField(outputFile.getFileName());
JTextField nameField = new JTextField(outputFile.toString());
nameField.addActionListener(ae -> {
String oldName = outputFile.getFileName();
String oldName = outputFile.toString();
String newName = nameField.getText();
if (!oldName.equals(newName)) {
String msg = "";
......@@ -505,7 +505,7 @@ class DataProcessingView extends JPanel implements IJsonView {
msg = "File name is already in use";
}
if (msg.isEmpty()) {
outputFile.setFileName(newName);
outputFile.setRelativeFileName(newName);
outputFilesTable.repaint();
refreshGUI();
} else {
......
......@@ -74,9 +74,11 @@ public class ScenarioRun implements Runnable {
// prepare processors and simulation data writer
processorManager = dataProcessingJsonManager.createProcessorManager(mainModel);
createAndSetOutputDirectory();
scenario.saveToOutputPath(outputPath);
// Only create output directory and write .scenario file if there is any output.
if(!processorManager.isEmpty()) {
createAndSetOutputDirectory();
scenario.saveToOutputPath(outputPath);
}
sealAllAttributes();
......
......@@ -90,7 +90,7 @@ public class DataProcessingJsonManager {
private OutputFile<?> instantiateOutputFile(final OutputFileStore fileStore) {
OutputFile<?> file = outputFileInstantiator.createObject(fileStore.getType());
file.setFileName(fileStore.getFilename());
file.setAbsoluteFileName(fileStore.getFilename());
file.setProcessorIds(fileStore.getProcessors());
file.setSeparator(fileStore.getSeparator());
return file;
......
......@@ -69,7 +69,7 @@ public class ProcessorManager {
}
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() {
......@@ -80,6 +80,14 @@ public class ProcessorManager {
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() {
processorMap.values().forEach(p -> p.sealAttributes());
}
......
......@@ -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.processor.DataProcessor;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
......@@ -42,10 +40,14 @@ public abstract class OutputFile<K extends DataKey<K>> {
this.dataProcessors = new ArrayList<>();
}
public void setFileName(final String fileName) {
public void setAbsoluteFileName(final String 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) {
this.processorIds = processorIds;
this.dataProcessors.clear();
......@@ -78,6 +80,10 @@ public abstract class OutputFile<K extends DataKey<K>> {
}
}
public boolean isEmpty() {
return this.dataProcessors.isEmpty();
}
private List<String> getFieldHeaders() {
return composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
}
......@@ -125,6 +131,6 @@ public abstract class OutputFile<K extends DataKey<K>> {
@Override
public String toString() {
return fileName;
return new File(fileName).getName();
}
}
......@@ -47,8 +47,7 @@ public abstract class IOOutput {
* Returns all valid output directories of the project.
*/
public static List<File> listAllOutputDirs(final VadereProject project) {
return listAllDirs(project).stream().filter(f -> isValidOutputDirectory(project, f))
.collect(Collectors.toList());
return listAllDirs(project).stream().filter(f -> isValidOutputDirectory(project, f)).collect(Collectors.toList());
}
/**
......@@ -59,20 +58,27 @@ public abstract class IOOutput {
.forEach(dir -> cleanDirectory(project, dir));
}
public static Map<Step, List<Agent>> readTrajectories(final VadereProject project,
final Scenario scenario, final String directoryName) throws IOException {
TrajectoryReader reader = new TrajectoryReader(
getPathToOutputFile(project, directoryName, IOUtils.TRAJECTORY_FILE_EXTENSION), scenario);
public static Map<Step, List<Agent>> readTrajectories(final VadereProject project, final Scenario scenario, final String directoryName) throws IOException {
TrajectoryReader reader = new TrajectoryReader(getPathToOutputFile(project, directoryName, IOUtils.TRAJECTORY_FILE_EXTENSION), scenario);
return reader.readFile();
}
public static Map<Step, List<Agent>> readTrajectories(final Path trajectoryFilePath,
final Scenario scenario) throws IOException {
public static Map<Step, List<Agent>> readTrajectories(final Path trajectoryFilePath, final Scenario scenario) throws IOException {
TrajectoryReader reader = new TrajectoryReader(trajectoryFilePath, scenario);
Map<Step, List<Agent>> result = reader.readFile();
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)
throws IOException {
String snapshotString = IOUtils
......@@ -190,7 +196,7 @@ public abstract class IOOutput {
}
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,
......
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.log4j.LogManager;
import org.apache.log4j.Logger;
......@@ -103,7 +102,7 @@ public class TrajectoryReader {
return Files.lines(this.trajectoryFilePath)
.skip(1) // Skip header line
.map(line -> line.split(" "))
.map(line -> line.split(SPLITTER))
.map(cells -> {
int step = Integer.parseInt(cells[stepIndex]);
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