started to implement or-conditions
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user