Search in sources :

Example 36 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultConditionalManagerXml method loadConditionals.

/**
     * Utility method to load the individual Logix objects. If there's no
     * additional info needed for a specific logix type, invoke this with the
     * parent of the set of Logix elements.
     *
     * @param conditionals Element containing the Logix elements to load.
     */
public void loadConditionals(Element conditionals) {
    List<Element> conditionalList = conditionals.getChildren("conditional");
    if (log.isDebugEnabled()) {
        log.debug("Found " + conditionalList.size() + " conditionals");
    }
    ConditionalManager tm = InstanceManager.getDefault(jmri.ConditionalManager.class);
    for (int i = 0; i < conditionalList.size(); i++) {
        Element condElem = conditionalList.get(i);
        String sysName = getSystemName(condElem);
        if (sysName == null) {
            log.warn("unexpected null in systemName " + condElem);
            break;
        }
        // omitted username is treated as empty, not null
        String userName = getUserName(condElem);
        if (userName == null) {
            userName = "";
        }
        if (log.isDebugEnabled()) {
            log.debug("create conditional: ({})({})", sysName, userName);
        }
        // Try getting the conditional.  This should fail
        Conditional c = tm.getBySystemName(sysName);
        if (c == null) {
            // Check for parent Logix
            Logix x = tm.getParentLogix(sysName);
            if (x == null) {
                log.warn("Conditional '{}' has no parent Logix", sysName);
                continue;
            }
            // Found a potential parent Logix, check the Logix index
            boolean inIndex = false;
            for (int j = 0; j < x.getNumConditionals(); j++) {
                String cName = x.getConditionalByNumberOrder(j);
                if (sysName.equals(cName)) {
                    inIndex = true;
                    break;
                }
            }
            if (!inIndex) {
                log.warn("Conditional '{}' is not in the Logix index", sysName);
                continue;
            }
            // Create the condtional
            c = tm.createNewConditional(sysName, userName);
        }
        if (c == null) {
            // Should never get here
            log.error("Conditional '{}' cannot be created", sysName);
            continue;
        }
        // conditional already exists
        // load common parts
        loadCommon(c, condElem);
        String ant = "";
        int logicType = Conditional.ALL_AND;
        if (condElem.getAttribute("antecedent") != null) {
            ant = condElem.getAttribute("antecedent").getValue();
        }
        if (condElem.getAttribute("logicType") != null) {
            logicType = Integer.parseInt(condElem.getAttribute("logicType").getValue());
        }
        c.setLogicType(logicType, ant);
        // load state variables, if there are any
        List<Element> conditionalVarList = condElem.getChildren("conditionalStateVariable");
        if (conditionalVarList.size() == 0) {
            log.warn("No state variables found for conditional " + sysName);
        }
        ArrayList<ConditionalVariable> variableList = new ArrayList<>();
        for (int n = 0; n < conditionalVarList.size(); n++) {
            ConditionalVariable variable = new ConditionalVariable();
            if (conditionalVarList.get(n).getAttribute("operator") == null) {
                log.warn("unexpected null in operator " + conditionalVarList.get(n) + " " + conditionalVarList.get(n).getAttributes());
            } else {
                int oper = Integer.parseInt(conditionalVarList.get(n).getAttribute("operator").getValue());
                if (oper == Conditional.OPERATOR_AND_NOT) {
                    variable.setNegation(true);
                    oper = Conditional.OPERATOR_AND;
                } else if (oper == Conditional.OPERATOR_NOT) {
                    variable.setNegation(true);
                    oper = Conditional.OPERATOR_NONE;
                }
                variable.setOpern(oper);
            }
            if (conditionalVarList.get(n).getAttribute("negated") != null) {
                if ("yes".equals(conditionalVarList.get(n).getAttribute("negated").getValue())) {
                    variable.setNegation(true);
                } else {
                    variable.setNegation(false);
                }
            }
            variable.setType(Integer.parseInt(conditionalVarList.get(n).getAttribute("type").getValue()));
            variable.setName(conditionalVarList.get(n).getAttribute("systemName").getValue());
            if (conditionalVarList.get(n).getAttribute("dataString") != null) {
                variable.setDataString(conditionalVarList.get(n).getAttribute("dataString").getValue());
            }
            if (conditionalVarList.get(n).getAttribute("num1") != null) {
                variable.setNum1(Integer.parseInt(conditionalVarList.get(n).getAttribute("num1").getValue()));
            }
            if (conditionalVarList.get(n).getAttribute("num2") != null) {
                variable.setNum2(Integer.parseInt(conditionalVarList.get(n).getAttribute("num2").getValue()));
            }
            variable.setTriggerActions(true);
            if (conditionalVarList.get(n).getAttribute("triggersCalc") != null) {
                if ("no".equals(conditionalVarList.get(n).getAttribute("triggersCalc").getValue())) {
                    variable.setTriggerActions(false);
                }
            }
            variableList.add(variable);
        }
        c.setStateVariables(variableList);
        // load actions - there better be some
        List<Element> conditionalActionList = condElem.getChildren("conditionalAction");
        // Really OK, since a user may use such conditionals to define a reusable
        // expression of state variables.  These conditions are then used as a 
        // state variable in other conditionals.  (pwc)
        //if (conditionalActionList.size() == 0) {
        //    log.warn("No actions found for conditional "+sysName);
        //}
        ArrayList<ConditionalAction> actionList = ((DefaultConditional) c).getActionList();
        org.jdom2.Attribute attr = null;
        for (int n = 0; n < conditionalActionList.size(); n++) {
            ConditionalAction action = new DefaultConditionalAction();
            attr = conditionalActionList.get(n).getAttribute("option");
            if (attr != null) {
                action.setOption(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in option " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            // actionDelay is removed.  delay data is stored as a String to allow
            // such data be referenced by internal memory.
            // For backward compatibility, set delay "int" as a string
            attr = conditionalActionList.get(n).getAttribute("delay");
            if (attr != null) {
                action.setActionString(attr.getValue());
            }
            attr = conditionalActionList.get(n).getAttribute("type");
            if (attr != null) {
                action.setType(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in type " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("systemName");
            if (attr != null) {
                action.setDeviceName(attr.getValue());
            } else {
                log.warn("unexpected null in systemName " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("data");
            if (attr != null) {
                action.setActionData(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in action data " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("string");
            if (attr != null) {
                action.setActionString(attr.getValue());
            } else {
                log.warn("unexpected null in action string " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            if (!actionList.contains(action))
                actionList.add(action);
        }
        c.setAction(actionList);
        // 1/16/2011 - trigger for execution of the action list changed to execute each 
        // time state is computed.  Formerly execution of the action list was done only
        // when state changes.  All conditionals are upgraded to this new policy.
        // However, for conditionals with actions that toggle on change of state
        // the old policy should be used.
        boolean triggerOnChange = false;
        if (condElem.getAttribute("triggerOnChange") != null) {
            if ("yes".equals(condElem.getAttribute("triggerOnChange").getValue())) {
                triggerOnChange = true;
            }
        } else {
            /* Don't upgrade -Let old be as is
                 for (int k=0; k<actionList.size(); k++){
                 ConditionalAction action = actionList.get(k);
                 if (action.getOption()==Conditional.ACTION_OPTION_ON_CHANGE){
                 triggerOnChange = true;
                 break;
                 }
                 }
                 */
            triggerOnChange = true;
        }
        c.setTriggerOnChange(triggerOnChange);
    }
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) DefaultConditional(jmri.implementation.DefaultConditional) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable) DefaultConditional(jmri.implementation.DefaultConditional) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) DefaultConditionalManager(jmri.managers.DefaultConditionalManager) ConditionalManager(jmri.ConditionalManager)

Example 37 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class LRouteTableAction method cancelPressed.

/**
     * Responds to the Cancel button.
     *
     * @param e the action event
     */
void cancelPressed(ActionEvent e) {
    Logix logix = checkNamesOK();
    if (logix != null) {
        logix.activateLogix();
    }
    clearPage();
}
Also used : Logix(jmri.Logix)

Example 38 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultLogixManager method createNewLogix.

/**
     * Method to create a new Logix if the Logix does not exist.
     * <p>
     * Returns null if
     * a Logix with the same systemName or userName already exists, or if there
     * is trouble creating a new Logix.
     */
@Override
public Logix createNewLogix(String systemName, String userName) {
    // Check that Logix does not already exist
    Logix x;
    if (userName != null && !userName.equals("")) {
        x = getByUserName(userName);
        if (x != null) {
            return null;
        }
    }
    x = getBySystemName(systemName);
    if (x == null) {
        // for compatibility?
        x = getBySystemName(systemName.toUpperCase());
    }
    if (x != null) {
        return null;
    }
    // Logix does not exist, create a new Logix
    x = new DefaultLogix(systemName, userName);
    // save in the maps
    register(x);
    /*The following keeps track of the last created auto system name.
         currently we do not reuse numbers, although there is nothing to stop the
         user from manually recreating them*/
    if (systemName.startsWith("IX:AUTO:")) {
        try {
            int autoNumber = Integer.parseInt(systemName.substring(8));
            if (autoNumber > lastAutoLogixRef) {
                lastAutoLogixRef = autoNumber;
            }
        } catch (NumberFormatException e) {
            log.warn("Auto generated SystemName " + systemName + " is not in the correct format");
        }
    }
    return x;
}
Also used : Logix(jmri.Logix) DefaultLogix(jmri.implementation.DefaultLogix) DefaultLogix(jmri.implementation.DefaultLogix)

Example 39 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultLogixManager method activateAllLogixs.

/**
     * Activate all Logixs that are not currently active This method is called
     * after a configuration file is loaded.
     */
@Override
public void activateAllLogixs() {
    // Guarantee Initializer executes first.
    Logix x = getBySystemName(LRouteTableAction.LOGIX_INITIALIZER);
    if (x != null) {
        x.activateLogix();
        x.setGuiNames();
    }
    // iterate thru all Logixs that exist
    java.util.Iterator<String> iter = getSystemNameList().iterator();
    while (iter.hasNext()) {
        // get the next Logix
        String sysName = iter.next();
        if (sysName == null) {
            log.error("System name null when activating Logixs");
            break;
        }
        if (sysName.equals(LRouteTableAction.LOGIX_INITIALIZER)) {
            continue;
        }
        x = getBySystemName(sysName);
        if (x == null) {
            log.error("Error getting Logix *" + sysName + "* when activating Logixs");
            break;
        }
        if (loadDisabled) {
            // user has requested that Logixs be loaded disabled
            log.warn("load disabled set - will not activate logic for: " + x.getDisplayName());
            x.setEnabled(false);
        }
        if (x.getEnabled()) {
            //System.out.println("logix set enabled");
            x.activateLogix();
        }
        x.setGuiNames();
    }
    // reset the load switch
    loadDisabled = false;
}
Also used : Logix(jmri.Logix) DefaultLogix(jmri.implementation.DefaultLogix)

Aggregations

Logix (jmri.Logix)39 Conditional (jmri.Conditional)15 ConditionalVariable (jmri.ConditionalVariable)8 ConditionalAction (jmri.ConditionalAction)7 ArrayList (java.util.ArrayList)6 LogixManager (jmri.LogixManager)6 ConditionalManager (jmri.ConditionalManager)5 DefaultConditionalAction (jmri.implementation.DefaultConditionalAction)5 SensorGroupConditional (jmri.implementation.SensorGroupConditional)5 DefaultListModel (javax.swing.DefaultListModel)4 DefaultConditional (jmri.implementation.DefaultConditional)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 JButton (javax.swing.JButton)3 Route (jmri.Route)3 Sensor (jmri.Sensor)3 UserPreferencesManager (jmri.UserPreferencesManager)3 Element (org.jdom2.Element)3 BoxLayout (javax.swing.BoxLayout)2 JLabel (javax.swing.JLabel)2