started reimplementation of route manager
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.3.51</version>
|
||||
<version>1.3.52</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -510,6 +510,13 @@ public abstract class BaseClass implements Constants{
|
||||
customFieldNames = new HashMap<Class<? extends BaseClass>, Set<String>>();
|
||||
}
|
||||
|
||||
public void sleep(long ms) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String t(String txt, Object...fills) {
|
||||
if (isSet(fills)) for (int i=0; i<fills.length; i++) {
|
||||
|
||||
@@ -37,6 +37,7 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.threads.PathFinder;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.BlockContact;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
@@ -387,7 +388,8 @@ public class Route extends BaseClass {
|
||||
public void finish() {
|
||||
LOG.debug("{}.finish()",this);
|
||||
|
||||
// TODO
|
||||
// TODO:
|
||||
|
||||
}
|
||||
|
||||
private String generateName() {
|
||||
@@ -635,19 +637,21 @@ public class Route extends BaseClass {
|
||||
LOG.debug("{}.lockIgnoring({})",this,ignoredRoute);
|
||||
HashSet<Tile> ignoredPath = new HashSet<Tile>();
|
||||
if (isSet(ignoredRoute)) ignoredPath.addAll(ignoredRoute.path);
|
||||
boolean success = true;
|
||||
for (Tile tile : path) {
|
||||
if (ignoredPath.contains(tile)) continue;
|
||||
try {
|
||||
tile.setRoute(this);
|
||||
} catch (IllegalStateException e) {
|
||||
LOG.debug("{}.lockIgnoring(...) failed at {}, rolling back",this,tile);
|
||||
success = false;
|
||||
break;
|
||||
for (Tile lockedTile : path) { // unlock the same tiles that have been locked before, until we encounter the unlockable tile
|
||||
if (lockedTile == tile) return false;
|
||||
lockedTile.unset(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (success) state = State.LOCKED;
|
||||
return success;
|
||||
state = State.LOCKED;
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Route> multiply(int size) {
|
||||
|
||||
@@ -36,6 +36,7 @@ import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.threads.PathFinder;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
@@ -168,9 +169,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String automatic() {
|
||||
return "not implemented";
|
||||
|
||||
public boolean automatic() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private Fieldset blockHistory() {
|
||||
@@ -776,6 +776,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return properties();
|
||||
}
|
||||
|
||||
protected Route setRoute(Route newRoute) {
|
||||
route = newRoute;
|
||||
return route;
|
||||
}
|
||||
|
||||
public void setSpeed(int newSpeed) {
|
||||
LOG.debug("{}.setSpeed({})",this,newSpeed);
|
||||
speed = Math.min(newSpeed,maxSpeed());
|
||||
@@ -829,15 +834,40 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
|
||||
public String start() {
|
||||
return "not implemented, yet";
|
||||
public void start() {
|
||||
Context context = new Context(this).block(currentBlock).direction(direction);
|
||||
new PathFinder(context) {
|
||||
|
||||
@Override
|
||||
public void found(Route r) {
|
||||
// TODO Auto-generated method stub
|
||||
LOG.debug("Route {} prepared for {}",r,Train.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void locked(Route r) {
|
||||
// TODO Auto-generated method stub
|
||||
LOG.debug("Route {} locked for {}",r,Train.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepared(Route r) {
|
||||
LOG.debug("Route {} prepared for {}",r,Train.this);
|
||||
setRoute(r).start(Train.this);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static void startAll() {
|
||||
LOG.debug("Train.startAll()");
|
||||
for (Train train : BaseClass.listElements(Train.class)) LOG.info(train.automatic());
|
||||
for (Train train : BaseClass.listElements(Train.class)) LOG.info(train.startAutopilot());
|
||||
}
|
||||
|
||||
|
||||
private String startAutopilot() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object stopNow() {
|
||||
|
||||
return properties();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.srsoftware.web4rail;
|
||||
package de.srsoftware.web4rail.threads;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -9,15 +9,27 @@ import java.util.Vector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
/**
|
||||
* @author Stephan Richter, SRSoftware 2020-2021
|
||||
*/
|
||||
public class PathFinder extends BaseClass{
|
||||
public abstract class PathFinder extends BaseClass implements Runnable{
|
||||
public static final Logger LOG = LoggerFactory.getLogger(PathFinder.class);
|
||||
private Context context;
|
||||
private boolean aborted = false;
|
||||
|
||||
public PathFinder(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void abort() {
|
||||
aborted = true;
|
||||
}
|
||||
|
||||
private static TreeMap<Integer,List<Route>> availableRoutes(Context context,HashSet<Route> visitedRoutes){
|
||||
String inset = "";
|
||||
@@ -108,9 +120,9 @@ public class PathFinder extends BaseClass{
|
||||
return availableRoutes;
|
||||
}
|
||||
|
||||
public static Route chooseRoute(Context context) {
|
||||
LOG.debug("PathFinder.chooseRoute({})",context);
|
||||
TreeMap<Integer, List<Route>> availableRoutes = PathFinder.availableRoutes(context,new HashSet<Route>());
|
||||
public Route chooseRoute() {
|
||||
LOG.debug("PathFinder.chooseRoute()");
|
||||
TreeMap<Integer, List<Route>> availableRoutes = availableRoutes(context,new HashSet<Route>());
|
||||
while (!availableRoutes.isEmpty()) {
|
||||
LOG.debug("availableRoutes: {}",availableRoutes);
|
||||
Entry<Integer, List<Route>> entry = availableRoutes.lastEntry();
|
||||
@@ -128,4 +140,29 @@ public class PathFinder extends BaseClass{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
Route route = chooseRoute();
|
||||
if (aborted) return;
|
||||
if (isSet(route)) {
|
||||
found(route);
|
||||
if (aborted) return;
|
||||
if (route.lock()) {
|
||||
locked(route);
|
||||
if (aborted) return;
|
||||
if (route.prepare()) {
|
||||
prepared(route);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void locked(Route r);
|
||||
public abstract void found(Route r);
|
||||
public abstract void prepared(Route r);
|
||||
}
|
||||
@@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.PathFinder;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.actions.AlterDirection;
|
||||
@@ -33,6 +32,7 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.threads.PathFinder;
|
||||
|
||||
/**
|
||||
* Base class for all tiles
|
||||
|
||||
Reference in New Issue
Block a user