Search in sources :

Example 6 with Block

use of jmri.Block in project JMRI by JMRI.

the class AutoTurnouts method turnoutUtil.

/**
     * Internal method implementing the above two methods Returns 'true' if
     * turnouts are set correctly, 'false' otherwise If 'set' is 'true' this
     * routine will attempt to set the turnouts, if 'false' it reports what it
     * finds.
     */
private boolean turnoutUtil(Section s, int seqNum, Section nextSection, ActiveTrain at, LayoutEditor le, boolean trustKnownTurnouts, boolean set, Section prevSection) {
    // validate input and initialize
    Transit tran = at.getTransit();
    if ((s == null) || (seqNum > tran.getMaxSequence()) || (!tran.containsSection(s)) || (le == null)) {
        log.error("Invalid argument when checking or setting turnouts in Section.");
        return false;
    }
    int direction = at.getAllocationDirectionFromSectionAndSeq(s, seqNum);
    if (direction == 0) {
        log.error("Invalid Section/sequence arguments when checking or setting turnouts");
        return false;
    }
    // check for no turnouts in this section
    if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALHEAD && (s.getForwardEntryPointList().size() <= 1) && (s.getReverseEntryPointList().size() <= 1)) {
        log.debug("No entry points lists");
        // no possibility of turnouts
        return true;
    }
    // initialize connectivity utilities and beginning block pointers
    ConnectivityUtil ct = le.getConnectivityUtil();
    EntryPoint entryPt = null;
    if (prevSection != null) {
        entryPt = s.getEntryPointFromSection(prevSection, direction);
    } else if (!s.containsBlock(at.getStartBlock())) {
        entryPt = s.getEntryPointFromBlock(at.getStartBlock(), direction);
    }
    EntryPoint exitPt = null;
    if (nextSection != null) {
        exitPt = s.getExitPointToSection(nextSection, direction);
    }
    // must be in the section
    Block curBlock = null;
    // must start outside the section or be null
    Block prevBlock = null;
    // sequence number of curBlock in Section
    int curBlockSeqNum = -1;
    if (entryPt != null) {
        curBlock = entryPt.getBlock();
        prevBlock = entryPt.getFromBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
    } else if (!at.isAllocationReversed() && s.containsBlock(at.getStartBlock())) {
        curBlock = at.getStartBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
        //Get the previous block so that we can set the turnouts in the current block correctly.
        if (direction == Section.FORWARD) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
        } else if (direction == Section.REVERSE) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
        }
    } else if (at.isAllocationReversed() && s.containsBlock(at.getEndBlock())) {
        curBlock = at.getEndBlock();
        curBlockSeqNum = s.getBlockSequenceNumber(curBlock);
        //Get the previous block so that we can set the turnouts in the current block correctly.
        if (direction == Section.REVERSE) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
        } else if (direction == Section.FORWARD) {
            prevBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
        }
    } else {
        if (_dispatcher.getSignalType() == DispatcherFrame.SIGNALMAST) {
            //This can be considered normal where SignalMast Logic is used.
            return true;
        }
        log.error("Error in turnout check/set request - initial Block and Section mismatch");
        return false;
    }
    Block nextBlock = null;
    // may be either in the section or the first block in the next section
    // sequence number of nextBlock in Section (-1 indicates outside Section)
    int nextBlockSeqNum = -1;
    if (exitPt != null && curBlock == exitPt.getBlock()) {
        // next Block is outside of the Section
        nextBlock = exitPt.getFromBlock();
    } else {
        // next Block is inside the Section
        if (direction == Section.FORWARD) {
            nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum + 1);
            nextBlockSeqNum = curBlockSeqNum + 1;
        } else if (direction == Section.REVERSE) {
            nextBlock = s.getBlockBySequenceNumber(curBlockSeqNum - 1);
            nextBlockSeqNum = curBlockSeqNum - 1;
        }
        if ((nextBlock == null) && (curBlock != at.getEndBlock())) {
            log.error("Error in block sequence numbers when setting/checking turnouts");
            return false;
        }
    }
    ArrayList<LayoutTurnout> turnoutList = new ArrayList<LayoutTurnout>();
    ArrayList<Integer> settingsList = new ArrayList<Integer>();
    // get turnouts by Block
    boolean turnoutsOK = true;
    while (curBlock != null) {
        /*No point in getting the list if the previous block is null as it will return empty and generate an error, 
             this will only happen on the first run.  Plus working on the basis that the turnouts in the current block would have already of 
             been set correctly for the train to have arrived in the first place.
             */
        if (prevBlock != null) {
            turnoutList = ct.getTurnoutList(curBlock, prevBlock, nextBlock);
            settingsList = ct.getTurnoutSettingList();
        }
        // loop over turnouts checking and optionally setting turnouts
        for (int i = 0; i < turnoutList.size(); i++) {
            Turnout to = turnoutList.get(i).getTurnout();
            int setting = settingsList.get(i).intValue();
            if (turnoutList.get(i) instanceof LayoutSlip) {
                setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutState(settingsList.get(i));
            }
            // check or ignore current setting based on flag, set in Options
            if (!trustKnownTurnouts) {
                log.debug("{}: setting turnout {} to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                to.setCommandedState(setting);
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                }
            //TODO: move this to separate thread
            } else {
                if (to.getKnownState() != setting) {
                    // turnout is not set correctly
                    if (set) {
                        // setting has been requested, is Section free and Block unoccupied
                        if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
                            // send setting command
                            log.debug("{}: turnout {} commanded to {}", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                            to.setCommandedState(setting);
                            try {
                                Thread.sleep(100);
                            } catch (Exception ex) {
                            }
                        //TODO: move this to separate thread
                        } else {
                            turnoutsOK = false;
                        }
                    } else {
                        turnoutsOK = false;
                    }
                } else {
                    log.debug("{}: turnout {} already {}, skipping", at.getTrainName(), to.getFullyFormattedDisplayName(), (setting == Turnout.CLOSED ? closedText : thrownText));
                }
            }
            if (turnoutList.get(i) instanceof LayoutSlip) {
                //Look at the state of the second turnout in the slip
                setting = ((LayoutSlip) turnoutList.get(i)).getTurnoutBState(settingsList.get(i));
                to = ((LayoutSlip) turnoutList.get(i)).getTurnoutB();
                if (!trustKnownTurnouts) {
                    to.setCommandedState(setting);
                } else if (to.getKnownState() != setting) {
                    // turnout is not set correctly
                    if (set) {
                        // setting has been requested, is Section free and Block unoccupied
                        if ((s.getState() == Section.FREE) && (curBlock.getState() != Block.OCCUPIED)) {
                            // send setting command
                            to.setCommandedState(setting);
                        } else {
                            turnoutsOK = false;
                        }
                    } else {
                        turnoutsOK = false;
                    }
                }
            }
        }
        if (turnoutsOK) {
            // move to next Block if any
            if (nextBlockSeqNum >= 0) {
                prevBlock = curBlock;
                curBlock = nextBlock;
                curBlockSeqNum = nextBlockSeqNum;
                if ((exitPt != null) && (curBlock == exitPt.getBlock())) {
                    // next block is outside of the Section
                    nextBlock = exitPt.getFromBlock();
                    nextBlockSeqNum = -1;
                } else {
                    if (direction == Section.FORWARD) {
                        nextBlockSeqNum++;
                    } else {
                        nextBlockSeqNum--;
                    }
                    nextBlock = s.getBlockBySequenceNumber(nextBlockSeqNum);
                    if (nextBlock == null) {
                        // there is no next Block
                        nextBlockSeqNum = -1;
                    }
                }
            } else {
                curBlock = null;
            }
        } else {
            curBlock = null;
        }
    }
    return turnoutsOK;
}
Also used : LayoutSlip(jmri.jmrit.display.layoutEditor.LayoutSlip) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) ArrayList(java.util.ArrayList) EntryPoint(jmri.EntryPoint) Transit(jmri.Transit) EntryPoint(jmri.EntryPoint) Block(jmri.Block) LayoutTurnout(jmri.jmrit.display.layoutEditor.LayoutTurnout) Turnout(jmri.Turnout) ConnectivityUtil(jmri.jmrit.display.layoutEditor.ConnectivityUtil)

Example 7 with Block

use of jmri.Block in project JMRI by JMRI.

the class LayoutBlockConnectivityTools method getLayoutBlocks.

/**
     * The is used in conjunction with the layout block routing protocol, to
     * discover a clear path from a source layout block through to a destination
     * layout block. By specifying the sourceLayoutBlock and
     * protectingLayoutBlock or sourceLayoutBlock+1, a direction of travel can
     * then be termined, eg east to west, south to north etc.
     * <p>
     * @param sourceLayoutBlock      - The layout block that we are starting
     *                               from, can also be considered as the block
     *                               facing a signal.
     * @param destinationLayoutBlock - The layout block that we want to get to
     * @param protectingLayoutBlock  - The next layout block connected to the
     *                               source block, this can also be considered
     *                               as the block being protected by a signal
     * @param validateOnly           - When set false, the system will not use
     *                               layout blocks that are set as either
     *                               reserved(useExtraColor set) or occupied, if
     *                               it finds any then it will try to find an
     *                               alternative path When set true, no block
     *                               state checking is performed.
     * @param pathMethod             - Performs a check to see if any signal
     *                               heads/masts are in the path, if there are
     *                               then the system will try to find an
     *                               alternative path. If set to NONE, then no
     *                               checking is performed.
     * @return an ArrayList of all the layoutblocks in the path.
     * @throws jmri.JmriException if it can not find a valid path or the routing
     *                            has not been enabled.
     */
public ArrayList<LayoutBlock> getLayoutBlocks(LayoutBlock sourceLayoutBlock, LayoutBlock destinationLayoutBlock, LayoutBlock protectingLayoutBlock, boolean validateOnly, int pathMethod) throws jmri.JmriException {
    lastErrorMessage = "Unknown Error Occured";
    LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
    if (!lbm.isAdvancedRoutingEnabled()) {
        log.info("Advanced routing has not been enabled therefore we cannot use this function");
        throw new jmri.JmriException("Advanced routing has not been enabled therefore we cannot use this function");
    }
    int directionOfTravel = sourceLayoutBlock.getNeighbourDirection(protectingLayoutBlock);
    Block currentBlock = sourceLayoutBlock.getBlock();
    Block destBlock = destinationLayoutBlock.getBlock();
    log.debug("Destination Block {} {}", destinationLayoutBlock.getDisplayName(), destBlock);
    Block nextBlock = protectingLayoutBlock.getBlock();
    if (log.isDebugEnabled()) {
        log.debug("s:" + sourceLayoutBlock.getDisplayName() + " p:" + protectingLayoutBlock.getDisplayName() + " d:" + destinationLayoutBlock.getDisplayName());
    }
    ArrayList<BlocksTested> blocksInRoute = new ArrayList<BlocksTested>();
    blocksInRoute.add(new BlocksTested(sourceLayoutBlock));
    if (!validateOnly) {
        if (canLBlockBeUsed(protectingLayoutBlock)) {
            blocksInRoute.add(new BlocksTested(protectingLayoutBlock));
        } else {
            lastErrorMessage = "Block we are protecting is already occupied or reserved";
            log.debug(lastErrorMessage);
            throw new jmri.JmriException(lastErrorMessage);
        }
        if (!canLBlockBeUsed(destinationLayoutBlock)) {
            lastErrorMessage = "Destination Block is already occupied or reserved";
            log.debug(lastErrorMessage);
            throw new jmri.JmriException(lastErrorMessage);
        }
    } else {
        blocksInRoute.add(new BlocksTested(protectingLayoutBlock));
    }
    if (destinationLayoutBlock == protectingLayoutBlock) {
        ArrayList<LayoutBlock> returnBlocks = new ArrayList<LayoutBlock>();
        for (int i = 0; i < blocksInRoute.size(); i++) {
            returnBlocks.add(blocksInRoute.get(i).getBlock());
        }
        return returnBlocks;
    }
    LayoutBlock currentLBlock = protectingLayoutBlock;
    BlocksTested bt = blocksInRoute.get(blocksInRoute.size() - 1);
    int ttl = 1;
    List<Integer> offSet = new ArrayList<Integer>();
    while (ttl < ttlSize) {
        //value should be higher but low for test!
        log.debug("===== Ttl value = {} ======", ttl);
        log.debug("Looking for next block");
        int nextBlockIndex = findBestHop(currentBlock, nextBlock, destBlock, directionOfTravel, offSet, validateOnly, pathMethod);
        if (nextBlockIndex != -1) {
            bt.addIndex(nextBlockIndex);
            if (log.isDebugEnabled()) {
                log.debug("block index returned " + nextBlockIndex + " Blocks in route size " + blocksInRoute.size());
            }
            //Sets the old next block to be our current block.
            currentLBlock = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(nextBlock);
            offSet = new ArrayList<Integer>();
            directionOfTravel = currentLBlock.getRouteDirectionAtIndex(nextBlockIndex);
            currentBlock = nextBlock;
            nextBlock = currentLBlock.getRouteNextBlockAtIndex(nextBlockIndex);
            LayoutBlock nextLBlock = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(nextBlock);
            if (log.isDebugEnabled()) {
                log.debug("Blocks in route size {}", blocksInRoute.size());
                log.debug("{} {}", nextBlock.getDisplayName(), destBlock.getDisplayName());
            }
            if (nextBlock == currentBlock) {
                nextBlock = currentLBlock.getRouteDestBlockAtIndex(nextBlockIndex);
                log.debug("the next block to our destination we are looking for is directly connected to this one");
            } else if (protectingLayoutBlock != nextLBlock) {
                log.debug("Add block {}", nextLBlock.getDisplayName());
                bt = new BlocksTested(nextLBlock);
                blocksInRoute.add(bt);
            }
            if (nextBlock == destBlock) {
                if (!validateOnly && !checkForLevelCrossing(destinationLayoutBlock)) {
                    throw new jmri.JmriException("Destination block is in conflict on a crossover");
                }
                ArrayList<LayoutBlock> returnBlocks = new ArrayList<LayoutBlock>();
                for (int i = 0; i < blocksInRoute.size(); i++) {
                    returnBlocks.add(blocksInRoute.get(i).getBlock());
                }
                returnBlocks.add(destinationLayoutBlock);
                if (log.isDebugEnabled()) {
                    log.debug("Adding destination Block {}", destinationLayoutBlock.getDisplayName());
                    log.debug("arrived at destination block");
                    log.debug("{} Return as Long", sourceLayoutBlock.getDisplayName());
                    for (int i = 0; i < returnBlocks.size(); i++) {
                        log.debug(returnBlocks.get(i).getDisplayName());
                    }
                    log.debug("Finished List");
                }
                return returnBlocks;
            }
        } else {
            //-1 is returned when there are no more valid besthop valids found
            //Block index is -1, so we need to go back a block and find another way.
            //So we have gone back as far as our starting block so we better return.
            int birSize = blocksInRoute.size();
            log.debug("block in route size {}", birSize);
            if (birSize <= 2) {
                log.debug("drop out here with ttl");
                ttl = ttlSize + 1;
            } else {
                if (log.isDebugEnabled()) {
                    for (int t = 0; t < blocksInRoute.size(); t++) {
                        log.debug("index {} block {}", t, blocksInRoute.get(t).getBlock().getDisplayName());
                    }
                    log.debug("To remove last block {}", blocksInRoute.get(birSize - 1).getBlock().getDisplayName());
                }
                currentBlock = blocksInRoute.get(birSize - 3).getBlock().getBlock();
                nextBlock = blocksInRoute.get(birSize - 2).getBlock().getBlock();
                offSet = blocksInRoute.get(birSize - 2).getTestedIndexes();
                bt = blocksInRoute.get(birSize - 2);
                blocksInRoute.remove(birSize - 1);
                ttl--;
            }
        }
        ttl++;
    }
    if (ttl == ttlSize) {
        lastErrorMessage = "ttlExpired";
    }
    //we exited the loop without either finding the destination or we had error.
    throw new jmri.JmriException(lastErrorMessage);
}
Also used : ArrayList(java.util.ArrayList) JmriException(jmri.JmriException) Block(jmri.Block)

Example 8 with Block

use of jmri.Block in project JMRI by JMRI.

the class LayoutBlockConnectivityTools method generateBlocksWithBeans.

ArrayList<FacingProtecting> generateBlocksWithBeans(LayoutEditor editor, Class<?> T) {
    LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
    ArrayList<FacingProtecting> beanList = new ArrayList<FacingProtecting>();
    List<String> lblksSysName = lbm.getSystemNameList();
    for (int i = 0; i < lblksSysName.size(); i++) {
        LayoutBlock curLblk = lbm.getLayoutBlock(lblksSysName.get(i));
        Block curBlk = curLblk.getBlock();
        LayoutEditor useEdit = editor;
        if (editor == null) {
            useEdit = curLblk.getMaxConnectedPanel();
        }
        if (curBlk != null) {
            int noNeigh = curLblk.getNumberOfNeighbours();
            for (int x = 0; x < noNeigh; x++) {
                Block blk = curLblk.getNeighbourAtIndex(x);
                ArrayList<Block> proBlk = new ArrayList<Block>();
                NamedBean bean = null;
                if (T == null) {
                    proBlk.add(blk);
                    bean = lbm.getFacingNamedBean(curBlk, blk, useEdit);
                } else if (T.equals(SignalMast.class)) {
                    bean = lbm.getFacingSignalMast(curBlk, blk, useEdit);
                    if (bean != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("Get list of protecting blocks for " + bean.getDisplayName() + " facing " + curBlk.getDisplayName());
                        }
                        List<LayoutBlock> lProBlk = lbm.getProtectingBlocksByNamedBean(bean, useEdit);
                        for (LayoutBlock lb : lProBlk) {
                            if (lb != null) {
                                proBlk.add(lb.getBlock());
                            }
                        }
                    }
                } else if (T.equals(Sensor.class)) {
                    bean = lbm.getFacingSensor(curBlk, blk, useEdit);
                    if (bean != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("Get list of protecting blocks for " + bean.getDisplayName());
                        }
                        List<LayoutBlock> lProBlk = lbm.getProtectingBlocksByNamedBean(bean, useEdit);
                        for (LayoutBlock lb : lProBlk) {
                            if (lb != null) {
                                proBlk.add(lb.getBlock());
                            }
                        }
                    }
                } else {
                    log.error("Past bean type is unknown " + T);
                }
                if (bean != null) {
                    FacingProtecting toadd = new FacingProtecting(curBlk, proBlk, bean);
                    boolean found = false;
                    for (FacingProtecting fp : beanList) {
                        if (fp.equals(toadd)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        beanList.add(toadd);
                    }
                }
            }
            if (noNeigh == 1) {
                NamedBean bean = null;
                if (log.isDebugEnabled()) {
                    log.debug("We have a dead end " + curBlk.getDisplayName());
                }
                if (T == null) {
                    bean = lbm.getNamedBeanAtEndBumper(curBlk, useEdit);
                } else if (T.equals(SignalMast.class)) {
                    bean = lbm.getSignalMastAtEndBumper(curBlk, useEdit);
                } else if (T.equals(Sensor.class)) {
                    bean = lbm.getSensorAtEndBumper(curBlk, useEdit);
                } else {
                    log.error("Past bean type is unknown " + T);
                }
                if (bean != null) {
                    FacingProtecting toadd = new FacingProtecting(curBlk, null, bean);
                    boolean found = false;
                    for (FacingProtecting fp : beanList) {
                        if (fp.equals(toadd)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        beanList.add(toadd);
                    }
                }
            }
        }
    }
    return beanList;
}
Also used : NamedBean(jmri.NamedBean) ArrayList(java.util.ArrayList) Block(jmri.Block) SignalMast(jmri.SignalMast) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with Block

use of jmri.Block in project JMRI by JMRI.

the class LayoutBlock method removeRouteFromNeighbour.

//This is used when a property event change is triggered for a removed route.
//Not sure that bulk removals will be necessary
void removeRouteFromNeighbour(LayoutBlock src, RoutingPacket update) {
    InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).setLastRoutingChange();
    Block srcblk = src.getBlock();
    Block destblk = update.getBlock();
    String msgPrefix = "From " + this.getDisplayName() + " notify block " + srcblk.getDisplayName() + " ";
    if (enableDeleteRouteLogging) {
        log.info(msgPrefix + " remove route from neighbour called");
    }
    if (InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(srcblk) == this) {
        if (enableDeleteRouteLogging) {
            log.info("From " + this.getDisplayName() + " source block is the same as our block! " + destblk.getDisplayName());
        }
        return;
    }
    if (enableDeleteRouteLogging) {
        log.info(msgPrefix + " (Direct Notification) neighbour " + srcblk.getDisplayName() + " has removed route to " + destblk.getDisplayName());
        log.info(msgPrefix + " routes in table " + routes.size() + " Remove route from neighbour");
    }
    ArrayList<Routes> routesToRemove = new ArrayList<Routes>();
    for (int i = routes.size() - 1; i > -1; i--) {
        Routes ro = routes.get(i);
        if ((ro.getNextBlock() == srcblk) && (ro.getDestBlock() == destblk)) {
            routesToRemove.add(new Routes(routes.get(i).getDestBlock(), routes.get(i).getNextBlock(), 0, 0, 0, 0));
            if (enableDeleteRouteLogging) {
                log.info(msgPrefix + " route to " + ro.getDestBlock().getDisplayName() + " from block " + ro.getNextBlock().getDisplayName() + " to be removed triggered by propertyChange");
            }
            routes.remove(i);
        //We only fire off routing update the once
        }
    }
    notifyNeighboursOfRemoval(routesToRemove, srcblk);
}
Also used : ArrayList(java.util.ArrayList) Block(jmri.Block)

Example 10 with Block

use of jmri.Block in project JMRI by JMRI.

the class LayoutBlockConnectivityTools method findBestHop.

//We need to take into account if the returned block has a signalmast attached.
int findBestHop(final Block preBlock, final Block currentBlock, Block destBlock, int direction, List<Integer> offSet, boolean validateOnly, int pathMethod) {
    int blockindex = 0;
    Block block;
    LayoutBlock currentLBlock = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(currentBlock);
    ArrayList<Integer> blkIndexTested = new ArrayList<Integer>(5);
    if (log.isDebugEnabled()) {
        log.debug("In find best hop current " + currentLBlock.getDisplayName() + " previous " + preBlock.getDisplayName());
    }
    while (blockindex != -1) {
        if (currentBlock == preBlock) {
            //Basically looking for the connected block, which there should only be one of!
            log.debug("At get ConnectedBlockRoute");
            blockindex = currentLBlock.getConnectedBlockRouteIndex(destBlock, direction);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Off Set " + offSet);
            }
            blockindex = currentLBlock.getNextBestBlock(preBlock, destBlock, offSet, METRIC);
        }
        if (blockindex != -1) {
            block = currentLBlock.getRouteNextBlockAtIndex(blockindex);
            LayoutBlock lBlock = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(block);
            Block blocktoCheck = block;
            if (block == currentBlock) {
                log.debug("current block matches returned block therefore the next block is directly connected");
                blocktoCheck = destBlock;
            }
            if ((block == currentBlock) && (currentLBlock.getThroughPathIndex(preBlock, destBlock) == -1)) {
                lastErrorMessage = "block " + block.getDisplayName() + " is directly attached, however the route to the destination block " + destBlock.getDisplayName() + " can not be directly used";
                log.debug(lastErrorMessage);
            } else if ((validateOnly) || ((checkForDoubleCrossOver(preBlock, currentLBlock, blocktoCheck) && checkForLevelCrossing(currentLBlock)) && canLBlockBeUsed(lBlock))) {
                if (log.isDebugEnabled()) {
                    log.debug(block.getDisplayName() + " not occupied & not reserved but we need to check if the anchor point between the two contains a signal or not");
                    log.debug(currentBlock.getDisplayName() + " " + block.getDisplayName());
                }
                jmri.NamedBean foundBean = null;
                /* We change the logging level to fatal in the layout block manager as we are testing to make sure that no signalhead/mast exists
                     this would generate an error message that is expected.*/
                MDC.put("loggingDisabled", LayoutBlockManager.class.getName());
                switch(pathMethod) {
                    case MASTTOMAST:
                        foundBean = InstanceManager.getDefault(LayoutBlockManager.class).getFacingSignalMast(currentBlock, blocktoCheck);
                        break;
                    case HEADTOHEAD:
                        foundBean = InstanceManager.getDefault(LayoutBlockManager.class).getFacingSignalHead(currentBlock, blocktoCheck);
                        break;
                    case SENSORTOSENSOR:
                        foundBean = InstanceManager.getDefault(LayoutBlockManager.class).getFacingSensor(currentBlock, blocktoCheck, null);
                        break;
                    case NONE:
                        break;
                    default:
                        foundBean = InstanceManager.getDefault(LayoutBlockManager.class).getFacingNamedBean(currentBlock, blocktoCheck, null);
                        break;
                }
                MDC.remove("loggingDisabled");
                if (foundBean == null) {
                    log.debug("No object found so okay to return");
                    return blockindex;
                } else {
                    lastErrorMessage = "Signal " + foundBean.getDisplayName() + " already exists between blocks " + currentBlock.getDisplayName() + " and " + blocktoCheck.getDisplayName() + " in the same direction on this path";
                    log.debug(lastErrorMessage);
                }
            } else {
                lastErrorMessage = "block " + block.getDisplayName() + " found not to be not usable";
                log.debug(lastErrorMessage);
            }
            if (blkIndexTested.contains(blockindex)) {
                lastErrorMessage = ("No valid free path found");
                return -1;
            }
            blkIndexTested.add(blockindex);
            offSet.add(blockindex);
        } else {
            log.debug("At this point the getNextBextBlock() has returned a -1");
        }
    }
    return -1;
}
Also used : NamedBean(jmri.NamedBean) ArrayList(java.util.ArrayList) 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