Search in sources :

Example 1 with ConditionalVariable

use of jmri.ConditionalVariable 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
    }
}
Also used : NamedBean(jmri.NamedBean) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable)

Example 2 with ConditionalVariable

use of jmri.ConditionalVariable in project JMRI by JMRI.

the class LRouteTableAction method updatePressed.

/**
     * Update the Route Table.
     */
void updatePressed() {
    Logix logix = checkNamesOK();
    if (logix == null) {
        log.error("No Logix found!");
        return;
    }
    String sName = logix.getSystemName();
    // Check if the User Name has been changed
    String uName = _userName.getText();
    logix.setUserName(uName);
    initializeIncludedInputList();
    initializeIncludedOutputList();
    initializeIncludedAlignList();
    if (log.isDebugEnabled()) {
        log.debug("updatePressed: _includedInputList.size()= " + _includedInputList.size() + ", _includedOutputList.size()= " + _includedOutputList.size() + ", _includedAlignList.size()= " + _includedAlignList.size());
    }
    ////// Construct output actions for trigger conditionals ///////////
    ArrayList<ConditionalAction> actionList = new ArrayList<>();
    for (int i = 0; i < _includedOutputList.size(); i++) {
        RouteOutputElement elt = _includedOutputList.get(i);
        String name = elt.getUserName();
        if (name == null || name.length() == 0) {
            name = elt.getSysName();
        }
        // actionData
        int state = elt.getState();
        int actionType = 0;
        String params = "";
        switch(elt.getType()) {
            case SENSOR_TYPE:
                actionType = Conditional.ACTION_SET_SENSOR;
                break;
            case TURNOUT_TYPE:
                actionType = Conditional.ACTION_SET_TURNOUT;
                break;
            case LIGHT_TYPE:
                actionType = Conditional.ACTION_SET_LIGHT;
                break;
            case SIGNAL_TYPE:
                actionType = Conditional.ACTION_SET_SIGNAL_APPEARANCE;
                if (state > OFFSET) {
                    actionType = state & ~OFFSET;
                }
                break;
            default:
                log.debug("updatePressed: Unknown action type " + elt.getType());
        }
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, actionType, name, state, params));
    }
    String file = scriptFile.getText();
    if (file.length() > 0) {
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_RUN_SCRIPT, "", -1, file));
    }
    file = soundFile.getText();
    if (file.length() > 0) {
        actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_PLAY_SOUND, "", -1, file));
    }
    ArrayList<ConditionalAction> onChangeList = cloneActionList(actionList, Conditional.ACTION_OPTION_ON_CHANGE);
    /////// Construct 'AND' clause from 'VETO' controls ////////
    ArrayList<ConditionalVariable> vetoList = new ArrayList<>();
    if (!_initialize) {
        for (int i = 0; i < _includedInputList.size(); i++) {
            RouteInputElement elt = _includedInputList.get(i);
            String name = elt.getUserName();
            if (name == null || name.length() == 0) {
                name = elt.getSysName();
            }
            //int opern = newRouteType ? Conditional.OPERATOR_AND : Conditional.OPERATOR_OR;
            int opern = Conditional.OPERATOR_AND;
            if (i == 0) {
                opern = Conditional.OPERATOR_NONE;
            }
            int state = elt.getState();
            if (VETO < state) {
                vetoList.add(new ConditionalVariable(true, opern, (state & ~VETO), name, _newRouteType));
            }
        }
    }
    ///////////////// Make Trigger Conditional Controls /////////////////
    ArrayList<ConditionalVariable> oneTriggerList = new ArrayList<>();
    ArrayList<ConditionalVariable> twoTriggerList = new ArrayList<>();
    if (!_initialize) {
        for (int i = 0; i < _includedInputList.size(); i++) {
            RouteInputElement elt = _includedInputList.get(i);
            String name = elt.getUserName();
            if (name == null || name.length() == 0) {
                name = elt.getSysName();
            }
            int opern = _newRouteType ? Conditional.OPERATOR_OR : Conditional.OPERATOR_AND;
            if (i == 0) {
                opern = Conditional.OPERATOR_NONE;
            }
            int type = elt.getState();
            if (VETO > type) {
                if (Route.ONCHANGE == type) {
                    switch(elt.getType()) {
                        case SENSOR_TYPE:
                            type = Conditional.TYPE_SENSOR_ACTIVE;
                            break;
                        case TURNOUT_TYPE:
                            type = Conditional.TYPE_TURNOUT_CLOSED;
                            break;
                        case LIGHT_TYPE:
                            type = Conditional.TYPE_LIGHT_ON;
                            break;
                        case SIGNAL_TYPE:
                            type = Conditional.TYPE_SIGNAL_HEAD_LIT;
                            break;
                        default:
                            log.debug("updatePressed: Unknown state variable type " + elt.getType());
                    }
                    twoTriggerList.add(new ConditionalVariable(false, opern, type, name, true));
                } else {
                    oneTriggerList.add(new ConditionalVariable(false, opern, type, name, true));
                }
            }
        }
        if (actionList.isEmpty()) {
            javax.swing.JOptionPane.showMessageDialog(_addFrame, rbx.getString("noAction"), rbx.getString("addErr"), javax.swing.JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        oneTriggerList.add(new ConditionalVariable(false, Conditional.OPERATOR_NONE, Conditional.TYPE_NONE, LOGIX_INITIALIZER, true));
    }
    if (log.isDebugEnabled()) {
        log.debug("actionList.size()= " + actionList.size() + ", oneTriggerList.size()= " + oneTriggerList.size() + ", twoTriggerList.size()= " + twoTriggerList.size() + ", onChangeList.size()= " + onChangeList.size() + ", vetoList.size()= " + vetoList.size());
    }
    logix.deActivateLogix();
    // remove old Conditionals for actions (ver 2.5.2 only -remove a bad idea)
    char[] ch = sName.toCharArray();
    int hash = 0;
    for (int i = 0; i < ch.length; i++) {
        hash += ch[i];
    }
    String cSystemName = CONDITIONAL_SYS_PREFIX + "T" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "F" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "A" + hash;
    removeConditionals(cSystemName, logix);
    cSystemName = CONDITIONAL_SYS_PREFIX + "L" + hash;
    removeConditionals(cSystemName, logix);
    int n = 0;
    do {
        n++;
        cSystemName = sName + n + "A";
    } while (removeConditionals(cSystemName, logix));
    n = 0;
    do {
        n++;
        cSystemName = sName + n + "T";
    } while (removeConditionals(cSystemName, logix));
    cSystemName = sName + "L";
    removeConditionals(cSystemName, logix);
    //String cUserName = null;
    int numConds = 1;
    if (_newRouteType) {
        numConds = makeRouteConditional(numConds, /*false,*/
        actionList, oneTriggerList, vetoList, logix, sName, uName, "T");
        if (!_initialize && twoTriggerList.size() > 0) {
            numConds = makeRouteConditional(numConds, /*true, actionList,*/
            onChangeList, twoTriggerList, null, logix, sName, uName, "T");
        }
    } else {
        for (int i = 0; i < oneTriggerList.size(); i++) {
            ArrayList<ConditionalVariable> vList = new ArrayList<>();
            vList.add(oneTriggerList.get(i));
            numConds = makeRouteConditional(numConds, /*false,*/
            actionList, vList, vetoList, logix, sName, uName, "T");
        }
        for (int i = 0; i < twoTriggerList.size(); i++) {
            ArrayList<ConditionalVariable> vList = new ArrayList<>();
            vList.add(twoTriggerList.get(i));
            numConds = makeRouteConditional(numConds, /*true, actionList,*/
            onChangeList, vList, vetoList, logix, sName, uName, "T");
        }
    }
    if (numConds == 1) {
        javax.swing.JOptionPane.showMessageDialog(_addFrame, rbx.getString("noVars"), rbx.getString("addErr"), javax.swing.JOptionPane.ERROR_MESSAGE);
        return;
    }
    ///////////////// Make Alignment Conditionals //////////////////////////
    numConds = 1;
    for (int i = 0; i < _includedAlignList.size(); i++) {
        ArrayList<ConditionalVariable> vList = new ArrayList<>();
        ArrayList<ConditionalAction> aList = new ArrayList<>();
        AlignElement sensor = _includedAlignList.get(i);
        String name = sensor.getUserName();
        if (name == null || name.length() == 0) {
            name = sensor.getSysName();
        }
        aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_SET_SENSOR, name, Sensor.ACTIVE, ""));
        aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE, Conditional.ACTION_SET_SENSOR, name, Sensor.INACTIVE, ""));
        int alignType = sensor.getState();
        for (int k = 0; k < _includedOutputList.size(); k++) {
            RouteOutputElement elt = _includedOutputList.get(k);
            int varType = 0;
            boolean add = (ALL_TYPE == alignType);
            switch(elt.getType()) {
                case SENSOR_TYPE:
                    if (alignType == SENSOR_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Sensor.INACTIVE:
                            varType = Conditional.TYPE_SENSOR_INACTIVE;
                            break;
                        case Sensor.ACTIVE:
                            varType = Conditional.TYPE_SENSOR_ACTIVE;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in SENSOR_TYPE", elt.getState());
                            break;
                    }
                    break;
                case TURNOUT_TYPE:
                    if (alignType == TURNOUT_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Turnout.CLOSED:
                            varType = Conditional.TYPE_TURNOUT_CLOSED;
                            break;
                        case Turnout.THROWN:
                            varType = Conditional.TYPE_TURNOUT_THROWN;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in TURNOUT_TYPE", elt.getState());
                            break;
                    }
                    break;
                case LIGHT_TYPE:
                    if (alignType == LIGHT_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case Light.ON:
                            varType = Conditional.TYPE_LIGHT_ON;
                            break;
                        case Light.OFF:
                            varType = Conditional.TYPE_LIGHT_OFF;
                            break;
                        case Route.TOGGLE:
                            add = false;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in LIGHT_TYPE", elt.getState());
                            break;
                    }
                    break;
                case SIGNAL_TYPE:
                    if (alignType == SIGNAL_TYPE) {
                        add = true;
                    }
                    switch(elt.getState()) {
                        case SignalHead.DARK:
                            varType = Conditional.TYPE_SIGNAL_HEAD_DARK;
                            break;
                        case SignalHead.RED:
                            varType = Conditional.TYPE_SIGNAL_HEAD_RED;
                            break;
                        case SignalHead.FLASHRED:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHRED;
                            break;
                        case SignalHead.YELLOW:
                            varType = Conditional.TYPE_SIGNAL_HEAD_YELLOW;
                            break;
                        case SignalHead.FLASHYELLOW:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW;
                            break;
                        case SignalHead.GREEN:
                            varType = Conditional.TYPE_SIGNAL_HEAD_GREEN;
                            break;
                        case SignalHead.FLASHGREEN:
                            varType = Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN;
                            break;
                        case SET_SIGNAL_HELD:
                            varType = Conditional.TYPE_SIGNAL_HEAD_HELD;
                            break;
                        case CLEAR_SIGNAL_HELD:
                            // don't know how to test for this
                            add = false;
                            break;
                        case SET_SIGNAL_DARK:
                            varType = Conditional.TYPE_SIGNAL_HEAD_DARK;
                            break;
                        case SET_SIGNAL_LIT:
                            varType = Conditional.TYPE_SIGNAL_HEAD_LIT;
                            break;
                        default:
                            log.warn("Unexpected state {} from elt.getState() in SIGNAL_TYPE", elt.getState());
                            break;
                    }
                    break;
                default:
                    log.debug("updatePressed: Unknown Alignment state variable type " + elt.getType());
            }
            if (add && !_initialize) {
                String eltName = elt.getUserName();
                if (eltName == null || eltName.length() == 0) {
                    eltName = elt.getSysName();
                }
                vList.add(new ConditionalVariable(false, Conditional.OPERATOR_AND, varType, eltName, true));
            }
        }
        if (vList.size() > 0) {
            numConds = makeAlignConditional(numConds, aList, vList, logix, sName, uName);
        } else {
            javax.swing.JOptionPane.showMessageDialog(_addFrame, java.text.MessageFormat.format(rbx.getString("NoAlign"), new Object[] { name, sensor.getAlignType() }), Bundle.getMessage("WarningTitle"), javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }
    ///////////////// Make Lock Conditional //////////////////////////
    numConds = 1;
    if (_lock) {
        ArrayList<ConditionalAction> aList = new ArrayList<>();
        for (int k = 0; k < _includedOutputList.size(); k++) {
            RouteOutputElement elt = _includedOutputList.get(k);
            if (elt.getType() != TURNOUT_TYPE) {
                continue;
            }
            if (elt.getState() == Route.TOGGLE) {
                continue;
            }
            String eltName = elt.getUserName();
            if (eltName == null || eltName.length() == 0) {
                eltName = elt.getSysName();
            }
            aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_LOCK_TURNOUT, eltName, Turnout.LOCKED, ""));
            aList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE, Conditional.ACTION_LOCK_TURNOUT, eltName, Turnout.UNLOCKED, ""));
        }
        numConds = makeRouteConditional(numConds, /*false,*/
        aList, oneTriggerList, vetoList, logix, sName, uName, "L");
    }
    log.debug("Conditionals added= " + logix.getNumConditionals());
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        log.debug("Conditional SysName= \"" + logix.getConditionalByNumberOrder(i) + "\"");
    }
    logix.activateLogix();
    log.debug("Conditionals added= " + logix.getNumConditionals());
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        log.debug("Conditional SysName= \"" + logix.getConditionalByNumberOrder(i) + "\"");
    }
    finishUpdate();
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) ArrayList(java.util.ArrayList) ConditionalVariable(jmri.ConditionalVariable) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction)

Example 3 with ConditionalVariable

use of jmri.ConditionalVariable 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;
}
Also used : ArrayList(java.util.ArrayList) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable)

Example 4 with ConditionalVariable

use of jmri.ConditionalVariable 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();
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) ArrayList(java.util.ArrayList) SensorGroupConditional(jmri.implementation.SensorGroupConditional) DefaultListModel(javax.swing.DefaultListModel) Conditional(jmri.Conditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) ConditionalVariable(jmri.ConditionalVariable) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction)

Example 5 with ConditionalVariable

use of jmri.ConditionalVariable in project JMRI by JMRI.

the class DefaultLogix method assembleListenerList.

/**
     * Assembles a list of Listeners needed to activate this Logix
     */
private void assembleListenerList() {
    // initialize
    for (int i = _listeners.size() - 1; i >= 0; i--) {
        removeListener(_listeners.get(i));
    }
    _listeners = new ArrayList<JmriSimplePropertyListener>();
    // cycle thru Conditionals to find objects to listen to
    for (int i = 0; i < _conditionalSystemNames.size(); i++) {
        Conditional conditional = getConditional(_conditionalSystemNames.get(i));
        if (conditional != null) {
            ArrayList<ConditionalVariable> variableList = conditional.getCopyOfStateVariables();
            for (int k = 0; k < variableList.size(); k++) {
                ConditionalVariable variable = variableList.get(k);
                // check if listening for a change has been suppressed
                int varListenerType = 0;
                String varName = variable.getName();
                NamedBeanHandle<?> namedBean = variable.getNamedBean();
                int varType = variable.getType();
                int signalAspect = -1;
                // Get Listener type from variable type
                switch(varType) {
                    case Conditional.TYPE_SENSOR_ACTIVE:
                    case Conditional.TYPE_SENSOR_INACTIVE:
                        varListenerType = LISTENER_TYPE_SENSOR;
                        break;
                    case Conditional.TYPE_TURNOUT_THROWN:
                    case Conditional.TYPE_TURNOUT_CLOSED:
                        varListenerType = LISTENER_TYPE_TURNOUT;
                        break;
                    case Conditional.TYPE_CONDITIONAL_TRUE:
                    case Conditional.TYPE_CONDITIONAL_FALSE:
                        varListenerType = LISTENER_TYPE_CONDITIONAL;
                        break;
                    case Conditional.TYPE_LIGHT_ON:
                    case Conditional.TYPE_LIGHT_OFF:
                        varListenerType = LISTENER_TYPE_LIGHT;
                        break;
                    case Conditional.TYPE_MEMORY_EQUALS:
                    case Conditional.TYPE_MEMORY_COMPARE:
                    case Conditional.TYPE_MEMORY_EQUALS_INSENSITIVE:
                    case Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE:
                        varListenerType = LISTENER_TYPE_MEMORY;
                        break;
                    case Conditional.TYPE_ROUTE_FREE:
                    case Conditional.TYPE_ROUTE_OCCUPIED:
                    case Conditional.TYPE_ROUTE_ALLOCATED:
                    case Conditional.TYPE_ROUTE_SET:
                    case Conditional.TYPE_TRAIN_RUNNING:
                        varListenerType = LISTENER_TYPE_WARRANT;
                        break;
                    case Conditional.TYPE_FAST_CLOCK_RANGE:
                        varListenerType = LISTENER_TYPE_FASTCLOCK;
                        varName = "clock";
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_RED:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.RED;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_YELLOW:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.YELLOW;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_GREEN:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.GREEN;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_DARK:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.DARK;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHRED:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHRED;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHYELLOW:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHYELLOW;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_FLASHGREEN:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        signalAspect = SignalHead.FLASHGREEN;
                        break;
                    case Conditional.TYPE_SIGNAL_HEAD_LIT:
                    case Conditional.TYPE_SIGNAL_HEAD_HELD:
                        varListenerType = LISTENER_TYPE_SIGNALHEAD;
                        break;
                    case Conditional.TYPE_SIGNAL_MAST_ASPECT_EQUALS:
                    case Conditional.TYPE_SIGNAL_MAST_LIT:
                    case Conditional.TYPE_SIGNAL_MAST_HELD:
                        varListenerType = LISTENER_TYPE_SIGNALMAST;
                        break;
                    case Conditional.TYPE_BLOCK_STATUS_EQUALS:
                        varListenerType = LISTENER_TYPE_OBLOCK;
                        break;
                    case Conditional.TYPE_ENTRYEXIT_ACTIVE:
                    case Conditional.TYPE_ENTRYEXIT_INACTIVE:
                        varListenerType = LISTENER_TYPE_ENTRYEXIT;
                        break;
                    default:
                        if (!LRouteTableAction.LOGIX_INITIALIZER.equals(varName)) {
                            log.warn("Unhandled conditional variable type: {}", varType);
                        }
                        break;
                }
                int positionOfListener = getPositionOfListener(varListenerType, varType, varName);
                // add to list if new
                JmriSimplePropertyListener listener = null;
                if (positionOfListener == -1) {
                    switch(varListenerType) {
                        case LISTENER_TYPE_SENSOR:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_SENSOR, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_TURNOUT:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_TURNOUT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_CONDITIONAL:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_CONDITIONAL, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_LIGHT:
                            listener = new JmriTwoStatePropertyListener("KnownState", LISTENER_TYPE_LIGHT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_MEMORY:
                            listener = new JmriTwoStatePropertyListener("value", LISTENER_TYPE_MEMORY, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_WARRANT:
                            listener = new JmriSimplePropertyListener(null, LISTENER_TYPE_WARRANT, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_FASTCLOCK:
                            listener = new JmriClockPropertyListener("minutes", LISTENER_TYPE_FASTCLOCK, varName, varType, conditional, variable.getNum1(), variable.getNum2());
                            break;
                        case LISTENER_TYPE_SIGNALHEAD:
                            if (signalAspect < 0) {
                                if (varType == Conditional.TYPE_SIGNAL_HEAD_LIT) {
                                    listener = new JmriTwoStatePropertyListener("Lit", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional);
                                } else {
                                    // varType == Conditional.TYPE_SIGNAL_HEAD_HELD
                                    listener = new JmriTwoStatePropertyListener("Held", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional);
                                }
                            } else {
                                listener = new JmriMultiStatePropertyListener("Appearance", LISTENER_TYPE_SIGNALHEAD, namedBean, varType, conditional, signalAspect);
                            }
                            break;
                        case LISTENER_TYPE_SIGNALMAST:
                            listener = new JmriTwoStatePropertyListener("Aspect", LISTENER_TYPE_SIGNALMAST, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_OBLOCK:
                            listener = new JmriTwoStatePropertyListener("state", LISTENER_TYPE_OBLOCK, namedBean, varType, conditional);
                            break;
                        case LISTENER_TYPE_ENTRYEXIT:
                            listener = new JmriTwoStatePropertyListener("active", LISTENER_TYPE_ENTRYEXIT, namedBean, varType, conditional);
                            break;
                        default:
                            if (!LRouteTableAction.LOGIX_INITIALIZER.equals(varName)) {
                                log.error("Unknown (new) Variable Listener type= " + varListenerType + ", for varName= " + varName + ", varType= " + varType + " in Conditional, " + _conditionalSystemNames.get(i));
                            }
                            continue;
                    }
                    _listeners.add(listener);
                //log.debug("Add listener for "+varName);
                } else {
                    switch(varListenerType) {
                        case LISTENER_TYPE_SENSOR:
                        case LISTENER_TYPE_TURNOUT:
                        case LISTENER_TYPE_CONDITIONAL:
                        case LISTENER_TYPE_LIGHT:
                        case LISTENER_TYPE_MEMORY:
                        case LISTENER_TYPE_WARRANT:
                        case LISTENER_TYPE_SIGNALMAST:
                        case LISTENER_TYPE_OBLOCK:
                        case LISTENER_TYPE_ENTRYEXIT:
                            listener = _listeners.get(positionOfListener);
                            listener.addConditional(conditional);
                            break;
                        case LISTENER_TYPE_FASTCLOCK:
                            JmriClockPropertyListener cpl = (JmriClockPropertyListener) _listeners.get(positionOfListener);
                            cpl.setRange(variable.getNum1(), variable.getNum2());
                            cpl.addConditional(conditional);
                            break;
                        case LISTENER_TYPE_SIGNALHEAD:
                            if (signalAspect < 0) {
                                listener = _listeners.get(positionOfListener);
                                listener.addConditional(conditional);
                            } else {
                                JmriMultiStatePropertyListener mpl = (JmriMultiStatePropertyListener) _listeners.get(positionOfListener);
                                mpl.addConditional(conditional);
                                mpl.setState(signalAspect);
                            }
                            break;
                        default:
                            log.error("Unknown (old) Variable Listener type= " + varListenerType + ", for varName= " + varName + ", varType= " + varType + " in Conditional, " + _conditionalSystemNames.get(i));
                    }
                }
                // addition listeners needed for memory compare
                if (varType == Conditional.TYPE_MEMORY_COMPARE || varType == Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE) {
                    positionOfListener = getPositionOfListener(varListenerType, varType, variable.getDataString());
                    if (positionOfListener == -1) {
                        String name = variable.getDataString();
                        try {
                            Memory my = InstanceManager.memoryManagerInstance().provideMemory(name);
                            NamedBeanHandle<?> nb = jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, my);
                            listener = new JmriTwoStatePropertyListener("value", LISTENER_TYPE_MEMORY, nb, varType, conditional);
                            _listeners.add(listener);
                        } catch (IllegalArgumentException ex) {
                            log.error("invalid memory name= \"" + name + "\" in state variable");
                            break;
                        }
                    } else {
                        listener = _listeners.get(positionOfListener);
                        listener.addConditional(conditional);
                    }
                }
            }
        } else {
            log.error("invalid conditional system name in Logix \"" + getSystemName() + "\" assembleListenerList DELETING " + _conditionalSystemNames.get(i) + " from Conditional list.");
            _conditionalSystemNames.remove(i);
        }
    }
}
Also used : Memory(jmri.Memory) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable)

Aggregations

ConditionalVariable (jmri.ConditionalVariable)23 Conditional (jmri.Conditional)16 ConditionalAction (jmri.ConditionalAction)10 DefaultConditionalAction (jmri.implementation.DefaultConditionalAction)9 ArrayList (java.util.ArrayList)8 Logix (jmri.Logix)8 ConditionalManager (jmri.ConditionalManager)3 DefaultListModel (javax.swing.DefaultListModel)2 DefaultConditional (jmri.implementation.DefaultConditional)2 SensorGroupConditional (jmri.implementation.SensorGroupConditional)2 DefaultConditionalManager (jmri.managers.DefaultConditionalManager)2 Element (org.jdom2.Element)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 Vector (java.util.Vector)1 JButton (javax.swing.JButton)1 JList (javax.swing.JList)1 JScrollPane (javax.swing.JScrollPane)1 Block (jmri.Block)1 Memory (jmri.Memory)1