Search in sources :

Example 16 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class SensorGroupFrame method deleteGroup.

void deleteGroup(boolean showMsg) {
    String group = _nameField.getText();
    if (group == null || group.equals("")) {
        if (showMsg) {
            javax.swing.JOptionPane.showMessageDialog(this, "'View' the group or enter the group name in the 'Group Name' field before selecting 'Undo Group'", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
        return;
    }
    String prefix = (namePrefix + group + nameDivider).toUpperCase();
    // remove the old routes
    RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
    List<String> l = rm.getSystemNameList();
    for (int i = 0; i < l.size(); i++) {
        String name = l.get(i);
        if (name.startsWith(prefix)) {
            // OK, kill this one
            Route r = rm.getBySystemName(l.get(i));
            r.deActivateRoute();
            rm.deleteRoute(r);
        }
    }
    String cSystemName = (ConditionalSystemPrefix + group).toUpperCase();
    String cUserName = ConditionalUserPrefix + group;
    Logix logix = getSystemLogix();
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        String name = logix.getConditionalByNumberOrder(i);
        if (cSystemName.equals(name) || cUserName.equals(name)) {
            Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(name);
            if (c == null) {
                log.error("Conditional \"" + name + "\" expected but NOT found in Logix " + logix.getSystemName());
            } else {
                logix.deleteConditional(cSystemName);
                break;
            }
        }
    }
    DefaultListModel<String> model = (DefaultListModel<String>) _sensorGroupList.getModel();
    int index = model.indexOf(group);
    if (index > -1) {
        model.remove(index);
    }
    index = _sensorGroupList.getSelectedIndex();
    if (index > -1) {
        String sysName = ConditionalSystemPrefix + model.elementAt(index);
        String[] msgs = logix.deleteConditional(sysName);
        if (msgs != null) {
            if (showMsg) {
                javax.swing.JOptionPane.showMessageDialog(this, "Conditional " + msgs[0] + " (" + msgs[1] + ") is a Conditional Variable in the Conditional,\n" + msgs[2] + " (" + msgs[3] + "), of Logix, " + msgs[4] + " (" + msgs[5] + ").\nPlease remove that variable first.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        } else {
            model.remove(index);
        }
    }
}
Also used : Logix(jmri.Logix) DefaultListModel(javax.swing.DefaultListModel) Conditional(jmri.Conditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) Route(jmri.Route) RouteManager(jmri.RouteManager)

Example 17 with Conditional

use of jmri.Conditional 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)

Example 18 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class DefaultLogix method setGuiNames.

/**
     * ConditionalVariables only have a single name field.  For user interface purposes
     * a gui name is used for the referenced conditional user name.  This is not used
     * for other object types.
     * <p>
     * In addition to setting the GUI name, any state variable references are changed to
     * conditional system names.  This converts the XML system/user name field to the system name
     * for conditional references.  It does not affect other objects such as sensors, turnouts, etc.
     * Called by {@link jmri.managers.DefaultLogixManager#activateAllLogixs}
     * @since 4.7.4
     */
@Override
public void setGuiNames() {
    if (_isGuiSet) {
        return;
    }
    if (getSystemName().equals("SYS")) {
        _isGuiSet = true;
        return;
    }
    for (int i = 0; i < _conditionalSystemNames.size(); i++) {
        String cName = _conditionalSystemNames.get(i);
        Conditional conditional = getConditional(cName);
        if (conditional == null) {
            // A Logix index entry exists without a corresponding conditional.  This
            // should never happen.
            log.error("setGuiNames: Missing conditional for Logix index entry,  Logix name = '{}', Conditional index name = '{}'", getSystemName(), cName);
            continue;
        }
        ArrayList<ConditionalVariable> varList = conditional.getCopyOfStateVariables();
        boolean isDirty = false;
        for (ConditionalVariable var : varList) {
            // Find any Conditional State Variables
            if (var.getType() == Conditional.TYPE_CONDITIONAL_TRUE || var.getType() == Conditional.TYPE_CONDITIONAL_FALSE) {
                // Get the referenced (target) conditonal -- The name can be either a system name or a user name
                Conditional cRef = InstanceManager.getDefault(jmri.ConditionalManager.class).getConditional(var.getName());
                if (cRef != null) {
                    // re-arrange names as needed
                    // The state variable reference is now a conditional system name
                    var.setName(cRef.getSystemName());
                    if (cRef.getUserName() == null || cRef.getUserName().length() < 1) {
                        var.setGuiName(cRef.getSystemName());
                    } else {
                        var.setGuiName(cRef.getUserName());
                    }
                    // Add the conditional reference to the where used map
                    InstanceManager.getDefault(jmri.ConditionalManager.class).addWhereUsed(var.getName(), cName);
                    isDirty = true;
                } else {
                    log.error("setGuiNames: For conditional '{}' in logix '{}', the referenced conditional, '{}',  does not exist", cName, getSystemName(), var.getName());
                }
            }
        }
        if (isDirty) {
            conditional.setStateVariables(varList);
        }
    }
    _isGuiSet = true;
}
Also used : ConditionalManager(jmri.ConditionalManager) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable)

Example 19 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class DefaultLogix method addConditional.

/**
     * Add a child Conditional to the parent Logix.
     *
     * @since 4.7.4
     * @param systemName The system name for the Conditional object.
     * @param conditional The Conditional object.
     * @return true if the Conditional was added, false otherwise.
     */
@Override
public boolean addConditional(String systemName, Conditional conditional) {
    Conditional chkDuplicate = _conditionalMap.putIfAbsent(systemName, conditional);
    if (chkDuplicate == null) {
        return (true);
    }
    log.error("Conditional '{}' has already been added to Logix '{}'", systemName, getSystemName());
    return (false);
}
Also used : Conditional(jmri.Conditional)

Example 20 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class DefaultConditionalManagerTest method testUserNameOverlap.

public void testUserNameOverlap() {
    ConditionalManager m = new DefaultConditionalManager();
    Conditional c1 = m.createNewConditional("IX02C01", "Foo");
    Conditional c2 = m.createNewConditional("IX02C02", "Foo");
    Assert.assertTrue(c1.getUserName().equals("Foo"));
    Assert.assertTrue(c2.getUserName().equals("Foo"));
}
Also used : ConditionalManager(jmri.ConditionalManager) Conditional(jmri.Conditional)

Aggregations

Conditional (jmri.Conditional)38 ConditionalVariable (jmri.ConditionalVariable)16 Logix (jmri.Logix)15 ConditionalAction (jmri.ConditionalAction)10 DefaultConditional (jmri.implementation.DefaultConditional)10 ConditionalManager (jmri.ConditionalManager)8 DefaultConditionalAction (jmri.implementation.DefaultConditionalAction)8 ArrayList (java.util.ArrayList)7 SensorGroupConditional (jmri.implementation.SensorGroupConditional)6 DefaultListModel (javax.swing.DefaultListModel)5 Sensor (jmri.Sensor)5 Light (jmri.Light)4 SignalHead (jmri.SignalHead)4 Turnout (jmri.Turnout)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 JButton (javax.swing.JButton)3 JScrollPane (javax.swing.JScrollPane)3 Block (jmri.Block)3 Memory (jmri.Memory)3