working on conditional actions

This commit is contained in:
Stephan Richter
2020-10-29 13:41:12 +01:00
parent daae49296f
commit 5379bd9da7
15 changed files with 295 additions and 44 deletions

View File

@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
@@ -10,16 +11,17 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Constants;
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.tiles.Contact;
public abstract class Action {
public abstract class Action implements Constants {
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
private static final String TYPE = "type";
private int id;
public static class Context {
public Plan plan = null;
@@ -34,8 +36,17 @@ public abstract class Action {
train = route.train;
}
}
public Action() {
id = new Date().hashCode();
}
public abstract boolean fire(Context context) throws IOException;
public int id() {
return id;
}
public abstract void fire(Context context) throws IOException;
public JSONObject json() {
JSONObject json = new JSONObject();

View File

@@ -9,7 +9,8 @@ public class ActivateRoute extends RouteAction {
}
@Override
public void fire(Context context) throws IOException {
public boolean fire(Context context) throws IOException {
context.route.activate();
return true;
}
}

View File

@@ -0,0 +1,93 @@
package de.srsoftware.web4rail.actions;
import static de.srsoftware.web4rail.Constants.TYPE;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Route;
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.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Select;
import de.srsoftware.web4rail.tiles.Contact;
public class ConditionalAction extends Action {
private Vector<Condition> conditions = new Vector<Condition>();
private Vector<Action> actions = new Vector<Action>();
private ConditionalAction addCondition(Condition condition) {
conditions.add(new TrainSelect());
return this;
}
private static void addToContact(Route route, Contact contact, String conditionType) {
Condition condition = null;
switch (conditionType) {
case "TrainSelect":
condition = new TrainSelect();
break;
default: return;
}
route.addAction(contact.trigger(), new ConditionalAction().addCondition(condition));
}
@Override
public boolean fire(Context context) throws IOException {
for (Condition condition : conditions) {
if (condition.fulfilledBy(context)) return fireActions(context);
}
return false;
}
private boolean fireActions(Context context) {
for (Action action : actions) try {
action.fire(context);
} catch (IOException e) {
LOG.warn("Was not able to fire {}",action);
}
return true;
}
public static Window propForm(HashMap<String, String> params, Route route, Contact contact) {
String condition = params.get(REALM_CONDITION);
if (condition != null) {
addToContact(route,contact,condition);
return route.properties();
}
Window win = Action.propForm(params);
String formId = "add-action-to-contact-"+contact.id();
Tag form = new Form(formId);
new Tag("div").content(t("Add Action {} to contact {} on route {}:",ConditionalAction.class.getSimpleName(),contact,route)).addTo(win);
new Input(REALM, REALM_ROUTE).hideIn(form);
new Input(ID,route.id()).hideIn(form);
new Input(ACTION,ACTION_ADD_ACTION).hideIn(form);
new Input(CONTACT,contact.id()).hideIn(form);
new Input(TYPE,ConditionalAction.class.getSimpleName()).hideIn(form);
Select select = new Select(REALM_CONDITION);
List<Class<? extends Condition>> conditionTypes = List.of(TrainSelect.class);
for (Class<? extends Condition> clazz : conditionTypes) select.addOption(clazz.getSimpleName());
select.addTo(form);
new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
return win;
}
@Override
public String toString() {
if (conditions.isEmpty()) return t("Invalid condition");
StringBuffer sb = new StringBuffer();
for (int i = 0; i<conditions.size(); i++) {
Condition condition = conditions.get(i);
Tag link = condition.link("span");
sb.append(link);
}
return t("if ({}):",sb);
}
}

View File

@@ -9,7 +9,8 @@ public class FinishRoute extends RouteAction {
}
@Override
public void fire(Context context) throws IOException {
public boolean fire(Context context) throws IOException {
context.route.finish();
return true;
}
}

View File

@@ -11,7 +11,8 @@ public class SetSignalsToStop extends RouteAction {
}
@Override
public void fire(Context context) throws IOException {
public boolean fire(Context context) throws IOException {
context.route.setSignals(Signal.STOP);
return true;
}
}

View File

@@ -25,8 +25,12 @@ public class SpeedReduction extends RouteAction implements Constants{
}
@Override
public void fire(Context context) {
if (context.train != null && context.train.speed > maxSpeed) context.train.setSpeed(maxSpeed);
public boolean fire(Context context) {
if (context.train != null && context.train.speed > maxSpeed) {
context.train.setSpeed(maxSpeed);
return true;
}
return false;
}
@Override

View File

@@ -9,7 +9,11 @@ public class TurnTrain extends RouteAction implements Constants{
}
@Override
public void fire(Context context) {
if (context.train != null) context.train.turn();
public boolean fire(Context context) {
if (context.train != null) {
context.train.turn();
return true;
}
return false;
}
}