Search in sources :

Example 16 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultConditionalManager method createNewConditional.

/**
     * Method to create a new Conditional if the Conditional does not exist If
     * the parent Logix cannot be found, the userName cannot be checked, but the
     * Conditional is still created. The scenario can happen when a Logix is
     * loaded from a file after its Conditionals.
     *
     * @param systemName properly formatted system name for the new Conditional
     * @param userName must not be null, use "" instead
     * @return null if a Conditional with the same systemName or userName
     *         already exists, or if there is trouble creating a new Conditional
     */
@Override
public Conditional createNewConditional(String systemName, String userName) {
    Conditional c = null;
    // Check system name
    if (systemName == null || systemName.length() < 1) {
        log.error("createNewConditional: systemName is null or empty");
        return null;
    }
    c = getBySystemName(systemName);
    if (c != null) {
        // Conditional already exists
        return null;
    }
    // Get the potential parent Logix
    Logix lgx = getParentLogix(systemName);
    if (lgx == null) {
        log.error("Unable to find the parent logix for condtional '{}'", systemName);
        return null;
    }
    // Check the user name
    if (userName != null && userName.length() > 0) {
        c = getByUserName(lgx, userName);
        if (c != null) {
            // Duplicate user name within the parent Logix
            return null;
        }
    }
    // Conditional does not exist, create a new Conditional
    if (systemName.startsWith(SensorGroupFrame.logixSysName)) {
        c = new SensorGroupConditional(systemName, userName);
    } else {
        c = new DefaultConditional(systemName, userName);
    }
    // save in the maps
    //@        register(c);
    boolean addCompleted = lgx.addConditional(systemName, c);
    if (!addCompleted) {
        return null;
    }
    return c;
}
Also used : DefaultConditional(jmri.implementation.DefaultConditional) Logix(jmri.Logix) SensorGroupConditional(jmri.implementation.SensorGroupConditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) DefaultConditional(jmri.implementation.DefaultConditional) Conditional(jmri.Conditional)

Example 17 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultConditionalManager method getParentLogix.

/**
     * Parses the Conditional system name to get the parent Logix system name,
     * then gets the parent Logix, and returns it.  For sensor groups, the parent
     * Logix name is 'SYS'.  LRoutes and exported Routes (RTX prefix) require
     * special logic
     *
     * @param name - system name of Conditional (must be trimmed and upper case)
     * @return the parent Logix or null
     */
@Override
public Logix getParentLogix(String name) {
    if (name == null || name.length() < 4) {
        return null;
    }
    // Check for standard names
    for (String pattern : PATTERNS) {
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(name);
        if (m.find()) {
            Logix lgx = InstanceManager.getDefault(jmri.LogixManager.class).getBySystemName(m.group(1));
            if (lgx != null) {
                return lgx;
            }
        }
    }
    // Now try non-standard names using a brute force scan
    jmri.LogixManager logixManager = InstanceManager.getDefault(jmri.LogixManager.class);
    for (String xName : logixManager.getSystemNameList()) {
        Logix lgx = logixManager.getLogix(xName);
        for (int i = 0; i < lgx.getNumConditionals(); i++) {
            String cdlName = lgx.getConditionalByNumberOrder(i);
            if (cdlName.equals(name)) {
                return lgx;
            }
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Logix(jmri.Logix) Matcher(java.util.regex.Matcher)

Example 18 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultConditionalManager method getSystemNameList.

/**
     * Get a list of all Conditional system names
     * Overrides the bean method
     * @since 4.7.4
     * @return a list of conditional system names regardless of parent Logix
     */
@Override
public List<String> getSystemNameList() {
    List<String> nameList = new ArrayList<>();
    jmri.LogixManager logixManager = InstanceManager.getDefault(jmri.LogixManager.class);
    for (String xName : logixManager.getSystemNameList()) {
        Logix lgx = logixManager.getLogix(xName);
        for (int i = 0; i < lgx.getNumConditionals(); i++) {
            nameList.add(lgx.getConditionalByNumberOrder(i));
        }
    }
    Collections.sort(nameList);
    return nameList;
}
Also used : Logix(jmri.Logix) ArrayList(java.util.ArrayList)

Example 19 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultLogixManagerXml method store.

/**
     * Default implementation for storing the contents of a LogixManager
     *
     * @param o Object to store, of type LogixManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element logixs = new Element("logixs");
    setStoreElementClass(logixs);
    LogixManager tm = (LogixManager) o;
    if (tm != null) {
        java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
        // don't return an element if there are not Logix to include
        if (!iter.hasNext()) {
            return null;
        }
        // store the Logix
        while (iter.hasNext()) {
            String sname = iter.next();
            if (sname == null) {
                log.error("System name null during store");
            }
            log.debug("logix system name is " + sname);
            Logix x = tm.getBySystemName(sname);
            boolean enabled = x.getEnabled();
            Element elem = new Element("logix");
            elem.addContent(new Element("systemName").addContent(sname));
            // store common part
            storeCommon(x, elem);
            if (enabled) {
                elem.setAttribute("enabled", "yes");
            } else {
                elem.setAttribute("enabled", "no");
            }
            // save child Conditionals
            int numConditionals = x.getNumConditionals();
            if (numConditionals > 0) {
                String cSysName = "";
                Element cElem = null;
                for (int k = 0; k < numConditionals; k++) {
                    cSysName = x.getConditionalByNumberOrder(k);
                    cElem = new Element("logixConditional");
                    cElem.setAttribute("systemName", cSysName);
                    cElem.setAttribute("order", Integer.toString(k));
                    elem.addContent(cElem);
                }
            }
            logixs.addContent(elem);
        }
    }
    return (logixs);
}
Also used : Logix(jmri.Logix) Element(org.jdom2.Element) LogixManager(jmri.LogixManager) DefaultLogixManager(jmri.managers.DefaultLogixManager)

Example 20 with Logix

use of jmri.Logix in project JMRI by JMRI.

the class DefaultLogixManagerXml method loadLogixs.

/**
     * 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 logixs Element containing the Logix elements to load.
     */
public void loadLogixs(Element logixs) {
    List<Element> logixList = logixs.getChildren("logix");
    if (log.isDebugEnabled()) {
        log.debug("Found " + logixList.size() + " logixs");
    }
    LogixManager tm = InstanceManager.getDefault(jmri.LogixManager.class);
    for (int i = 0; i < logixList.size(); i++) {
        String sysName = getSystemName(logixList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + logixList.get(i));
            break;
        }
        String userName = getUserName(logixList.get(i));
        String yesno = "";
        if (logixList.get(i).getAttribute("enabled") != null) {
            yesno = logixList.get(i).getAttribute("enabled").getValue();
        }
        if (log.isDebugEnabled()) {
            log.debug("create logix: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Logix x = tm.createNewLogix(sysName, userName);
        if (x != null) {
            // load common part
            loadCommon(x, logixList.get(i));
            // set enabled/disabled if attribute was present
            if ((yesno != null) && (!yesno.equals(""))) {
                if (yesno.equals("yes")) {
                    x.setEnabled(true);
                } else if (yesno.equals("no")) {
                    x.setEnabled(false);
                }
            }
            // load conditionals, if there are any
            List<Element> logixConditionalList = logixList.get(i).getChildren("logixConditional");
            if (logixConditionalList.size() > 0) {
                // add conditionals
                for (int n = 0; n < logixConditionalList.size(); n++) {
                    if (logixConditionalList.get(n).getAttribute("systemName") == null) {
                        log.warn("unexpected null in systemName " + logixConditionalList.get(n) + " " + logixConditionalList.get(n).getAttributes());
                        break;
                    }
                    String cSysName = logixConditionalList.get(n).getAttribute("systemName").getValue();
                    int cOrder = Integer.parseInt(logixConditionalList.get(n).getAttribute("order").getValue());
                    // add conditional to logix
                    x.addConditional(cSysName, cOrder);
                }
            }
        }
    }
}
Also used : Logix(jmri.Logix) Element(org.jdom2.Element) LogixManager(jmri.LogixManager) DefaultLogixManager(jmri.managers.DefaultLogixManager)

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