implemented AddDestination action and evaluation of destination tags of trains.
This commit is contained in:
@@ -506,8 +506,33 @@ public class Route extends BaseClass {
|
||||
train.set(endBlock);
|
||||
train.heading(endDirection);
|
||||
if (endBlock == train.destination()) {
|
||||
train.destination(null).quitAutopilot();
|
||||
plan.stream(t("{} reached it`s destination!",train));
|
||||
String destTag = null;
|
||||
for (String tag : train.tags()) {
|
||||
if (tag.startsWith("@")) {
|
||||
destTag = tag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
train.destination(null);
|
||||
if (isSet(destTag)) {
|
||||
String[] parts = destTag.split("@");
|
||||
String destId = parts[1];
|
||||
boolean turn = destId.endsWith("+turn");
|
||||
if (turn) destId = destId.substring(0,destId.length()-5);
|
||||
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();
|
||||
plan.stream(t("{} reached it`s destination!",train));
|
||||
}
|
||||
} else {
|
||||
train.setWaitTime(endBlock.getWaitTime(train,train.direction()));
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ public abstract class Action extends BaseClass {
|
||||
|
||||
public static List<Class<? extends Action>> classes() {
|
||||
return List.of(
|
||||
AddDestination.class,
|
||||
AddRemoveTag.class,
|
||||
AlterDirection.class,
|
||||
BrakeCancel.class,
|
||||
|
||||
118
src/main/java/de/srsoftware/web4rail/actions/AddDestination.java
Normal file
118
src/main/java/de/srsoftware/web4rail/actions/AddDestination.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class AddDestination extends Action {
|
||||
|
||||
private static final String TURN = "turn";
|
||||
private Block destination;
|
||||
private boolean turnAtDestination;
|
||||
|
||||
public AddDestination(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
Train train = context.train();
|
||||
if (isNull(train)) return false;
|
||||
if (isNull(destination)) { // clear destinations!
|
||||
Iterator<String> it = train.tags().iterator();
|
||||
while (it.hasNext()) {
|
||||
String tag = it.next();
|
||||
if (tag.startsWith("@")) it = train.removeTag(tag);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String dest = "@"+destination.id()+(turnAtDestination?"+turn":"");
|
||||
for (String tag: train.tags()) {
|
||||
if (tag.startsWith("@")) {
|
||||
train.removeTag(tag);
|
||||
dest = tag+dest;
|
||||
break;
|
||||
}
|
||||
}
|
||||
train.addTag(dest);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(destination)) json.put(Train.DESTINATION,destination.id().toString());
|
||||
if (turnAtDestination) json.put(TURN,true);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
if (json.has(TURN)) turnAtDestination = json.getBoolean(TURN);
|
||||
if (json.has(Train.DESTINATION)) {
|
||||
Id blockId = new Id(json.getString(Train.DESTINATION));
|
||||
destination = BaseClass.get(blockId);
|
||||
if (isNull(destination)) {
|
||||
Application.threadPool.execute(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
destination = BaseClass.get(blockId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
Tag span = new Tag("span");
|
||||
button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,Train.DESTINATION)).addTo(span);
|
||||
button(t("Clear destinations"),Map.of(ACTION,ACTION_UPDATE,Train.DESTINATION,"0")).addTo(span);
|
||||
formInputs.add(t("Destination")+": "+(isNull(destination) ? t("Clear destinations") : destination),span);
|
||||
formInputs.add(t("Turn at destination"),new Checkbox(TURN, t("Turn"), turnAtDestination));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return isSet(destination) ? t("Add {} to destinations of train",destination) : t("Clear destinations of train");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(Train.DESTINATION)) {
|
||||
String destId = params.get(Train.DESTINATION);
|
||||
if ("0".equals(destId)) {
|
||||
destination = null;
|
||||
} else {
|
||||
Tile tile = plan.get(new Id(destId), true);
|
||||
if (tile instanceof Block) {
|
||||
destination = (Block) tile;
|
||||
} else {
|
||||
return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
}
|
||||
}
|
||||
turnAtDestination = "on".equals(params.get(TURN));
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -10,6 +11,7 @@ import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class DetermineTrainInBlock extends Action {
|
||||
|
||||
@@ -57,7 +59,7 @@ public class DetermineTrainInBlock extends Action {
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select block"),Block.selector(block, null));
|
||||
formInputs.add(t("Block")+": "+(isNull(block) ? t("unset") : block),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,BLOCK)));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -74,8 +76,12 @@ public class DetermineTrainInBlock extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Id blockId = Id.from(params,Block.class.getSimpleName());
|
||||
if (isSet(blockId)) block = Block.get(blockId);
|
||||
return properties();
|
||||
if (params.containsKey(BLOCK)) {
|
||||
Tile tile = plan.get(new Id(params.get(BLOCK)), true);
|
||||
if (tile instanceof Block) {
|
||||
block = (Block) tile;
|
||||
} else return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -11,6 +12,7 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tiles.TextDisplay;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class SetDisplayText extends TextAction{
|
||||
|
||||
@@ -29,7 +31,9 @@ public class SetDisplayText extends TextAction{
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(DISPLAY, display.id());
|
||||
JSONObject json = super.json();
|
||||
if (isSet(display)) json.put(DISPLAY, display.id());
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +64,7 @@ public class SetDisplayText extends TextAction{
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select display"),TextDisplay.selector(display, null));
|
||||
formInputs.add(t("Display")+": "+(isNull(display) ? t("unset") : display),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,DISPLAY)));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -70,9 +74,14 @@ public class SetDisplayText extends TextAction{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
String displayId = params.get(TextDisplay.class.getSimpleName());
|
||||
if (isSet(displayId)) display = (TextDisplay) plan.get(new Id(displayId), false);
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(DISPLAY)) {
|
||||
Tile object = plan.get(new Id(params.get(DISPLAY)), true);
|
||||
if (object instanceof TextDisplay) {
|
||||
display = (TextDisplay) object;
|
||||
} else return t("Clicked tile is not a {}!",t("display"));
|
||||
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -73,8 +74,7 @@ public class SetTurnout extends Action {
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
|
||||
formInputs.add(t("Select turnout"),Turnout.selector(turnout,null));
|
||||
|
||||
formInputs.add(t("Turnout")+": "+(isNull(turnout) ? t("unset") : turnout),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,TURNOUT)));
|
||||
if (isSet(turnout)) {
|
||||
Select select = new Select(Turnout.STATE);
|
||||
|
||||
@@ -112,8 +112,12 @@ public class SetTurnout extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Id turnoutId = new Id(params.get(TURNOUT));
|
||||
turnout = BaseClass.get(turnoutId);
|
||||
if (params.containsKey(TURNOUT)) {
|
||||
BaseClass object = BaseClass.get(new Id(params.get(TURNOUT)));
|
||||
if (object instanceof Turnout) {
|
||||
turnout = (Turnout) object;
|
||||
} else return t("Clicked tile is not a {}!",t("turnout"));
|
||||
}
|
||||
String st = params.get(Turnout.STATE);
|
||||
if (isSet(st)) state = Turnout.State.valueOf(st);
|
||||
return super.update(params);
|
||||
|
||||
@@ -54,7 +54,7 @@ public abstract class TextAction extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
text = params.get(TEXT);
|
||||
if (params.containsKey(TEXT)) text = params.get(TEXT);
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,9 +77,7 @@ public class BlockFree extends Condition {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(BLOCK)) return t("No block id passed to BlockFree.update()!");
|
||||
Id bid = new Id(params.get(BLOCK));
|
||||
|
||||
Tile tile = plan.get(bid, true);
|
||||
Tile tile = plan.get(new Id(params.get(BLOCK)), true);
|
||||
if (tile instanceof Block) {
|
||||
block = (Block) tile;
|
||||
} else {
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -11,6 +12,7 @@ import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class RouteEndBlock extends Condition{
|
||||
|
||||
@@ -58,7 +60,7 @@ public class RouteEndBlock extends Condition{
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select block"), Block.selector(block, null));
|
||||
formInputs.add(t("Block")+": "+(isNull(block) ? t("unset") : block),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,BLOCK)));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -77,11 +79,12 @@ public class RouteEndBlock extends Condition{
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(BLOCK)) return t("No block id passed to RouteEndBlock.update()!");
|
||||
Id bid = new Id(params.get(BLOCK));
|
||||
Block block = Block.get(bid);
|
||||
if (block == null) return t("No block with id {} found!",bid);
|
||||
this.block = block;
|
||||
if (params.containsKey(BLOCK)) {
|
||||
Tile tile = plan.get(new Id(params.get(BLOCK)), true);
|
||||
if (tile instanceof Block) {
|
||||
block = (Block) tile;
|
||||
} else return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -68,7 +69,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private static final String TAGS = "tags";
|
||||
|
||||
private static final String DESTINATION = "destination";
|
||||
public static final String DESTINATION = "destination";
|
||||
|
||||
private static final String ACTION_REVERSE = "reverse";
|
||||
|
||||
@@ -172,6 +173,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
public void addTag(String tag) {
|
||||
tags.add(tag);
|
||||
}
|
||||
|
||||
|
||||
public void addToTrace(Vector<Tile> newTiles) {
|
||||
Route.LOG.debug("{}.addToTrace({})",this,newTiles);
|
||||
@@ -329,6 +335,22 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public Block destination() {
|
||||
if (isNull(destination)) {
|
||||
String destTag = null;
|
||||
for (String tag : tags) {
|
||||
if (tag.startsWith("@")) {
|
||||
destTag = tag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isSet(destTag)) {
|
||||
String[] parts = destTag.split("@");
|
||||
destTag = parts[1];
|
||||
if (destTag.endsWith("+turn")) destTag = destTag.substring(0,destTag.length()-5);
|
||||
BaseClass object = BaseClass.get(new Id(destTag));
|
||||
if (object instanceof Block) destination = (Block) object;
|
||||
}
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
@@ -663,6 +685,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
super.removeChild(child);
|
||||
}
|
||||
|
||||
public Iterator<String> removeTag(String tag) {
|
||||
tags.remove(tag);
|
||||
return tags().iterator();
|
||||
}
|
||||
|
||||
|
||||
public void reserveNext() {
|
||||
LOG.debug("{}.reserveNext()",this);
|
||||
Context context = new Context(this).route(route).block(route.endBlock()).direction(route.endDirection);
|
||||
|
||||
@@ -243,6 +243,11 @@ public class Contact extends Tile{
|
||||
public String title() {
|
||||
return t("Contact {} @ ({}, {})",addr,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Contact")+"("+x+","+y+")";
|
||||
}
|
||||
|
||||
public String trigger() {
|
||||
if (trigger == null) trigger = getClass().getSimpleName()+"-"+id();
|
||||
|
||||
@@ -151,6 +151,12 @@ public abstract class Decoupler extends Tile implements Device{
|
||||
return select;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Decoupler")+"("+x+","+y+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile update(HashMap<String, String> params) {
|
||||
if (params.containsKey(PROTOCOL)) protocol = Protocol.valueOf(params.get(PROTOCOL));
|
||||
|
||||
Reference in New Issue
Block a user