use of jmri.Conditional 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.Conditional 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);
}
use of jmri.Conditional in project JMRI by JMRI.
the class LRouteTableAction method makeAlignConditional.
/**
* Create a new alignment conditional.
*
* @param numConds number of existing route conditionals
* @param actionList actions to take in conditional
* @param triggerList triggers for conditional to take actions
* @param logix Logix to add the conditional to
* @param sName system name for conditional
* @param uName user name for conditional
* @return number of conditionals after the creation
* @throws IllegalArgumentException if "user input no good"
*/
int makeAlignConditional(int numConds, ArrayList<ConditionalAction> actionList, ArrayList<ConditionalVariable> triggerList, Logix logix, String sName, String uName) {
if (triggerList.size() == 0) {
return numConds;
}
String cSystemName = sName + numConds + "A";
String cUserName = CONDITIONAL_USER_PREFIX + numConds + "A " + uName;
Conditional c = null;
try {
c = _conditionalManager.createNewConditional(cSystemName, cUserName);
} catch (Exception ex) {
// user input no good
handleCreateException(sName);
// throw without creating any
throw new IllegalArgumentException("user input no good");
}
c.setStateVariables(triggerList);
//c.setAction(cloneActionList(actionList, Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE));
c.setAction(actionList);
c.setLogicType(Conditional.ALL_AND, "");
logix.addConditional(cSystemName, 0);
log.debug("Conditional added: SysName= \"" + cSystemName + "\"");
c.calculate(true, null);
numConds++;
return numConds;
}
use of jmri.Conditional in project JMRI by JMRI.
the class LRouteTableAction method getAlignmentSensors.
// getControlsAndActions
/**
* Extract the Alignment Sensors and their types.
*
* @param cSysName the conditional system name
*/
void getAlignmentSensors(String cSysName) {
Conditional c = _conditionalManager.getBySystemName(cSysName);
if (c != null) {
AlignElement element = null;
ArrayList<ConditionalAction> actionList = c.getCopyOfActions();
for (int k = 0; k < actionList.size(); k++) {
ConditionalAction action = actionList.get(k);
if (action.getType() != Conditional.ACTION_SET_SENSOR) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("AlignWarn1"), new Object[] { action.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
} else {
String name = action.getDeviceName();
String key = SENSOR_TYPE + name;
element = _alignUserMap.get(key);
if (element == null) {
// try in system name map
element = _alignMap.get(key);
}
if (element == null) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarn"), new Object[] { action.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
} else if (!name.equals(action.getDeviceName())) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("AlignWarn2"), new Object[] { action.toString(), action.getDeviceName(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
} else {
element.setIncluded(true);
}
}
}
// the action elements are identified in getControlsAndActions().
// Just identify the type of sensing
ArrayList<ConditionalVariable> varList = c.getCopyOfStateVariables();
int atype = 0;
for (int k = 0; k < varList.size(); k++) {
ConditionalVariable variable = varList.get(k);
int testState = variable.getType();
int type;
switch(testState) {
case Conditional.TYPE_SENSOR_ACTIVE:
case Conditional.TYPE_SENSOR_INACTIVE:
type = SENSOR_TYPE;
break;
case Conditional.TYPE_TURNOUT_CLOSED:
case Conditional.TYPE_TURNOUT_THROWN:
type = TURNOUT_TYPE;
break;
case Conditional.TYPE_LIGHT_ON:
case Conditional.TYPE_LIGHT_OFF:
type = LIGHT_TYPE;
break;
case Conditional.TYPE_SIGNAL_HEAD_LIT:
case Conditional.TYPE_SIGNAL_HEAD_RED:
case Conditional.TYPE_SIGNAL_HEAD_YELLOW:
case Conditional.TYPE_SIGNAL_HEAD_GREEN:
case Conditional.TYPE_SIGNAL_HEAD_DARK:
case Conditional.TYPE_SIGNAL_HEAD_FLASHRED:
case Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW:
case Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN:
case Conditional.TYPE_SIGNAL_HEAD_HELD:
type = SIGNAL_TYPE;
break;
default:
if (!LOGIX_INITIALIZER.equals(variable.getName())) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarnVar"), new Object[] { variable.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
continue;
}
if (k == 0) {
atype = type;
} else if (atype != type) {
// more than one type. therefor, ALL
atype = ALL_TYPE;
break;
}
}
if (element != null) {
element.setState(atype);
}
}
}
use of jmri.Conditional in project JMRI by JMRI.
the class LRouteTableAction method getControlsAndActions.
/**
* Extract the Control (input) and Action (output) elements and their
* states.
*
* @param cSysName the conditional system name
*/
void getControlsAndActions(String cSysName) {
Conditional c = _conditionalManager.getBySystemName(cSysName);
if (c != null) {
ArrayList<ConditionalAction> actionList = c.getCopyOfActions();
boolean onChange = false;
for (int k = 0; k < actionList.size(); k++) {
ConditionalAction action = actionList.get(k);
int type;
switch(action.getType()) {
case Conditional.ACTION_SET_SENSOR:
type = SENSOR_TYPE;
break;
case Conditional.ACTION_SET_TURNOUT:
type = TURNOUT_TYPE;
break;
case Conditional.ACTION_SET_LIGHT:
type = LIGHT_TYPE;
break;
case Conditional.ACTION_SET_SIGNAL_APPEARANCE:
case Conditional.ACTION_SET_SIGNAL_HELD:
case Conditional.ACTION_CLEAR_SIGNAL_HELD:
case Conditional.ACTION_SET_SIGNAL_DARK:
case Conditional.ACTION_SET_SIGNAL_LIT:
type = SIGNAL_TYPE;
break;
case Conditional.ACTION_RUN_SCRIPT:
scriptFile.setText(action.getActionString());
continue;
case Conditional.ACTION_PLAY_SOUND:
soundFile.setText(action.getActionString());
continue;
default:
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarn"), new Object[] { action.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
continue;
}
String name = action.getDeviceName();
String key = type + name;
RouteOutputElement elt = _outputUserMap.get(key);
if (elt == null) {
// try in system name map
elt = _outputMap.get(key);
}
if (elt == null) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarn"), new Object[] { action.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
} else {
elt.setIncluded(true);
elt.setState(action.getActionData());
boolean change = (action.getOption() == Conditional.ACTION_OPTION_ON_CHANGE);
if (k == 0) {
onChange = change;
} else if (change != onChange) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("OnChangeWarn"), new Object[] { action.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
}
ArrayList<ConditionalVariable> varList = c.getCopyOfStateVariables();
for (int k = 0; k < varList.size(); k++) {
ConditionalVariable variable = varList.get(k);
int testState = variable.getType();
//boolean negated = variable.isNegated();
int type;
switch(testState) {
case Conditional.TYPE_SENSOR_ACTIVE:
type = SENSOR_TYPE;
//if (negated) testState = Conditional.TYPE_SENSOR_INACTIVE;
break;
case Conditional.TYPE_SENSOR_INACTIVE:
type = SENSOR_TYPE;
//if (negated) testState = Conditional.TYPE_SENSOR_ACTIVE;
break;
case Conditional.TYPE_TURNOUT_CLOSED:
type = TURNOUT_TYPE;
//if (negated) testState = Conditional.TYPE_TURNOUT_THROWN;
break;
case Conditional.TYPE_TURNOUT_THROWN:
type = TURNOUT_TYPE;
//if (negated) testState = Conditional.TYPE_TURNOUT_CLOSED;
break;
case Conditional.TYPE_LIGHT_ON:
type = LIGHT_TYPE;
//if (negated) testState = Conditional.TYPE_LIGHT_OFF;
break;
case Conditional.TYPE_LIGHT_OFF:
type = LIGHT_TYPE;
//if (negated) testState = Conditional.TYPE_LIGHT_ON;
break;
case Conditional.TYPE_SIGNAL_HEAD_LIT:
case Conditional.TYPE_SIGNAL_HEAD_RED:
case Conditional.TYPE_SIGNAL_HEAD_YELLOW:
case Conditional.TYPE_SIGNAL_HEAD_GREEN:
case Conditional.TYPE_SIGNAL_HEAD_DARK:
case Conditional.TYPE_SIGNAL_HEAD_FLASHRED:
case Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW:
case Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN:
case Conditional.TYPE_SIGNAL_HEAD_HELD:
type = SIGNAL_TYPE;
break;
default:
if (!LOGIX_INITIALIZER.equals(variable.getName())) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarnVar"), new Object[] { variable.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
continue;
}
int opern = variable.getOpern();
if (k != 0 && (opern == Conditional.OPERATOR_AND || opern == Conditional.OPERATOR_AND_NOT)) {
// guess this is a VETO
testState += VETO;
} else if (onChange) {
testState = Route.ONCHANGE;
}
String name = variable.getName();
String key = type + name;
RouteInputElement elt = _inputUserMap.get(key);
if (elt == null) {
// try in system name map
elt = _inputMap.get(key);
}
if (elt == null) {
if (!LOGIX_INITIALIZER.equals(name)) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("TypeWarnVar"), new Object[] { variable.toString(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
} else {
elt.setIncluded(true);
elt.setState(testState);
}
}
}
}
Aggregations