Search in sources :

Example 1 with SectionManager

use of jmri.SectionManager in project JMRI by JMRI.

the class SectionManagerXml method loadSections.

/**
     * Utility method to load the individual Section objects. If there's no
     * additional info needed for a specific Section type, invoke this with the
     * parent of the set of Section elements.
     *
     * @param sharedSections  Element containing the Section elements to load.
     * @param perNodeSections Per-node Element containing the Section elements
     *                        to load.
     */
public void loadSections(Element sharedSections, Element perNodeSections) {
    List<Element> sectionList = sharedSections.getChildren("section");
    if (log.isDebugEnabled()) {
        log.debug("Found " + sectionList.size() + " sections");
    }
    SectionManager tm = InstanceManager.getDefault(jmri.SectionManager.class);
    for (int i = 0; i < sectionList.size(); i++) {
        String sysName = getSystemName(sectionList.get(i));
        String userName = getUserName(sectionList.get(i));
        Section x = tm.createNewSection(sysName, userName);
        if (x != null) {
            // load common part
            loadCommon(x, (sectionList.get(i)));
            if (sectionList.get(i).getAttribute("creationtype") != null) {
                String creationType = sectionList.get(i).getAttribute("creationtype").getValue();
                if (creationType.equals("userdefined")) {
                    x.setSectionType(Section.USERDEFINED);
                } else if (creationType.equals("signalmastlogic")) {
                    x.setSectionType(Section.SIGNALMASTLOGIC);
                }
            }
            if (sectionList.get(i).getAttribute("fsensorname") != null) {
                String forName = (sectionList.get(i)).getAttribute("fsensorname").getValue();
                x.delayedSetForwardBlockingSensorName(forName);
            }
            if (sectionList.get(i).getAttribute("rsensorname") != null) {
                String revName = sectionList.get(i).getAttribute("rsensorname").getValue();
                x.delayedSetReverseBlockingSensorName(revName);
            }
            if (sectionList.get(i).getAttribute("fstopsensorname") != null) {
                String forName = sectionList.get(i).getAttribute("fstopsensorname").getValue();
                x.delayedSetForwardStoppingSensorName(forName);
            }
            if (sectionList.get(i).getAttribute("rstopsensorname") != null) {
                String revName = sectionList.get(i).getAttribute("rstopsensorname").getValue();
                x.delayedSetReverseStoppingSensorName(revName);
            }
            // load block entry children
            List<Element> sectionBlockList = sectionList.get(i).getChildren("blockentry");
            for (int n = 0; n < sectionBlockList.size(); n++) {
                Element elem = sectionBlockList.get(n);
                x.delayedAddBlock(elem.getAttribute("sName").getValue());
            // insert code here to verify sequence number if needed in the future
            }
            // load entry point children
            List<Element> sectionEntryPointList = sectionList.get(i).getChildren("entrypoint");
            for (int n = 0; n < sectionEntryPointList.size(); n++) {
                Element elem = sectionEntryPointList.get(n);
                String blockName = elem.getAttribute("toblock").getValue();
                String fromBlockName = elem.getAttribute("fromblock").getValue();
                String fromBlockDirection = "";
                if (elem.getAttribute("fromblockdirection") != null) {
                    fromBlockDirection = elem.getAttribute("fromblockdirection").getValue();
                }
                EntryPoint ep = new EntryPoint(blockName, fromBlockName, fromBlockDirection);
                //if (ep!=null) {
                try {
                    ep.setDirection(elem.getAttribute("direction").getIntValue());
                } catch (Exception e) {
                    log.error("Data Conversion Exception when loading direction of entry point - " + e);
                }
                boolean fixed = true;
                if (elem.getAttribute("fixed").getValue().equals("no")) {
                    fixed = false;
                }
                ep.setFixed(fixed);
                if (ep.isForwardType()) {
                    x.addToForwardList(ep);
                } else if (ep.isReverseType()) {
                    x.addToReverseList(ep);
                }
            //}
            }
        }
    }
}
Also used : SectionManager(jmri.SectionManager) Element(org.jdom2.Element) EntryPoint(jmri.EntryPoint) Section(jmri.Section) EntryPoint(jmri.EntryPoint)

Example 2 with SectionManager

use of jmri.SectionManager in project JMRI by JMRI.

the class SectionManagerXml method store.

/**
     * Implementation for storing the contents of a SectionManager
     *
     * @param o Object to store, of type SectionManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element sections = new Element("sections");
    setStoreElementClass(sections);
    SectionManager tm = (SectionManager) o;
    if (tm != null) {
        java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
        // don't return an element if there are not Sections to include
        if (!iter.hasNext()) {
            return null;
        }
        // store the Section
        while (iter.hasNext()) {
            String sname = iter.next();
            if (sname == null) {
                log.error("System name null during store");
            } else {
                log.debug("Section system name is " + sname);
                Section x = tm.getBySystemName(sname);
                if (x.getSectionType() != Section.DYNAMICADHOC) {
                    Element elem = new Element("section");
                    elem.addContent(new Element("systemName").addContent(sname));
                    // store common part
                    storeCommon(x, elem);
                    String txt = "userdefined";
                    if (x.getSectionType() == Section.SIGNALMASTLOGIC) {
                        txt = "signalmastlogic";
                    }
                    elem.setAttribute("creationtype", txt);
                    txt = x.getForwardStoppingSensorName();
                    if ((txt != null) && (!txt.equals(""))) {
                        elem.setAttribute("fstopsensorname", txt);
                    }
                    txt = x.getReverseStoppingSensorName();
                    if ((txt != null) && (!txt.equals(""))) {
                        elem.setAttribute("rstopsensorname", txt);
                    }
                    txt = x.getForwardBlockingSensorName();
                    if ((txt != null) && (!txt.equals(""))) {
                        elem.setAttribute("fsensorname", txt);
                    }
                    txt = x.getReverseBlockingSensorName();
                    if ((txt != null) && (!txt.equals(""))) {
                        elem.setAttribute("rsensorname", txt);
                    }
                    if (x.getSectionType() == Section.USERDEFINED) {
                        // save child block entries
                        int index = 0;
                        Block b = x.getBlockBySequenceNumber(index);
                        Element bElem = null;
                        while (b != null) {
                            bElem = new Element("blockentry");
                            bElem.setAttribute("sName", b.getSystemName());
                            bElem.setAttribute("order", Integer.toString(index));
                            elem.addContent(bElem);
                            index++;
                            b = x.getBlockBySequenceNumber(index);
                        }
                        // save child entry points
                        List<EntryPoint> epList = x.getEntryPointList();
                        Element epElem = null;
                        EntryPoint ep = null;
                        for (int i = 0; i < epList.size(); i++) {
                            ep = epList.get(i);
                            if (ep != null) {
                                epElem = new Element("entrypoint");
                                // add some protection against a reading problem
                                if (ep.getFromBlock() == null) {
                                    log.error("Unexpected null getFromBlock while storing ep " + i + " in Section " + sname + ", skipped");
                                    break;
                                }
                                epElem.setAttribute("fromblock", ep.getFromBlock().getSystemName());
                                if (ep.getBlock() == null) {
                                    log.error("Unexpected null getBlock while storing ep " + i + " in Section " + sname + ", skipped");
                                    break;
                                }
                                epElem.setAttribute("toblock", ep.getBlock().getSystemName());
                                epElem.setAttribute("direction", Integer.toString(ep.getDirection()));
                                epElem.setAttribute("fixed", "" + (ep.isFixed() ? "yes" : "no"));
                                epElem.setAttribute("fromblockdirection", "" + ep.getFromBlockDirection());
                                elem.addContent(epElem);
                            }
                        }
                    }
                    sections.addContent(elem);
                }
            }
        }
    }
    return (sections);
}
Also used : SectionManager(jmri.SectionManager) Element(org.jdom2.Element) Block(jmri.Block) EntryPoint(jmri.EntryPoint) Section(jmri.Section) EntryPoint(jmri.EntryPoint)

Example 3 with SectionManager

use of jmri.SectionManager in project JMRI by JMRI.

the class DefaultSignalMastLogicManager method generateSection.

/**
     * Populate Sections of type SIGNALMASTLOGIC used with Layout Editor with Signal Mast attributes
     * as stored in Signal Mast Logic.
     */
public void generateSection() {
    SectionManager sm = InstanceManager.getDefault(jmri.SectionManager.class);
    for (NamedBean nb : sm.getNamedBeanList()) {
        if (((Section) nb).getSectionType() == Section.SIGNALMASTLOGIC) {
            nb.removeProperty("intermediateSection");
        }
        nb.removeProperty("forwardMast");
    }
    for (SignalMastLogic sml : getSignalMastLogicList()) {
        jmri.jmrit.display.layoutEditor.LayoutBlock faceLBlock = sml.getFacingBlock();
        if (faceLBlock != null) {
            boolean sourceIntermediate = false;
            if (sml.getSourceMast().getProperty("intermediateSignal") != null) {
                sourceIntermediate = ((Boolean) sml.getSourceMast().getProperty("intermediateSignal")).booleanValue();
            }
            for (SignalMast destMast : sml.getDestinationList()) {
                if (sml.getAutoBlocksBetweenMasts(destMast).size() != 0) {
                    Section sec = sm.createNewSection(sml.getSourceMast().getDisplayName() + ":" + destMast.getDisplayName());
                    if (sec == null) {
                        //A Section already exists, lets grab it and check that it is one used with the SML, if so carry on using that.
                        sec = sm.getSection(sml.getSourceMast().getDisplayName() + ":" + destMast.getDisplayName());
                        if (sec.getSectionType() != Section.SIGNALMASTLOGIC) {
                            break;
                        }
                    } else {
                        sec.setSectionType(Section.SIGNALMASTLOGIC);
                        try {
                            //Auto running requires forward/reverse sensors, but at this stage SML does not support that, so just create dummy internal ones for now.
                            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor("IS:" + sec.getSystemName() + ":forward");
                            sen.setUserName(sec.getSystemName() + ":forward");
                            sen = InstanceManager.sensorManagerInstance().provideSensor("IS:" + sec.getSystemName() + ":reverse");
                            sen.setUserName(sec.getSystemName() + ":reverse");
                            sec.setForwardBlockingSensorName(sec.getSystemName() + ":forward");
                            sec.setReverseBlockingSensorName(sec.getSystemName() + ":reverse");
                        } catch (IllegalArgumentException ex) {
                            log.warn("Failed to provide Sensor in generateSection");
                        }
                    }
                    sml.setAssociatedSection(sec, destMast);
                    sec.setProperty("forwardMast", destMast.getDisplayName());
                    boolean destIntermediate = false;
                    if (destMast.getProperty("intermediateSignal") != null) {
                        destIntermediate = ((Boolean) destMast.getProperty("intermediateSignal")).booleanValue();
                    }
                    if (sourceIntermediate || destIntermediate) {
                        sec.setProperty("intermediateSection", true);
                    } else {
                        sec.setProperty("intermediateSection", false);
                    }
                //Not 100% sure about this for now so will comment out
                //sml.addSensor(sec.getSystemName()+":forward", Sensor.INACTIVE, destMast);
                }
            }
        } else {
            log.info("No facing block found " + sml.getSourceMast().getDisplayName());
        }
    }
}
Also used : SectionManager(jmri.SectionManager) NamedBean(jmri.NamedBean) SignalMastLogic(jmri.SignalMastLogic) DefaultSignalMastLogic(jmri.implementation.DefaultSignalMastLogic) SignalMast(jmri.SignalMast) Section(jmri.Section) Sensor(jmri.Sensor)

Aggregations

Section (jmri.Section)3 SectionManager (jmri.SectionManager)3 EntryPoint (jmri.EntryPoint)2 Element (org.jdom2.Element)2 Block (jmri.Block)1 NamedBean (jmri.NamedBean)1 Sensor (jmri.Sensor)1 SignalMast (jmri.SignalMast)1 SignalMastLogic (jmri.SignalMastLogic)1 DefaultSignalMastLogic (jmri.implementation.DefaultSignalMastLogic)1