use of jmri.Block in project JMRI by JMRI.
the class AutoAllocate method willLevelXingsBlockTrain.
private boolean willLevelXingsBlockTrain(ActiveTrain at) {
// returns true if any LevelXings in _levelXingList will block the specified train
if (at == null) {
log.error("null argument on entry to 'willLevelXingsBlockTrain'");
// returns true to be safe
return true;
}
if (_levelXingList.size() <= 0) {
return false;
}
for (int i = 0; i < _levelXingList.size(); i++) {
LevelXing lx = _levelXingList.get(i);
Block bAC = lx.getLayoutBlockAC().getBlock();
Block bBD = lx.getLayoutBlockBD().getBlock();
if (at.getTransit().containsBlock(bAC) || at.getTransit().containsBlock(bBD)) {
return true;
}
}
return false;
}
use of jmri.Block in project JMRI by JMRI.
the class BlockContentsIconXml method load.
/**
* Load, starting with the BlockContentsIcon element, then all the
* value-icon pairs
*
* @param element Top level Element to unpack.
* @param o an Editor as an Object
*/
@Override
public void load(Element element, Object o) {
Editor ed = null;
BlockContentsIcon l;
if (o instanceof LayoutEditor) {
ed = (LayoutEditor) o;
l = new jmri.jmrit.display.layoutEditor.BlockContentsIcon(" ", (LayoutEditor) ed);
} else if (o instanceof jmri.jmrit.display.Editor) {
ed = (Editor) o;
l = new BlockContentsIcon("", ed);
} else {
log.error("Unrecognizable class - " + o.getClass().getName());
return;
}
String name;
Attribute attr = element.getAttribute("blockcontents");
if (attr == null) {
log.error("incorrect information for a block contents; must use block name");
ed.loadFailed();
return;
} else {
name = attr.getValue();
}
loadTextInfo(l, element);
Block m = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
if (m != null) {
l.setBlock(name);
} else {
log.error("Block named '" + attr.getValue() + "' not found.");
ed.loadFailed();
}
Attribute a = element.getAttribute("selectable");
if (a != null && a.getValue().equals("yes")) {
l.setSelectable(true);
} else {
l.setSelectable(false);
}
// get the icon pairs
List<Element> items = element.getChildren("blockstate");
for (int i = 0; i < items.size(); i++) {
// get the class, hence the adapter object to do loading
Element item = items.get(i);
String iconName = item.getAttribute("icon").getValue();
NamedIcon icon = NamedIcon.getIconByName(iconName);
if (icon == null) {
icon = ed.loadFailed("Memory " + name, iconName);
if (icon == null) {
log.info("Memory \"" + name + "\" icon removed for url= " + iconName);
}
}
if (icon != null) {
String keyValue = item.getAttribute("value").getValue();
l.addKeyAndIcon(icon, keyValue);
}
}
ed.putItem(l);
// load individual item's option settings after editor has set its global settings
loadCommonAttributes(l, Editor.MEMORIES, element);
int x = 0;
int y = 0;
try {
x = element.getAttribute("x").getIntValue();
y = element.getAttribute("y").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert positional attribute");
}
l.setOriginalLocation(x, y);
l.displayState();
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method addRouteFromNeighbour.
void addRouteFromNeighbour(LayoutBlock src, RoutingPacket update) {
if (enableAddRouteLogging) {
//log.info("From " + this.getDisplayName() + " packet to be added from neighbour " + src.getDisplayName());
log.info("From " + this.getDisplayName() + " src: " + src.getDisplayName() + ", block: " + update.getBlock().getDisplayName() + ", hopCount: " + update.getHopCount() + ", metric: " + update.getMetric() + ", status: " + update.getBlockState() + ", packetID: " + update.getPacketId());
}
InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).setLastRoutingChange();
Block destBlock = update.getBlock();
Block srcblk = src.getBlock();
if (destBlock == this.getBlock()) {
if (enableAddRouteLogging) {
log.info("Reject packet update as it is to a route advertised by our selves");
}
return;
}
Adjacencies adj = getAdjacency(srcblk);
if (adj == null) {
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " packet is from a src that is not registered " + srcblk.getDisplayName());
}
//Then we will simply reject it.
return;
} else if (adj.getPacketFlow() == TXONLY) {
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " packet is from a src " + src.getDisplayName() + " that is registered as one that we should be transmitting to only");
}
//we should only be transmitting routes to this neighbour not receiving them
return;
}
int hopCount = update.getHopCount();
int updatemetric = update.getMetric();
float length = update.getLength();
if (hopCount > 255) {
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " hop count exceeded " + destBlock.getDisplayName());
}
return;
}
for (Routes ro : routes) {
if ((ro.getNextBlock() == srcblk) && (ro.getDestBlock() == destBlock)) {
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " Route to " + destBlock.getDisplayName() + " is already configured");
log.info(ro.getHopCount() + " v " + hopCount);
log.info(ro.getMetric() + " v " + updatemetric);
}
updateRoutingInfo(src, update);
return;
}
}
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " We should be adding route " + destBlock.getDisplayName());
}
//We need to propergate out the routes that we have added to our neighbour
int direction = adj.getDirection();
Routes route = new Routes(destBlock, srcblk, hopCount, direction, updatemetric, length);
routes.add(route);
//Need to propergate the route down to our neighbours
addRouteToNeighbours(route);
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method getNextBlock.
/**
* Used if we already know the block prior to our block, and the destination
* block. direction, is optional and is used where the previousBlock is
* equal to our block.
*/
@CheckForNull
public Block getNextBlock(Block previousBlock, Block destBlock) {
int bestMetric = 965000;
Block bestBlock = null;
for (Routes r : routes) {
if (r.getDestBlock() == destBlock) {
//Check that the route through from the previous block, to the next hop is valid
if (validThroughPath(previousBlock, r.getNextBlock())) {
if (r.getMetric() < bestMetric) {
bestMetric = r.getMetric();
//bestBlock=r.getDestBlock();
bestBlock = r.getNextBlock();
}
}
}
}
return bestBlock;
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method addThroughPath.
void addThroughPath(Adjacencies adj) {
Block newAdj = adj.getBlock();
int packetFlow = adj.getPacketFlow();
if (enableAddRouteLogging) {
log.debug("From " + this.getDisplayName() + " addThroughPathCalled with adj " + adj.getBlock().getDisplayName());
}
for (int i = 0; i < neighbours.size(); i++) {
//cycle through all the neighbours
if (neighbours.get(i).getBlock() != newAdj) {
int neighPacketFlow = neighbours.get(i).getPacketFlow();
if (enableAddRouteLogging) {
log.info("From " + this.getDisplayName() + " our direction: " + decodePacketFlow(packetFlow) + ", neighbour direction: " + decodePacketFlow(neighPacketFlow));
}
if ((packetFlow == RXTX) && (neighPacketFlow == RXTX)) {
//if both are RXTX then add flow in both directions
addThroughPath(neighbours.get(i).getBlock(), newAdj);
addThroughPath(newAdj, neighbours.get(i).getBlock());
} else if ((packetFlow == RXONLY) && (neighPacketFlow == TXONLY)) {
addThroughPath(neighbours.get(i).getBlock(), newAdj);
} else if ((packetFlow == TXONLY) && (neighPacketFlow == RXONLY)) {
addThroughPath(newAdj, neighbours.get(i).getBlock());
} else if ((packetFlow == RXTX) && (neighPacketFlow == TXONLY)) {
//was RX
addThroughPath(neighbours.get(i).getBlock(), newAdj);
} else if ((packetFlow == RXTX) && (neighPacketFlow == RXONLY)) {
//was TX
addThroughPath(newAdj, neighbours.get(i).getBlock());
} else if ((packetFlow == RXONLY) && (neighPacketFlow == RXTX)) {
addThroughPath(neighbours.get(i).getBlock(), newAdj);
} else if ((packetFlow == TXONLY) && (neighPacketFlow == RXTX)) {
addThroughPath(newAdj, neighbours.get(i).getBlock());
} else {
if (enableAddRouteLogging) {
log.info("Invalid combination " + decodePacketFlow(packetFlow) + " and " + decodePacketFlow(neighPacketFlow));
}
}
}
}
}
Aggregations