Browse Source

code optimization

lookup-tables
Stephan Richter 4 years ago
parent
commit
853ae2a0c1
  1. 8
      doc/routing/routing.plantuml
  2. 2
      pom.xml
  3. 22
      resources/logback.xml
  4. 3
      src/main/java/de/srsoftware/web4rail/BaseClass.java
  5. 2
      src/main/java/de/srsoftware/web4rail/Constants.java
  6. 154
      src/main/java/de/srsoftware/web4rail/Route.java
  7. 2
      src/main/java/de/srsoftware/web4rail/actions/ActionList.java
  8. 9
      src/main/java/de/srsoftware/web4rail/actions/AddDestination.java
  9. 2
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
  10. 76
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  11. 17
      src/main/java/de/srsoftware/web4rail/tiles/Contact.java
  12. 2
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

8
doc/routing/routing.plantuml

@ -14,14 +14,8 @@ FREE --> LOCKED : lockIngoring(ignoredRoute)
LOCKED --> PREPARED : prepare() LOCKED --> PREPARED : prepare()
PREPARED --> STARTED : start(newTrain) PREPARED --> STARTED : start(newTrain)
STARTED --> FREE : reset() STARTED --> FREE : reset()
STARTED -left-> FREE : finish()
PREPARED --> FREE : reset() PREPARED --> FREE : reset()
LOCKED --> FREE : reset() LOCKED --> FREE : reset()
@enduml @enduml
@startuml
start
:HelloWorld;
end
@enduml

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId> <groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId> <artifactId>web4rail</artifactId>
<version>1.3.38</version> <version>1.3.39</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>

22
resources/logback.xml

@ -1,8 +1,11 @@
<configuration scan="true" scanPeriod="60 seconds" debug="true"> <configuration scan="true" scanPeriod="60 seconds"
debug="true">
<!-- scan="true" enables automatic updates if config file changes, see http://logback.qos.ch/manual/configuration.html --> <!-- scan="true" enables automatic updates if config file changes, see http://logback.qos.ch/manual/configuration.html -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder> <encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder> </encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level> <level>DEBUG</level>
@ -16,12 +19,21 @@
<level>DEBUG</level> <level>DEBUG</level>
</filter> </filter>
<encoder> <encoder>
<pattern>%d{HH:mm:ss.SSS} [%15thread] %-5level %logger - %msg%n</pattern> <pattern>%d{HH:mm:ss.SSS} [%15thread] %-5level %logger - %msg%n
</pattern>
</encoder> </encoder>
</appender> </appender>
<root> <root level="DEBUG">
<appender-ref ref="FILE" /> <appender-ref ref="FILE" />
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>
<logger name="de.srsoftware.web4rail" level="INFO" />
<logger name="de.srsoftware.web4rail.Application" level="DEBUG" />
<logger name="de.srsoftware.web4rail.Route" level="DEBUG" />
<logger name="de.srsoftware.web4rail.actions.ActionList" level="DEBUG" />
<logger name="de.srsoftware.web4rail.moving.Train" level="DEBUG" />
<logger name="de.srsoftware.web4rail.tiles.Contact" level="DEBUG" />
</configuration> </configuration>

3
src/main/java/de/srsoftware/web4rail/BaseClass.java

@ -125,8 +125,9 @@ public abstract class BaseClass implements Constants{
return contact; return contact;
} }
public void contact(Contact newContact) { public Context contact(Contact newContact) {
contact = newContact; contact = newContact;
return this;
} }

2
src/main/java/de/srsoftware/web4rail/Constants.java

@ -52,7 +52,7 @@ public interface Constants {
public static final String ASSIGN = "assign"; public static final String ASSIGN = "assign";
public static final String BLOCK = "block"; public static final String BLOCK = "block";
public static final String COL = ":&nbsp;"; public static final String COL = ": ";
public static final String CONTACT = "contact"; public static final String CONTACT = "contact";
public static final String CONTEXT = "context"; public static final String CONTEXT = "context";
public static final String DEFAULT_SPEED_UNIT = "km/h"; public static final String DEFAULT_SPEED_UNIT = "km/h";

154
src/main/java/de/srsoftware/web4rail/Route.java

@ -77,11 +77,6 @@ public class Route extends BaseClass {
private static final String ROUTE_SETUP = "route_setup"; private static final String ROUTE_SETUP = "route_setup";
public static final String DESTINATION_PREFIX = "@";
public static final char TURN_FLAG = '±';
public static final char FLAG_SEPARATOR = '+';
public static final char SHUNTING_FLAG = '¥';
private int startSpeed; private int startSpeed;
private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze() private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze()
@ -137,6 +132,7 @@ public class Route extends BaseClass {
train.setSpeed(0); train.setSpeed(0);
LOG.debug("Estimated distance: {}",estimatedDistance); LOG.debug("Estimated distance: {}",estimatedDistance);
if (startSpeed <= endSpeed) return;
Integer newTimeStep = timeStep; Integer newTimeStep = timeStep;
while (calcDistance(newTimeStep) < estimatedDistance) { // zu schnell gebremst while (calcDistance(newTimeStep) < estimatedDistance) { // zu schnell gebremst
newTimeStep += (1+newTimeStep/8); newTimeStep += (1+newTimeStep/8);
@ -423,12 +419,11 @@ public class Route extends BaseClass {
if (triggeredContacts.contains(contact)) return; // don't trigger contact a second time if (triggeredContacts.contains(contact)) return; // don't trigger contact a second time
triggeredContacts.add(contact); triggeredContacts.add(contact);
LOG.debug("{} on {} activated {}.",train,this,contact); LOG.debug("{} on {} activated {}.",train,this,contact);
traceTrainFrom(contact);
ActionList actions = triggeredActions.get(contact.trigger()); ActionList actions = triggeredActions.get(contact.trigger());
LOG.debug("Contact has id {} / trigger {} and is assigned with {}",contact.id(),contact.trigger(),isNull(actions)?t("nothing"):actions); LOG.debug("Contact has id {} / trigger {} and is assigned with {}",contact.id(),contact.trigger(),isNull(actions)?t("nothing"):actions);
if (isNull(actions)) return; if (isNull(actions)) return;
context.contact(contact);
actions.fire(context); actions.fire(context);
traceTrainFrom(contact);
} }
public Vector<Contact> contacts() { public Vector<Contact> contacts() {
@ -482,6 +477,7 @@ public class Route extends BaseClass {
public void finish() { public void finish() {
LOG.debug("{}.finish()",this); LOG.debug("{}.finish()",this);
if (isSet(train)) { if (isSet(train)) {
if (train.nextRoutePrepared()) { if (train.nextRoutePrepared()) {
LOG.debug("{} has prepared next route: {}",train,train.nextRoute()); LOG.debug("{} has prepared next route: {}",train,train.nextRoute());
@ -495,72 +491,27 @@ public class Route extends BaseClass {
} }
brakeProcessor = null; brakeProcessor = null;
context.clear(); // prevent delayed actions from firing after route has finished free();
setSignals(Signal.RED);
for (Tile tile : path) try { // remove route from tiles on path
tile.unset(this);
} catch (IllegalArgumentException e) {}
Tile lastTile = path.lastElement();
if (lastTile instanceof Contact) {
lastTile.setTrain(null);
if (isSet(train)) train.removeChild(lastTile);
}
if (isSet(train)) { if (isSet(train)) {
train.set(endBlock); moveTrainToEndBlock();
train.heading(endDirection); if (train.route() == this) train.route(null);
if (endBlock == train.destination()) { train = null;
String destTag = null;
for (String tag : train.tags()) {
if (tag.startsWith(DESTINATION_PREFIX)) {
destTag = tag;
break;
}
} }
train.destination(null);
if (isSet(destTag)) {
String[] parts = destTag.split("@");
String destId = parts[1];
boolean turn = false;
for (int i=destId.length()-1; i>0; i--) { state = State.FREE;
switch (destId.charAt(i)) {
case FLAG_SEPARATOR:
destId = destId.substring(0,i);
i=0;
break;
case TURN_FLAG:
turn = true;
break;
}
}
if (destId.equals(endBlock.id().toString())) {
if (turn) train.turn();
train.removeTag(destTag);
destTag = destTag.substring(parts[1].length()+1);
if (destTag.isEmpty()) { // no further destinations
destTag = null;
} else train.addTag(destTag);
}
} }
if (isNull(destTag)) { /**
train.quitAutopilot(); * sets all signals of this route to RED,
plan.stream(t("{} reached it`s destination!",train)); * frees all tiles occupied by this route
} */
} else { private void free() {
train.setWaitTime(endBlock.getWaitTime(train,train.direction())); LOG.debug("{}.reset()",this);
} context.clear(); // prevent delayed actions from firing after route has finished
if (train.route() == this) train.route(null);
if (startBlock.train() == train && !train.onTrace(startBlock)) startBlock.setTrain(null); // withdraw train from start block only if trace does not go back there
}
train = null;
}
public boolean free() {
LOG.debug("{}.free()",this);
setSignals(Signal.RED); setSignals(Signal.RED);
for (Tile tile : path) try { for (Tile tile : path) try { // remove route from tiles on path
tile.unset(this); tile.unset(this);
} catch (IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
@ -569,14 +520,6 @@ public class Route extends BaseClass {
lastTile.setTrain(null); lastTile.setTrain(null);
if (isSet(train)) train.removeChild(lastTile); if (isSet(train)) train.removeChild(lastTile);
} }
if (isSet(train)) {
train.set(startBlock);
train.heading(startDirection);
if (train.route() == this) train.route(null);
train = null;
}
state = State.FREE;
return true;
} }
private String generateName() { private String generateName() {
@ -816,11 +759,12 @@ public class Route extends BaseClass {
} }
public boolean lock() { public boolean lock() {
LOG.debug("{}.lock({})",this); LOG.debug("{}.lock()",this);
return lockIgnoring(null); return lockIgnoring(null);
} }
public boolean lockIgnoring(Route ignoredRoute) { public boolean lockIgnoring(Route ignoredRoute) {
LOG.debug("{}.lockIgnoring({})",this,ignoredRoute);
HashSet<Tile> ignoredPath = new HashSet<Tile>(); HashSet<Tile> ignoredPath = new HashSet<Tile>();
if (isSet(ignoredRoute)) ignoredPath.addAll(ignoredRoute.path); if (isSet(ignoredRoute)) ignoredPath.addAll(ignoredRoute.path);
boolean success = true; boolean success = true;
@ -838,6 +782,51 @@ public class Route extends BaseClass {
return success; return success;
} }
private void moveTrainToEndBlock() {
if (isNull(train)) return;
train.set(endBlock);
train.heading(endDirection);
if (endBlock == train.destination()) {
train.destination(null); // unset old destination
String destTag = train.destinationTag();
if (isSet(destTag)) {
String[] parts = destTag.split(Train.DESTINATION_PREFIX);
String destId = parts[1];
boolean turn = false;
for (int i=destId.length()-1; i>0; i--) {
switch (destId.charAt(i)) {
case Train.FLAG_SEPARATOR:
destId = destId.substring(0,i);
i=0;
break;
case Train.TURN_FLAG:
turn = true;
break;
}
}
if (destId.equals(endBlock.id().toString())) {
if (turn) train.turn();
// update destination tag: remove and add altered tag:
train.removeTag(destTag);
destTag = destTag.substring(parts[1].length()+1);
if (destTag.isEmpty()) { // no further destinations
destTag = null;
} else train.addTag(destTag);
}
}
if (isNull(destTag)) {
train.quitAutopilot();
plan.stream(t("{} reached it`s destination!",train));
}
} else train.setWaitTime(endBlock.getWaitTime(train,train.direction()));
if (startBlock.train() == train && !train.onTrace(startBlock)) startBlock.setTrain(null); // withdraw train from start block only if trace does not go back there
}
public List<Route> multiply(int size) { public List<Route> multiply(int size) {
Vector<Route> routes = new Vector<Route>(); Vector<Route> routes = new Vector<Route>();
for (int i=0; i<size; i++) routes.add(i==0 ? this : this.clone()); for (int i=0; i<size; i++) routes.add(i==0 ? this : this.clone());
@ -866,7 +855,7 @@ public class Route extends BaseClass {
} }
public boolean prepare() { public boolean prepare() {
LOG.debug("{}.firesSetupActions({})",this); LOG.debug("{}.prepare()",this);
ActionList setupActions = triggeredActions.get(ROUTE_SETUP); ActionList setupActions = triggeredActions.get(ROUTE_SETUP);
if (isSet(setupActions) && !setupActions.fire(context)) return false; if (isSet(setupActions) && !setupActions.fire(context)) return false;
state = State.PREPARED; state = State.PREPARED;
@ -939,6 +928,20 @@ public class Route extends BaseClass {
super.removeChild(child); super.removeChild(child);
} }
public boolean reset() {
LOG.debug("{}.reset()",this);
free();
if (isSet(train)) {
train.set(startBlock);
train.heading(startDirection);
if (train.route() == this) train.route(null);
train = null;
}
state = State.FREE;
return true;
}
public static void saveAll(String filename) throws IOException { public static void saveAll(String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename)); BufferedWriter file = new BufferedWriter(new FileWriter(filename));
file.write("{\""+ROUTES+"\":[\n"); file.write("{\""+ROUTES+"\":[\n");
@ -954,6 +957,7 @@ public class Route extends BaseClass {
} }
public Context set(Context newContext) { public Context set(Context newContext) {
LOG.debug("{}.set({})",this,newContext);
context = newContext; context = newContext;
context.route(this); context.route(this);
return context; return context;

2
src/main/java/de/srsoftware/web4rail/actions/ActionList.java

@ -263,6 +263,6 @@ public class ActionList extends Action implements Iterable<Action>{
@Override @Override
public String toString() { public String toString() {
return actions.toString(); return actions.isEmpty() ? "[no actions]" : actions.toString();
} }
} }

9
src/main/java/de/srsoftware/web4rail/actions/AddDestination.java

@ -10,7 +10,6 @@ import org.json.JSONObject;
import de.srsoftware.tools.Tag; import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application; import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass; import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.moving.Train; import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Checkbox; import de.srsoftware.web4rail.tags.Checkbox;
import de.srsoftware.web4rail.tags.Fieldset; import de.srsoftware.web4rail.tags.Fieldset;
@ -43,11 +42,11 @@ public class AddDestination extends Action {
return true; return true;
} }
String flags = "+"; String flags = "+";
if (turnAtDestination) flags += Route.TURN_FLAG; if (turnAtDestination) flags += Train.TURN_FLAG;
if (shunting) flags += Route.SHUNTING_FLAG; if (shunting) flags += Train.SHUNTING_FLAG;
String dest = Route.DESTINATION_PREFIX+destination.id() + (flags.length()>1 ? flags : ""); String dest = Train.DESTINATION_PREFIX+destination.id() + (flags.length()>1 ? flags : "");
for (String tag: train.tags()) { for (String tag: train.tags()) {
if (tag.startsWith(Route.DESTINATION_PREFIX)) { if (tag.startsWith(Train.DESTINATION_PREFIX)) {
train.removeTag(tag); train.removeTag(tag);
dest = tag+dest; dest = tag+dest;
break; break;

2
src/main/java/de/srsoftware/web4rail/moving/Locomotive.java

@ -143,7 +143,7 @@ public class Locomotive extends Car implements Constants,Device{
par.addTo(fieldset); par.addTo(fieldset);
Tag direction = new Tag("p"); Tag direction = new Tag("p");
if ((isSet(train) && train.speed > 0) || (isSet(loco) && loco.speed > 0)) { if ((isSet(train) && (train.speed > 0 || isSet(train.route()))) || (isSet(loco) && loco.speed > 0)) {
params.put(ACTION, ACTION_STOP); params.put(ACTION, ACTION_STOP);
new Button(t("Stop"),params).clazz(ACTION_STOP).addTo(direction); new Button(t("Stop"),params).clazz(ACTION_STOP).addTo(direction);
} }

76
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -75,6 +75,10 @@ public class Train extends BaseClass implements Comparable<Train> {
public static final String DESTINATION = "destination"; public static final String DESTINATION = "destination";
private static final String ACTION_REVERSE = "reverse"; private static final String ACTION_REVERSE = "reverse";
public static final String DESTINATION_PREFIX = "@";
public static final char TURN_FLAG = '±';
public static final char FLAG_SEPARATOR = '+';
public static final char SHUNTING_FLAG = '¥';
private HashSet<String> tags = new HashSet<String>(); private HashSet<String> tags = new HashSet<String>();
private boolean f1,f2,f3,f4; private boolean f1,f2,f3,f4;
@ -105,7 +109,7 @@ public class Train extends BaseClass implements Comparable<Train> {
if (stop) return; if (stop) return;
if (isNull(route)) { // may have been set by start action in between if (isNull(route)) { // may have been set by start action in between
Object o = Train.this.start(); Object o = Train.this.start();
LOG.debug("{}.start called, route now is {}",this,route); LOG.debug("{}.start called, route now is {}",Train.this,route);
if (isSet(route)) { if (isSet(route)) {
if (o instanceof String) plan.stream((String)o); if (o instanceof String) plan.stream((String)o);
//if (isSet(destination)) Thread.sleep(1000); // limit load on PathFinder //if (isSet(destination)) Thread.sleep(1000); // limit load on PathFinder
@ -162,17 +166,18 @@ public class Train extends BaseClass implements Comparable<Train> {
case ACTION_QUIT: case ACTION_QUIT:
return train.quitAutopilot(); return train.quitAutopilot();
case ACTION_REVERSE: case ACTION_REVERSE:
return train.reverse(); return train.reverse().properties();
case ACTION_SLOWER10: case ACTION_SLOWER10:
return train.slower(10); return train.slower(10);
case ACTION_START: case ACTION_START:
return train.start(); train.start();
return train.properties();
case ACTION_STOP: case ACTION_STOP:
return train.stopNow(); return train.stopNow();
case ACTION_TIMES: case ACTION_TIMES:
return train.removeBrakeTimes(); return train.removeBrakeTimes();
case ACTION_TURN: case ACTION_TURN:
return train.turn(); return train.turn().properties();
case ACTION_UPDATE: case ACTION_UPDATE:
return train.update(params); return train.update(params);
} }
@ -247,7 +252,7 @@ public class Train extends BaseClass implements Comparable<Train> {
TreeSet<String> carIds = new TreeSet<String>(); TreeSet<String> carIds = new TreeSet<String>();
cars.stream().map(car -> car.id()+":"+(car.orientation == reversed ? "r":"f")).forEach(carIds::add); cars.stream().map(car -> car.id()+":"+(car.orientation == reversed ? "r":"f")).forEach(carIds::add);
String brakeId = md5sum(carIds); String brakeId = md5sum(carIds);
LOG.debug("generated new brake id for {}: {}",this,brakeId); LOG.debug("generated new {} brake id for {}: {}",reversed?"backward":"forward",this,brakeId);
return brakeId; return brakeId;
} }
@ -395,31 +400,22 @@ public class Train extends BaseClass implements Comparable<Train> {
public Block destination() { public Block destination() {
if (isNull(destination)) { if (isNull(destination)) {
String destId = null; String destTag = destinationTag();
for (String tag : tags) { if (isSet(destTag)) {
if (tag.startsWith(Route.DESTINATION_PREFIX)) { for (int i=destTag.length()-1; i>0; i--) {
destId = tag; switch (destTag.charAt(i)) {
break; case FLAG_SEPARATOR:
} destTag = destTag.substring(0,i);
}
if (isSet(destId)) {
String[] parts = destId.split(Route.DESTINATION_PREFIX);
destId = parts[1];
for (int i=destId.length()-1; i>0; i--) {
switch (destId.charAt(i)) {
case Route.FLAG_SEPARATOR:
destId = destId.substring(0,i);
i=0; i=0;
break; break;
case Route.SHUNTING_FLAG: case SHUNTING_FLAG:
shunting = true; shunting = true;
break; break;
} }
} }
BaseClass object = BaseClass.get(new Id(destId)); Block block = BaseClass.get(new Id(destTag));
if (object instanceof Block) destination = (Block) object; if (isSet(block)) destination = block;
} }
} }
return destination; return destination;
@ -430,6 +426,17 @@ public class Train extends BaseClass implements Comparable<Train> {
return this; return this;
} }
public String destinationTag() {
for (String tag : tags()) { // check, if endBlock is in train's destinations
if (tag.startsWith(DESTINATION_PREFIX)) {
String[] parts = tag.split(DESTINATION_PREFIX);
return parts[1];
}
}
return null;
}
public String directedName() { public String directedName() {
String result = name(); String result = name();
String mark = isSet(autopilot) ? "ⓐ" : ""; String mark = isSet(autopilot) ? "ⓐ" : "";
@ -730,7 +737,7 @@ public class Train extends BaseClass implements Comparable<Train> {
public Object quitAutopilot() { public Object quitAutopilot() {
if (isSet(nextRoute)) { if (isSet(nextRoute)) {
nextRoute.free(); nextRoute.reset();
nextRoute = null; nextRoute = null;
} }
if (isSet(autopilot)) { if (isSet(autopilot)) {
@ -786,7 +793,7 @@ public class Train extends BaseClass implements Comparable<Train> {
error = error || !nextRoute.prepare(); error = error || !nextRoute.prepare();
if (error) { if (error) {
nextRoute.free(); // may unlock tiles belonging to the current route. nextRoute.reset(); // may unlock tiles belonging to the current route.
route.lock(); // corrects unlocked tiles of nextRoute route.lock(); // corrects unlocked tiles of nextRoute
} else { } else {
this.nextRoute = nextRoute; this.nextRoute = nextRoute;
@ -799,7 +806,7 @@ public class Train extends BaseClass implements Comparable<Train> {
* before: CabCar MiddleCar Loco * before: CabCar MiddleCar Loco
* after: Loco MiddleCar CabCar * after: Loco MiddleCar CabCar
*/ */
public Tag reverse() { public Train reverse() {
LOG.debug("train.reverse();"); LOG.debug("train.reverse();");
if (isSet(direction)) { if (isSet(direction)) {
@ -807,7 +814,7 @@ public class Train extends BaseClass implements Comparable<Train> {
reverseTrace(); reverseTrace();
} }
if (isSet(currentBlock)) plan.place(currentBlock); if (isSet(currentBlock)) plan.place(currentBlock);
return properties(); return this;
} }
private void reverseTrace() { private void reverseTrace() {
@ -981,7 +988,7 @@ public class Train extends BaseClass implements Comparable<Train> {
LOG.debug("{}.start()",this); LOG.debug("{}.start()",this);
if (isNull(currentBlock)) return t("{} not in a block",this); if (isNull(currentBlock)) return t("{} not in a block",this);
if (maxSpeed() == 0) return t("Train has maximum speed of 0 {}, cannot go!",speedUnit); if (maxSpeed() == 0) return t("Train has maximum speed of 0 {}, cannot go!",speedUnit);
if (isSet(route)) route.free(); // reset route previously chosen if (isSet(route)) route.reset(); // reset route previously chosen
String error = null; String error = null;
if (isSet(nextRoute)) { if (isSet(nextRoute)) {
@ -1003,14 +1010,13 @@ public class Train extends BaseClass implements Comparable<Train> {
if (isNull(error) && !route.start(this)) error = t("Was not able to assign {} to {}!",this,route); if (isNull(error) && !route.start(this)) error = t("Was not able to assign {} to {}!",this,route);
if (isSet(error)) { if (isSet(error)) {
LOG.debug("{}.start:error = {}",this,error); LOG.debug("{}.start:error = {}",this,error);
route.free(); route.reset();
route = null; route = null;
return error; return error;
} }
startSimulation(); startSimulation();
Window win = properties(); plan.stream(t("Started {}",this));
new Tag("p").content(t("Started {}",this)).addTo(win); return this;
return win;
} }
public static void startAll() { public static void startAll() {
@ -1019,7 +1025,7 @@ public class Train extends BaseClass implements Comparable<Train> {
} }
private void startSimulation() { private void startSimulation() {
LOG.debug("{}.startSimulation({})",this); LOG.debug("{}.startSimulation()",this);
for (Contact contact : route.contacts()) { for (Contact contact : route.contacts()) {
if (contact.addr() != 0) { if (contact.addr() != 0) {
LOG.debug("{}.startSimulation aborted!",this); LOG.debug("{}.startSimulation aborted!",this);
@ -1056,7 +1062,7 @@ public class Train extends BaseClass implements Comparable<Train> {
quitAutopilot(); quitAutopilot();
if (isSet(route)) { if (isSet(route)) {
route.brakeCancel(); route.brakeCancel();
route.free(); route.reset();
route = null; route = null;
} }
setSpeed(0); setSpeed(0);
@ -1096,7 +1102,7 @@ public class Train extends BaseClass implements Comparable<Train> {
* after: CabCar MiddleCar Loco * after: CabCar MiddleCar Loco
* @return * @return
*/ */
public Tag turn() { public Train turn() {
LOG.debug("{}.turn()",this); LOG.debug("{}.turn()",this);
for (Car car : cars) car.turn(); for (Car car : cars) car.turn();
Collections.reverse(cars); Collections.reverse(cars);

17
src/main/java/de/srsoftware/web4rail/tiles/Contact.java

@ -10,6 +10,8 @@ import java.util.TreeMap;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.srsoftware.tools.Tag; import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application; import de.srsoftware.web4rail.Application;
@ -23,7 +25,7 @@ import de.srsoftware.web4rail.tags.Select;
import de.srsoftware.web4rail.tags.Window; import de.srsoftware.web4rail.tags.Window;
public class Contact extends Tile{ public class Contact extends Tile{
private static Logger LOG = LoggerFactory.getLogger(Contact.class);
private static final String ADDRESS = "address"; private static final String ADDRESS = "address";
private static final HashMap<Integer, Contact> contactsByAddr = new HashMap<Integer, Contact>(); private static final HashMap<Integer, Contact> contactsByAddr = new HashMap<Integer, Contact>();
private boolean state = false; private boolean state = false;
@ -81,17 +83,12 @@ public class Contact extends Tile{
LOG.debug("{} activated.",this); LOG.debug("{} activated.",this);
state = true; state = true;
if (isSet(timer)) timer.abort(); if (isSet(timer)) timer.abort();
Context context = null;
Route route = route(); Route route = route();
if (isSet(route)) { Context context = isSet(route) ? route.context().contact(this) : new Context(this);
context = route.context();
actions.fire(context);
route.contact(this);
}
if (isNull(context)) {
context = new Context(this);
actions.fire(context); actions.fire(context);
} if (isSet(route)) route.contact(this);
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.fired(); listener.fired();
} }

2
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -378,7 +378,7 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
} }
public Tile setRoute(Route lockingRoute) { public Tile setRoute(Route lockingRoute) {
Route.LOG.debug("{}.setRoute({})",this,lockingRoute); LOG.debug("{}.setRoute({})",this,lockingRoute);
if (isNull(lockingRoute)) throw new NullPointerException(); if (isNull(lockingRoute)) throw new NullPointerException();
if (isSet(route)) { if (isSet(route)) {
if (route == lockingRoute) return this; // nothing changed if (route == lockingRoute) return this; // nothing changed

Loading…
Cancel
Save