Commit 776c1e0f authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add utils subparser to vadere-console to lookup Scenario hash values and calculate cache

parent b913118d
......@@ -5,7 +5,8 @@ public enum SubCommand {
PROJECT_RUN("project-run"),
SCENARO_RUN("scenario-run"),
SUQ("suq"),
MIGRATE("migrate");
MIGRATE("migrate"),
UTILS("utils");
private String cmdName;
......
package org.vadere.simulator.entrypoints.cmd;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.*;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.entrypoints.cmd.commands.MigrationSubCommand;
import org.vadere.simulator.entrypoints.cmd.commands.ProjectRunSubCommand;
import org.vadere.simulator.entrypoints.cmd.commands.ScenarioRunSubCommand;
import org.vadere.simulator.entrypoints.cmd.commands.SuqSubCommand;
import org.vadere.simulator.entrypoints.cmd.commands.UtilsSubCommand;
import org.vadere.simulator.utils.scenariochecker.ScenarioChecker;
import org.vadere.util.io.VadereArgumentParser;
import org.vadere.util.logging.Logger;
......@@ -184,6 +190,34 @@ public class VadereConsole {
.help("Create new transformation and identity file based on current latest version. " +
"PATH must point to the directory containing the old transformation files." +
" This Argument takes the new Version Label as input.");
// run mis
UtilsSubCommand utilsSubCommand = new UtilsSubCommand();
Subparser misc = subparsers
.addParser(SubCommand.UTILS.getCmdName())
.help("Run utility functions.")
.setDefault("func", utilsSubCommand);
misc.addArgument("--scenario-file", "-f")
.required(true)
.type(String.class)
.dest("scenario-file")
.help("Scenario to work with.");
misc.addArgument("--output", "-o")
.required(false)
.type(String.class)
.dest("output")
.help("A output file or folder depending on called method.");
String[] utilMethods = utilsSubCommand.methodsString();
misc.addArgument("-m")
.required(true)
.type(String.class)
.choices(utilMethods)
.dest("method")
.help("Method name to call.");
}
}
package org.vadere.simulator.entrypoints.cmd.commands;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.Namespace;
import org.vadere.simulator.entrypoints.ScenarioFactory;
import org.vadere.simulator.entrypoints.cmd.SubCommandRunner;
import org.vadere.simulator.models.potential.fields.IPotentialField;
import org.vadere.simulator.models.potential.fields.PotentialFieldDistancesBruteForce;
import org.vadere.simulator.models.potential.solver.EikonalSolverCacheProvider;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.utils.cache.ScenarioCache;
import org.vadere.state.attributes.models.AttributesFloorField;
import org.vadere.state.scenario.Target;
import org.vadere.state.types.CacheType;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.logging.Logger;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Set;
import java.util.stream.Collectors;
public class UtilsSubCommand implements SubCommandRunner {
private final static Logger logger = Logger.getLogger(UtilsSubCommand.class);
private HashMap<String, SubCommandRunner> methods;
public UtilsSubCommand() {
methods = new HashMap<>();
methods.put("getHash", this::getHash);
methods.put("binCache", this::calculateBinCache);
methods.put("txtCache", this::calculateTextCache);
}
public String[] methodsString(){
Set<String> mSet = methods.keySet();
String[] ret = new String[mSet.size()];
mSet.toArray(ret);
return ret;
}
private Scenario createScenario(String path) throws IOException {
Scenario scenario = null;
try {
scenario = ScenarioFactory.createScenarioWithScenarioFilePath(Paths.get(path));
} catch (IOException e) {
logger.errorf("cannot read scenario from %s", Paths.get(path).toAbsolutePath().toString());
System.exit(-1);
}
return scenario;
}
@Override
public void run(Namespace ns, ArgumentParser parser) throws Exception {
String m = ns.get("method");
SubCommandRunner runner = methods.get(m);
if (runner != null){
runner.run(ns, parser);
} else {
logger.errorf("no method found with name %s", m );
System.exit(-1);
}
}
/**
* Return the hash used to check if a new cache value needed to be calculated.
*/
private void getHash(Namespace ns, ArgumentParser parser) throws Exception{
Scenario scenario = createScenario(ns.getString("scenario-file"));
System.out.println(ScenarioCache.getHash(scenario));
}
private void calculateBinCache(Namespace ns, ArgumentParser parser) throws Exception{
calculateCache(ns, parser, CacheType.BIN_CACHE);
}
private void calculateTextCache(Namespace ns, ArgumentParser parser) throws Exception{
calculateCache(ns, parser, CacheType.TXT_CACHE);
}
/**
* Recalculated cache and save to given location. This method does not lookup any preexisting
* cache files anywhere on the system. Only existing files in the output folder will be checked.
*
* To ensure recalculation use a clean output directory
*/
private void calculateCache(Namespace ns, ArgumentParser parser, CacheType cacheType) throws Exception{
Scenario scenario = createScenario(ns.getString("scenario-file"));
if (ns.getString("output") == null){
logger.errorf("need output folder for this method");
System.exit(-1);
}
Path out = Paths.get(ns.getString("output"));
if (out.toFile().isFile()){
logger.errorf("given output is an existing file. This method needs a directory.");
System.exit(-1);
}
logger.infof("Scenario: %s with hash: %s", scenario.getName(), ScenarioCache.getHash(scenario));
AttributesFloorField attFF = scenario.getModelAttributes()
.stream()
.filter(a -> a instanceof AttributesFloorField)
.map(a ->(AttributesFloorField)a)
.findFirst().orElse(null);
if (attFF == null){
logger.errorf("Given scenario has no floor field attributes");
System.exit(-1);
}
// override cache location and type with given absolute values.
attFF.setCacheDir(out.toAbsolutePath().toString());
attFF.setCacheType(cacheType);
logger.infof("write Distance cache");
ScenarioCache cache = ScenarioCache.load(scenario, out.toAbsolutePath());
IPotentialField distanceField = new PotentialFieldDistancesBruteForce(
scenario.getTopography().getObstacles().stream().map(obs -> obs.getShape()).collect(Collectors.toList()),
new VRectangle(scenario.getTopography().getBounds()),
new AttributesFloorField(), cache);
EikonalSolverCacheProvider provider = new EikonalSolverCacheProvider(cache);
for (Target target : scenario.getTopography().getTargets()) {
logger.infof("write cache for target %s", target.getId());
provider.provide(scenario.getTopography()
, target.getId()
, scenario.getTopography().getTargetShapes().get(target.getId())
, scenario.getTopography().getAttributesPedestrian()
, attFF);
}
}
}
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