From f9a3466f330cd004ac2b60d6d0c1d33dab457d2e Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 4 Nov 2020 11:25:21 +0100 Subject: [PATCH] started to refactor train head and tail settings in tiles --- .../java/de/srsoftware/web4rail/Route.java | 14 +- .../de/srsoftware/web4rail/moving/Train.java | 18 +- .../de/srsoftware/web4rail/tiles/Block.java | 37 ++-- .../de/srsoftware/web4rail/tiles/Tile.java | 163 +++++++++--------- 4 files changed, 115 insertions(+), 117 deletions(-) diff --git a/src/main/java/de/srsoftware/web4rail/Route.java b/src/main/java/de/srsoftware/web4rail/Route.java index b919533..414a490 100644 --- a/src/main/java/de/srsoftware/web4rail/Route.java +++ b/src/main/java/de/srsoftware/web4rail/Route.java @@ -131,10 +131,10 @@ public class Route implements Constants{ */ public void activate() throws IOException { for (Tile tile : path) { - if (!(tile instanceof Block)) tile.train(train); + if (!(tile instanceof Block)) tile.trainHead(train); } train.heading(endDirection.inverse()); - endBlock.train(train); + endBlock.trainHead(train); startBlock.trailingTrain(train); } @@ -312,7 +312,7 @@ public class Route implements Constants{ /** * Kontakt der Route aktivieren * @param contact - * @param train + * @param trainHead */ public void contact(Contact contact) { LOG.debug("{} on {} activated {}.",train,this,contact); @@ -346,7 +346,7 @@ public class Route implements Constants{ public boolean free() { for (int i=1; i lockedTiles = new ArrayList(); try { - for (Tile tile : path) lockedTiles.add(tile.lock(this)); + for (Tile tile : path) lockedTiles.add(tile.setRoute(this)); } catch (IllegalStateException e) { for (Tile tile: lockedTiles) tile.unlock(); return false; @@ -538,8 +538,8 @@ public class Route implements Constants{ for (Tile tile : path) { if (!(tile instanceof Block)) tile.unlock(); } - if (endBlock.route() == this) endBlock.lock(null); - if (startBlock.route() == this) startBlock.lock(null); + if (endBlock.route() == this) endBlock.setRoute(null); + if (startBlock.route() == this) startBlock.setRoute(null); if (train != null) { train.heading(startDirection); train.block(startBlock, false); diff --git a/src/main/java/de/srsoftware/web4rail/moving/Train.java b/src/main/java/de/srsoftware/web4rail/moving/Train.java index e03455d..4dc5d1e 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Train.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Train.java @@ -39,7 +39,9 @@ import de.srsoftware.web4rail.tiles.Block; public class Train implements Comparable,Constants { private static final Logger LOG = LoggerFactory.getLogger(Train.class); - + + public static final String HEAD = "train_head"; + public static final String TAIL = "train_tile"; private static final HashMap trains = new HashMap<>(); public static final String ID = "id"; @@ -236,7 +238,7 @@ public class Train implements Comparable,Constants { previousBlocks.add(this.block); } this.block = block; - block.train(this); + block.trainHead(this); if (resetPreviousBlocks) resetPreviousBlocks(); return this; } @@ -480,14 +482,14 @@ public class Train implements Comparable,Constants { } public void removeFromBlock(Block block) { - if (block.train() == this) block.train(null); + if (block.trainHead() == this) block.trainHead(null); if (this.block == block) this.block = null; previousBlocks.remove(block); } public void resetPreviousBlocks() { for (Block block : previousBlocks) { - if (block.train() == this || block.trailingTrain() == this) block.unlock(); + if (block.trainHead() == this || block.trailingTrain() == this) block.unlock(); } previousBlocks.clear(); } @@ -503,7 +505,7 @@ public class Train implements Comparable,Constants { public static Select selector(Train preselected,Collection exclude) { if (exclude == null) exclude = new Vector(); - Select select = new Select(Train.class.getSimpleName()); + Select select = new Select(Train.HEAD); new Tag("option").attr("value","0").content(t("unset")).addTo(select); for (Train train : Train.list()) { if (exclude.contains(train)) continue; @@ -547,8 +549,8 @@ public class Train implements Comparable,Constants { setSpeed(0); if (route != null) try { route.unlock(); - route.endBlock().train(null); - route.startBlock().train(this); + route.endBlock().trainHead(null); + route.startBlock().trainHead(this); } catch (IOException e) { e.printStackTrace(); } @@ -571,7 +573,7 @@ public class Train implements Comparable,Constants { direction = direction.inverse(); for (Locomotive loco : locos) loco.turn(); } - if (block != null) plan.place(block.train(this)); + if (block != null) plan.place(block.trainHead(this)); return t("{} turned.",this); } diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Block.java b/src/main/java/de/srsoftware/web4rail/tiles/Block.java index 8734cd3..5d23cf9 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Block.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Block.java @@ -23,9 +23,7 @@ public abstract class Block extends StretchableTile{ private static final String ALLOW_TURN = "allowTurn"; public boolean turnAllowed = false; private Train trailingTrain = null; - - private static final String TRAIN = Train.class.getSimpleName(); - + @Override public JSONObject config() { JSONObject config = super.config(); @@ -34,8 +32,8 @@ public abstract class Block extends StretchableTile{ } @Override - public boolean free() { - return super.free() && trailingTrain == null; + public boolean isFree() { + return super.isFree() && trailingTrain == null; } @Override @@ -43,7 +41,6 @@ public abstract class Block extends StretchableTile{ JSONObject json = super.json(); json.put(NAME, name); json.put(ALLOW_TURN, turnAllowed); - if (train != null) json.put(TRAIN, train.id); return json; } @@ -52,10 +49,6 @@ public abstract class Block extends StretchableTile{ super.load(json); name = json.has(NAME) ? json.getString(NAME) : "Block"; turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN); - if (json.has(TRAIN)) { - Train tr = Train.get(json.getInt(TRAIN)); - train(tr.block(this, false)); - } return this; } @@ -68,7 +61,7 @@ public abstract class Block extends StretchableTile{ new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form); - Select select = Train.selector(train, null); + Select select = Train.selector(trainHead, null); select.addTo(new Label(t("Train:")+NBSP)).addTo(new Tag("p")).addTo(form); return form; @@ -78,10 +71,10 @@ public abstract class Block extends StretchableTile{ public Tag propMenu() { Tag window = super.propMenu(); - if (train != null) { - window.children().insertElementAt(new Button(t("stop"),"train("+train.id+",'"+ACTION_STOP+"')"), 1); - window.children().insertElementAt(new Button(t("start"),"train("+train.id+",'"+ACTION_START+"')"), 1); - window.children().insertElementAt(train.link("span"), 1); + if (trainHead != null) { + window.children().insertElementAt(new Button(t("stop"),"train("+trainHead.id+",'"+ACTION_STOP+"')"), 1); + window.children().insertElementAt(new Button(t("start"),"train("+trainHead.id+",'"+ACTION_START+"')"), 1); + window.children().insertElementAt(trainHead.link("span"), 1); window.children().insertElementAt(new Tag("h4").content(t("Train:")), 1); } return window; @@ -94,9 +87,9 @@ public abstract class Block extends StretchableTile{ if (replacements == null) replacements = new HashMap(); replacements.put("%text%",name); if (trailingTrain != null) replacements.put("%text%","("+trailingTrain.name()+")"); - if (train != null) replacements.put("%text%",train.directedName()); + if (trainHead != null) replacements.put("%text%",trainHead.directedName()); Tag tag = super.tag(replacements); - if (train != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied"); + if (trainHead != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied"); return tag; } @@ -112,7 +105,7 @@ public abstract class Block extends StretchableTile{ public void trailingTrain(Train train) { trailingTrain = train; - this.train = null; + this.trainHead = null; plan.place(this); } @@ -129,13 +122,13 @@ public abstract class Block extends StretchableTile{ @Override public Tile update(HashMap params) throws IOException { if (params.containsKey(NAME)) name=params.get(NAME); - if (params.containsKey(TRAIN)) { - int trainId = Integer.parseInt(params.get(TRAIN)); + if (params.containsKey(Train.HEAD)) { + int trainId = Integer.parseInt(params.get(Train.HEAD)); if (trainId == 0) { - train(null); + trainHead(null); } else { Train t = Train.get(trainId); - if (t != null) train = t.block(this,true); + if (t != null) trainHead = t.block(this,true); } } turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on"); diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java index ac5022c..df79724 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java @@ -37,38 +37,36 @@ import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Radio; public abstract class Tile implements Constants{ + protected static Logger LOG = LoggerFactory.getLogger(Tile.class); - public static final String ID = "id"; - private static final String TYPE = "type"; - private static final String LOCKED = "locked"; - - private static final String POS = "pos"; - private static final String X = "x"; - private static final String Y = "y"; - public int x = -1,y = -1; - - private static final String ROUTE = "route"; - protected Route route; + public static final String ID = "id"; + private static final String LOCKED = "locked"; + private static final String OCCUPIED = "occupied"; + private static final String ONEW_WAY = "one_way"; + private static final String POS = "pos"; + private static final String ROUTE = "route"; + private static final String TYPE = "type"; + private static final String X = "x"; + private static final String Y = "y"; - private static final String OCCUPIED = "occupied"; - protected Train train; - private static final String ONEW_WAY = "one_way"; - protected Direction oneWay = null; + private boolean disabled = false; + protected Direction oneWay = null; + protected Plan plan = null;; + protected Route route = null; + private HashSet routes = new HashSet<>(); + protected HashSet shadows = new HashSet<>(); + protected Train trainHead = null; + protected Train trainTail = null; + public Integer x = null; + public Integer y = null; - protected HashSet shadows = new HashSet<>(); - private HashSet routes = new HashSet<>(); - protected Plan plan; - private boolean disabled = false; - - protected static Logger LOG = LoggerFactory.getLogger(Tile.class); - protected Vector classes(){ Vector classes = new Vector(); classes.add("tile"); classes.add(getClass().getSimpleName()); - if (route != null) classes.add(LOCKED); - if (train != null) classes.add(OCCUPIED); + if (isSet(route)) classes.add(LOCKED); + if (isSet(trainHead) || isSet(trainTail)) classes.add(OCCUPIED); if (disabled) classes.add(DISABLED); return classes; } @@ -94,11 +92,8 @@ public abstract class Tile implements Constants{ return new HashMap<>(); } - public boolean free() { - if (disabled) return false; - if (route != null) return false; - if (train != null) return false; - return true; + public boolean isFree() { + return !(disabled || isSet(route) || isSet(trainHead) || isSet(trainTail)); } public int height() { @@ -121,14 +116,24 @@ public abstract class Tile implements Constants{ plan.set(tile.x, tile.y, tile); } + protected static boolean isNull(Object o) { + return o==null; + } + + protected static boolean isSet(Object o) { + return o != null; + } + public JSONObject json() { JSONObject json = new JSONObject(); json.put(TYPE, getClass().getSimpleName()); JSONObject pos = new JSONObject(Map.of(X,x,Y,y)); json.put(POS, pos); - if (route != null) json.put(ROUTE, route.id()); - if (oneWay != null) json.put(ONEW_WAY, oneWay); - if (disabled) json.put(DISABLED, true); + if (isSet(route)) json.put(ROUTE, route.id()); + if (isSet(oneWay)) json.put(ONEW_WAY, oneWay); + if (disabled) json.put(DISABLED, true); + if (isSet(trainHead)) json.put(Train.HEAD, trainHead.id); + if (isSet(trainTail)) json.put(Train.TAIL, trainTail.id); return json; } @@ -139,7 +144,7 @@ public abstract class Tile implements Constants{ public static void loadAll(String filename, Plan plan) throws IOException { BufferedReader file = new BufferedReader(new FileReader(filename)); String line = file.readLine(); - while (line != null) { + while (isSet(line)) { JSONObject json = new JSONObject(line); String clazz = json.getString(TYPE); @@ -158,18 +163,13 @@ public abstract class Tile implements Constants{ JSONObject pos = json.getJSONObject(POS); x = pos.getInt(X); y = pos.getInt(Y); - if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY)); - if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED); + if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY)); + if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED); + if (json.has(Train.HEAD)) trainHead = Train.get(json.getInt(Train.HEAD)); + if (json.has(Train.TAIL)) trainTail = Train.get(json.getInt(Train.TAIL)); return this; } - - public Tile lock(Route lockingRoute) { - if (route == lockingRoute) return this; - if (route != null && lockingRoute != null) throw new IllegalStateException(this.toString()); - route = lockingRoute; - return plan.place(this); - } - + public Plan plan() { return plan; } @@ -199,7 +199,7 @@ public abstract class Tile implements Constants{ List pd = possibleDirections(); if (!pd.isEmpty()) { new Tag("h4").content(t("One way:")).addTo(form); - new Radio("oneway","none",t("No"),oneWay == null).addTo(form); + new Radio("oneway","none",t("No"),isNull(oneWay)).addTo(form); for (Direction d:pd) { new Radio("oneway",d.toString(),t(d.toString()),d == oneWay).addTo(form); } @@ -216,7 +216,7 @@ public abstract class Tile implements Constants{ new Button(t("Apply"),"submitForm('"+formId+"')").addTo(form); form.addTo(window); - if (route != null) { + if (isSet(route)) { new Tag("p").content(t("Locked by {}",route)).addTo(window); } @@ -249,9 +249,7 @@ public abstract class Tile implements Constants{ int end2 = line.indexOf("<",start); if (end2>0 && (end<0 || end2len) { - val = Integer.parseInt(tag.substring(len)) + (int) val; - } + if (tag.length()>len) val = Integer.parseInt(tag.substring(len)) + (int) val; line = line.replace(tag, ""+val); start = line.indexOf(key); } @@ -269,11 +267,18 @@ public abstract class Tile implements Constants{ public static void saveAll(HashMap tiles ,String filename) throws IOException { BufferedWriter file = new BufferedWriter(new FileWriter(filename)); for (Tile tile : tiles.values()) { - if (tile == null || tile instanceof Shadow) continue; + if (isNull(tile) || tile instanceof Shadow) continue; file.append(tile.json()+"\n"); } file.close(); } + + public Tile setRoute(Route lockingRoute) { + if (route == lockingRoute) return this; // nothing changed + if (isSet(route) && isSet(lockingRoute)) throw new IllegalStateException(this.toString()); // tile already locked by other route + route = lockingRoute; + return plan.place(this); + } protected static String t(String txt, Object...fills) { return Translation.get(Application.class, txt, fills); @@ -282,17 +287,17 @@ public abstract class Tile implements Constants{ public Tag tag(Map replacements) throws IOException { int width = 100*len(); int height = 100*height(); - if (replacements == null) replacements = new HashMap(); + if (isNull(replacements)) replacements = new HashMap(); replacements.put("%width%",width); replacements.put("%height%",height); String style = ""; Tag svg = new Tag("svg") - .id((x!=-1 && y!=-1)?(id()):(getClass().getSimpleName())) + .id(isSet(x) && isSet(y) ? id() : getClass().getSimpleName()) .clazz(classes()) .size(100,100) .attr("name", getClass().getSimpleName()) .attr("viewbox", "0 0 "+width+" "+height); - if (x>-1) style="left: "+(30*x)+"px; top: "+(30*y)+"px;"; + if (isSet(x)) style="left: "+(30*x)+"px; top: "+(30*y)+"px;"; if (len()>1) style+=" width: "+(30*len())+"px;"; if (height()>1) style+=" height: "+(30*height())+"px;"; @@ -305,30 +310,27 @@ public abstract class Tile implements Constants{ while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.startsWith("")) continue; - for (Entry replacement : replacements.entrySet()) { - line = replace(line,replacement); - } + for (Entry replacement : replacements.entrySet()) line = replace(line,replacement); sb.append(line+"\n"); } scanner.close(); svg.content(sb.toString()); - if (oneWay != null) { - switch (oneWay) { - - case EAST: - new Tag("polygon").clazz("oneway").attr("points", "100,50 75,35 75,65").addTo(svg); - break; - case WEST: - new Tag("polygon").clazz("oneway").attr("points", "0,50 25,35 25,65").addTo(svg); - break; - case SOUTH: - new Tag("polygon").clazz("oneway").attr("points", "50,100 35,75 65,75").addTo(svg); - break; - case NORTH: - new Tag("polygon").clazz("oneway").attr("points", "50,0 35,25 65,25").addTo(svg); - break; - default: + if (isSet(oneWay)) { + switch (oneWay) { + case EAST: + new Tag("polygon").clazz("oneway").attr("points", "100,50 75,35 75,65").addTo(svg); + break; + case WEST: + new Tag("polygon").clazz("oneway").attr("points", "0,50 25,35 25,65").addTo(svg); + break; + case SOUTH: + new Tag("polygon").clazz("oneway").attr("points", "50,100 35,75 65,75").addTo(svg); + break; + case NORTH: + new Tag("polygon").clazz("oneway").attr("points", "50,0 35,25 65,25").addTo(svg); + break; + default: } } } else { @@ -339,7 +341,7 @@ public abstract class Tile implements Constants{ .addTo(svg); } String title = title(); - if (title!=null) new Tag("title").content(title()).addTo(svg); + if (isSet(title)) new Tag("title").content(title()).addTo(svg); return svg; } @@ -353,26 +355,27 @@ public abstract class Tile implements Constants{ return t("{}({},{})",getClass().getSimpleName(),x,y) ; } - public Train train() { - return train; + public Train trainHead() { + return trainHead; } - public Tile train(Train train) { - if (this.train == train) return this; // nothing to update - this.train = train; + public Tile trainHead(Train train) { + if (this.trainHead == train) return this; // nothing to update + this.trainHead = train; return plan.place(this); } public void unlock() { - route = null; - train = null; + route = null; + trainHead = null; + trainTail = null; plan.place(this); } public Tile update(HashMap params) throws IOException { LOG.debug("{}.update({})",getClass().getSimpleName(),params); String oneWayDir = params.get("oneway"); - if (oneWayDir != null) { + if (isSet(oneWayDir)) { try { oneWay = Direction.valueOf(oneWayDir); } catch (Exception e) {