Search in sources :

Example 21 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class LogixTableAction method checkConditionalReferences.

/**
     * Check for conditional references
     *
     * @since 4.7.4
     * @param logixName The Logix under consideration
     * @return true if no references
     */
boolean checkConditionalReferences(String logixName) {
    _saveTargetList.clear();
    Logix x = _logixManager.getLogix(logixName);
    int numConditionals = x.getNumConditionals();
    if (numConditionals > 0) {
        for (int i = 0; i < numConditionals; i++) {
            String csName = x.getConditionalByNumberOrder(i);
            // If the conditional is a where used source, retain it for later
            ArrayList<String> targetList = InstanceManager.getDefault(jmri.ConditionalManager.class).getTargetList(csName);
            if (targetList.size() > 0) {
                _saveTargetList.put(csName, targetList);
            }
            // If the conditional is a where used target, check scope
            ArrayList<String> refList = InstanceManager.getDefault(jmri.ConditionalManager.class).getWhereUsed(csName);
            if (refList != null) {
                for (String refName : refList) {
                    Logix xRef = _conditionalManager.getParentLogix(refName);
                    String xsName = xRef.getSystemName();
                    if (logixName.equals(xsName)) {
                        // Member of the same Logix
                        continue;
                    }
                    // External references have to be removed before the Logix can be deleted.
                    Conditional c = x.getConditional(csName);
                    Conditional cRef = xRef.getConditional(refName);
                    String[] msgs = new String[] { c.getUserName(), c.getSystemName(), cRef.getUserName(), cRef.getSystemName(), xRef.getUserName(), xRef.getSystemName() };
                    javax.swing.JOptionPane.showMessageDialog(editLogixFrame, java.text.MessageFormat.format(rbx.getString("Error11"), (Object[]) msgs), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Logix(jmri.Logix) ConditionalManager(jmri.ConditionalManager) Conditional(jmri.Conditional) DefaultConditional(jmri.implementation.DefaultConditional)

Example 22 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class LogixTableAction method calculatePressed.

/**
     * Responds to the Calculate Button in the Edit Logix window
     *
     * @param e The event heard
     */
void calculatePressed(ActionEvent e) {
    if (checkEditConditional()) {
        return;
    }
    // are there Conditionals to calculate?
    if (numConditionals > 0) {
        // There are conditionals to calculate
        String cName = "";
        Conditional c = null;
        for (int i = 0; i < numConditionals; i++) {
            cName = _curLogix.getConditionalByNumberOrder(i);
            if (cName != null) {
                c = _conditionalManager.getBySystemName(cName);
                if (c == null) {
                    log.error("Invalid conditional system name when calculating - " + cName);
                } else {
                    // calculate without taking any action
                    c.calculate(false, null);
                }
            } else {
                log.error("null conditional system name when calculating");
            }
        }
        // force the table to update
        conditionalTableModel.fireTableDataChanged();
    }
}
Also used : Conditional(jmri.Conditional) DefaultConditional(jmri.implementation.DefaultConditional)

Example 23 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class LogixTableAction method copyConditionalToLogix.

/**
     * Copy a given Conditional from one Logix to another.
     *
     * @param cSysName system name of the Conditional
     * @param srcLogix original Logix containing the Conditional
     * @param targetLogix target Logix to copy to
     */
void copyConditionalToLogix(String cSysName, Logix srcLogix, Logix targetLogix) {
    Conditional cOld = _conditionalManager.getBySystemName(cSysName);
    if (cOld == null) {
        log.error("Failure to find Conditional with System Name: " + cSysName);
        return;
    }
    String cOldSysName = cOld.getSystemName();
    String cOldUserName = cOld.getUserName();
    // make system name for new conditional
    int num = targetLogix.getNumConditionals() + 1;
    String cNewSysName = targetLogix.getSystemName() + "C" + Integer.toString(num);
    // add to Logix at the end of the calculate order
    String cNewUserName = java.text.MessageFormat.format(rbx.getString("CopyOf"), cOldUserName);
    if (cOldUserName != null && cOldUserName.length() == 0) {
        cNewUserName += "C" + Integer.toString(num);
    }
    do {
        cNewUserName = JOptionPane.showInputDialog(f, java.text.MessageFormat.format(rbx.getString("NameConditionalCopy"), new Object[] { cOldUserName, cOldSysName, _logixSysName, targetLogix.getUserName(), targetLogix.getSystemName() }), cNewUserName);
        if (cNewUserName == null || cNewUserName.length() == 0) {
            return;
        }
    } while (!checkConditionalUserName(cNewUserName, targetLogix));
    while (!checkConditionalSystemName(cNewSysName)) {
        cNewSysName = targetLogix.getSystemName() + "C" + ++num;
    }
    Conditional cNew = _conditionalManager.createNewConditional(cNewSysName, cNewUserName);
    if (cNew == null) {
        // should never get here unless there is an assignment conflict
        log.error("Failure to create Conditional with System Name: \"" + cNewSysName + "\" and User Name: \"" + cNewUserName + "\"");
        return;
    }
    cNew.setLogicType(cOld.getLogicType(), cOld.getAntecedentExpression());
    cNew.setStateVariables(cOld.getCopyOfStateVariables());
    cNew.setAction(cOld.getCopyOfActions());
    targetLogix.addConditional(cNewSysName, -1);
    // Update where used with the copy results
    _saveTargetNames.clear();
    TreeSet<String> newTargetNames = new TreeSet<String>();
    loadReferenceNames(cOld.getCopyOfStateVariables(), newTargetNames);
    updateWhereUsed(newTargetNames, cNewSysName);
}
Also used : TreeSet(java.util.TreeSet) Conditional(jmri.Conditional) DefaultConditional(jmri.implementation.DefaultConditional)

Example 24 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class DefaultLogix method calculateConditionals.

/**
     * Calculate all Conditionals, triggering action if the user specified
     * conditions are met, and the Logix is enabled.
     */
@Override
public void calculateConditionals() {
    // are there Conditionals to calculate?
    // There are conditionals to calculate
    String cName = "";
    Conditional c = null;
    for (int i = 0; i < _conditionalSystemNames.size(); i++) {
        cName = _conditionalSystemNames.get(i);
        c = getConditional(cName);
        if (c == null) {
            log.error("Invalid conditional system name when calculating Logix - " + cName);
        } else {
            // calculate without taking any action unless Logix is enabled
            c.calculate(mEnabled, null);
        }
    }
}
Also used : Conditional(jmri.Conditional)

Example 25 with Conditional

use of jmri.Conditional in project JMRI by JMRI.

the class LogixTableAction method validateVariable.

/**
     * Validate Variable data from Edit Variable Window, and transfer it to
     * current action object as appropriate.
     * <p>
     * Messages are sent to the user for any errors found. This routine returns
     * false immediately after finding the first error, even if there might be more
     * errors.
     *
     * @return true if all data checks out OK, otherwise false
     */
boolean validateVariable() {
    String name = _variableNameField.getText().trim();
    _variableNameField.setText(name);
    _curVariable.setDataString("");
    _curVariable.setNum1(0);
    _curVariable.setNum2(0);
    int itemType = _variableTypeBox.getSelectedIndex();
    int testType = 0;
    switch(itemType) {
        case Conditional.ITEM_TYPE_SENSOR:
            testType = Conditional.ITEM_TO_SENSOR_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_TURNOUT:
            testType = Conditional.ITEM_TO_TURNOUT_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_LIGHT:
            testType = Conditional.ITEM_TO_LIGHT_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_SIGNALHEAD:
            testType = Conditional.ITEM_TO_SIGNAL_HEAD_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_SIGNALMAST:
            testType = Conditional.ITEM_TO_SIGNAL_MAST_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_MEMORY:
            testType = Conditional.ITEM_TO_MEMORY_TEST[_variableCompareTypeBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_CONDITIONAL:
            testType = Conditional.ITEM_TO_CONDITIONAL_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_WARRANT:
            testType = Conditional.ITEM_TO_WARRANT_TEST[_variableStateBox.getSelectedIndex()];
            break;
        case Conditional.ITEM_TYPE_CLOCK:
            testType = Conditional.TYPE_FAST_CLOCK_RANGE;
            break;
        case Conditional.ITEM_TYPE_OBLOCK:
            testType = Conditional.TYPE_BLOCK_STATUS_EQUALS;
            break;
        case Conditional.ITEM_TYPE_ENTRYEXIT:
            testType = Conditional.ITEM_TO_ENTRYEXIT_TEST[_variableStateBox.getSelectedIndex()];
            break;
        default:
            javax.swing.JOptionPane.showMessageDialog(editConditionalFrame, rbx.getString("ErrorVariableType"), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
            return false;
    }
    _curVariable.setType(testType);
    if (log.isDebugEnabled()) {
        log.debug("validateVariable: itemType= " + itemType + ", testType= " + testType);
    }
    switch(itemType) {
        case Conditional.ITEM_TYPE_SENSOR:
            name = validateSensorReference(name);
            if (name == null) {
                return false;
            }
            break;
        case Conditional.ITEM_TYPE_TURNOUT:
            name = validateTurnoutReference(name);
            if (name == null) {
                return false;
            }
            break;
        case Conditional.ITEM_TYPE_CONDITIONAL:
            name = validateConditionalReference(name);
            if (name == null) {
                return false;
            }
            _curVariable.setName(name);
            Conditional c = _conditionalManager.getBySystemName(name);
            if (c.getUserName().length() > 0) {
                _curVariable.setGuiName(c.getUserName());
            } else {
                _curVariable.setGuiName(c.getSystemName());
            }
            break;
        case Conditional.ITEM_TYPE_LIGHT:
            name = validateLightReference(name);
            if (name == null) {
                return false;
            }
            break;
        case Conditional.ITEM_TYPE_MEMORY:
            name = validateMemoryReference(name);
            if (name == null) {
                return false;
            }
            String name2 = _variableData1Field.getText();
            if ((testType == Conditional.TYPE_MEMORY_COMPARE) || (testType == Conditional.TYPE_MEMORY_COMPARE_INSENSITIVE)) {
                name2 = validateMemoryReference(name2);
                if (name2 == null) {
                    return false;
                }
            }
            _curVariable.setDataString(name2);
            _curVariable.setNum1(_variableCompareOpBox.getSelectedIndex() + 1);
            break;
        case Conditional.ITEM_TYPE_CLOCK:
            int beginTime = parseTime(_variableData1Field.getText());
            if (beginTime < 0) {
                // parse error occurred - message has been sent
                return (false);
            }
            int endTime = parseTime(_variableData2Field.getText());
            if (endTime < 0) {
                return (false);
            }
            // set beginning and end time (minutes since midnight)
            _curVariable.setNum1(beginTime);
            _curVariable.setNum2(endTime);
            name = "Clock";
            break;
        case Conditional.ITEM_TYPE_SIGNALHEAD:
            name = validateSignalHeadReference(name);
            if (name == null) {
                return false;
            }
            if (testType == Conditional.TYPE_SIGNAL_HEAD_APPEARANCE_EQUALS) {
                String appStr = (String) _variableSignalBox.getSelectedItem();
                int type = ConditionalVariable.stringToVariableTest(appStr);
                if (type < 0) {
                    javax.swing.JOptionPane.showMessageDialog(editConditionalFrame, rbx.getString("ErrorAppearance"), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                _curVariable.setType(type);
                _curVariable.setDataString(appStr);
                if (log.isDebugEnabled()) {
                    log.debug("SignalHead \"" + name + "\"of type '" + testType + "' _variableSignalBox.getSelectedItem()= " + _variableSignalBox.getSelectedItem());
                }
            }
            break;
        case Conditional.ITEM_TYPE_SIGNALMAST:
            name = validateSignalMastReference(name);
            if (name == null) {
                return false;
            }
            if (testType == Conditional.TYPE_SIGNAL_MAST_ASPECT_EQUALS) {
                if (_variableSignalBox.getSelectedIndex() < 0) {
                    javax.swing.JOptionPane.showMessageDialog(editConditionalFrame, rbx.getString("ErrorAspect"), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                // save the selected aspect for comparison
                _curVariable.setDataString((String) _variableSignalBox.getSelectedItem());
            //                _curVariable.setType(ConditionalVariable.stringToVariableTest(appStr));
            }
            break;
        case Conditional.ITEM_TYPE_WARRANT:
            name = validateWarrantReference(name);
            if (name == null) {
                return false;
            }
            break;
        case Conditional.ITEM_TYPE_OBLOCK:
            name = validateOBlockReference(name);
            if (name == null) {
                return false;
            }
            String str = (String) _variableStateBox.getSelectedItem();
            _curVariable.setDataString(OBlock.getSystemStatusName(str));
            if (log.isDebugEnabled()) {
                log.debug("OBlock \"" + name + "\"of type '" + testType + "' _variableStateBox.getSelectedItem()= " + _variableStateBox.getSelectedItem());
            }
            break;
        case Conditional.ITEM_TYPE_ENTRYEXIT:
            name = validateEntryExitReference(name);
            if (name == null) {
                return false;
            }
            break;
        default:
            javax.swing.JOptionPane.showMessageDialog(editConditionalFrame, rbx.getString("ErrorVariableType"), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
            return false;
    }
    _curVariable.setName(name);
    boolean result = _curVariable.evaluate();
    if (log.isDebugEnabled()) {
        log.debug("State Variable \"" + name + "\"of type '" + ConditionalVariable.getTestTypeString(testType) + "' state= " + result + " type= " + _curVariable.getType());
    }
    if (_curVariable.getType() == Conditional.TYPE_NONE) {
        javax.swing.JOptionPane.showMessageDialog(editConditionalFrame, rbx.getString("ErrorVariableState"), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
        return false;
    }
    return (true);
}
Also used : Conditional(jmri.Conditional) DefaultConditional(jmri.implementation.DefaultConditional)

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