Browse Source

refactoring of load/save processes

lookup-tables
Stephan Richter 5 years ago
parent
commit
500af0a547
  1. 2
      pom.xml
  2. 4
      src/main/java/de/srsoftware/web4rail/BaseClass.java
  3. 163
      src/main/java/de/srsoftware/web4rail/Route.java
  4. 5
      src/main/java/de/srsoftware/web4rail/actions/Action.java
  5. 26
      src/main/java/de/srsoftware/web4rail/actions/ActionList.java
  6. 5
      src/main/java/de/srsoftware/web4rail/actions/ConditionalAction.java
  7. 8
      src/main/java/de/srsoftware/web4rail/actions/DelayedAction.java
  8. 30
      src/main/java/de/srsoftware/web4rail/conditions/ConditionList.java
  9. 23
      src/main/java/de/srsoftware/web4rail/tiles/Contact.java

2
pom.xml

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.2.10</version>
<version>1.2.11</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

4
src/main/java/de/srsoftware/web4rail/BaseClass.java

@ -352,7 +352,7 @@ public abstract class BaseClass implements Constants{ @@ -352,7 +352,7 @@ public abstract class BaseClass implements Constants{
protected Window properties(List<Fieldset> preForm,FormInput formInputs,List<Fieldset> postForm) {
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {} ({})",this.title(),id));
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this.title()));
preForm.forEach(fieldset -> fieldset.addTo(win));
@ -372,7 +372,7 @@ public abstract class BaseClass implements Constants{ @@ -372,7 +372,7 @@ public abstract class BaseClass implements Constants{
}
private String title() {
return getClass().getSimpleName();
return toString();
}
private Map<String,String> props(Map<String,String> additionalProps){

163
src/main/java/de/srsoftware/web4rail/Route.java

@ -53,20 +53,22 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -53,20 +53,22 @@ public class Route extends BaseClass implements Comparable<Route>{
private static final Logger LOG = LoggerFactory.getLogger(Route.class);
private static final String ACTIONS = "actions";
private static final String ACTION_LISTS = "action_lists";
private static final String BRAKE_TIMES = "brake_times";
private static final String CONDITIONS = "conditions";
private static final String CONDITION_LIST = "condition_list";
private static final String END_DIRECTION = "direction_end";
private static final String ROUTES = "routes";
private static final String SETUP_ACTIONS = "setup_actions";
private static final String START_ACTIONS = "start_actions";
private static final String START_DIRECTION = "direction_start";
private static final String TRIGGER = "trigger";
static final String NAME = "name";
static final String PATH = "path";
static final String SIGNALS = "signals";
static final String TURNOUTS = "turnouts";
private static final String ROUTE_START = "route_start";
private static final String ROUTE_SETUP = "route_setup";
private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze()
private class BrakeProcessor extends Thread {
@ -144,17 +146,10 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -144,17 +146,10 @@ public class Route extends BaseClass implements Comparable<Route>{
public Train train;
private HashMap<String,ActionList> triggers = new HashMap<String, ActionList>();
private HashMap<Turnout,Turnout.State> turnouts;
private ActionList setupActions;
private ActionList startActions;
private Block startBlock = null;
public Direction startDirection;
private HashSet<Contact> triggeredContacts = new HashSet<>();
public Route() {
setupActions = new ActionList(this);
startActions = new ActionList(this);
}
/**
* process commands from the client
* @param params
@ -253,10 +248,10 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -253,10 +248,10 @@ public class Route extends BaseClass implements Comparable<Route>{
Tag list = new Tag("ol");
Tag setup = new Tag("li").content(t("Setup actions")+NBSP);
setupActions.list().addTo(setup).addTo(list);
triggers.get(ROUTE_SETUP).list().addTo(setup).addTo(list);
Tag start = new Tag("li").content(t("Start actions")+NBSP);
startActions.list().addTo(start).addTo(list);
triggers.get(ROUTE_START).list().addTo(start).addTo(list);
for (Contact c : contacts) {
Tag item = c.link("span", c).addTo(new Tag("li")).content(NBSP);
@ -277,7 +272,6 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -277,7 +272,6 @@ public class Route extends BaseClass implements Comparable<Route>{
conditions.addAll(existingRoute.conditions);
setupActions.addActionsFrom(existingRoute.setupActions);
for (Entry<String, ActionList> entry : triggers.entrySet()) {
String trigger = entry.getKey();
ActionList existingActionList = existingRoute.triggers.get(trigger);
@ -376,8 +370,8 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -376,8 +370,8 @@ public class Route extends BaseClass implements Comparable<Route>{
add(lastContact.trigger(), new BrakeStop(this));
add(lastContact.trigger(), new FinishRoute(this));
}
for (Signal signal : signals) setupActions.add(new SetSignal(this).set(signal).to(Signal.GO));
startActions.add(new SetSpeed(this).to(999));
for (Signal signal : signals) add(ROUTE_SETUP,new SetSignal(this).set(signal).to(Signal.GO));
add(ROUTE_START,new SetSpeed(this).to(999));
return this;
}
@ -437,7 +431,7 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -437,7 +431,7 @@ public class Route extends BaseClass implements Comparable<Route>{
}
public boolean fireSetupActions(Context context) {
return setupActions.fire(context);
return triggers.get(ROUTE_SETUP).fire(context);
}
private String generateName() {
@ -492,21 +486,17 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -492,21 +486,17 @@ public class Route extends BaseClass implements Comparable<Route>{
json.put(BRAKE_TIMES, brakeTimes);
if (!conditions.isEmpty()) json.put(CONDITIONS, conditions.jsonArray());
if (!conditions.isEmpty()) {
json.put(CONDITION_LIST, conditions.json());
}
JSONArray jTriggers = new JSONArray();
JSONObject jActions = new JSONObject();
for (Entry<String, ActionList> entry : triggers.entrySet()) {
JSONObject trigger = new JSONObject();
trigger.put(TRIGGER, entry.getKey());
ActionList actionList = entry.getValue();
trigger.put(ACTIONS, actionList.jsonArray());
jTriggers.put(trigger);
String trigger = entry.getKey();
ActionList lst = entry.getValue();
jActions.put(trigger,lst.json());
}
if (!jTriggers.isEmpty()) json.put(ACTION_LISTS, jTriggers);
if (!setupActions.isEmpty()) json.put(SETUP_ACTIONS, setupActions.json());
if (!startActions.isEmpty()) json.put(START_ACTIONS, startActions.json());
json.put(ACTIONS, jActions);
String name = name();
if (isSet(name)) json.put(NAME, name);
@ -550,10 +540,96 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -550,10 +540,96 @@ public class Route extends BaseClass implements Comparable<Route>{
if (json.has(SIGNALS)) {
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get(new Id((String) signalId), false));
}
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS)).parent(this);
if (json.has(SETUP_ACTIONS)) setupActions.load(json.getJSONArray(SETUP_ACTIONS)).parent(this);
if (json.has(START_ACTIONS)) startActions.load(json.getJSONArray(START_ACTIONS)).parent(this);
if (json.has(ACTIONS)) {
loadActions(json.getJSONObject(ACTIONS));
}
if (json.has("action_lists")) { // TODO: this is legacy!
JSONArray jarr = json.getJSONArray("action_lists");
for (Object o : jarr) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
ActionList aList = new ActionList(this);
String trigger = jo.getString("trigger");
JSONArray jActions = jo.getJSONArray(ACTIONS);
for (Object ja : jActions) {
JSONObject jao = (JSONObject) ja;
String type = jao.getString(TYPE);
Action action = Action.create(type, aList);
if (isSet(action)) {
action.load(jao);
aList.add(action);
}
}
triggers.put(trigger, aList);
}
}
}
if (json.has("conditions")) { // TODO: this is legacy!
JSONArray jConditions = json.getJSONArray("conditions");
for (Object o : jConditions) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
String type = jo.getString(TYPE);
Condition condition = Condition.create(type);
if (isSet(condition)) {
condition.load(jo);
conditions.add(condition);
}
}
}
}
if (json.has(CONDITION_LIST)) conditions.load(json.getJSONObject(CONDITION_LIST)).parent(this);
if (json.has(SETUP_ACTIONS)) { // TODO: this is legacy!
Object so = json.get(SETUP_ACTIONS);
if (so instanceof JSONObject) {
JSONObject jo = (JSONObject) so;
ActionList setupActions = new ActionList(this);
setupActions.load(jo).parent(this);
triggers.put(ROUTE_SETUP, setupActions);
}
if (so instanceof JSONArray) {
JSONArray ja = (JSONArray) so;
ActionList setupActions = new ActionList(this);
for (Object o : ja) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
String type = jo.getString(TYPE);
Action action = Action.create(type, setupActions);
if (isSet(action)) {
action.load(jo);
setupActions.add(action);
}
}
}
triggers.put(ROUTE_SETUP, setupActions);
}
}
if (json.has(START_ACTIONS)) { // TODO: this is legacy!
Object so = json.get(START_ACTIONS);
if (so instanceof JSONObject) {
JSONObject jo = (JSONObject) so;
ActionList startActions = new ActionList(this);
startActions.load(jo).parent(this);
triggers.put(ROUTE_START, startActions);
}
if (so instanceof JSONArray) {
JSONArray ja = (JSONArray) so;
ActionList startActions = new ActionList(this);
for (Object o : ja) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
String type = jo.getString(TYPE);
Action action = Action.create(type, startActions);
if (isSet(action)) {
action.load(jo);
startActions.add(action);
}
}
}
triggers.put(ROUTE_START, startActions);
}
}
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
if (json.has(BRAKE_TIMES)) {
JSONObject dummy = json.getJSONObject(BRAKE_TIMES);
@ -562,12 +638,13 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -562,12 +638,13 @@ public class Route extends BaseClass implements Comparable<Route>{
return plan.registerRoute(this);
}
private void loadActions(JSONArray arr) {
for (int i=0; i<arr.length(); i++) {
JSONObject json = arr.getJSONObject(i);
String trigger = json.getString(TRIGGER);
ActionList actionList = new ActionList(this).load(json.getJSONArray(ACTIONS));
actionList.parent(this);
private void loadActions(JSONObject jsonObject) {
for (String trigger : jsonObject.keySet()) {
JSONObject json = jsonObject.getJSONObject(trigger);
String type = json.getString(TYPE);
ActionList actionList = Action.create(type, this);
if (isNull(actionList)) continue;
actionList.load(json);
triggers.put(trigger, actionList);
}
}
@ -578,7 +655,9 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -578,7 +655,9 @@ public class Route extends BaseClass implements Comparable<Route>{
JSONObject json = new JSONObject(tokener);
JSONArray routes = json.getJSONArray(ROUTES);
for (Object o : routes) {
if (o instanceof JSONObject) new Route().load((JSONObject)o, plan);
if (o instanceof JSONObject) {
new Route().load((JSONObject)o, plan);
}
}
fis.close();
}
@ -659,8 +738,6 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -659,8 +738,6 @@ public class Route extends BaseClass implements Comparable<Route>{
ActionList actionList = triggers.remove(key);
if (isSet(actionList)) actionList.remove();
};
setupActions.remove();
startActions.remove();
return this;
}
@ -674,8 +751,6 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -674,8 +751,6 @@ public class Route extends BaseClass implements Comparable<Route>{
if (child == train) train = null;
for (ActionList list : triggers.values()) list.removeChild(child);
turnouts.remove(child);
setupActions.removeChild(child);
startActions.removeChild(child);
if (child == startBlock) startBlock = null;
triggeredContacts.remove(child);
}
@ -771,7 +846,7 @@ public class Route extends BaseClass implements Comparable<Route>{ @@ -771,7 +846,7 @@ public class Route extends BaseClass implements Comparable<Route>{
public boolean train(Train newTrain) {
if (isSet(train) && newTrain != train) return false;
train = newTrain;
return isSet(train) ? startActions.fire(new Context(this).train(train)) : true;
return isSet(train) ? triggers.get(ROUTE_START).fire(new Context(this).train(train)) : true;
}
public Route unlock() throws IOException {

5
src/main/java/de/srsoftware/web4rail/actions/Action.java

@ -37,9 +37,10 @@ public abstract class Action extends BaseClass { @@ -37,9 +37,10 @@ public abstract class Action extends BaseClass {
return context;
}
public static Action create(String type,BaseClass parent) {
@SuppressWarnings("unchecked")
public static <T extends Action> T create(String type,BaseClass parent) {
try {
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor(BaseClass.class).newInstance(parent);
return (T) Class.forName(PREFIX+"."+type).getDeclaredConstructor(BaseClass.class).newInstance(parent);
} catch (Exception e) {
e.printStackTrace();
}

26
src/main/java/de/srsoftware/web4rail/actions/ActionList.java

@ -22,6 +22,7 @@ import de.srsoftware.web4rail.tags.Input; @@ -22,6 +22,7 @@ import de.srsoftware.web4rail.tags.Input;
public class ActionList extends Action implements Iterable<Action>{
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
private static final String ACTIONS = "actions";
protected Vector<Action> actions;
@ -104,14 +105,11 @@ public class ActionList extends Action implements Iterable<Action>{ @@ -104,14 +105,11 @@ public class ActionList extends Action implements Iterable<Action>{
@Override
public JSONObject json() {
String cls = getClass().getSimpleName();
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
}
public JSONArray jsonArray() {
JSONArray result = new JSONArray();
for (Action action : actions) result.put(action.json());
return result;
JSONObject json = super.json();
JSONArray jActions = new JSONArray();
actions.forEach(action -> jActions.put(action.json()));
json.put(ACTIONS,jActions);
return json;
}
public Tag list() {
@ -136,12 +134,16 @@ public class ActionList extends Action implements Iterable<Action>{ @@ -136,12 +134,16 @@ public class ActionList extends Action implements Iterable<Action>{
return span;
}
public ActionList load(JSONArray list) {
public Action load(JSONObject json) {
super.load(json);
if (json.has(ACTIONS)) {
JSONArray list = json.getJSONArray(ACTIONS);
for (Object o : list) {
if (o instanceof JSONObject) {
JSONObject json = (JSONObject) o;
Action action = Action.create(json.getString(TYPE),this);
if (action != null) add(action.load(json));
JSONObject jsonObject = (JSONObject) o;
Action action = Action.create(jsonObject.getString(TYPE),this);
if (action != null) add(action.load(jsonObject));
}
}
}
return this;

5
src/main/java/de/srsoftware/web4rail/actions/ConditionalAction.java

@ -15,7 +15,6 @@ import de.srsoftware.web4rail.tags.Fieldset; @@ -15,7 +15,6 @@ import de.srsoftware.web4rail.tags.Fieldset;
public class ConditionalAction extends ActionList {
private static final String CONDITIONS = "conditions";
private static final String ACTIONS = "actions";
private ConditionList conditions = new ConditionList();
public ConditionalAction(BaseClass parent) {
@ -41,13 +40,13 @@ public class ConditionalAction extends ActionList { @@ -41,13 +40,13 @@ public class ConditionalAction extends ActionList {
JSONArray conditions = new JSONArray();
for (Condition condition : this.conditions) conditions.put(condition.json());
json.put(CONDITIONS, conditions);
json.put(ACTIONS, super.jsonArray());
return json;
}
@Override
public Action load(JSONObject json) {
super.load(json);
if (json.has(CONDITIONS)) {
for (Object o : json.getJSONArray(CONDITIONS)) {
if (o instanceof JSONObject) {
JSONObject j = (JSONObject) o;
@ -58,7 +57,7 @@ public class ConditionalAction extends ActionList { @@ -58,7 +57,7 @@ public class ConditionalAction extends ActionList {
}
}
}
super.load(json.getJSONArray(ACTIONS));
}
return this;
}

8
src/main/java/de/srsoftware/web4rail/actions/DelayedAction.java

@ -13,8 +13,6 @@ import de.srsoftware.web4rail.tags.Input; @@ -13,8 +13,6 @@ import de.srsoftware.web4rail.tags.Input;
public class DelayedAction extends ActionList {
private static final String ACTIONS = "actions";
public static final String DELAY = "delay";
private static final int DEFAULT_DELAY = 1000;
private int delay = DEFAULT_DELAY;
@ -45,16 +43,12 @@ public class DelayedAction extends ActionList { @@ -45,16 +43,12 @@ public class DelayedAction extends ActionList {
@Override
public JSONObject json() {
JSONObject json = super.json();
json.put(DELAY, delay);
json.put(ACTIONS, jsonArray());
return json;
return super.json().put(DELAY, delay);
}
public DelayedAction load(JSONObject json) {
super.load(json);
delay = json.getInt(DELAY);
if (json.has(ACTIONS)) super.load(json.getJSONArray(ACTIONS));
return this;
}

30
src/main/java/de/srsoftware/web4rail/conditions/ConditionList.java

@ -18,6 +18,7 @@ import de.srsoftware.web4rail.tags.Input; @@ -18,6 +18,7 @@ import de.srsoftware.web4rail.tags.Input;
public class ConditionList extends Condition implements Iterable<Condition>{
private static final String CONDITIONS = "conditions";
private Vector<Condition> conditions = new Vector<Condition>();
public ConditionList add(Condition condition) {
@ -53,13 +54,10 @@ public class ConditionList extends Condition implements Iterable<Condition>{ @@ -53,13 +54,10 @@ public class ConditionList extends Condition implements Iterable<Condition>{
@Override
public JSONObject json() {
String cls = getClass().getSimpleName();
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
}
public JSONArray jsonArray() {
JSONArray json = new JSONArray();
for (Condition condition : conditions) json.put(condition.json());
JSONObject json = super.json();
JSONArray jConditions = new JSONArray();
conditions.stream().map(Condition::json).forEach(js -> jConditions.put(js));
json.put(CONDITIONS, jConditions);
return json;
}
@ -89,13 +87,17 @@ public class ConditionList extends Condition implements Iterable<Condition>{ @@ -89,13 +87,17 @@ public class ConditionList extends Condition implements Iterable<Condition>{
}
public ConditionList 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) {
condition.parent(this);
conditions.add(condition.load(json));
@Override
public Condition load(JSONObject json) {
super.load(json);
if (json.has(CONDITIONS)) {
JSONArray jConditions = json.getJSONArray(CONDITIONS);
for (Object o : jConditions) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
Condition condition = Condition.create(jo.getString(TYPE));
if (isSet(condition)) add(condition.load(jo));
}
}
}
return this;

23
src/main/java/de/srsoftware/web4rail/tiles/Contact.java

@ -8,11 +8,13 @@ import java.util.Map; @@ -8,11 +8,13 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.json.JSONArray;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.actions.Action;
import de.srsoftware.web4rail.actions.ActionList;
import de.srsoftware.web4rail.tags.Fieldset;
import de.srsoftware.web4rail.tags.Input;
@ -124,7 +126,26 @@ public class Contact extends Tile{ @@ -124,7 +126,26 @@ public class Contact extends Tile{
@Override
public Tile load(JSONObject json) {
if (json.has(ADDRESS)) addr(json.getInt(ADDRESS));
if (json.has(REALM_ACTIONS)) actions.load(json.getJSONArray(REALM_ACTIONS));
if (json.has(REALM_ACTIONS)) {
Object dummy = json.get(REALM_ACTIONS);
if (dummy instanceof JSONArray) {
JSONArray jarr = (JSONArray) dummy;
for (Object o : jarr) {
if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
String type = jo.getString("type");
Action action = Action.create(type, actions);
if (isSet(action)) {
action.load(jo);
actions.add(action);
}
}
}
}
if (dummy instanceof JSONObject) {
actions.load((JSONObject) dummy);
}
}
return super.load(json);
}

Loading…
Cancel
Save