use of jmri.ConditionalManager in project JMRI by JMRI.
the class DefaultConditionalManagerTest method testCreate.
public void testCreate() {
ConditionalManager m = new DefaultConditionalManager();
Conditional c1 = m.createNewConditional("IX01C01", "");
Conditional c2 = m.createNewConditional("IX01C02", "");
Assert.assertFalse(c1 == c2);
Assert.assertFalse(c1.equals(c2));
}
use of jmri.ConditionalManager in project JMRI by JMRI.
the class DefaultConditional method takeActionIfNeeded.
/**
* Compares action options, and takes action if appropriate
* <P>
* Only get here if a change in state has occurred when calculating this
* Conditional
*/
@SuppressWarnings({ "deprecation", "fallthrough" })
@SuppressFBWarnings(value = "SF_SWITCH_FALLTHROUGH")
private // have to apply to more than 500 lines of code - jake
void takeActionIfNeeded() {
if (log.isTraceEnabled()) {
log.trace("takeActionIfNeeded starts for " + getSystemName());
}
int actionCount = 0;
int actionNeeded = 0;
int act = 0;
int state = 0;
ArrayList<String> errorList = new ArrayList<>();
// Use a local copy of state to guarantee the entire list of actions will be fired off
// before a state change occurs that may block their completion.
int currentState = _currentState;
for (int i = 0; i < _actionList.size(); i++) {
ConditionalAction action = _actionList.get(i);
int neededAction = actionNeeded;
int option = action.getOption();
if (log.isTraceEnabled()) {
log.trace(" takeActionIfNeeded considers action " + i + " with currentState: " + currentState + " and option: " + option);
}
if (((currentState == TRUE) && (option == ACTION_OPTION_ON_CHANGE_TO_TRUE)) || ((currentState == FALSE) && (option == ACTION_OPTION_ON_CHANGE_TO_FALSE)) || (option == ACTION_OPTION_ON_CHANGE)) {
// need to take this action
actionNeeded++;
SignalHead h = null;
SignalMast f = null;
Logix x = null;
Light lgt = null;
Warrant w = null;
NamedBean nb = null;
if (action.getNamedBean() != null) {
nb = action.getNamedBean().getBean();
}
int value = 0;
Timer timer = null;
int type = action.getType();
String devName = getDeviceName(action);
if (devName == null) {
errorList.add("invalid memory name in action - " + action);
continue;
}
if (log.isDebugEnabled()) {
log.debug("getDeviceName()=" + action.getDeviceName() + " devName= " + devName);
}
switch(type) {
case Conditional.ACTION_NONE:
break;
case Conditional.ACTION_SET_TURNOUT:
Turnout t = (Turnout) nb;
if (t == null) {
errorList.add("invalid turnout name in action - " + action.getDeviceName());
} else {
act = action.getActionData();
if (act == Route.TOGGLE) {
state = t.getKnownState();
if (state == Turnout.CLOSED) {
act = Turnout.THROWN;
} else {
act = Turnout.CLOSED;
}
}
t.setCommandedState(act);
actionCount++;
}
break;
case Conditional.ACTION_RESET_DELAYED_TURNOUT:
action.stopTimer();
// fall through
case Conditional.ACTION_DELAYED_TURNOUT:
if (!action.isTimerActive()) {
// Create a timer if one does not exist
timer = action.getTimer();
if (timer == null) {
action.setListener(new TimeTurnout(i));
timer = new Timer(2000, action.getListener());
timer.setRepeats(true);
}
// Start the Timer to set the turnout
value = getMillisecondValue(action);
if (value < 0) {
break;
}
timer.setInitialDelay(value);
action.setTimer(timer);
action.startTimer();
actionCount++;
} else {
log.warn("timer already active on request to start delayed turnout action - " + devName);
}
break;
case Conditional.ACTION_CANCEL_TURNOUT_TIMERS:
ConditionalManager cmg = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
java.util.Iterator<String> iter = cmg.getSystemNameList().iterator();
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
errorList.add("Conditional system name null during cancel turnout timers for " + action.getDeviceName());
}
Conditional c = cmg.getBySystemName(sname);
if (c == null) {
errorList.add("Conditional null during cancel turnout timers for " + action.getDeviceName());
} else {
c.cancelTurnoutTimer(devName);
actionCount++;
}
}
break;
case Conditional.ACTION_LOCK_TURNOUT:
Turnout tl = (Turnout) nb;
if (tl == null) {
errorList.add("invalid turnout name in action - " + action.getDeviceName());
} else {
act = action.getActionData();
if (act == Route.TOGGLE) {
if (tl.getLocked(Turnout.CABLOCKOUT)) {
act = Turnout.UNLOCKED;
} else {
act = Turnout.LOCKED;
}
}
if (act == Turnout.LOCKED) {
tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
} else if (act == Turnout.UNLOCKED) {
tl.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, false);
}
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNAL_APPEARANCE:
h = (SignalHead) nb;
if (h == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
h.setAppearance(action.getActionData());
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNAL_HELD:
h = (SignalHead) nb;
if (h == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
h.setHeld(true);
actionCount++;
}
break;
case Conditional.ACTION_CLEAR_SIGNAL_HELD:
h = (SignalHead) nb;
if (h == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
h.setHeld(false);
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNAL_DARK:
h = (SignalHead) nb;
if (h == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
h.setLit(false);
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNAL_LIT:
h = (SignalHead) nb;
if (h == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
h.setLit(true);
actionCount++;
}
break;
case Conditional.ACTION_TRIGGER_ROUTE:
Route r = (Route) nb;
if (r == null) {
errorList.add("invalid Route name in action - " + action.getDeviceName());
} else {
r.setRoute();
actionCount++;
}
break;
case Conditional.ACTION_SET_SENSOR:
Sensor sn = (Sensor) nb;
if (sn == null) {
errorList.add("invalid Sensor name in action - " + action.getDeviceName());
} else {
act = action.getActionData();
if (act == Route.TOGGLE) {
state = sn.getState();
if (state == Sensor.ACTIVE) {
act = Sensor.INACTIVE;
} else {
act = Sensor.ACTIVE;
}
}
try {
sn.setKnownState(act);
actionCount++;
} catch (JmriException e) {
log.warn("Exception setting Sensor " + devName + " in action");
}
}
break;
case Conditional.ACTION_RESET_DELAYED_SENSOR:
action.stopTimer();
// fall through
case Conditional.ACTION_DELAYED_SENSOR:
if (!action.isTimerActive()) {
// Create a timer if one does not exist
timer = action.getTimer();
if (timer == null) {
action.setListener(new TimeSensor(i));
timer = new Timer(2000, action.getListener());
timer.setRepeats(true);
}
// Start the Timer to set the turnout
value = getMillisecondValue(action);
if (value < 0) {
break;
}
timer.setInitialDelay(value);
action.setTimer(timer);
action.startTimer();
actionCount++;
} else {
log.warn("timer already active on request to start delayed sensor action - " + devName);
}
break;
case Conditional.ACTION_CANCEL_SENSOR_TIMERS:
ConditionalManager cm = jmri.InstanceManager.getDefault(jmri.ConditionalManager.class);
java.util.Iterator<String> itr = cm.getSystemNameList().iterator();
while (itr.hasNext()) {
String sname = itr.next();
if (sname == null) {
errorList.add("Conditional system name null during cancel sensor timers for " + action.getDeviceName());
}
Conditional c = cm.getBySystemName(sname);
if (c == null) {
errorList.add("Conditional null during cancel sensor timers for " + action.getDeviceName());
} else {
c.cancelSensorTimer(devName);
actionCount++;
}
}
break;
case Conditional.ACTION_SET_LIGHT:
lgt = (Light) nb;
if (lgt == null) {
errorList.add("invalid light name in action - " + action.getDeviceName());
} else {
act = action.getActionData();
if (act == Route.TOGGLE) {
state = lgt.getState();
if (state == Light.ON) {
act = Light.OFF;
} else {
act = Light.ON;
}
}
lgt.setState(act);
actionCount++;
}
break;
case Conditional.ACTION_SET_LIGHT_INTENSITY:
lgt = (Light) nb;
if (lgt == null) {
errorList.add("invalid light name in action - " + action.getDeviceName());
} else {
try {
value = getIntegerValue(action);
if (value < 0) {
break;
}
lgt.setTargetIntensity((value) / 100.0);
actionCount++;
} catch (IllegalArgumentException e) {
errorList.add("Exception in set light intensity action - " + action.getDeviceName());
}
}
break;
case Conditional.ACTION_SET_LIGHT_TRANSITION_TIME:
lgt = (Light) nb;
if (lgt == null) {
errorList.add("invalid light name in action - " + action.getDeviceName());
} else {
try {
value = getIntegerValue(action);
if (value < 0) {
break;
}
lgt.setTransitionTime(value);
actionCount++;
} catch (IllegalArgumentException e) {
errorList.add("Exception in set light transition time action - " + action.getDeviceName());
}
}
break;
case Conditional.ACTION_SET_MEMORY:
Memory m = (Memory) nb;
if (m == null) {
errorList.add("invalid memory name in action - " + action.getDeviceName());
} else {
m.setValue(action.getActionString());
actionCount++;
}
break;
case Conditional.ACTION_COPY_MEMORY:
Memory mFrom = (Memory) nb;
if (mFrom == null) {
errorList.add("invalid memory name in action - " + action.getDeviceName());
} else {
Memory mTo = getMemory(action.getActionString());
if (mTo == null) {
errorList.add("invalid memory name in action - " + action.getActionString());
} else {
mTo.setValue(mFrom.getValue());
actionCount++;
}
}
break;
case Conditional.ACTION_ENABLE_LOGIX:
x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
if (x == null) {
errorList.add("invalid logix name in action - " + action.getDeviceName());
} else {
x.setEnabled(true);
actionCount++;
}
break;
case Conditional.ACTION_DISABLE_LOGIX:
x = InstanceManager.getDefault(jmri.LogixManager.class).getLogix(devName);
if (x == null) {
errorList.add("invalid logix name in action - " + action.getDeviceName());
} else {
x.setEnabled(false);
actionCount++;
}
break;
case Conditional.ACTION_PLAY_SOUND:
String path = getActionString(action);
if (!path.equals("")) {
Sound sound = action.getSound();
if (sound == null) {
try {
sound = new Sound(path);
} catch (NullPointerException ex) {
errorList.add("invalid path to sound: " + path);
}
}
if (sound != null) {
sound.play();
}
actionCount++;
}
break;
case Conditional.ACTION_RUN_SCRIPT:
if (!(getActionString(action).equals(""))) {
JmriScriptEngineManager.getDefault().runScript(new File(jmri.util.FileUtil.getExternalFilename(getActionString(action))));
actionCount++;
}
break;
case Conditional.ACTION_SET_FAST_CLOCK_TIME:
Date date = InstanceManager.getDefault(jmri.Timebase.class).getTime();
date.setHours(action.getActionData() / 60);
date.setMinutes(action.getActionData() - ((action.getActionData() / 60) * 60));
date.setSeconds(0);
InstanceManager.getDefault(jmri.Timebase.class).userSetTime(date);
actionCount++;
break;
case Conditional.ACTION_START_FAST_CLOCK:
InstanceManager.getDefault(jmri.Timebase.class).setRun(true);
actionCount++;
break;
case Conditional.ACTION_STOP_FAST_CLOCK:
InstanceManager.getDefault(jmri.Timebase.class).setRun(false);
actionCount++;
break;
case Conditional.ACTION_CONTROL_AUDIO:
Audio audio = InstanceManager.getDefault(jmri.AudioManager.class).getAudio(devName);
if (audio.getSubType() == Audio.SOURCE) {
AudioSource audioSource = (AudioSource) audio;
switch(action.getActionData()) {
case Audio.CMD_PLAY:
audioSource.play();
break;
case Audio.CMD_STOP:
audioSource.stop();
break;
case Audio.CMD_PLAY_TOGGLE:
audioSource.togglePlay();
break;
case Audio.CMD_PAUSE:
audioSource.pause();
break;
case Audio.CMD_RESUME:
audioSource.resume();
break;
case Audio.CMD_PAUSE_TOGGLE:
audioSource.togglePause();
break;
case Audio.CMD_REWIND:
audioSource.rewind();
break;
case Audio.CMD_FADE_IN:
audioSource.fadeIn();
break;
case Audio.CMD_FADE_OUT:
audioSource.fadeOut();
break;
case Audio.CMD_RESET_POSITION:
audioSource.resetCurrentPosition();
break;
default:
break;
}
} else if (audio.getSubType() == Audio.LISTENER) {
AudioListener audioListener = (AudioListener) audio;
switch(action.getActionData()) {
case Audio.CMD_RESET_POSITION:
audioListener.resetCurrentPosition();
break;
default:
// nothing needed for others
break;
}
}
break;
case Conditional.ACTION_JYTHON_COMMAND:
if (!(getActionString(action).isEmpty())) {
// add the text to the output frame
ScriptOutput.writeScript(getActionString(action));
// and execute
try {
JmriScriptEngineManager.getDefault().eval(getActionString(action), JmriScriptEngineManager.getDefault().getEngine(JmriScriptEngineManager.PYTHON));
} catch (ScriptException ex) {
log.error("Error executing script:", ex);
}
actionCount++;
}
break;
case Conditional.ACTION_ALLOCATE_WARRANT_ROUTE:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
String msg = w.allocateRoute(null);
if (msg != null) {
log.info("Warrant " + action.getDeviceName() + " - " + msg);
}
actionCount++;
}
break;
case Conditional.ACTION_DEALLOCATE_WARRANT_ROUTE:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
w.deAllocate();
actionCount++;
}
break;
case Conditional.ACTION_SET_ROUTE_TURNOUTS:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
String msg = w.setRoute(0, null);
if (msg != null) {
log.info("Warrant " + action.getDeviceName() + " unable to Set Route - " + msg);
}
actionCount++;
}
break;
case Conditional.ACTION_THROTTLE_FACTOR:
log.info("Set warrant Throttle Factor deprecated - Use Warrrant Preferences");
break;
case Conditional.ACTION_SET_TRAIN_ID:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
if (!w.setTrainId(getActionString(action))) {
String s = "Unable to find train Id " + getActionString(action) + " in Roster - " + action.getDeviceName();
log.info(s);
// could be serious - display error to UI
errorList.add(s);
}
actionCount++;
}
break;
case Conditional.ACTION_SET_TRAIN_NAME:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
w.setTrainName(getActionString(action));
actionCount++;
}
break;
case Conditional.ACTION_AUTO_RUN_WARRANT:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
jmri.jmrit.logix.WarrantTableFrame frame = jmri.jmrit.logix.WarrantTableFrame.getDefault();
String err = frame.runTrain(w, Warrant.MODE_RUN);
if (err != null) {
w.stopWarrant(true);
}
if (err != null) {
errorList.add("runAutoTrain error - " + err);
w.stopWarrant(true);
}
actionCount++;
}
break;
case Conditional.ACTION_MANUAL_RUN_WARRANT:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
String err = w.setRoute(0, null);
if (err == null) {
err = w.setRunMode(Warrant.MODE_MANUAL, null, null, null, false);
}
if (err != null) {
errorList.add("runManualTrain error - " + err);
}
actionCount++;
}
break;
case Conditional.ACTION_CONTROL_TRAIN:
w = (Warrant) nb;
if (w == null) {
errorList.add("invalid Warrant name in action - " + action.getDeviceName());
} else {
if (!w.controlRunTrain(action.getActionData())) {
log.info("Train " + w.getTrainId() + " not running - " + devName);
}
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNALMAST_ASPECT:
f = (SignalMast) nb;
if (f == null) {
errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
} else {
f.setAspect(getActionString(action));
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNALMAST_HELD:
f = (SignalMast) nb;
if (f == null) {
errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
} else {
f.setHeld(true);
actionCount++;
}
break;
case Conditional.ACTION_CLEAR_SIGNALMAST_HELD:
f = (SignalMast) nb;
if (f == null) {
errorList.add("invalid Signal Mast name in action - " + action.getDeviceName());
} else {
f.setHeld(false);
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNALMAST_DARK:
f = (SignalMast) nb;
if (f == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
f.setLit(false);
actionCount++;
}
break;
case Conditional.ACTION_SET_SIGNALMAST_LIT:
f = (SignalMast) nb;
if (f == null) {
errorList.add("invalid Signal Head name in action - " + action.getDeviceName());
} else {
f.setLit(true);
actionCount++;
}
break;
case Conditional.ACTION_SET_BLOCK_VALUE:
OBlock b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.setValue(getActionString(action));
actionCount++;
}
break;
case Conditional.ACTION_SET_BLOCK_ERROR:
b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.setError(true);
actionCount++;
}
break;
case Conditional.ACTION_CLEAR_BLOCK_ERROR:
b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.setError(false);
}
break;
case ACTION_DEALLOCATE_BLOCK:
b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.deAllocate(null);
actionCount++;
}
break;
case ACTION_SET_BLOCK_OUT_OF_SERVICE:
b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.setOutOfService(true);
actionCount++;
}
break;
case ACTION_SET_BLOCK_IN_SERVICE:
b = (OBlock) nb;
if (b == null) {
errorList.add("invalid Block name in action - " + action.getDeviceName());
} else {
b.setOutOfService(false);
actionCount++;
}
break;
default:
log.warn("takeActionIfNeeded drops through switch statement for action " + i + " of " + getSystemName());
break;
}
}
if (PARKS_DEBUG) {
System.out.println("Global state= " + _currentState + " Local state= " + currentState + " - Action " + (actionNeeded > neededAction ? "WAS" : "NOT") + " taken for action = " + action.getTypeString() + " " + action.getActionString() + " for device " + action.getDeviceName());
}
}
if (errorList.size() > 0) {
for (int i = 0; i < errorList.size(); i++) {
log.error(getDisplayName() + " - " + errorList.get(i));
}
if (!GraphicsEnvironment.isHeadless()) {
java.awt.Toolkit.getDefaultToolkit().beep();
if (!_skipErrorDialog) {
new ErrorDialog(errorList, this);
}
}
}
if (log.isDebugEnabled()) {
log.debug("Conditional \"" + getUserName() + "\" (" + getSystemName() + " has " + _actionList.size() + " actions and has executed " + actionCount + " actions of " + actionNeeded + " actions needed on state change to " + currentState);
}
if (PARKS_DEBUG) {
System.out.println("Conditional \"" + getUserName() + "\" (" + getSystemName() + " has " + _actionList.size() + " actions and has executed " + actionCount + " actions of " + actionNeeded + " actions needed on state change to " + currentState);
}
}
use of jmri.ConditionalManager in project JMRI by JMRI.
the class DefaultConditionalManagerXml method loadConditionals.
/**
* Utility method to load the individual Logix objects. If there's no
* additional info needed for a specific logix type, invoke this with the
* parent of the set of Logix elements.
*
* @param conditionals Element containing the Logix elements to load.
*/
public void loadConditionals(Element conditionals) {
List<Element> conditionalList = conditionals.getChildren("conditional");
if (log.isDebugEnabled()) {
log.debug("Found " + conditionalList.size() + " conditionals");
}
ConditionalManager tm = InstanceManager.getDefault(jmri.ConditionalManager.class);
for (int i = 0; i < conditionalList.size(); i++) {
Element condElem = conditionalList.get(i);
String sysName = getSystemName(condElem);
if (sysName == null) {
log.warn("unexpected null in systemName " + condElem);
break;
}
// omitted username is treated as empty, not null
String userName = getUserName(condElem);
if (userName == null) {
userName = "";
}
if (log.isDebugEnabled()) {
log.debug("create conditional: ({})({})", sysName, userName);
}
// Try getting the conditional. This should fail
Conditional c = tm.getBySystemName(sysName);
if (c == null) {
// Check for parent Logix
Logix x = tm.getParentLogix(sysName);
if (x == null) {
log.warn("Conditional '{}' has no parent Logix", sysName);
continue;
}
// Found a potential parent Logix, check the Logix index
boolean inIndex = false;
for (int j = 0; j < x.getNumConditionals(); j++) {
String cName = x.getConditionalByNumberOrder(j);
if (sysName.equals(cName)) {
inIndex = true;
break;
}
}
if (!inIndex) {
log.warn("Conditional '{}' is not in the Logix index", sysName);
continue;
}
// Create the condtional
c = tm.createNewConditional(sysName, userName);
}
if (c == null) {
// Should never get here
log.error("Conditional '{}' cannot be created", sysName);
continue;
}
// conditional already exists
// load common parts
loadCommon(c, condElem);
String ant = "";
int logicType = Conditional.ALL_AND;
if (condElem.getAttribute("antecedent") != null) {
ant = condElem.getAttribute("antecedent").getValue();
}
if (condElem.getAttribute("logicType") != null) {
logicType = Integer.parseInt(condElem.getAttribute("logicType").getValue());
}
c.setLogicType(logicType, ant);
// load state variables, if there are any
List<Element> conditionalVarList = condElem.getChildren("conditionalStateVariable");
if (conditionalVarList.size() == 0) {
log.warn("No state variables found for conditional " + sysName);
}
ArrayList<ConditionalVariable> variableList = new ArrayList<>();
for (int n = 0; n < conditionalVarList.size(); n++) {
ConditionalVariable variable = new ConditionalVariable();
if (conditionalVarList.get(n).getAttribute("operator") == null) {
log.warn("unexpected null in operator " + conditionalVarList.get(n) + " " + conditionalVarList.get(n).getAttributes());
} else {
int oper = Integer.parseInt(conditionalVarList.get(n).getAttribute("operator").getValue());
if (oper == Conditional.OPERATOR_AND_NOT) {
variable.setNegation(true);
oper = Conditional.OPERATOR_AND;
} else if (oper == Conditional.OPERATOR_NOT) {
variable.setNegation(true);
oper = Conditional.OPERATOR_NONE;
}
variable.setOpern(oper);
}
if (conditionalVarList.get(n).getAttribute("negated") != null) {
if ("yes".equals(conditionalVarList.get(n).getAttribute("negated").getValue())) {
variable.setNegation(true);
} else {
variable.setNegation(false);
}
}
variable.setType(Integer.parseInt(conditionalVarList.get(n).getAttribute("type").getValue()));
variable.setName(conditionalVarList.get(n).getAttribute("systemName").getValue());
if (conditionalVarList.get(n).getAttribute("dataString") != null) {
variable.setDataString(conditionalVarList.get(n).getAttribute("dataString").getValue());
}
if (conditionalVarList.get(n).getAttribute("num1") != null) {
variable.setNum1(Integer.parseInt(conditionalVarList.get(n).getAttribute("num1").getValue()));
}
if (conditionalVarList.get(n).getAttribute("num2") != null) {
variable.setNum2(Integer.parseInt(conditionalVarList.get(n).getAttribute("num2").getValue()));
}
variable.setTriggerActions(true);
if (conditionalVarList.get(n).getAttribute("triggersCalc") != null) {
if ("no".equals(conditionalVarList.get(n).getAttribute("triggersCalc").getValue())) {
variable.setTriggerActions(false);
}
}
variableList.add(variable);
}
c.setStateVariables(variableList);
// load actions - there better be some
List<Element> conditionalActionList = condElem.getChildren("conditionalAction");
// Really OK, since a user may use such conditionals to define a reusable
// expression of state variables. These conditions are then used as a
// state variable in other conditionals. (pwc)
//if (conditionalActionList.size() == 0) {
// log.warn("No actions found for conditional "+sysName);
//}
ArrayList<ConditionalAction> actionList = ((DefaultConditional) c).getActionList();
org.jdom2.Attribute attr = null;
for (int n = 0; n < conditionalActionList.size(); n++) {
ConditionalAction action = new DefaultConditionalAction();
attr = conditionalActionList.get(n).getAttribute("option");
if (attr != null) {
action.setOption(Integer.parseInt(attr.getValue()));
} else {
log.warn("unexpected null in option " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
}
// actionDelay is removed. delay data is stored as a String to allow
// such data be referenced by internal memory.
// For backward compatibility, set delay "int" as a string
attr = conditionalActionList.get(n).getAttribute("delay");
if (attr != null) {
action.setActionString(attr.getValue());
}
attr = conditionalActionList.get(n).getAttribute("type");
if (attr != null) {
action.setType(Integer.parseInt(attr.getValue()));
} else {
log.warn("unexpected null in type " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
}
attr = conditionalActionList.get(n).getAttribute("systemName");
if (attr != null) {
action.setDeviceName(attr.getValue());
} else {
log.warn("unexpected null in systemName " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
}
attr = conditionalActionList.get(n).getAttribute("data");
if (attr != null) {
action.setActionData(Integer.parseInt(attr.getValue()));
} else {
log.warn("unexpected null in action data " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
}
attr = conditionalActionList.get(n).getAttribute("string");
if (attr != null) {
action.setActionString(attr.getValue());
} else {
log.warn("unexpected null in action string " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
}
if (!actionList.contains(action))
actionList.add(action);
}
c.setAction(actionList);
// 1/16/2011 - trigger for execution of the action list changed to execute each
// time state is computed. Formerly execution of the action list was done only
// when state changes. All conditionals are upgraded to this new policy.
// However, for conditionals with actions that toggle on change of state
// the old policy should be used.
boolean triggerOnChange = false;
if (condElem.getAttribute("triggerOnChange") != null) {
if ("yes".equals(condElem.getAttribute("triggerOnChange").getValue())) {
triggerOnChange = true;
}
} else {
/* Don't upgrade -Let old be as is
for (int k=0; k<actionList.size(); k++){
ConditionalAction action = actionList.get(k);
if (action.getOption()==Conditional.ACTION_OPTION_ON_CHANGE){
triggerOnChange = true;
break;
}
}
*/
triggerOnChange = true;
}
c.setTriggerOnChange(triggerOnChange);
}
}
use of jmri.ConditionalManager in project JMRI by JMRI.
the class DefaultConditionalManagerXml method replaceConditionalManager.
/**
* Replace the current ConditionalManager, if there is one, with one newly
* created during a load operation. This is skipped if they are of the same
* absolute type.
*/
protected void replaceConditionalManager() {
if (InstanceManager.getDefault(jmri.ConditionalManager.class).getClass().getName().equals(DefaultConditionalManager.class.getName())) {
return;
}
// if old manager exists, remove it from configuration process
if (InstanceManager.getNullableDefault(jmri.ConditionalManager.class) != null) {
InstanceManager.getDefault(jmri.ConfigureManager.class).deregister(InstanceManager.getDefault(jmri.ConditionalManager.class));
}
// register new one with InstanceManager
DefaultConditionalManager pManager = DefaultConditionalManager.instance();
InstanceManager.store(pManager, ConditionalManager.class);
InstanceManager.setDefault(ConditionalManager.class, pManager);
// register new one for configuration
InstanceManager.getDefault(jmri.ConfigureManager.class).registerConfig(pManager, jmri.Manager.CONDITIONALS);
}
use of jmri.ConditionalManager in project JMRI by JMRI.
the class DefaultConditionalManagerXml method store.
/**
* Default implementation for storing the contents of a ConditionalManager
*
* @param o Object to store, of type ConditionalManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
// long numCond = 0;
// long numStateVars = 0;
Element conditionals = new Element("conditionals");
setStoreElementClass(conditionals);
ConditionalManager tm = (ConditionalManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not conditionals to include
if (!iter.hasNext()) {
return null;
}
// store the conditionals
while (iter.hasNext()) {
// numCond++;
// long condTime = System.currentTimeMillis();
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
}
log.debug("conditional system name is " + sname);
Conditional c = tm.getBySystemName(sname);
if (c == null) {
log.error("Unable to save '{}' to the XML file", sname);
continue;
}
Element elem = new Element("conditional").setAttribute("systemName", sname);
elem.addContent(new Element("systemName").addContent(sname));
// store common parts
storeCommon(c, elem);
elem.setAttribute("antecedent", c.getAntecedentExpression());
elem.setAttribute("logicType", Integer.toString(c.getLogicType()));
if (c.getTriggerOnChange()) {
elem.setAttribute("triggerOnChange", "yes");
} else {
elem.setAttribute("triggerOnChange", "no");
}
// save child state variables
// Creating StateVariables gets very slow when more than c10,000 exist.
// creation time goes from less than 1ms to more than 5000ms.
// Don't need a clone for read-only use.
// List <ConditionalVariable> variableList = c.getCopyOfStateVariables();
List<ConditionalVariable> variableList = ((jmri.implementation.DefaultConditional) c).getStateVariableList();
/* numStateVars += variableList.size();
if (numCond>1190) {
partTime = System.currentTimeMillis() - partTime;
System.out.println("time to for getCopyOfStateVariables "+partTime+"ms. total stateVariable= "+numStateVars);
}*/
for (int k = 0; k < variableList.size(); k++) {
ConditionalVariable variable = variableList.get(k);
Element vElem = new Element("conditionalStateVariable");
int oper = variable.getOpern();
if (oper == Conditional.OPERATOR_AND && variable.isNegated()) {
// backward compatibility
oper = Conditional.OPERATOR_AND_NOT;
} else if (oper == Conditional.OPERATOR_NONE && variable.isNegated()) {
// backward compatibility
oper = Conditional.OPERATOR_NOT;
}
vElem.setAttribute("operator", Integer.toString(oper));
if (variable.isNegated()) {
vElem.setAttribute("negated", "yes");
} else {
vElem.setAttribute("negated", "no");
}
vElem.setAttribute("type", Integer.toString(variable.getType()));
vElem.setAttribute("systemName", variable.getName());
vElem.setAttribute("dataString", variable.getDataString());
vElem.setAttribute("num1", Integer.toString(variable.getNum1()));
vElem.setAttribute("num2", Integer.toString(variable.getNum2()));
if (variable.doTriggerActions()) {
vElem.setAttribute("triggersCalc", "yes");
} else {
vElem.setAttribute("triggersCalc", "no");
}
elem.addContent(vElem);
}
// save action information
ArrayList<ConditionalAction> actionList = c.getCopyOfActions();
/* if (numCond>1190) {
partTime = System.currentTimeMillis() - partTime;
System.out.println("time to for getCopyOfActions "+partTime+"ms. numActions= "+actionList.size());
}*/
for (int k = 0; k < actionList.size(); k++) {
ConditionalAction action = actionList.get(k);
Element aElem = new Element("conditionalAction");
aElem.setAttribute("option", Integer.toString(action.getOption()));
aElem.setAttribute("type", Integer.toString(action.getType()));
aElem.setAttribute("systemName", action.getDeviceName());
aElem.setAttribute("data", Integer.toString(action.getActionData()));
// add "delay" attribute
try {
Integer.parseInt(action.getActionString());
aElem.setAttribute("delay", action.getActionString());
} catch (NumberFormatException nfe) {
aElem.setAttribute("delay", "0");
}
aElem.setAttribute("string", action.getActionString());
elem.addContent(aElem);
}
conditionals.addContent(elem);
/* condTime = System.currentTimeMillis() - condTime;
if (condTime>1) {
System.out.println(numCond+"th Conditional \""+sname+"\" took "+condTime+"ms to store.");
}*/
}
}
// System.out.println("Elapsed time to store "+numCond+" Conditional "+(System.currentTimeMillis()-time)+"ms.");
return (conditionals);
}
Aggregations