use of jmri.Section 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.Section in project JMRI by JMRI.
the class AutoAllocate method checkForPassingPlan.
private boolean checkForPassingPlan(AllocationRequest ar, ActiveTrain nt, ArrayList<ActiveTrain> neededByTrainList) {
// returns 'true' if an AllocationPlan has been set up, returns 'false' otherwise
Section nSec = null;
Section aSec = null;
int nSecSeq = 0;
int aSecSeq = 0;
ActiveTrain at = ar.getActiveTrain();
AllocationPlan apx = getPlanThisTrain(nt);
if (apx != null) {
if (apx.getPlanType() != AllocationPlan.PASSING_MEET) {
return false;
}
// already in a PASSING_MEET Allocation Plan - find target Section and sequence
Section oSection = null;
// ActiveTrain oTrain = null;
if (apx.getActiveTrain(1) == nt) {
nSecSeq = apx.getTargetSectionSequenceNum(1);
nSec = apx.getTargetSection(1);
oSection = apx.getTargetSection(2);
// oTrain = apx.getActiveTrain(2);
} else {
nSecSeq = apx.getTargetSectionSequenceNum(2);
nSec = apx.getTargetSection(2);
oSection = apx.getTargetSection(1);
// oTrain = apx.getActiveTrain(1);
}
int aCurrentSeq = getCurrentSequenceNumber(at);
aSecSeq = willTraverse(nSec, at, aCurrentSeq);
if (aSecSeq == 0) {
return false;
}
// int tnSecSeq = nSecSeq;
// if (nt.getPriority() > oTrain.getPriority()) {
// if (!nt.isAllocationReversed()) {
// tnSecSeq--;
// if (tnSecSeq <= 0) {
// tnSecSeq = nSecSeq;
// }
// } else {
// tnSecSeq++;
// if (tnSecSeq > nt.getTransit().getMaxSequence()) {
// tnSecSeq = nSecSeq;
// }
// }
// }
ArrayList<Section> nSections = nt.getTransit().getSectionListBySeq(nSecSeq);
if (nSections.size() <= 1) {
return false;
}
// is a passing siding, find a suitable track
for (int i = 0; (i < nSections.size()) && (aSec == null); i++) {
if (nSections.get(i) == oSection) {
aSecSeq = willTraverse(nSections.get(i), at, aCurrentSeq);
if (aSecSeq > 0) {
aSec = nSections.get(i);
}
}
}
if (aSec == null) {
for (int i = 0; (i < nSections.size()) && (aSec == null); i++) {
if (nSections.get(i) != nSec) {
aSecSeq = willTraverse(nSections.get(i), at, aCurrentSeq);
if (aSecSeq > 0) {
aSec = nSections.get(i);
}
}
}
}
if (aSec == null) {
return false;
}
} else {
// both trains are not in Allocation plans
int aSeq = ar.getSectionSeqNumber();
// is an alternate Section available here or ahead
aSecSeq = findPassingSection(at, aSeq);
if (aSecSeq == 0) {
// does higher priority train have a passing section ahead
int nCurrentSeq = getCurrentSequenceNumber(nt);
nSecSeq = findPassingSection(nt, nCurrentSeq);
if (nSecSeq > 0) {
// has passing section ahead, will this train traverse a Section in it
ArrayList<Section> nSections = nt.getTransit().getSectionListBySeq(nSecSeq);
for (int i = 0; (i < nSections.size()) && (aSec == null); i++) {
aSecSeq = willTraverse(nSections.get(i), at, aSeq);
if (aSecSeq > 0) {
aSec = at.getTransit().getSectionListBySeq(aSecSeq).get(0);
}
}
if (aSec != null) {
// found passing Section that should work out
nSec = getBestOtherSection(nSections, aSec);
}
}
} else {
// will the higher priority train go through any of these alternate sections
ArrayList<Section> aSections = at.getTransit().getSectionListBySeq(aSecSeq);
int nCurrentSeq = getCurrentSequenceNumber(nt);
for (int i = 0; (i < aSections.size()) && (aSec == null); i++) {
nSecSeq = willTraverse(aSections.get(i), nt, nCurrentSeq);
if (nSecSeq > 0) {
nSec = aSections.get(i);
aSec = getBestOtherSection(aSections, nSec);
}
}
}
// if could not find a suitable passing siding, return
if ((aSec == null) || (nSec == null)) {
return false;
}
// push higher priority train one section further, if possible
if (!nt.isAllocationReversed()) {
if (nSecSeq < nt.getTransit().getMaxSequence()) {
nSecSeq++;
nSec = nt.getTransit().getSectionListBySeq(nSecSeq).get(0);
}
} else {
if (nSecSeq > 1) {
nSecSeq--;
nSec = nt.getTransit().getSectionListBySeq(nSecSeq).get(0);
}
}
}
// is there another train trying to let this high priority train pass
if (neededByTrainList.size() > 2) {
// is there another train between these two
if (!areTrainsAdjacent(at, nt)) {
return false;
}
if (isThereConflictingPlan(at, aSec, aSecSeq, nt, nSec, nSecSeq, AllocationPlan.PASSING_MEET)) {
return false;
}
}
// set up allocation plan
AllocationPlan ap = new AllocationPlan(this, nextPlanNum);
nextPlanNum++;
ap.setPlanType(AllocationPlan.PASSING_MEET);
ap.setActiveTrain(at, 1);
ap.setTargetSection(aSec, aSecSeq, 1);
ap.setActiveTrain(nt, 2);
ap.setTargetSection(nSec, nSecSeq, 2);
_planList.add(ap);
return true;
}
use of jmri.Section 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