This commit is contained in:
Stephan Richter
2020-12-05 22:54:43 +01:00
parent 00411d4398
commit fd12d2b515
12 changed files with 223 additions and 203 deletions

View File

@@ -81,7 +81,7 @@ public abstract class Action extends BaseClass {
return null;
}
public boolean equals(Action other) {
public boolean corresponsTo(Action other) {
return this.toString().equals(other.toString());
}

View File

@@ -58,30 +58,6 @@ public class ActionList extends Action implements Iterable<Action>{
return new Tag("span").content(t("Unknown action type: {}",type)).addTo(actionTypeForm());
}
public void addActionsFrom(ActionList other) {
for (Action otherAction : other.actions) {
//LOG.debug("old action ({}): {}",otherAction.getClass().getSimpleName(),otherAction);
boolean exists = false;
int len = actions.size();
for (int i=0; i<len; i++) {
Action thisAction = actions.get(i);
LOG.debug("→ {} ?",thisAction);
if (thisAction.equals(otherAction)) {
LOG.debug("Action already existing!");
exists = true;
break;
}
}
if (exists) {
LOG.debug("action not added.");
} else { // bestehemde Aktion der neuen Liste zuweisen
this.add(otherAction);
LOG.debug("action added.");
}
}
actions.forEach(action -> other.removeChild(action)); // zugewiesene Aktionen von alter Liste löschen
}
public void clear() {
while (!actions.isEmpty()) actions.firstElement().remove();
}
@@ -91,6 +67,10 @@ public class ActionList extends Action implements Iterable<Action>{
}
public boolean fire(Context context) {
if (context.invalidated()) {
LOG.debug("Context has been invalidated, aborting {}",this);
return false;
}
if (!isEmpty()) LOG.debug(t("Firing {}"),actions);
for (Action action : actions) {
if (!action.fire(context)) return false;
@@ -151,6 +131,20 @@ public class ActionList extends Action implements Iterable<Action>{
return this;
}
public void merge(ActionList oldActions) {
for (Action oldAction : oldActions.actions) {
for (Action newAction : actions) {
if (oldAction.corresponsTo(newAction)) {
actions.remove(newAction);
LOG.debug("new action {} replaced by {}",newAction,oldAction);
break;
}
}
add(oldAction);
}
oldActions.actions.clear();
}
public boolean moveUp(Action action) {
if (isNull(action)) return false;
if (actions.firstElement() == action && parent() instanceof ActionList) {

View File

@@ -19,13 +19,14 @@ public class PreserveRoute extends Action {
if (isNull(train)) return false;
if (isNull(route)) return false;
Range waitTime = route.endBlock().getWaitTime(train,route.endDirection);
// These are NOT errors:
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;
Range waitTime = route.endBlock().getWaitTime(train,route.endDirection);
if (waitTime.max > 0) return true; // train is expected to wait in next block.
train.reserveNext();
return true;
}

View File

@@ -75,10 +75,10 @@ public class SetRelay extends Action {
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
Id relayId = new Id(params.get(RELAY));
Id relayId = new Id(params.get(Relay.class.getSimpleName()));
relay = Relay.get(relayId);
String st = params.get(Relay.STATE);
if (isSet(st)) state = st.equals("true");
return properties();
return context().properties();
}
}

View File

@@ -19,6 +19,11 @@ public class SetSpeed extends Action{
public static final String MAX_SPEED = "max_speed";
private int speed = 0;
@Override
public boolean corresponsTo(Action other) {
return other instanceof SetSpeed;
}
@Override
public boolean fire(Context context) {
if (isNull(context.train())) return false;
@@ -66,10 +71,7 @@ public class SetSpeed extends Action{
try {
int s = Integer.parseInt(ms);
if (s<0) error = t("Speed must not be less than zero!");
if (error == null) {
this.speed = s;
return t("Action updated!");
}
if (isNull(error)) speed = s;
} catch (NumberFormatException e) {
error = t("Not a valid number!");
}