improved actions and conditions code,
added TrainLength condition, added FreeStartBlock action
This commit is contained in:
@@ -2,7 +2,10 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@@ -16,11 +19,14 @@ import de.srsoftware.web4rail.Plan;
|
||||
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.Contact;
|
||||
|
||||
public abstract class Action implements Constants {
|
||||
private static final HashMap<Integer,Action> actions = new HashMap<Integer, 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 {
|
||||
@@ -47,31 +53,20 @@ public abstract class Action implements Constants {
|
||||
}
|
||||
|
||||
public static Action create(String type) {
|
||||
switch (type) { // TODO: das kann man mit Reflection generischer lösen
|
||||
case "ConditionalAction":
|
||||
return new ConditionalAction();
|
||||
case "FinishRoute":
|
||||
return new FinishRoute();
|
||||
case "SetSignalsToStop":
|
||||
return new SetSignalsToStop();
|
||||
case "SetSpeed":
|
||||
return new SetSpeed(0);
|
||||
case "TurnTrain":
|
||||
return new TurnTrain();
|
||||
case "StopAuto":
|
||||
return new StopAuto();
|
||||
case "PowerOff":
|
||||
return new PowerOff();
|
||||
case "SetRelay":
|
||||
return new SetRelay();
|
||||
case "DelayedAction":
|
||||
return new DelayedAction();
|
||||
try {
|
||||
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract boolean fire(Context context) throws IOException;
|
||||
|
||||
public static Action get(int actionId) {
|
||||
return actions.get(actionId);
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
@@ -86,30 +81,23 @@ public abstract class Action implements Constants {
|
||||
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
|
||||
}
|
||||
|
||||
public static Action load(JSONObject json) {
|
||||
String clazz = json.getString(TYPE);
|
||||
switch (clazz) { // TODO: das kann generisch mittels Reflection implementiert werden!
|
||||
case "ActivateRoute":
|
||||
return new ActivateRoute();
|
||||
case "ConditionalAction":
|
||||
return ConditionalAction.load(json);
|
||||
case "FinishRoute":
|
||||
return new FinishRoute();
|
||||
case "PowerOff":
|
||||
return new PowerOff();
|
||||
case "SetSignalsToStop":
|
||||
return new SetSignalsToStop();
|
||||
case "SetSpeed":
|
||||
return SetSpeed.load(json);
|
||||
case "SetRelay":
|
||||
return SetRelay.load(json);
|
||||
case "TurnTrain":
|
||||
return new TurnTrain();
|
||||
case "DelayedAction":
|
||||
return DelayedAction.load(json);
|
||||
}
|
||||
LOG.error("Found unknwon action \"{}\" in json!",clazz);
|
||||
return null;
|
||||
public static List<Class<? extends Action>> list() {
|
||||
return List.of(
|
||||
ConditionalAction.class,
|
||||
SetSpeed.class,
|
||||
SetSignalsToStop.class,
|
||||
FreeStartBlock.class,
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class,
|
||||
SetRelay.class,
|
||||
DelayedAction.class
|
||||
);
|
||||
}
|
||||
|
||||
public Action load(JSONObject json) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Window properties(HashMap<String, String> params) {
|
||||
@@ -129,7 +117,16 @@ public abstract class Action implements Constants {
|
||||
return t("Nothing changed");
|
||||
}
|
||||
|
||||
public static Action get(int actionId) {
|
||||
return actions.get(actionId);
|
||||
public static Tag selector() {
|
||||
Select select = new Select(TYPE);
|
||||
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
for (Class<? extends Action> clazz : Action.list()) {
|
||||
String s = t(clazz.getSimpleName());
|
||||
names.put(s, clazz.getSimpleName());
|
||||
}
|
||||
|
||||
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||
return select.addTo(new Label(t("Action type:")+NBSP));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,6 @@ 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;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
public class ActionList extends Vector<Action> implements Constants{
|
||||
|
||||
@@ -55,20 +53,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
new Input(ID,id).hideIn(typeForm);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
||||
new Input(CONTEXT,context).hideIn(typeForm);
|
||||
Select select = new Select(TYPE);
|
||||
List<Class<? extends Action>> classes = List.of(
|
||||
ConditionalAction.class,
|
||||
SetSpeed.class,
|
||||
SetSignalsToStop.class,
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class,
|
||||
SetRelay.class,
|
||||
DelayedAction.class
|
||||
);
|
||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||
select.addTo(new Label(t("Action type:")+NBSP)).addTo(typeForm);
|
||||
Action.selector().addTo(typeForm);
|
||||
return new Button(t("Create action"),typeForm).addTo(typeForm).addTo(win);
|
||||
}
|
||||
|
||||
@@ -161,8 +146,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
ActionList actionList = new ActionList();
|
||||
for (Object o : list) {
|
||||
if (o instanceof JSONObject) {
|
||||
Action action = Action.load((JSONObject) o);
|
||||
if (action != null) actionList.add(action);
|
||||
JSONObject json = (JSONObject) o;
|
||||
Action action = Action.create(json.getString(TYPE));
|
||||
if (action != null) actionList.add(action.load(json));
|
||||
}
|
||||
}
|
||||
return actionList;
|
||||
|
||||
@@ -2,7 +2,6 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONArray;
|
||||
@@ -11,12 +10,10 @@ import org.json.JSONObject;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.conditions.Condition;
|
||||
import de.srsoftware.web4rail.conditions.TrainSelect;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
public class ConditionalAction extends Action {
|
||||
|
||||
@@ -50,10 +47,7 @@ public class ConditionalAction extends Action {
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
|
||||
Select select = new Select(REALM_CONDITION);
|
||||
List<Class<? extends Condition>> classes = List.of(TrainSelect.class);
|
||||
for (Class<? extends Condition> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||
select.addTo(form);
|
||||
Condition.selector().addTo(form);
|
||||
return new Button(t("Add condition"),form).addTo(form).addTo(fieldset);
|
||||
}
|
||||
|
||||
@@ -75,16 +69,17 @@ public class ConditionalAction extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static ConditionalAction load(JSONObject json) {
|
||||
ConditionalAction action = new ConditionalAction();
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||
if (o instanceof JSONObject) {
|
||||
Condition condition = Condition.load((JSONObject)o);
|
||||
if (condition != null) action.conditions.add(condition);
|
||||
if (condition != null) conditions.add(condition);
|
||||
}
|
||||
}
|
||||
action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return action;
|
||||
actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,16 +104,9 @@ public class ConditionalAction extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
String conditionClass = params.get(REALM_CONDITION);
|
||||
if (conditionClass != null) {
|
||||
switch (conditionClass) {
|
||||
case "TrainSelect":
|
||||
conditions.add(new TrainSelect());
|
||||
break;
|
||||
|
||||
default:
|
||||
return t("Unknown type of condition: {}",conditionClass);
|
||||
}
|
||||
}
|
||||
Condition condition = Condition.create(conditionClass);
|
||||
if (condition == null) return t("Unknown type of condition: {}",conditionClass);
|
||||
conditions.add(condition);
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,11 +70,11 @@ public class DelayedAction extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static DelayedAction load(JSONObject json) {
|
||||
DelayedAction action = new DelayedAction();
|
||||
action.delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return action;
|
||||
public DelayedAction load(JSONObject json) {
|
||||
super.load(json);
|
||||
delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FreeStartBlock extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
context.route.freeStartBlock();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -39,14 +39,15 @@ public class SetRelay extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static SetRelay load(JSONObject json) {
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
String relayId = json.getString(RELAY);
|
||||
SetRelay result = new SetRelay();
|
||||
if (relayId != null) {
|
||||
result.relay = Relay.get(relayId);
|
||||
result.state = json.getBoolean(Relay.STATE);
|
||||
relay = Relay.get(relayId);
|
||||
state = json.getBoolean(Relay.STATE);
|
||||
}
|
||||
return result;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,12 +14,7 @@ import de.srsoftware.web4rail.tags.Label;
|
||||
public class SetSpeed extends Action{
|
||||
|
||||
public static final String MAX_SPEED = "max_speed";
|
||||
private int maxSpeed = -1;
|
||||
|
||||
public SetSpeed(int kmh) {
|
||||
super();
|
||||
maxSpeed = kmh;
|
||||
}
|
||||
private int maxSpeed = 0;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
@@ -37,9 +32,12 @@ public class SetSpeed extends Action{
|
||||
return json;
|
||||
}
|
||||
|
||||
public static SetSpeed load(JSONObject json) {
|
||||
int s = json.getInt(MAX_SPEED);
|
||||
return new SetSpeed(s);
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
maxSpeed = json.getInt(MAX_SPEED);
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,6 +59,11 @@ public class SetSpeed extends Action{
|
||||
public String toString() {
|
||||
return t("Reduce speed to {} km/h",maxSpeed);
|
||||
}
|
||||
|
||||
public SetSpeed speed(int kmh) {
|
||||
maxSpeed = kmh;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
|
||||
Reference in New Issue
Block a user