Search in sources :

Example 41 with Block

use of jmri.Block in project JMRI by JMRI.

the class ManuallySetRoute method findDestPoint.

boolean findDestPoint(LayoutBlock pro, LayoutBlock facing) {
    depth++;
    if (//This is to prevent a loop, only look as far as 50 blocks
    depth > 50) {
        return false;
    }
    boolean looking = true;
    if (pro.getNumberOfThroughPaths() == 0) {
        destLoc = lbm.getSensorAtEndBumper(pro.getBlock(), sourcePoint.getPanel());
    } else {
        while (looking) {
            Block found = cycle(pro, facing);
            if (found != null) {
                destLoc = lbm.getFacingBean(pro.getBlock(), found, sourcePoint.getPanel(), jmri.Sensor.class);
                if (destLoc != null) {
                    looking = false;
                } else {
                    findDestPoint(lbm.getLayoutBlock(found), pro);
                    looking = false;
                }
            } else {
                looking = false;
            }
        }
    }
    if (destLoc != null) {
        return true;
    }
    return false;
}
Also used : LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) Block(jmri.Block)

Example 42 with Block

use of jmri.Block in project JMRI by JMRI.

the class BlockManagerXml method loadPath.

/**
     * Load path into an existing Block from XML.
     *
     * @param block   Block to receive path
     * @param element Element containing path information
     * @return true if path added to block; false otherwise
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public boolean loadPath(Block block, Element element) throws JmriConfigureXmlException {
    // load individual path
    int toDir = 0;
    int fromDir = 0;
    try {
        toDir = element.getAttribute("todir").getIntValue();
        fromDir = element.getAttribute("fromdir").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("Could not parse path attribute");
    } catch (NullPointerException e) {
        handleException("Block Path entry in file missing required attribute", null, block.getSystemName(), block.getUserName(), null);
    }
    Block toBlock = null;
    if (element.getAttribute("block") != null) {
        String name = element.getAttribute("block").getValue();
        toBlock = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
    }
    Path path = new Path(toBlock, toDir, fromDir);
    List<Element> settings = element.getChildren("beansetting");
    for (int i = 0; i < settings.size(); i++) {
        Element setting = settings.get(i);
        loadBeanSetting(path, setting);
    }
    // check if path already in block
    if (!block.hasPath(path)) {
        block.addPath(path);
        return true;
    } else {
        log.debug("Skipping load of duplicate path {}", path);
        return false;
    }
}
Also used : Path(jmri.Path) Element(org.jdom2.Element) Block(jmri.Block)

Example 43 with Block

use of jmri.Block in project JMRI by JMRI.

the class BlockManagerXml method store.

/**
     * Store the contents of a BlockManager.
     *
     * @param o Object to store, of type BlockManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element blocks = new Element("blocks");
    setStoreElementClass(blocks);
    BlockManager tm = (BlockManager) o;
    if (tm != null) {
        java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
        // don't return an element if there are not blocks to include
        if (!iter.hasNext()) {
            return null;
        }
        blocks.addContent(new Element("defaultspeed").addContent(tm.getDefaultSpeed()));
        // write out first set of blocks without contents
        while (iter.hasNext()) {
            try {
                String sname = iter.next();
                if (sname == null) {
                    log.error("System name null during store");
                } else {
                    Block b = tm.getBySystemName(sname);
                    // the following null check is to catch a null pointer exception that sometimes was found to happen
                    if (b == null) {
                        log.error("Null block during store - sname = " + sname);
                    } else {
                        Element elem = new Element("block");
                        elem.addContent(new Element("systemName").addContent(sname));
                        // the following null check is to catch a null pointer exception that sometimes was found to happen
                        String uname = b.getUserName();
                        if ((uname != null) && (!uname.equals(""))) {
                            elem.addContent(new Element("userName").addContent(b.getUserName()));
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("initial store Block " + sname);
                        }
                        // and put this element out
                        blocks.addContent(elem);
                    }
                }
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
        // write out again with contents
        iter = tm.getSystemNameList().iterator();
        while (iter.hasNext()) {
            String sname = iter.next();
            if (sname == null) {
                log.error("System name null during store skipped for this block");
            } else {
                Block b = tm.getBySystemName(sname);
                // the following null check is to catch a null pointer exception that sometimes was found to happen
                if (b == null) {
                    log.error("Null Block during store - second store skipped for this block - " + sname);
                } else {
                    String uname = b.getUserName();
                    if (uname == null) {
                        uname = "";
                    }
                    Element elem = new Element("block");
                    elem.addContent(new Element("systemName").addContent(sname));
                    if (log.isDebugEnabled()) {
                        log.debug("second store Block " + sname + ":" + uname);
                    }
                    // store length and curvature attributes
                    elem.setAttribute("length", Float.toString(b.getLengthMm()));
                    elem.setAttribute("curve", Integer.toString(b.getCurvature()));
                    // store common parts
                    storeCommon(b, elem);
                    if ((!b.getBlockSpeed().equals("")) && !b.getBlockSpeed().contains("Global")) {
                        elem.addContent(new Element("speed").addContent(b.getBlockSpeed()));
                    }
                    String perm = "no";
                    if (b.getPermissiveWorking()) {
                        perm = "yes";
                    }
                    elem.addContent(new Element("permissive").addContent(perm));
                    // Add content. First, the sensor.
                    if (b.getNamedSensor() != null) {
                        elem.addContent(new Element("occupancysensor").addContent(b.getNamedSensor().getName()));
                    }
                    if (b.getDeniedBlocks().size() > 0) {
                        Element denied = new Element("deniedBlocks");
                        b.getDeniedBlocks().forEach((deniedBlock) -> {
                            denied.addContent(new Element("block").addContent(deniedBlock));
                        });
                        elem.addContent(denied);
                    }
                    // Now the Reporter
                    Reporter r = b.getReporter();
                    if (r != null) {
                        Element re = new Element("reporter");
                        re.setAttribute("systemName", r.getSystemName());
                        re.setAttribute("useCurrent", b.isReportingCurrent() ? "yes" : "no");
                        elem.addContent(re);
                    }
                    if (tm.isSavedPathInfo()) {
                        // then the paths
                        List<Path> paths = b.getPaths();
                        for (int i = 0; i < paths.size(); i++) {
                            addPath(elem, paths.get(i));
                        }
                    // and put this element out
                    }
                    blocks.addContent(elem);
                }
            }
        }
    }
    return blocks;
}
Also used : Path(jmri.Path) BlockManager(jmri.BlockManager) Element(org.jdom2.Element) Reporter(jmri.Reporter) Block(jmri.Block)

Example 44 with Block

use of jmri.Block in project JMRI by JMRI.

the class BlockManagerXml method loadBlock.

/**
     * Utility method to load the individual Block objects.
     *
     * @param element Element containing one block
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public void loadBlock(Element element) throws JmriConfigureXmlException {
    String sysName = getSystemName(element);
    String userName = getUserName(element);
    if (log.isDebugEnabled()) {
        log.debug("defined Block: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
    }
    Block block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    if (block == null) {
        // create it if doesn't exist
        InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sysName, userName);
        block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    }
    if (block == null) {
        log.error("Unable to load block with system name " + sysName + " and username of " + (userName == null ? "<null>" : userName));
        return;
    }
    if (userName != null) {
        block.setUserName(userName);
    }
    if (element.getAttribute("length") != null) {
        // load length in millimeters
        block.setLength(Float.parseFloat(element.getAttribute("length").getValue()));
    }
    if (element.getAttribute("curve") != null) {
        // load curve attribute
        block.setCurvature(Integer.parseInt((element.getAttribute("curve")).getValue()));
    }
    try {
        block.setBlockSpeed("Global");
        if (element.getChild("speed") != null) {
            String speed = element.getChild("speed").getText();
            if (speed != null && !speed.equals("") && !speed.contains("Global")) {
                block.setBlockSpeed(speed);
            }
        }
    } catch (jmri.JmriException ex) {
        log.error(ex.toString());
    }
    if (element.getChild("permissive") != null) {
        boolean permissive = false;
        if (element.getChild("permissive").getText().equals("yes")) {
            permissive = true;
        }
        block.setPermissiveWorking(permissive);
    }
    Element deniedBlocks = element.getChild("deniedBlocks");
    if (deniedBlocks != null) {
        List<Element> denyBlock = deniedBlocks.getChildren("block");
        for (Element deny : denyBlock) {
            block.addBlockDenyList(deny.getText());
        }
    }
    // load common parts
    loadCommon(block, element);
    // load sensor if present
    List<Element> sensors = element.getChildren("sensor");
    if (sensors.size() > 1) {
        log.error("More than one sensor present: " + sensors.size());
    }
    if (sensors.size() == 1) {
        //Old method of saving sensors
        if (sensors.get(0).getAttribute("systemName") != null) {
            String name = sensors.get(0).getAttribute("systemName").getValue();
            if (!name.equals("")) {
                block.setSensor(name);
            }
        }
    }
    if (element.getChild("occupancysensor") != null) {
        String name = element.getChild("occupancysensor").getText();
        if (!name.equals("")) {
            block.setSensor(name);
        }
    }
    // load Reporter if present
    List<Element> reporters = element.getChildren("reporter");
    if (reporters.size() > 1) {
        log.error("More than one reporter present: " + reporters.size());
    }
    if (reporters.size() == 1) {
        // Reporter
        String name = reporters.get(0).getAttribute("systemName").getValue();
        try {
            Reporter reporter = InstanceManager.getDefault(jmri.ReporterManager.class).provideReporter(name);
            block.setReporter(reporter);
            block.setReportingCurrent(reporters.get(0).getAttribute("useCurrent").getValue().equals("yes"));
        } catch (IllegalArgumentException ex) {
            log.warn("failed to create Reporter \"{}\" during Block load", name);
        }
    }
    // load paths if present
    List<Element> paths = element.getChildren("path");
    int startSize = block.getPaths().size();
    int loadCount = 0;
    for (int i = 0; i < paths.size(); i++) {
        Element path = paths.get(i);
        if (loadPath(block, path)) {
            loadCount++;
        }
    }
    if (startSize > 0 && loadCount > 0) {
        log.warn("Added " + loadCount++ + " paths to block " + sysName + " that already had " + startSize + " blocks.");
    }
    if (startSize + loadCount != block.getPaths().size()) {
        log.error("Started with " + startSize + " paths in block " + sysName + ", added " + loadCount + " but final count is " + block.getPaths().size() + "; something not right.");
    }
}
Also used : BlockManager(jmri.BlockManager) Element(org.jdom2.Element) Reporter(jmri.Reporter) Block(jmri.Block)

Example 45 with Block

use of jmri.Block in project JMRI by JMRI.

the class SectionTableAction method initializeEntryPoints.

private void initializeEntryPoints() {
    // Copy old Entry Point List, if there are entries, and clear it.
    ArrayList<EntryPoint> oldList = new ArrayList<EntryPoint>();
    for (int i = 0; i < entryPointList.size(); i++) {
        oldList.add(entryPointList.get(i));
    }
    entryPointList.clear();
    if (blockList.size() > 0) {
        // cycle through Blocks to find Entry Points
        for (int i = 0; i < blockList.size(); i++) {
            Block sb = blockList.get(i);
            List<Path> paths = sb.getPaths();
            for (int j = 0; j < paths.size(); j++) {
                Path p = paths.get(j);
                if (!inSection(p.getBlock())) {
                    // this is path to an outside block, so need an Entry Point
                    String pbDir = Path.decodeDirection(p.getFromBlockDirection());
                    EntryPoint ep = getEntryPointInList(oldList, sb, p.getBlock(), pbDir);
                    if (ep == null) {
                        ep = new EntryPoint(sb, p.getBlock(), pbDir);
                    }
                    entryPointList.add(ep);
                }
            }
        }
        // Set directions where possible
        ArrayList<EntryPoint> epList = getBlockEntryPointsList(beginBlock);
        if ((epList.size() == 2) && (blockList.size() == 1)) {
            if (((epList.get(0)).isUnknownType()) && ((epList.get(1)).isUnknownType())) {
                (epList.get(0)).setTypeForward();
                (epList.get(1)).setTypeReverse();
            }
        } else if (epList.size() == 1) {
            (epList.get(0)).setTypeForward();
        }
        epList = getBlockEntryPointsList(endBlock);
        if (epList.size() == 1) {
            (epList.get(0)).setTypeReverse();
        }
    }
    // djd debugging
    // here add code to use Layout Editor connectivity if desired in the future 
    /*  if (!manualEntryPoints) {
         // use Layout Editor connectivity to set directions of Entry Points that have UNKNOWN direction
         // check entry points for first Block
         ArrayList<EntryPoint> tEPList = getSubEPListForBlock(beginBlock);
         EntryPoint firstEP = null;
         int numUnknown = 0;
         for (int i = 0; i<tEPList.size(); i++) {
         if (tEPList.get(i).getDirection==EntryPoint.UNKNOWN) numUnknown ++;
         else if (firstEP==null) firstEP = tEPList.get(i);
         }
         if (numUnknown>0) {
         // first Block has unknown entry point(s)
         if ( (firstEP!=null) && (blockList.getSize()==1) ) {
         firstEP = tEPList.get(0);
         firstEP.setTypeForward();
         }
         else if (firstEP==null) {
         // find connection from the second Block
         }
         }   */
    entryPointTableModel.fireTableDataChanged();
}
Also used : Path(jmri.Path) ArrayList(java.util.ArrayList) EntryPoint(jmri.EntryPoint) Block(jmri.Block) EntryPoint(jmri.EntryPoint)

Aggregations

Block (jmri.Block)84 ArrayList (java.util.ArrayList)19 EntryPoint (jmri.EntryPoint)16 Sensor (jmri.Sensor)10 Element (org.jdom2.Element)9 BlockManager (jmri.BlockManager)8 SignalMast (jmri.SignalMast)8 Turnout (jmri.Turnout)8 Test (org.junit.Test)7 Path (jmri.Path)6 Reporter (jmri.Reporter)6 ActionEvent (java.awt.event.ActionEvent)5 ActionListener (java.awt.event.ActionListener)5 Section (jmri.Section)5 Hashtable (java.util.Hashtable)4 NamedBean (jmri.NamedBean)4 SignalHead (jmri.SignalHead)4 LayoutBlockManager (jmri.jmrit.display.layoutEditor.LayoutBlockManager)4 PropertyChangeEvent (java.beans.PropertyChangeEvent)3 PropertyChangeListener (java.beans.PropertyChangeListener)3