Commit 8a93ee50 authored by Hengwen Zhang's avatar Hengwen Zhang
Browse files

Add a builder class for simulator.

parent 5dec645d
Pipeline #349804 failed with stage
in 28 seconds
package de.rwth.montisim.simulation.simulator;
import de.rwth.montisim.commons.map.Pathfinding;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.SerializationException;
import de.rwth.montisim.simulation.eesimulator.message.MessageTypeManager;
import de.rwth.montisim.simulation.environment.world.World;
import java.util.Objects;
public class SimulatorBuilder {
boolean fromJson = false;
SimulationConfig config;
String jsonConfig;
World world;
Pathfinding pathfinding;
MessageTypeManager mtManager;
public static SimulatorBuilder fromJsonConfig(String jsonConfig) {
SimulatorBuilder builder = new SimulatorBuilder();
builder.fromJson = true;
builder.jsonConfig = jsonConfig;
return builder;
}
public static SimulatorBuilder withDefaultConfig() {
SimulatorBuilder bd = new SimulatorBuilder();
bd.config = new SimulationConfig();
return bd;
}
public Simulator build() throws SerializationException {
if (fromJson){
Objects.requireNonNull(jsonConfig);
config = Json.instantiateFromJson(jsonConfig, SimulationConfig.class);
fromJson = false;
return build();
}
// throw runtime exception if there are anything missing
Objects.requireNonNull(config);
Objects.requireNonNull(world);
Objects.requireNonNull(pathfinding);
Objects.requireNonNull(mtManager);
return new Simulator(config, world, pathfinding, mtManager);
}
public SimulatorBuilder setConfig(SimulationConfig config) {
this.config = config;
return this;
}
public SimulatorBuilder setWorld(World world) {
this.world = world;
return this;
}
public SimulatorBuilder setPathfinding(Pathfinding pathfinding) {
this.pathfinding = pathfinding;
return this;
}
public SimulatorBuilder setMtManager(MessageTypeManager mtManager) {
this.mtManager = mtManager;
return this;
}
}
package de.rwth.montisim.simulation.simulator;
import de.rwth.montisim.commons.map.Pathfinding;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.simulation.eesimulator.message.MessageTypeManager;
import de.rwth.montisim.simulation.environment.osmmap.OsmMap;
import de.rwth.montisim.simulation.environment.osmmap.OsmToWorldLoader;
import de.rwth.montisim.simulation.environment.pathfinding.PathfindingImpl;
import de.rwth.montisim.simulation.environment.world.World;
import junit.framework.TestCase;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class SimulatorBuilderTest extends TestCase {
public void testJson() throws Exception {
World world = new OsmToWorldLoader(new OsmMap("aachen", new File("src/test/resources/aachen.osm"))).getWorld();
Pathfinding pathfinding = new PathfindingImpl(world);
MessageTypeManager mtManager = new MessageTypeManager();
String configStr = new String(Files.readAllBytes(Paths.get("src/test/resources/simmulation_config.json")));
SimulationConfig config = Json.instantiateFromJson(configStr, SimulationConfig.class);
Simulator simulator = SimulatorBuilder
.fromJsonConfig(configStr)
.setWorld(world)
.setPathfinding(pathfinding)
.setMtManager(mtManager)
.build();
simulator.config.equals(config);
}
public void testWithDefaultConfig() throws Exception {
World world = new OsmToWorldLoader(new OsmMap("aachen", new File("src/test/resources/aachen.osm"))).getWorld();
Pathfinding pathfinding = new PathfindingImpl(world);
MessageTypeManager mtManager = new MessageTypeManager();
Simulator simulator = SimulatorBuilder
.withDefaultConfig()
.setWorld(world)
.setPathfinding(pathfinding)
.setMtManager(mtManager)
.build();
simulator.world.equals(world);
simulator.pathfinding.equals(pathfinding);
simulator.mtManager.equals(mtManager);
simulator.config.equals(new SimulationConfig());
}
}
\ No newline at end of file
{"max_duration":[60,0],"tick_duration":[0,10000000],"start_time":[1603901018,875625900],"cars":[]}
\ No newline at end of file
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