started to implement or-conditions
This commit is contained in:
@@ -32,10 +32,10 @@ import de.srsoftware.web4rail.actions.PreserveRoute;
|
||||
import de.srsoftware.web4rail.actions.SetSignal;
|
||||
import de.srsoftware.web4rail.actions.SetSpeed;
|
||||
import de.srsoftware.web4rail.conditions.Condition;
|
||||
import de.srsoftware.web4rail.conditions.ConditionList;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -139,7 +139,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
private BrakeProcessor brakeProcessor = null;
|
||||
private HashMap<String,Integer> brakeTimes = new HashMap<String, Integer>();
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
private ConditionList conditions = new ConditionList();
|
||||
private Vector<Contact> contacts;
|
||||
private boolean disabled = false;
|
||||
private Block endBlock = null;
|
||||
@@ -223,6 +223,10 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
actions.add(action);
|
||||
}
|
||||
|
||||
public void add(Condition condition) {
|
||||
conditions.add(condition);
|
||||
}
|
||||
|
||||
private void addBasicPropertiesTo(Window win) {
|
||||
if (isSet(train)) link("span",Map.of(REALM,REALM_TRAIN,ID,train.id,ACTION,ACTION_PROPS),t("Train: {}",train)).addTo(win);
|
||||
new Tag("h4").content(t("Origin and destination")).addTo(win);
|
||||
@@ -255,26 +259,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
private void addConditionsTo(Window win) {
|
||||
new Tag("h4").content(t("Conditions")).addTo(win);
|
||||
if (!conditions.isEmpty()) {
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : conditions) {
|
||||
Tag li = new Tag("li");
|
||||
link("span",Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_PROPS,CONTEXT,REALM_ROUTE+":"+id),condition).addTo(li);
|
||||
Map<String, Object> params = Map.of(REALM,REALM_ROUTE,ID,id(),ACTION,DROP_CONDITION,REALM_CONDITION,condition.id());
|
||||
new Button(t("delete"), params).addTo(li.content(NBSP)).addTo(list);
|
||||
}
|
||||
list.addTo(win);
|
||||
}
|
||||
|
||||
new Tag("div").content(t("Route will only be available, if all conditions are fulfilled.")).addTo(win);
|
||||
Form form = new Form("action-prop-form-"+id);
|
||||
Fieldset fieldset = new Fieldset(t("Add condition"));
|
||||
new Input(REALM,REALM_ROUTE).hideIn(form);
|
||||
new Input(ID,id()).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
|
||||
Condition.selector().addTo(fieldset);
|
||||
new Button(t("Add condition"),form).addTo(fieldset).addTo(form).addTo(win);
|
||||
conditions.tag(REALM_ROUTE+":"+id()).addTo(win);
|
||||
}
|
||||
|
||||
private void addContactsTo(Window win) {
|
||||
@@ -361,10 +347,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
*/
|
||||
public boolean allowed(Context context) {
|
||||
if (disabled) return false;
|
||||
for (Condition condition : conditions) {
|
||||
if (!condition.fulfilledBy(context)) return false;
|
||||
}
|
||||
return true;
|
||||
return conditions.fulfilledBy(context);
|
||||
}
|
||||
|
||||
public Route begin(Block block,Direction to) {
|
||||
@@ -459,15 +442,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
private Object dropCodition(HashMap<String, String> params) {
|
||||
String condId = params.get(REALM_CONDITION);
|
||||
if (isSet(condId)) {
|
||||
int cid = Integer.parseInt(condId);
|
||||
for (Condition condition : conditions) {
|
||||
if (condition.id() == cid) {
|
||||
conditions.remove(condition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isSet(condId)) conditions.removeById(Integer.parseInt(condId));
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
@@ -556,9 +531,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
json.put(BRAKE_TIMES, brakeTimes);
|
||||
|
||||
JSONArray jConditions = new JSONArray();
|
||||
for (Condition condition : conditions) jConditions.put(condition.json());
|
||||
if (!jConditions.isEmpty()) json.put(CONDITIONS, jConditions);
|
||||
if (!conditions.isEmpty()) json.put(CONDITIONS, conditions.json());
|
||||
|
||||
JSONArray jTriggers = new JSONArray();
|
||||
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
||||
@@ -617,7 +590,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get((String) signalId, false));
|
||||
}
|
||||
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
|
||||
if (json.has(CONDITIONS)) loadConditions(json.getJSONArray(CONDITIONS));
|
||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS));
|
||||
if (json.has(SETUP_ACTIONS)) setupActions = ActionList.load(json.getJSONArray(SETUP_ACTIONS));
|
||||
if (json.has(START_ACTIONS)) startActions = ActionList.load(json.getJSONArray(START_ACTIONS));
|
||||
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
|
||||
@@ -647,15 +620,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
}
|
||||
fis.close();
|
||||
}
|
||||
|
||||
private void loadConditions(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (isSet(condition)) conditions.add(condition.parent(this).load(json));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean lock() {
|
||||
return lockIgnoring(null);
|
||||
}
|
||||
|
||||
@@ -45,27 +45,62 @@ public abstract class Condition extends BaseClass {
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params,Plan plan) {
|
||||
if (!params.containsKey(ID)) return t("No id passed to Condition.action!");
|
||||
int cid = Integer.parseInt(params.get(ID));
|
||||
Condition condition = conditions.get(cid);
|
||||
if (condition == null) return t("No condition with id {}!",cid);
|
||||
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to Condition.action!");
|
||||
|
||||
Integer cid = (params.containsKey(ID)) ? Integer.parseInt(params.get(ID)) : null;
|
||||
|
||||
if (isSet(cid)) {
|
||||
Condition condition = conditions.get(cid);
|
||||
if (condition == null) return t("No condition with id {}!",cid);
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties(params);
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return plan.showContext(params);
|
||||
case ACTION_DROP:
|
||||
condition.drop();
|
||||
return plan.showContext(params);
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties(params);
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return plan.showContext(params);
|
||||
case ACTION_DROP:
|
||||
condition.drop();
|
||||
return plan.showContext(params);
|
||||
case ACTION_ADD:
|
||||
return addCondition(params);
|
||||
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
|
||||
}
|
||||
|
||||
private static Object addCondition(HashMap<String, String> params) {
|
||||
String type = params.get(REALM_CONDITION);
|
||||
String context = params.get(CONTEXT);
|
||||
if (isNull(type)) return t("No type supplied to addCondition!");
|
||||
if (isNull(context)) return t("No context supplied to addCondtion!");
|
||||
|
||||
Condition condition = Condition.create(type);
|
||||
if (isNull(condition)) return t("Unknown type \"{}\" of condition!",type);
|
||||
String[] parts = context.split(":");
|
||||
String contextId = parts[1];
|
||||
String realm = parts[0];
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
Route route = plan.route(Integer.parseInt(contextId));
|
||||
if (isNull(route)) return t("Unknown route: {}",contextId);
|
||||
route.add(condition);
|
||||
return route.properties(new HashMap<String,String>(Map.of(REALM,REALM_ROUTE,ACTION,ACTION_PROPS,ID,contextId)));
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return t("Cannot handle context of type {} in addCondition!");
|
||||
}
|
||||
|
||||
public static Condition create(String type) {
|
||||
if (type == null) return null;
|
||||
try {
|
||||
@@ -117,6 +152,7 @@ public abstract class Condition extends BaseClass {
|
||||
private static List<Class<? extends Condition>> list() {
|
||||
return List.of(
|
||||
BlockFree.class,
|
||||
OrCondition.class,
|
||||
PushPullTrain.class,
|
||||
TrainHasTag.class,
|
||||
TrainSelect.class,
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
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;
|
||||
|
||||
public class ConditionList extends Vector<Condition> implements Constants{
|
||||
|
||||
private static final long serialVersionUID = 5826717120751473807L;
|
||||
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : this) {
|
||||
if (!condition.fulfilledBy(context)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public JSONArray json() {
|
||||
JSONArray json = new JSONArray();
|
||||
for (Condition condition : this) json.put(condition.json());
|
||||
return json;
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (condition != null) add(condition.parent(this).load(json));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeById(int cid) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.id() == cid) {
|
||||
remove(condition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Tag tag(String context) {
|
||||
if (context != null) {
|
||||
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : this) {
|
||||
condition.link(condition.toString(),"li",context).addTo(list);
|
||||
}
|
||||
Tag div = list.addTo(new Tag("div"));
|
||||
|
||||
Form form = new Form("add-condition-form");
|
||||
new Input(REALM, REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(form);
|
||||
new Input(CONTEXT,context).hideIn(form);
|
||||
Condition.selector().addTo(form);
|
||||
new Button(t("Add condition"), form).addTo(form).addTo(div);
|
||||
|
||||
return div;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
private static String t(String tx, Object...fills) {
|
||||
return Translation.get(Application.class, tx, fills);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
|
||||
public class OrCondition extends Condition{
|
||||
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private ConditionList conditions = new ConditionList();
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : conditions) {
|
||||
if (condition.fulfilledBy(context)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(CONDITIONS, conditions.json());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(HashMap<String, String> params) {
|
||||
Window win = super.properties(params);
|
||||
|
||||
win.children().insertElementAt(conditions.tag(REALM_CONDITION+":"+id()),2);
|
||||
return win;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return conditions.isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", conditions.stream().map(Object::toString).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
|
||||
public abstract class Bridge extends Tile {
|
||||
private static Bridge pendingConnection = null;
|
||||
|
||||
Reference in New Issue
Block a user