Browse Source

implemented shunting trains

lookup-tables
Stephan Richter 5 years ago
parent
commit
22dfd0512a
  1. 2
      pom.xml
  2. 4
      resources/translations/Application.de.translation
  3. 11
      src/main/java/de/srsoftware/web4rail/PathFinder.java
  4. 7
      src/main/java/de/srsoftware/web4rail/Route.java
  5. 2
      src/main/java/de/srsoftware/web4rail/actions/DetermineTrainInBlock.java
  6. 1
      src/main/java/de/srsoftware/web4rail/conditions/Condition.java
  7. 14
      src/main/java/de/srsoftware/web4rail/conditions/TrainIsShunting.java
  8. 2
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
  9. 39
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  10. 13
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

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.19</version> <version>1.3.20</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>

4
resources/translations/Application.de.translation

@ -262,6 +262,7 @@ Set speed to : Geschwindigkeit setzen
SetTurnout : Weiche stellen SetTurnout : Weiche stellen
Setup actions : Vorbereitung-Aktionen Setup actions : Vorbereitung-Aktionen
ShowText : Text anzeigen ShowText : Text anzeigen
Shunting : Rangieren
Signals : Signale Signals : Signale
simplify name : Name vereinfachen simplify name : Name vereinfachen
Simulating movement of {}... : Simuliere Fahrt von {}... Simulating movement of {}... : Simuliere Fahrt von {}...
@ -307,7 +308,10 @@ train is a push-pull train : Zug ist ein Wendezug
train is faster than {} {} : Zug ist schneller als {} {} train is faster than {} {} : Zug ist schneller als {} {}
train is longer than {} {} : Zug ist länger als {} {} train is longer than {} {} : Zug ist länger als {} {}
train is not a push-pull train : Zug ist kein Wendezug train is not a push-pull train : Zug ist kein Wendezug
train is not shunting : Zug rangiert nicht
train is shorter than {} {} : Zug ist kürzer als {} {} train is shorter than {} {} : Zug ist kürzer als {} {}
TrainIsShunting : Zug rangiert
train is shunting : Zug rangiert
train is slower than {} {} : Zug ist langsamer als {} {} train is slower than {} {} : Zug ist langsamer als {} {}
TrainLength : Zug-Länge TrainLength : Zug-Länge
Train manager : Zug-Verwaltung Train manager : Zug-Verwaltung

11
src/main/java/de/srsoftware/web4rail/PathFinder.java

@ -17,11 +17,12 @@ public class PathFinder extends BaseClass{
public static final Logger LOG = LoggerFactory.getLogger(PathFinder.class); public static final Logger LOG = LoggerFactory.getLogger(PathFinder.class);
private static TreeMap<Integer,List<Route>> availableRoutes(Context context,HashSet<Route> visitedRoutes){ private static TreeMap<Integer,List<Route>> availableRoutes(Context context,HashSet<Route> visitedRoutes){
LOG.debug("PathFinder.availableRoutes({})",context);
TreeMap<Integer,List<Route>> availableRoutes = new TreeMap<Integer, List<Route>>();
String inset = ""; String inset = "";
for (int i=0; i<visitedRoutes.size(); i++) inset+=" "; for (int i=0; i<visitedRoutes.size(); i++) inset+=" ";
LOG.debug(inset+"PathFinder.availableRoutes({})",context);
TreeMap<Integer,List<Route>> availableRoutes = new TreeMap<Integer, List<Route>>();
boolean error = false; boolean error = false;
Block block = context.block(); Block block = context.block();
@ -38,11 +39,11 @@ public class PathFinder extends BaseClass{
Block destination = train.destination(); Block destination = train.destination();
Direction direction = context.direction(); Direction direction = context.direction();
/* if (isSet(direction)) { if (isSet(direction)) {
LOG.debug("{}Looking for {}-bound routes from {}",inset,direction,block); LOG.debug("{}Looking for {}-bound routes from {}",inset,direction,block);
} else { } else {
LOG.debug("{}Looking for all routes from {}",inset,block); LOG.debug("{}Looking for all routes from {}",inset,block);
}*/ }//*/
if (isSet(destination) && visitedRoutes.isEmpty()) LOG.debug("{}- Destination: {}",inset,destination); if (isSet(destination) && visitedRoutes.isEmpty()) LOG.debug("{}- Destination: {}",inset,destination);
Route currentRoute = context.route(); Route currentRoute = context.route();

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

@ -126,7 +126,7 @@ public class Route extends BaseClass {
public void finish() { public void finish() {
LOG.debug("BrakeProcessor.finish(){}",aborted?" got aborted":""); LOG.debug("BrakeProcessor.finish(){}",aborted?" got aborted":"");
if (aborted) return; if (aborted) return;
long runtime = BaseClass.timestamp() - timestamp; long runtime = 2+BaseClass.timestamp() - timestamp;
estimatedDistance += train.speed * runtime; estimatedDistance += train.speed * runtime;
train.setSpeed(0); train.setSpeed(0);
LOG.debug("Estimated distance: {}",estimatedDistance); LOG.debug("Estimated distance: {}",estimatedDistance);
@ -509,7 +509,7 @@ public class Route extends BaseClass {
} else { } else {
train.setWaitTime(endBlock.getWaitTime(train,train.direction())); train.setWaitTime(endBlock.getWaitTime(train,train.direction()));
} }
if (train.route == this) train.route = null; 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 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; train = null;
@ -888,7 +888,7 @@ public class Route extends BaseClass {
if (isSet(train)) { if (isSet(train)) {
train.set(startBlock); train.set(startBlock);
train.heading(startDirection); train.heading(startDirection);
if (train.route == this) train.route = null; if (train.route() == this) train.route(null);
train = null; train = null;
} }
LOG.debug("chlearing triggeredContacts of {}",this); LOG.debug("chlearing triggeredContacts of {}",this);
@ -1021,6 +1021,7 @@ public class Route extends BaseClass {
condition.parent(this); condition.parent(this);
conditions.add(condition); conditions.add(condition);
} }
super.update(params);
return properties(); return properties();
} }

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

@ -68,7 +68,7 @@ public class DetermineTrainInBlock extends Action {
} }
public String toString() { public String toString() {
return isSet(block) ? t("Determine, which train is in {}",block) : t("[Click here to select block!]"); return isSet(block) ? t("Determine, which train is in {}",block) : "["+t("Click here to select block!")+"]";
}; };
@Override @Override

1
src/main/java/de/srsoftware/web4rail/conditions/Condition.java

@ -123,6 +123,7 @@ public abstract class Condition extends BaseClass {
PushPullTrain.class, PushPullTrain.class,
RouteEndBlock.class, RouteEndBlock.class,
TrainHasTag.class, TrainHasTag.class,
TrainIsShunting.class,
TrainLength.class, TrainLength.class,
TrainSelect.class, TrainSelect.class,
TrainSpeed.class, TrainSpeed.class,

14
src/main/java/de/srsoftware/web4rail/conditions/TrainIsShunting.java

@ -0,0 +1,14 @@
package de.srsoftware.web4rail.conditions;
public class TrainIsShunting extends Condition {
@Override
public boolean fulfilledBy(Context context) {
return context.train().isShunting() != inverted;
}
@Override
public String toString() {
return t(inverted ? "train is not shunting":"train is shunting") ;
}
}

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

@ -127,7 +127,7 @@ public class Locomotive extends Car implements Constants,Device{
if (isSet(train)) { if (isSet(train)) {
Block currentBlock = train.currentBlock(); Block currentBlock = train.currentBlock();
if (isSet(currentBlock)) { if (isSet(currentBlock)) {
if (isNull(train.route)) { if (isNull(train.route())) {
params.put(ACTION, ACTION_START); params.put(ACTION, ACTION_START);
new Button(t("start"),params).addTo(direction); new Button(t("start"),params).addTo(direction);
} }

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

@ -54,7 +54,7 @@ public class Train extends BaseClass implements Comparable<Train> {
private static final String ROUTE = "route"; private static final String ROUTE = "route";
public Route route; private Route route;
private static final String DIRECTION = "direction"; private static final String DIRECTION = "direction";
private Direction direction; private Direction direction;
@ -82,6 +82,8 @@ public class Train extends BaseClass implements Comparable<Train> {
public int speed = 0; public int speed = 0;
private Autopilot autopilot = null; private Autopilot autopilot = null;
private Route nextRoute; private Route nextRoute;
private static final String SHUNTING = "shunting";
private boolean shunting = false;
private class Autopilot extends Thread{ private class Autopilot extends Thread{
@ -362,6 +364,10 @@ public class Train extends BaseClass implements Comparable<Train> {
cars.remove(car); cars.remove(car);
car.train(null); car.train(null);
} }
if (cars.isEmpty()) {
this.remove();
return t("Removed train \"{}\"",this);
}
return properties(); return properties();
} }
@ -373,6 +379,13 @@ public class Train extends BaseClass implements Comparable<Train> {
setSpeed(speed+steps); setSpeed(speed+steps);
return properties(); return properties();
} }
private boolean hasLoco() {
for (Car c:cars) {
if (c instanceof Locomotive) return true;
}
return false;
}
public Train heading(Direction dir) { public Train heading(Direction dir) {
LOG.debug("{}.heading({})",this,dir); LOG.debug("{}.heading({})",this,dir);
@ -385,6 +398,11 @@ public class Train extends BaseClass implements Comparable<Train> {
return trace.getFirst(); return trace.getFirst();
} }
public boolean isShunting() {
return shunting;
}
public JSONObject json() { public JSONObject json() {
JSONObject json = super.json(); JSONObject json = super.json();
json.put(PUSH_PULL, pushPull); json.put(PUSH_PULL, pushPull);
@ -592,11 +610,12 @@ public class Train extends BaseClass implements Comparable<Train> {
propList.addTo(otherTrainProps); propList.addTo(otherTrainProps);
formInputs.add(t("Name"), new Input(NAME,name)); formInputs.add(t("Name"), new Input(NAME,name()));
formInputs.add(t("Shunting"),new Checkbox(SHUNTING, t("train is shunting"), shunting));
formInputs.add(t("Push-pull train"),new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull)); formInputs.add(t("Push-pull train"),new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull));
formInputs.add(t("Tags"), new Input(TAGS,String.join(", ", tags))); formInputs.add(t("Tags"), new Input(TAGS,String.join(", ", tags)));
preForm.add(Locomotive.cockpit(this)); if (this.hasLoco()) preForm.add(Locomotive.cockpit(this));
postForm.add(otherTrainProps); postForm.add(otherTrainProps);
postForm.add(brakeTimes()); postForm.add(brakeTimes());
postForm.add(blockHistory()); postForm.add(blockHistory());
@ -680,6 +699,18 @@ public class Train extends BaseClass implements Comparable<Train> {
trace = reversed; trace = reversed;
LOG.debug("reversed: {}",trace); LOG.debug("reversed: {}",trace);
} }
public Route route() {
return route;
}
public Train route(Route newRoute) {
route = newRoute;
if (isNull(route)) shunting = false; // quit shunting on finish route
return this;
}
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));
@ -704,6 +735,7 @@ public class Train extends BaseClass implements Comparable<Train> {
public void set(Block newBlock) { public void set(Block newBlock) {
LOG.debug("{}.set({})",this,newBlock); LOG.debug("{}.set({})",this,newBlock);
if (isSet(currentBlock)) currentBlock.setTrain(null);
currentBlock = newBlock; currentBlock = newBlock;
if (isSet(currentBlock)) { if (isSet(currentBlock)) {
currentBlock.setTrain(this); currentBlock.setTrain(this);
@ -927,6 +959,7 @@ public class Train extends BaseClass implements Comparable<Train> {
protected Train update(HashMap<String, String> params) { protected Train update(HashMap<String, String> params) {
LOG.debug("update({})",params); LOG.debug("update({})",params);
pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on"); pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
shunting = params.containsKey(SHUNTING) && params.get(SHUNTING).equals("on");
if (params.containsKey(NAME)) name = params.get(NAME); if (params.containsKey(NAME)) name = params.get(NAME);
if (params.containsKey(TAGS)) { if (params.containsKey(TAGS)) {
String[] parts = params.get(TAGS).replace(",", " ").split(" "); String[] parts = params.get(TAGS).replace(",", " ").split(" ");

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

@ -127,16 +127,19 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
} }
if (isSet(route)) { if (isSet(route)) {
PathFinder.LOG.debug("{} is occupied by {}",this,route); PathFinder.LOG.debug("{} is occupied by {}",this,route);
return false; return false;
} }
} }
if (isSet(train)) { if (isSet(train)) {
boolean free = train == context.train(); // during train.reserveNext, we may encounter, parts, that are already reserved by the respective train, but having another route. do not compare routes in that case! Train contextTrain = context.train();
boolean free = train == contextTrain; // during train.reserveNext, we may encounter, parts, that are already reserved by the respective train, but having another route. do not compare routes in that case!
if (free) { if (free) {
PathFinder.LOG.debug("already reserved by {} → true",train); PathFinder.LOG.debug("already reserved by {} → true",train);
} else { } else {
PathFinder.LOG.debug("occupied by {} → false",train); if (isSet(contextTrain) && contextTrain.isShunting()) {
PathFinder.LOG.debug("occupied by {}. Allowed for shunting {}",train,contextTrain);
free = true;
} else PathFinder.LOG.debug("occupied by {} → false",train);
} }
return free; return free;
} }
@ -262,7 +265,7 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
fieldset.children().firstElement().content(" / "+t("Train")); fieldset.children().firstElement().content(" / "+t("Train"));
} else fieldset = new Fieldset(t("Train")); } else fieldset = new Fieldset(t("Train"));
train.link("span", t("Train")+":"+NBSP+train+NBSP).addTo(fieldset); train.link("span", t("Train")+":"+NBSP+train+NBSP).addTo(fieldset);
if (isSet(train.route)) { if (isSet(train.route())) {
train.button(t("stop"), Map.of(ACTION,ACTION_STOP)).addTo(fieldset); train.button(t("stop"), Map.of(ACTION,ACTION_STOP)).addTo(fieldset);
} else { } else {
train.button(t("start"), Map.of(ACTION,ACTION_START)).addTo(fieldset); train.button(t("start"), Map.of(ACTION,ACTION_START)).addTo(fieldset);

Loading…
Cancel
Save