use of jmri.Conditional in project JMRI by JMRI.
the class DefaultLogix method vetoableChange.
@Override
public void vetoableChange(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException {
if ("CanDelete".equals(evt.getPropertyName())) {
//IN18N
NamedBean nb = (NamedBean) evt.getOldValue();
for (JmriSimplePropertyListener listener : _listeners) {
if (nb.equals(listener.getBean())) {
java.beans.PropertyChangeEvent e = new java.beans.PropertyChangeEvent(this, "DoNotDelete", null, null);
//IN18N
throw new java.beans.PropertyVetoException(Bundle.getMessage("InUseLogixListener", nb.getBeanType(), getDisplayName()), e);
}
}
String cName = "";
Conditional c = null;
for (int i = 0; i < _conditionalSystemNames.size(); i++) {
cName = _conditionalSystemNames.get(i);
c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(cName);
if (c != null) {
for (jmri.ConditionalAction ca : c.getCopyOfActions()) {
if (nb.equals(ca.getBean())) {
java.beans.PropertyChangeEvent e = new java.beans.PropertyChangeEvent(this, "DoNotDelete", null, null);
//IN18N
throw new java.beans.PropertyVetoException(Bundle.getMessage("InUseLogixAction", nb.getBeanType(), getDisplayName()), e);
}
}
for (ConditionalVariable v : c.getCopyOfStateVariables()) {
if (nb.equals(v.getBean()) || nb.equals(v.getNamedBeanData())) {
java.beans.PropertyChangeEvent e = new java.beans.PropertyChangeEvent(this, "DoNotDelete", null, null);
//IN18N
throw new java.beans.PropertyVetoException(Bundle.getMessage("InUseLogixAction", nb.getBeanType(), getDisplayName()), e);
}
}
}
}
} else if ("DoDelete".equals(evt.getPropertyName())) {
//IN18N
//Do nothing at this stage
}
}
use of jmri.Conditional in project JMRI by JMRI.
the class DefaultLogix method deleteConditional.
/**
* Delete a Conditional and remove it from this Logix
* <P>
* Note: Since each Logix must have at least one Conditional to do anything,
* the user is warned in Logix Table Action when the last Conditional is
* deleted.
* <P>
* Returns true if Conditional was successfully deleted, otherwise returns
* false.
*
* @param systemName The Conditional system name
*/
@Override
public String[] deleteConditional(String systemName) {
if (_conditionalSystemNames.size() <= 0) {
return (null);
}
// check other Logix(es) for use of this conditional (systemName) for use as a
// variable in one of their conditionals
ArrayList<String> checkReferences = InstanceManager.getDefault(jmri.ConditionalManager.class).getWhereUsed(systemName);
if (checkReferences != null) {
Conditional c = getConditional(systemName);
String refName = checkReferences.get(0);
Logix x = InstanceManager.getDefault(jmri.ConditionalManager.class).getParentLogix(refName);
Conditional cRef = x.getConditional(refName);
String[] result = new String[] { c.getUserName(), c.getSystemName(), cRef.getUserName(), cRef.getSystemName(), x.getUserName(), x.getSystemName() };
return result;
}
// Remove Conditional from this logix
if (!_conditionalSystemNames.remove(systemName)) {
log.error("attempt to delete Conditional not in Logix: " + systemName);
return null;
}
// delete the Conditional object
Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(systemName);
if (c == null) {
log.error("attempt to delete non-existant Conditional - " + systemName);
return null;
}
_conditionalMap.remove(systemName);
return (null);
}
use of jmri.Conditional in project JMRI by JMRI.
the class LRouteTableAction method getLockConditions.
/**
* Extract the Lock expression. For now, same as action control expression.
*
* @param cSysName the conditional system name
*/
void getLockConditions(String cSysName) {
Conditional c = _conditionalManager.getBySystemName(cSysName);
if (c != null) {
_lock = true;
// Verify conditional is what we think it is
ArrayList<RouteOutputElement> tList = makeTurnoutLockList();
ArrayList<ConditionalAction> actionList = c.getCopyOfActions();
if (actionList.size() != tList.size()) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("LockWarn1"), new Object[] { Integer.toString(tList.size()), c.getSystemName(), Integer.toString(actionList.size()) }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
for (int k = 0; k < actionList.size(); k++) {
ConditionalAction action = actionList.get(k);
if (action.getType() != Conditional.ACTION_LOCK_TURNOUT) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("LockWarn2"), new Object[] { action.getDeviceName(), c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
} else {
String name = action.getDeviceName();
boolean found = false;
ArrayList<RouteOutputElement> lockList = makeTurnoutLockList();
for (int j = 0; j < lockList.size(); j++) {
RouteOutputElement elt = lockList.get(j);
if (name.equals(elt.getUserName()) || name.equals(elt.getSysName())) {
found = true;
break;
}
}
if (!found) {
javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("LockWarn3"), new Object[] { name, c.getSystemName() }), rbx.getString("EditDiff"), javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
}
}
}
use of jmri.Conditional in project JMRI by JMRI.
the class LRouteTableAction method makeRouteConditional.
/**
* Create a new route conditional.
*
* @param numConds number of existing route conditionals
* @param actionList actions to take in conditional
* @param triggerList triggers for conditional to take actions
* @param vetoList controls that veto taking actions
* @param logix Logix to add the conditional to
* @param sName system name for conditional
* @param uName user name for conditional
* @param type type of conditional
* @return number of conditionals after the creation
* @throws IllegalArgumentException if "user input no good"
*/
int makeRouteConditional(int numConds, /*boolean onChange,*/
ArrayList<ConditionalAction> actionList, ArrayList<ConditionalVariable> triggerList, ArrayList<ConditionalVariable> vetoList, Logix logix, String sName, String uName, String type) {
if (log.isDebugEnabled()) {
log.debug("makeRouteConditional: numConds= " + numConds + ", triggerList.size()= " + triggerList.size());
}
if (triggerList.isEmpty() && (vetoList == null || vetoList.isEmpty())) {
return numConds;
}
StringBuilder antecedent = new StringBuilder();
ArrayList<ConditionalVariable> varList = new ArrayList<>();
int tSize = triggerList.size();
if (tSize > 0) {
if (tSize > 1) {
antecedent.append("(");
}
//NOI18N
antecedent.append("R1");
for (int i = 1; i < tSize; i++) {
//NOI18N
antecedent.append(" ").append(Bundle.getMessage("LogicOR")).append(" R").append(i + 1);
}
if (tSize > 1) {
antecedent.append(")");
}
for (int i = 0; i < triggerList.size(); i++) {
//varList.add(cloneVariable(triggerList.get(i)));
varList.add(triggerList.get(i));
}
} else {
}
if (vetoList != null && vetoList.size() > 0) {
int vSize = vetoList.size();
if (tSize > 0) {
antecedent.append(" ").append(Bundle.getMessage("LogicAND")).append(" ");
}
if (vSize > 1) {
antecedent.append("(");
}
//NOI18N
antecedent.append(Bundle.getMessage("LogicNOT")).append(" R").append(1 + tSize);
for (int i = 1; i < vSize; i++) {
//NOI18N
antecedent.append(" ").append(Bundle.getMessage("LogicAND")).append(" ").append(Bundle.getMessage("LogicNOT")).append(" R").append(i + 1 + tSize);
}
if (vSize > 1) {
antecedent.append(")");
}
for (int i = 0; i < vetoList.size(); i++) {
//varList.add(cloneVariable(vetoList.get(i)));
varList.add(vetoList.get(i));
}
}
String cSystemName = sName + numConds + type;
String cUserName = CONDITIONAL_USER_PREFIX + numConds + "C " + 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(varList);
//int option = onChange ? Conditional.ACTION_OPTION_ON_CHANGE : Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE;
//c.setAction(cloneActionList(actionList, option));
c.setAction(actionList);
int logicType = _newRouteType ? Conditional.MIXED : Conditional.ALL_AND;
c.setLogicType(logicType, antecedent.toString());
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 SensorGroupFrame method addPressed.
void addPressed() {
deleteGroup(false);
String group = _nameField.getText();
if (group == null || group.length() == 0) {
javax.swing.JOptionPane.showMessageDialog(this, "Please enter a name for this group.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
Logix logix = getSystemLogix();
logix.deActivateLogix();
String cSystemName = ConditionalSystemPrefix + group.toUpperCase();
String cUserName = ConditionalUserPrefix + group;
// add new Conditional
ArrayList<ConditionalVariable> variableList = new ArrayList<ConditionalVariable>();
ArrayList<ConditionalAction> actionList = new ArrayList<ConditionalAction>();
int count = 0;
for (int i = 0; i < _sensorModel.getRowCount(); i++) {
if ((Boolean) _sensorModel.getValueAt(i, BeanTableModel.INCLUDE_COLUMN)) {
String sensor = (String) _sensorModel.getValueAt(i, BeanTableModel.SNAME_COLUMN);
variableList.add(new ConditionalVariable(false, Conditional.OPERATOR_OR, Conditional.TYPE_SENSOR_ACTIVE, sensor, true));
actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_SET_SENSOR, sensor, Sensor.INACTIVE, ""));
count++;
}
}
if (count < 2) {
javax.swing.JOptionPane.showMessageDialog(this, "A Sensor Group needs to have at least 2 sensors to be useful.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}
Conditional c = new SensorGroupConditional(cSystemName, cUserName);
// InstanceManager.getDefault(jmri.ConditionalManager.class).register(c);
c.setStateVariables(variableList);
c.setLogicType(Conditional.ALL_OR, "");
c.setAction(actionList);
// Update the Logix Conditional names list
logix.addConditional(cSystemName, 0);
// Update the Logix Conditional hash map
logix.addConditional(cSystemName, c);
logix.setEnabled(true);
logix.activateLogix();
((DefaultListModel<String>) _sensorGroupList.getModel()).addElement(cUserName.substring(ConditionalUserPrefix.length()));
clear();
}
Aggregations