Search in sources :

Example 76 with Block

use of jmri.Block in project JMRI by JMRI.

the class LayoutBlock method addRouteToNeighbours.

void addRouteToNeighbours(Routes ro) {
    if (enableAddRouteLogging) {
        log.info("From " + this.getDisplayName() + " Add route to neighbour");
    }
    Block nextHop = ro.getNextBlock();
    ArrayList<LayoutBlock> validFromPath = new ArrayList<LayoutBlock>();
    if (enableAddRouteLogging) {
        log.info("From " + this.getDisplayName() + " new block " + nextHop.getDisplayName());
    }
    for (int i = 0; i < throughPaths.size(); i++) {
        LayoutBlock validBlock = null;
        if (enableAddRouteLogging) {
            log.info("Through routes index " + i);
            log.info("From " + this.getDisplayName() + " A through routes " + throughPaths.get(i).getSourceBlock().getDisplayName() + " " + throughPaths.get(i).getDestinationBlock().getDisplayName());
        }
        /*As the through paths include each possible path, ie 2 > 3 and 3 > 2
               as seperate entries then we only need to forward the new route to those
               source blocks that have a desination of the next hop*/
        if (throughPaths.get(i).getDestinationBlock() == nextHop) {
            if (getAdjacency(throughPaths.get(i).getSourceBlock()).isMutual()) {
                validBlock = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(throughPaths.get(i).getSourceBlock());
            }
        }
        //only need to add it the once.  Not sure if the contains is required.
        if ((validBlock != null) && (!validFromPath.contains(validBlock))) {
            validFromPath.add(validBlock);
        }
    }
    if (enableAddRouteLogging) {
        log.info("From " + this.getDisplayName() + " ===== valid from size path " + validFromPath.size() + " ==== (addroutetoneigh)");
        for (LayoutBlock valid : validFromPath) {
            log.info(valid.getDisplayName());
        }
        log.info("Next Hop " + nextHop.getDisplayName());
    }
    RoutingPacket update = new RoutingPacket(ADDITION, ro.getDestBlock(), ro.getHopCount() + 1, ro.getMetric() + metric, (ro.getLength() + getBlock().getLengthMm()), -1, getNextPacketID());
    for (int i = 0; i < validFromPath.size(); i++) {
        Adjacencies adj = getAdjacency(validFromPath.get(i).getBlock());
        if (adj.advertiseRouteToNeighbour(ro)) {
            //((getBestRouteByMetric(destBlock).getMetric())+block.getLengthMm())
            if (enableAddRouteLogging) {
                log.info("From " + this.getDisplayName() + " Sending update to " + validFromPath.get(i).getDisplayName() + " As this has a better hop count or metric");
            }
            adj.addRouteAdvertisedToNeighbour(ro);
            validFromPath.get(i).addRouteFromNeighbour(this, update);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Block(jmri.Block)

Example 77 with Block

use of jmri.Block in project JMRI by JMRI.

the class ConnectivityUtil method getConnectedBlocks.

/**
     * Returns a list of all Blocks connected to a specified Block
     */
public ArrayList<Block> getConnectedBlocks(Block block) {
    ArrayList<Block> list = new ArrayList<Block>();
    lb = layoutBlockManager.getByUserName(block.getUserName());
    ArrayList<LayoutConnectivity> cList = auxTools.getConnectivityList(lb);
    for (int i = 0; i < cList.size(); i++) {
        LayoutConnectivity lc = cList.get(i);
        if (lc.getBlock1().getBlock() == block) {
            list.add((lc.getBlock2()).getBlock());
        } else if (lc.getBlock2().getBlock() == block) {
            list.add((lc.getBlock1()).getBlock());
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) Block(jmri.Block) EntryPoint(jmri.EntryPoint)

Example 78 with Block

use of jmri.Block in project JMRI by JMRI.

the class ConnectivityUtil method getExitBlockForTrackNode.

/**
     * Returns an "exit block" for the specified track node if there is one,
     * else returns null. An "exit block" must be different from the block of
     * the track segment in the node. If the node is a PositionablePoint, it is
     * assumed to be a block boundary anchor point. If an "excludedBlock" is
     * entered, that block will not be returned as the exit block of a Node of
     * type TURNOUT_x.
     */
public Block getExitBlockForTrackNode(TrackNode node, Block excludedBlock) {
    if ((node == null) || node.reachedEndOfTrack()) {
        return null;
    }
    Block block = null;
    switch(node.getNodeType()) {
        case LayoutTrack.POS_POINT:
            PositionablePoint p = (PositionablePoint) node.getNode();
            block = p.getConnect1().getLayoutBlock().getBlock();
            if (block == node.getTrackSegment().getLayoutBlock().getBlock()) {
                block = p.getConnect2().getLayoutBlock().getBlock();
            }
            break;
        case LayoutTrack.TURNOUT_A:
            LayoutTurnout lt = (LayoutTurnout) node.getNode();
            Block tBlock = ((TrackSegment) lt.getConnectB()).getLayoutBlock().getBlock();
            if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                block = tBlock;
            } else if (lt.getTurnoutType() != LayoutTurnout.LH_XOVER) {
                tBlock = ((TrackSegment) lt.getConnectC()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.TURNOUT_B:
            lt = (LayoutTurnout) node.getNode();
            tBlock = ((TrackSegment) lt.getConnectA()).getLayoutBlock().getBlock();
            if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                block = tBlock;
            } else if ((lt.getTurnoutType() == LayoutTurnout.LH_XOVER) || (lt.getTurnoutType() == LayoutTurnout.DOUBLE_XOVER)) {
                tBlock = ((TrackSegment) lt.getConnectD()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.TURNOUT_C:
            lt = (LayoutTurnout) node.getNode();
            if (lt.getTurnoutType() != LayoutTurnout.LH_XOVER) {
                tBlock = ((TrackSegment) lt.getConnectA()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            if ((block == null) && ((lt.getTurnoutType() == LayoutTurnout.LH_XOVER) || (lt.getTurnoutType() == LayoutTurnout.DOUBLE_XOVER))) {
                tBlock = ((TrackSegment) lt.getConnectD()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.TURNOUT_D:
            lt = (LayoutTurnout) node.getNode();
            if ((lt.getTurnoutType() == LayoutTurnout.LH_XOVER) || (lt.getTurnoutType() == LayoutTurnout.DOUBLE_XOVER)) {
                tBlock = ((TrackSegment) lt.getConnectB()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.LEVEL_XING_A:
            LevelXing x = (LevelXing) node.getNode();
            tBlock = ((TrackSegment) x.getConnectC()).getLayoutBlock().getBlock();
            if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                block = tBlock;
            }
            break;
        case LayoutTrack.LEVEL_XING_B:
            x = (LevelXing) node.getNode();
            tBlock = ((TrackSegment) x.getConnectD()).getLayoutBlock().getBlock();
            if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                block = tBlock;
            }
            break;
        case LayoutTrack.LEVEL_XING_C:
            x = (LevelXing) node.getNode();
            tBlock = ((TrackSegment) x.getConnectA()).getLayoutBlock().getBlock();
            if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                block = tBlock;
            }
            break;
        case LayoutTrack.LEVEL_XING_D:
            x = (LevelXing) node.getNode();
            tBlock = ((TrackSegment) x.getConnectB()).getLayoutBlock().getBlock();
            if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                block = tBlock;
            }
            break;
        case LayoutTrack.SLIP_A:
            LayoutSlip ls = (LayoutSlip) node.getNode();
            tBlock = ((TrackSegment) ls.getConnectC()).getLayoutBlock().getBlock();
            if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                block = tBlock;
            } else {
                tBlock = ((TrackSegment) ls.getConnectD()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.SLIP_B:
            ls = (LayoutSlip) node.getNode();
            tBlock = ((TrackSegment) ls.getConnectD()).getLayoutBlock().getBlock();
            if (ls.getTurnoutType() == LayoutSlip.DOUBLE_SLIP) {
                //Double slip
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                } else {
                    tBlock = ((TrackSegment) ls.getConnectC()).getLayoutBlock().getBlock();
                    if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                        block = tBlock;
                    }
                }
            } else {
                if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.SLIP_C:
            ls = (LayoutSlip) node.getNode();
            tBlock = ((TrackSegment) ls.getConnectA()).getLayoutBlock().getBlock();
            if (ls.getTurnoutType() == LayoutSlip.DOUBLE_SLIP) {
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                } else {
                    tBlock = ((TrackSegment) ls.getConnectB()).getLayoutBlock().getBlock();
                    if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                        block = tBlock;
                    }
                }
            } else {
                if (tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) {
                    block = tBlock;
                }
            }
            break;
        case LayoutTrack.SLIP_D:
            ls = (LayoutSlip) node.getNode();
            tBlock = ((TrackSegment) ls.getConnectB()).getLayoutBlock().getBlock();
            if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                block = tBlock;
            } else {
                tBlock = ((TrackSegment) ls.getConnectA()).getLayoutBlock().getBlock();
                if ((tBlock != node.getTrackSegment().getLayoutBlock().getBlock()) && (tBlock != excludedBlock)) {
                    block = tBlock;
                }
            }
            break;
        default:
            break;
    }
    return block;
}
Also used : Block(jmri.Block)

Example 79 with Block

use of jmri.Block in project JMRI by JMRI.

the class BlockValueFile method writeBlockValues.

/*
     *  Writes out block values to a file in the user's preferences directory
     *  If there are no defined Blocks, no file is written.
     *  If none of the defined Blocks have values, no file is written.
     */
public void writeBlockValues() throws java.io.IOException {
    log.debug("entered writeBlockValues");
    List<String> blocks = blockManager.getSystemNameList();
    if (blocks.size() > 0) {
        // there are blocks defined, create root element
        root = new Element("block_values");
        doc = newDocument(root, dtdLocation + "block-values.dtd");
        boolean valuesFound = false;
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
        m.put("type", "text/xsl");
        m.put("href", xsltLocation + "blockValues.xsl");
        org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        // save block values in xml format
        Element values = new Element("blockvalues");
        for (int i = 0; i < blocks.size(); i++) {
            String sname = blocks.get(i);
            Block b = blockManager.getBySystemName(sname);
            if (b != null) {
                Object o = b.getValue();
                if (o != null) {
                    // block has value, save it
                    Element val = new Element("block");
                    val.setAttribute("systemname", sname);
                    if (o instanceof jmri.jmrit.roster.RosterEntry) {
                        val.setAttribute("value", ((jmri.jmrit.roster.RosterEntry) o).getId());
                        val.setAttribute("valueClass", "jmri.jmrit.roster.RosterEntry");
                    } else {
                        val.setAttribute("value", o.toString());
                    }
                    int v = b.getDirection();
                    if (v != jmri.Path.NONE) {
                        val.setAttribute("dir", "" + v);
                    }
                    values.addContent(val);
                    valuesFound = true;
                }
            } else {
                log.error("Block " + sname + " was not found.");
            }
        }
        root.addContent(values);
        // write out the file if values were found
        if (valuesFound) {
            try {
                if (!checkFile(defaultFileName)) {
                    // file does not exist, create it
                    File file = new File(defaultFileName);
                    if (// create and check result
                    !file.createNewFile()) {
                        log.error("createNewFile failed");
                    }
                }
                // write content to file
                writeXML(findFile(defaultFileName), doc);
            } catch (java.io.IOException ioe) {
                log.error("IO Exception " + ioe);
                throw (ioe);
            }
        }
    }
}
Also used : Element(org.jdom2.Element) Block(jmri.Block) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 80 with Block

use of jmri.Block in project JMRI by JMRI.

the class BlockTableAction method okPressed.

void okPressed(ActionEvent e) {
    int NumberOfBlocks = 1;
    if (range.isSelected()) {
        NumberOfBlocks = (Integer) numberToAdd.getValue();
    }
    if (NumberOfBlocks >= 65) {
        // limited by JSpinnerModel to 100
        if (JOptionPane.showConfirmDialog(addFrame, Bundle.getMessage("WarnExcessBeans", Bundle.getMessage("Blocks"), NumberOfBlocks), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION) == 1) {
            return;
        }
    }
    String user = userName.getText();
    if (user.equals("")) {
        user = null;
    }
    String sName = sysName.getText().toUpperCase();
    StringBuilder b;
    for (int x = 0; x < NumberOfBlocks; x++) {
        if (x != 0) {
            if (user != null) {
                b = new StringBuilder(userName.getText());
                b.append(":");
                b.append(Integer.toString(x));
                user = b.toString();
            }
            if (!_autoSystemName.isSelected()) {
                b = new StringBuilder(sysName.getText());
                b.append(":");
                b.append(Integer.toString(x));
                sName = b.toString();
            }
        }
        Block blk;
        try {
            if (_autoSystemName.isSelected()) {
                blk = InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(user);
            } else {
                blk = InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sName, user);
            }
        } catch (IllegalArgumentException ex) {
            // user input no good
            handleCreateException(sName);
            // without creating       
            return;
        }
        if (blk != null) {
            if (lengthField.getText().length() != 0) {
                blk.setLength(Integer.parseInt(lengthField.getText()));
            }
            /*if (blockSpeed.getText().length()!=0)
                 blk.setSpeedLimit(Integer.parseInt(blockSpeed.getText()));*/
            try {
                blk.setBlockSpeed((String) speeds.getSelectedItem());
            } catch (jmri.JmriException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage() + "\n" + (String) speeds.getSelectedItem());
            }
            if (checkPerm.isSelected()) {
                blk.setPermissiveWorking(true);
            }
            String cName = (String) cur.getSelectedItem();
            if (cName.equals(noneText)) {
                blk.setCurvature(Block.NONE);
            } else if (cName.equals(gradualText)) {
                blk.setCurvature(Block.GRADUAL);
            } else if (cName.equals(tightText)) {
                blk.setCurvature(Block.TIGHT);
            } else if (cName.equals(severeText)) {
                blk.setCurvature(Block.SEVERE);
            }
        }
    }
    pref.setSimplePreferenceState(systemNameAuto, _autoSystemName.isSelected());
// InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sName, user);
}
Also used : Block(jmri.Block)

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