started major refactoring

This commit is contained in:
Stephan Richter
2020-12-01 11:20:35 +01:00
parent 614600aacd
commit 72db5da58f
42 changed files with 387 additions and 353 deletions

View File

@@ -13,14 +13,9 @@ import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Label;
import de.srsoftware.web4rail.tags.Select;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.Contact;
/**
* Base Class for all other actions
@@ -28,81 +23,12 @@ import de.srsoftware.web4rail.tiles.Contact;
*
*/
public abstract class Action extends BaseClass {
private static final HashMap<Integer,Action> actions = new HashMap<Integer, Action>();
private static final HashMap<Id,Action> actions = new HashMap<Id, Action>();
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
private static final String PREFIX = Action.class.getPackageName();
protected int id;
public static class Context {
public Contact contact = null;
public Route route = null;
public Train train = null;
public Block block = null;
public Direction direction = null;
public Context(Contact c, Route r, Train t, Block b, Direction d) {
contact = c;
route = r;
train = t;
block = b;
direction = d;
}
public Context(Contact c) {
contact = c;
setRoute(contact.route());
}
public Context(Train train) {
setTrain(train);
}
public Context(Route route) {
setRoute(route);
}
protected Context clone() {
return new Context(contact, route, train, block, direction);
}
private void setRoute(Route route) {
this.route = route;
if (isSet(route)) setTrain(route.train);
}
private void setTrain(Train train) {
this.train = train;
if (isSet(train)) {
if (isNull(route)) route = train.route;
setBlock(train.currentBlock());
setDirection(train.direction());
}
}
private void setDirection(Direction dir) {
direction = dir;
}
private void setBlock(Block block) {
this.block = block;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer(getClass().getSimpleName());
sb.append("(");
sb.append(t("Train: {}",train));
if (isSet(route)) sb.append(", "+t("Route: {}",route));
if (isSet(contact)) sb.append(", "+t("Contact: {}",contact));
sb.append(")");
return sb.toString();
}
}
public Action() {
id = Application.createId();
actions.put(id, this);
actions.put(id(), this);
}
public static Action create(String type) {
@@ -120,13 +46,9 @@ public abstract class Action extends BaseClass {
public abstract boolean fire(Context context);
public static Action get(int actionId) {
public static Action get(Id actionId) {
return actions.get(actionId);
}
public int id() {
return id;
}
public JSONObject json() {
return new JSONObject().put(TYPE, getClass().getSimpleName());

View File

@@ -13,11 +13,11 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.BaseClass.Context;
import de.srsoftware.web4rail.BaseClass.Id;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.actions.Action.Context;
import de.srsoftware.web4rail.tags.Button;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
@@ -26,25 +26,25 @@ public class ActionList extends Vector<Action> implements Constants{
private static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
private static final long serialVersionUID = 4862000041987682112L;
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>();
private int id;
private static final HashMap<Id, ActionList> actionLists = new HashMap<Id, ActionList>();
private Id id;
public ActionList() {
id = Application.createId();
id = new Id();
actionLists.put(id,this);
}
private static Integer actionId(HashMap<String, String> params) {
private static Id actionId(HashMap<String, String> params) {
if (!params.containsKey(ID)) return null;
String[] parts = params.get(ID).split("/");
if (parts.length<2) return null;
return Integer.parseInt(parts[1]);
return new Id(parts[1]);
}
private static Integer actionListId(HashMap<String, String> params) {
private static Id actionListId(HashMap<String, String> params) {
if (!params.containsKey(ID)) return null;
String[] parts = params.get(ID).split("/");
return Integer.parseInt(parts[0]);
return new Id(parts[0]);
}
private Object actionTypeForm(Window win, String context) {
@@ -110,7 +110,7 @@ public class ActionList extends Vector<Action> implements Constants{
boolean first = true;
for (Action action : this) {
props.put(ID, id+"/"+action.id());
Tag act = BaseClass.link("span", Map.of(REALM,REALM_ACTIONS,ID,id+"/"+action.id(),ACTION,ACTION_PROPS,CONTEXT,context), action+NBSP).addTo(new Tag("li"));;
Tag act = action.link("span", action+NBSP, Map.of(CONTEXT,context,ID,id+"/"+action.id())).addTo(new Tag("li"));
if (!first) {
props.put(ACTION, ACTION_MOVE);
new Button("",props).addTo(act);
@@ -132,9 +132,9 @@ public class ActionList extends Vector<Action> implements Constants{
}
}
public boolean drop(int actionId) {
public boolean drop(Id actionId) {
for (Action action : this) {
if (action.id() == actionId) {
if (action.id().equals(actionId)) {
this.remove(action);
return true;
}
@@ -150,7 +150,7 @@ public class ActionList extends Vector<Action> implements Constants{
return true;
}
public int id() {
public Id id() {
return id;
}
@@ -172,9 +172,9 @@ public class ActionList extends Vector<Action> implements Constants{
return actionList;
}
public boolean moveUp(int actionId) {
public boolean moveUp(Id actionId) {
for (int i=1; i<size(); i++) {
if (actionId == elementAt(i).id()) {
if (actionId.equals(elementAt(i).id())) {
Action action = remove(i);
insertElementAt(action, i-1);
return true;
@@ -184,11 +184,11 @@ public class ActionList extends Vector<Action> implements Constants{
}
public static Object process(HashMap<String, String> params, Plan plan) {
Integer listId = actionListId(params);
Id listId = actionListId(params);
if (listId == null) return t("No action list id passed to ActionList.process()!");
ActionList actionList = actionLists.get(listId);
Integer actionId = actionId(params);
Id actionId = actionId(params);
String action = params.get(ACTION);
if (action == null) return t("No action passed to ActionList.process()!");
if (actionList == null && !List.of(ACTION_UPDATE,ACTION_PROPS).contains(action)) return t("No action list with id {} found!",listId);
@@ -209,7 +209,7 @@ public class ActionList extends Vector<Action> implements Constants{
}
private static Object propsOf(HashMap<String, String> params) {
int actionId = actionId(params);
Id actionId = actionId(params);
Action action = Action.get(actionId);
if (action != null) return action.properties(params);
return t("No action with id {} found!",actionId);
@@ -219,7 +219,7 @@ public class ActionList extends Vector<Action> implements Constants{
return Translation.get(Application.class, text, fills);
}
private static Object update(int actionId, HashMap<String, String> params, Plan plan) {
private static Object update(Id actionId, HashMap<String, String> params, Plan plan) {
Action action = Action.get(actionId);
if (action != null) {
plan.stream(action.update(params).toString());

View File

@@ -4,8 +4,8 @@ public class BrakeCancel extends Action {
@Override
public boolean fire(Context context) {
if (isNull(context.route)) return false;
context.route.brakeCancel();
if (isNull(context.route())) return false;
context.route().brakeCancel();
return true;
}

View File

@@ -4,8 +4,8 @@ public class BrakeStart extends Action {
@Override
public boolean fire(Context context) {
if (isNull(context.route)) return false;
context.route.brakeStart();
if (isNull(context.route())) return false;
context.route().brakeStart();
LOG.debug("Started brake process...");
return true;
}

View File

@@ -4,8 +4,8 @@ public class BrakeStop extends Action {
@Override
public boolean fire(Context context) {
if (isNull(context.route)) return false;
context.route.brakeStop();
if (isNull(context.route())) return false;
context.route().brakeStop();
return true;
}

View File

@@ -49,10 +49,8 @@ public class ConditionalAction extends Action {
if (!conditions.isEmpty()) {
Tag list = new Tag("ul");
for (Condition condition : conditions) {
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_PROPS,CONTEXT, context));
Tag li = link("span", props, condition+NBSP).addTo(new Tag("li"));
props.put(ACTION, ACTION_DROP);
props.put(CONTEXT,REALM_ACTIONS+":"+id());
Tag li = link("span", condition+NBSP,Map.of(CONTEXT,context)).addTo(new Tag("li"));
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_DROP,CONTEXT, REALM_ACTIONS+":"+id()));
new Button(t("delete"), props).addTo(li).addTo(list);
}
list.addTo(fieldset);
@@ -67,7 +65,7 @@ public class ConditionalAction extends Action {
Condition.selector().addTo(form);
new Button(t("Add condition"),form).addTo(form);
return contextButton(context,t("Back")).addTo(form).addTo(fieldset);
return button(t("Back")).addTo(form).addTo(fieldset);
}
public boolean equals(ConditionalAction other) {

View File

@@ -18,8 +18,8 @@ public class DetermineTrainInBlock extends Action {
@Override
public boolean fire(Context context) {
context.block = block;
context.train = block.train();
context.block(block);
context.train(block.train());
return true;
}
@@ -33,7 +33,7 @@ public class DetermineTrainInBlock extends Action {
@Override
public Action load(JSONObject json) {
super.load(json);
String blockId = json.getString(BLOCK);
Id blockId = Id.from(json,BLOCK);
if (isSet(blockId)) block = Block.get(blockId);
return this;
}
@@ -61,7 +61,7 @@ public class DetermineTrainInBlock extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String blockId = params.get(Block.class.getSimpleName());
Id blockId = Id.from(params,Block.class.getSimpleName());
if (isSet(blockId)) block = Block.get(blockId);
return properties(params);
}

View File

@@ -6,7 +6,7 @@ public class FinishRoute extends Action {
@Override
public boolean fire(Context context) {
Route route = context.route;
Route route = context.route();
if (isSet(route)) route.finish();
return true;
}

View File

@@ -8,20 +8,20 @@ public class PreserveRoute extends Action {
@Override
public boolean fire(Context context) {
Train train = context.train;
Route route = context.route;
Train train = context.train();
Route route = context.route();
// These are errors:
if (isNull(train)) return false;
if (isNull(route)) return false;
Range waitTime = context.route.endBlock().getWaitTime(context.train,context.route.endDirection);
Range waitTime = route.endBlock().getWaitTime(train,route.endDirection);
// These are NOT errors:
if (!context.train.usesAutopilot()) return true;
if (!train.usesAutopilot()) return true;
if (waitTime.max > 0) return true; // train is expected to wait in next block.
if (train.destination() == route.endBlock()) return true;
context.train.reserveNext();
train.reserveNext();
return true;
}

View File

@@ -18,14 +18,14 @@ public class SetContextTrain extends Action {
@Override
public boolean fire(Context context) {
context.train = train;
context.train(train);
return true;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
if (isSet(train)) json.put(REALM_TRAIN, train.id);
if (isSet(train)) json.put(REALM_TRAIN, train.id());
return json;
}
@@ -37,7 +37,7 @@ public class SetContextTrain extends Action {
public void run() {
try {
sleep(1000);
int trainId = json.getInt(REALM_TRAIN);
Id trainId = Id.from(json,REALM_TRAIN);
if (isSet(trainId)) train = Train.get(trainId);
} catch (InterruptedException e) {}
};
@@ -69,8 +69,8 @@ public class SetContextTrain extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String trainId = params.get(Train.class.getSimpleName());
if (isSet(trainId)) train = Train.get(Integer.parseInt(trainId));
Id trainId = Id.from(params,Train.class.getSimpleName());
if (isSet(trainId)) train = Train.get(trainId);
return properties(params);
}

View File

@@ -39,7 +39,7 @@ public class SetDisplayText extends TextAction{
public void run() {
try {
sleep(1000);
display = (TextDisplay) plan.get(json.getString(DISPLAY), false);
display = (TextDisplay) plan.get(Id.from(json,DISPLAY), false);
} catch (InterruptedException e) {}
};
}.start();
@@ -72,7 +72,7 @@ public class SetDisplayText extends TextAction{
protected Object update(HashMap<String, String> params) {
super.update(params);
String displayId = params.get(TextDisplay.class.getSimpleName());
if (isSet(displayId)) display = (TextDisplay) plan.get(displayId, false);
if (isSet(displayId)) display = (TextDisplay) plan.get(new Id(displayId), false);
return properties(params);
}
}

View File

@@ -41,7 +41,7 @@ public class SetRelay extends Action {
super.load(json);
String relayId = json.getString(RELAY);
if (isSet(relayId)) {
relay = Relay.get(relayId);
relay = Relay.get(new Id(relayId));
state = json.getBoolean(Relay.STATE);
}
return this;
@@ -81,7 +81,7 @@ public class SetRelay extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String relayId = params.get(RELAY);
Id relayId = new Id(params.get(RELAY));
relay = Relay.get(relayId);
String st = params.get(Relay.STATE);
if (isSet(st)) state = st.equals("true");

View File

@@ -39,7 +39,7 @@ public class SetSignal extends Action {
@Override
public Action load(JSONObject json) {
super.load(json);
Tile tile = plan.get(json.getString(SIGNAL), false);
Tile tile = plan.get(new Id(json.getString(SIGNAL)), false);
if (tile instanceof Signal) signal = (Signal) tile;
state = json.getString(Signal.STATE);
return this;
@@ -91,7 +91,7 @@ public class SetSignal extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
Tile tile = plan.get(params.get(SIGNAL), false);
Tile tile = plan.get(new Id(params.get(SIGNAL)), false);
if (tile instanceof Signal) signal = (Signal) tile;
String st = params.get(Signal.STATE);
if (isSet(st)) state = st;

View File

@@ -18,8 +18,8 @@ public class SetSpeed extends Action{
@Override
public boolean fire(Context context) {
if (isNull(context.train)) return false;
context.train.setSpeed(speed);
if (isNull(context.train())) return false;
context.train().setSpeed(speed);
return true;
}

View File

@@ -4,8 +4,8 @@ public class StopAuto extends Action {
@Override
public boolean fire(Context context) {
if (isNull(context.train)) return false;
context.train.quitAutopilot();
if (isNull(context.train())) return false;
context.train().quitAutopilot();
return true;
}

View File

@@ -18,11 +18,11 @@ public abstract class TextAction extends Action {
public String fill(String tx,Context context) {
if (isSet(context.block)) tx = tx.replace("%block%", context.block.name);
if (isSet(context.contact)) tx = tx.replace("%contact%", context.contact.id());
if (isSet(context.direction)) tx = tx.replace("%dir%", context.direction.name());
if (isSet(context.route)) tx = tx.replace("%route%", context.route.name());
if (isSet(context.train)) tx = tx.replace("%train%", context.train.name());
if (isSet(context.block())) tx = tx.replace("%block%", context.block().name);
if (isSet(context.contact())) tx = tx.replace("%contact%", context.contact().id().toString());
if (isSet(context.direction())) tx = tx.replace("%dir%", context.direction().name());
if (isSet(context.route())) tx = tx.replace("%route%", context.route().name());
if (isSet(context.train())) tx = tx.replace("%train%", context.train().name());
return tx;
}

View File

@@ -32,7 +32,7 @@ public class TriggerContact extends Action {
@Override
public Action load(JSONObject json) {
super.load(json);
String contactId = json.getString(CONTACT);
Id contactId = Id.from(json,CONTACT);
if (isSet(contactId)) contact = Contact.get(contactId);
return this;
}
@@ -60,7 +60,7 @@ public class TriggerContact extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String contactId = params.get(CONTACT);
Id contactId = Id.from(params,CONTACT);
if (isSet(contactId)) contact = Contact.get(contactId);
return properties(params);
}

View File

@@ -4,8 +4,8 @@ public class TurnTrain extends Action{
@Override
public boolean fire(Context context) {
if (context.train != null) {
context.train.turn();
if (context.train() != null) {
context.train().turn();
return true;
}
return false;