use of jmri.TransitSection in project JMRI by JMRI.
the class TransitManagerXml method loadTransits.
/**
* Utility method to load the individual Transit objects. If there's no
* additional info needed for a specific Transit type, invoke this with the
* parent of the set of Transit elements.
*
* @param sharedTransits Element containing the Transit elements to load.
* @param perNodeTransits Per-node Element containing the Transit elements
* to load.
*/
@SuppressWarnings("null")
public void loadTransits(Element sharedTransits, Element perNodeTransits) {
List<Element> transitList = sharedTransits.getChildren("transit");
if (log.isDebugEnabled()) {
log.debug("Found " + transitList.size() + " transits");
}
TransitManager tm = InstanceManager.getDefault(jmri.TransitManager.class);
for (int i = 0; i < transitList.size(); i++) {
String sysName = getSystemName(transitList.get(i));
String userName = getUserName(transitList.get(i));
Transit x = tm.createNewTransit(sysName, userName);
if (x != null) {
// load common part
loadCommon(x, transitList.get(i));
// load transitsection children
List<Element> transitTransitSectionList = transitList.get(i).getChildren("transitsection");
for (int n = 0; n < transitTransitSectionList.size(); n++) {
Element elem = transitTransitSectionList.get(n);
int seq = 0;
int dir = Section.UNKNOWN;
boolean alt = false;
String sectionName = elem.getAttribute("sectionname").getValue();
if (sectionName.equals("null")) {
log.warn("When loading configuration - missing Section in Transit " + sysName);
}
try {
seq = elem.getAttribute("sequence").getIntValue();
dir = elem.getAttribute("direction").getIntValue();
} catch (Exception e) {
log.error("Data Conversion Exception when loading direction of entry point - " + e);
}
if (elem.getAttribute("alternate").getValue().equals("yes")) {
alt = true;
}
TransitSection ts = new TransitSection(sectionName, seq, dir, alt);
x.addTransitSection(ts);
// load transitsectionaction children, if any
List<Element> transitTransitSectionActionList = transitTransitSectionList.get(n).getChildren("transitsectionaction");
for (int m = 0; m < transitTransitSectionActionList.size(); m++) {
Element elemx = transitTransitSectionActionList.get(m);
int tWhen = 1;
int tWhat = 1;
int tWhenData = 0;
String tWhenString = elemx.getAttribute("whenstring").getValue();
int tWhatData1 = 0;
int tWhatData2 = 0;
String tWhatString = elemx.getAttribute("whatstring").getValue();
try {
tWhen = elemx.getAttribute("whencode").getIntValue();
tWhat = elemx.getAttribute("whatcode").getIntValue();
tWhenData = elemx.getAttribute("whendata").getIntValue();
tWhatData1 = elemx.getAttribute("whatdata1").getIntValue();
tWhatData2 = elemx.getAttribute("whatdata2").getIntValue();
} catch (Exception e) {
log.error("Data Conversion Exception when loading transit section action - " + e);
}
TransitSectionAction tsa = new TransitSectionAction(tWhen, tWhat, tWhenData, tWhatData1, tWhatData2, tWhenString, tWhatString);
ts.addAction(tsa);
}
}
}
}
}
use of jmri.TransitSection in project JMRI by JMRI.
the class TransitManagerXml method store.
/**
* Default implementation for storing the contents of a TransitManager
*
* @param o Object to store, of type TransitManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element transits = new Element("transits");
setStoreElementClass(transits);
TransitManager tm = (TransitManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not Transits to include
if (!iter.hasNext()) {
return null;
}
// store the Transit
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
} else {
log.debug("Transit system name is " + sname);
Transit x = tm.getBySystemName(sname);
Element elem = new Element("transit");
elem.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(x, elem);
// save child transitsection entries
ArrayList<TransitSection> tsList = x.getTransitSectionList();
Element tsElem = null;
for (int k = 0; k < tsList.size(); k++) {
TransitSection ts = tsList.get(k);
if (ts != null && !ts.isTemporary()) {
tsElem = new Element("transitsection");
Section tSection = ts.getSection();
if (tSection != null) {
tsElem.setAttribute("sectionname", tSection.getSystemName());
} else {
tsElem.setAttribute("sectionname", "null");
}
tsElem.setAttribute("sequence", Integer.toString(ts.getSequenceNumber()));
tsElem.setAttribute("direction", Integer.toString(ts.getDirection()));
tsElem.setAttribute("alternate", "" + (ts.isAlternate() ? "yes" : "no"));
// save child transitsectionaction entries if any
ArrayList<TransitSectionAction> tsaList = ts.getTransitSectionActionList();
if (tsaList.size() > 0) {
Element tsaElem = null;
for (int m = 0; m < tsaList.size(); m++) {
TransitSectionAction tsa = tsaList.get(m);
if (tsa != null) {
tsaElem = new Element("transitsectionaction");
tsaElem.setAttribute("whencode", Integer.toString(tsa.getWhenCode()));
tsaElem.setAttribute("whatcode", Integer.toString(tsa.getWhatCode()));
tsaElem.setAttribute("whendata", Integer.toString(tsa.getDataWhen()));
tsaElem.setAttribute("whenstring", tsa.getStringWhen());
tsaElem.setAttribute("whatdata1", Integer.toString(tsa.getDataWhat1()));
tsaElem.setAttribute("whatdata2", Integer.toString(tsa.getDataWhat2()));
tsaElem.setAttribute("whatstring", tsa.getStringWhat());
tsElem.addContent(tsaElem);
}
}
}
elem.addContent(tsElem);
}
}
transits.addContent(elem);
}
}
}
return (transits);
}
use of jmri.TransitSection in project JMRI by JMRI.
the class AutoAllocate method areTrainsAdjacent.
private boolean areTrainsAdjacent(ActiveTrain at, ActiveTrain nt) {
// returns 'false' if a different ActiveTrain has allocated track between the
// two trains, returns 'true' otherwise
ArrayList<AllocatedSection> allocatedSections = _dispatcher.getAllocatedSectionsList();
ArrayList<TransitSection> atsList = at.getTransit().getTransitSectionList();
int aSeq = getCurrentSequenceNumber(at);
Section nSec = getCurSection();
if (willTraverse(nSec, at, aSeq) != 0) {
// at is moving toward nt
if (!at.isTransitReversed()) {
for (int i = 0; i < atsList.size(); i++) {
if (atsList.get(i).getSequenceNumber() > aSeq) {
Section tSec = atsList.get(i).getSection();
if (tSec == nSec) {
// reached second train position, no train in between
return true;
} else {
for (int j = 0; j < allocatedSections.size(); j++) {
if (allocatedSections.get(j).getSection() == tSec) {
if ((allocatedSections.get(j).getActiveTrain() != at) && (allocatedSections.get(j).getActiveTrain() != nt)) {
// allocated to a third train, trains not adjacent
return false;
}
}
}
}
}
}
} else {
for (int i = atsList.size() - 1; i <= 0; i--) {
if (atsList.get(i).getSequenceNumber() < aSeq) {
Section tSec = atsList.get(i).getSection();
if (tSec == nSec) {
// reached second train position, no train in between
return true;
} else {
for (int j = 0; j < allocatedSections.size(); j++) {
if (allocatedSections.get(j).getSection() == tSec) {
if ((allocatedSections.get(j).getActiveTrain() != at) && (allocatedSections.get(j).getActiveTrain() != nt)) {
// allocated to a third train, trains not adjacent
return false;
}
}
}
}
}
}
}
} else {
// at is moving away from nt, so backtrack
if (at.isTransitReversed()) {
for (int i = 0; i < atsList.size(); i++) {
if (atsList.get(i).getSequenceNumber() > aSeq) {
Section tSec = atsList.get(i).getSection();
if (tSec == nSec) {
// reached second train position, no train in between
return true;
} else {
for (int j = 0; j < allocatedSections.size(); j++) {
if (allocatedSections.get(j).getSection() == tSec) {
if ((allocatedSections.get(j).getActiveTrain() != at) && (allocatedSections.get(j).getActiveTrain() != nt)) {
// allocated to a third train, trains not adjacent
return false;
}
}
}
}
}
}
} else {
for (int i = atsList.size() - 1; i <= 0; i--) {
if (atsList.get(i).getSequenceNumber() < aSeq) {
Section tSec = atsList.get(i).getSection();
if (tSec == nSec) {
// reached second train position, no train in between
return true;
} else {
for (int j = 0; j < allocatedSections.size(); j++) {
if (allocatedSections.get(j).getSection() == tSec) {
if ((allocatedSections.get(j).getActiveTrain() != at) && (allocatedSections.get(j).getActiveTrain() != nt)) {
// allocated to a third train, trains not adjacent
return false;
}
}
}
}
}
}
}
}
return false;
}
use of jmri.TransitSection in project JMRI by JMRI.
the class AutoAllocate method getCurrentSequenceNumber.
private int getCurrentSequenceNumber(ActiveTrain at) {
// finds the current position of the head of the ActiveTrain in its Transit
// returns sequence number of current position. getCurSection() returns Section.
int seq = 0;
curSection = null;
if (at == null) {
log.error("null argument on entry to 'getCurrentSeqNumber'");
return seq;
}
Section temSection = null;
ArrayList<TransitSection> tsList = at.getTransit().getTransitSectionList();
if (!at.isTransitReversed()) {
// find the highest numbered occupied section
for (int i = 0; i < tsList.size(); i++) {
if ((tsList.get(i).getSection().getOccupancy() == Section.OCCUPIED) && isSectionAllocatedToTrain(tsList.get(i).getSection(), tsList.get(i).getSequenceNumber(), at)) {
seq = tsList.get(i).getSequenceNumber();
temSection = tsList.get(i).getSection();
}
}
if (seq == at.getTransit().getMaxSequence()) {
if (at.getResetWhenDone()) {
// train may have passed the last Section during continuous running
boolean further = true;
for (int j = 0; (j < tsList.size()) && further; j++) {
if ((tsList.get(j).getSection().getOccupancy() == Section.OCCUPIED) && isSectionAllocatedToTrain(tsList.get(j).getSection(), tsList.get(j).getSequenceNumber(), at)) {
seq = tsList.get(j).getSequenceNumber();
temSection = tsList.get(j).getSection();
} else {
further = false;
}
}
}
}
} else {
// transit is running in reverse
for (int i = tsList.size() - 1; i >= 0; i--) {
if ((tsList.get(i).getSection().getOccupancy() == Section.OCCUPIED) && isSectionAllocatedToTrain(tsList.get(i).getSection(), tsList.get(i).getSequenceNumber(), at)) {
seq = tsList.get(i).getSequenceNumber();
temSection = tsList.get(i).getSection();
}
}
}
if (seq == 0) {
if (at.getMode() != ActiveTrain.MANUAL) {
log.error("{}: ActiveTrain has no occupied Section. Halting immediately to avoid runaway.", at.getTrainName());
at.getAutoActiveTrain().getAutoEngineer().setHalt(true);
} else {
log.debug("{}: ActiveTrain has no occupied Section, running in Manual mode.", at.getTrainName());
}
} else {
curSection = temSection;
}
return seq;
}
Aggregations