Search in sources :

Example 11 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 12 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)

Example 13 with ConditionalVariable

use of jmri.ConditionalVariable 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 14 with ConditionalVariable

use of jmri.ConditionalVariable in project JMRI by JMRI.

the class DefaultConditional method getCopyOfStateVariables.

/**
     * Make deep clone of variables
     */
@Override
public ArrayList<ConditionalVariable> getCopyOfStateVariables() {
    ArrayList<ConditionalVariable> variableList = new ArrayList<>();
    for (int i = 0; i < _variableList.size(); i++) {
        ConditionalVariable variable = _variableList.get(i);
        ConditionalVariable clone = new ConditionalVariable();
        clone.setNegation(variable.isNegated());
        clone.setOpern(variable.getOpern());
        clone.setType(variable.getType());
        clone.setName(variable.getName());
        clone.setDataString(variable.getDataString());
        clone.setNum1(variable.getNum1());
        clone.setNum2(variable.getNum2());
        clone.setTriggerActions(variable.doTriggerActions());
        clone.setState(variable.getState());
        clone.setGuiName(variable.getGuiName());
        variableList.add(clone);
    }
    return variableList;
}
Also used : ArrayList(java.util.ArrayList) ConditionalVariable(jmri.ConditionalVariable)

Example 15 with ConditionalVariable

use of jmri.ConditionalVariable in project JMRI by JMRI.

the class LogixTableAction method makeAntecedent.

/**
     * Build the antecedent statement.
     */
void makeAntecedent() {
    String str = "";
    if (_variableList.size() != 0) {
        String not = Bundle.getMessage("LogicNOT").toLowerCase();
        //NOI18N
        String row = "R";
        String and = " " + Bundle.getMessage("LogicAND").toLowerCase() + " ";
        String or = " " + Bundle.getMessage("LogicOR").toLowerCase() + " ";
        if (_variableList.get(0).isNegated()) {
            str = not + " ";
        }
        str = str + row + "1";
        for (int i = 1; i < _variableList.size(); i++) {
            ConditionalVariable variable = _variableList.get(i);
            switch(variable.getOpern()) {
                case Conditional.OPERATOR_AND:
                    str = str + and;
                    break;
                case Conditional.OPERATOR_OR:
                    str = str + or;
                    break;
                default:
                    break;
            }
            if (variable.isNegated()) {
                str = str + not + " ";
            }
            str = str + row + (i + 1);
            if (i > 0 && i + 1 < _variableList.size()) {
                str = "(" + str + ")";
            }
        }
    }
    _antecedent = str;
    _antecedentField.setText(_antecedent);
    _showReminder = true;
}
Also used : 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