use of jmri.jmrit.operations.locations.schedules.ScheduleItem in project JMRI by JMRI.
the class Track method scheduleNext.
/**
* Check to see if track has schedule and if it does will schedule the next
* item in the list. Load the car with the next schedule load if one exists,
* and set the car's final destination if there's one in the schedule.
* @param car The Car to be modified.
*
* @return Track.OKAY or Track.SCHEDULE
*/
public String scheduleNext(Car car) {
// clean up the car's final destination if sent to that destination and there isn't a schedule
if (getScheduleId().equals(NONE) && car.getDestination() != null && car.getDestination().equals(car.getFinalDestination()) && car.getDestinationTrack() != null && (car.getDestinationTrack().equals(car.getFinalDestinationTrack()) || car.getFinalDestinationTrack() == null)) {
car.setFinalDestination(null);
car.setFinalDestinationTrack(null);
}
// check for schedule, only spurs can have a schedule
if (getScheduleId().equals(NONE) || getSchedule() == null) {
return OKAY;
}
// is car part of a kernel?
if (car.getKernel() != null && !car.getKernel().isLead(car)) {
log.debug("Car ({}) is part of kernel ({}) not lead", car.toString(), car.getKernelName());
return OKAY;
}
if (!car.getScheduleItemId().equals(Car.NONE)) {
String id = car.getScheduleItemId();
log.debug("Car ({}) has schedule item id ({})", car.toString(), car.getScheduleItemId());
Schedule sch = getSchedule();
if (sch != null) {
ScheduleItem si = sch.getItemById(id);
car.setScheduleItemId(Car.NONE);
if (si != null) {
loadNext(si, car);
return OKAY;
}
log.debug("Schedule id ({}) not valid for track ({})", id, getName());
// user could have deleted the schedule item after build train, so not really an error
// return SCHEDULE + " ERROR id " + id + " not valid for track ("+ getName() + ")"; // NOI18N
}
}
if (getScheduleMode() == MATCH && !searchSchedule(car).equals(OKAY)) {
return SCHEDULE + MessageFormat.format(Bundle.getMessage("matchMessage"), new Object[] { getScheduleName() });
}
ScheduleItem currentSi = getCurrentScheduleItem();
log.debug("Destination track ({}) has schedule ({}) item id ({}) mode: {} ({})", getName(), getScheduleName(), getScheduleItemId(), getScheduleMode(), // NOI18N
getScheduleMode() == SEQUENTIAL ? "Sequential" : "Match");
if (currentSi != null && (currentSi.getSetoutTrainScheduleId().equals(ScheduleItem.NONE) || TrainManager.instance().getTrainScheduleActiveId().equals(currentSi.getSetoutTrainScheduleId())) && car.getTypeName().equals(currentSi.getTypeName()) && (currentSi.getRoadName().equals(ScheduleItem.NONE) || car.getRoadName().equals(currentSi.getRoadName())) && (currentSi.getReceiveLoadName().equals(ScheduleItem.NONE) || car.getLoadName().equals(currentSi.getReceiveLoadName()))) {
loadNext(currentSi, car);
car.setScheduleItemId(Car.NONE);
// bump schedule
bumpSchedule();
} else if (currentSi != null) {
// log.debug("Car (" + toString() + ") type (" + getType() + ") road (" + getRoad() + ") load ("
// + getLoad() + ") arrived out of sequence, needed type (" + currentSi.getType() // NOI18N
// + ") road (" + currentSi.getRoad() + ") load (" + currentSi.getLoad() + ")"); // NOI18N
// build return message
String timetableName = "";
String currentTimetableName = "";
TrainSchedule sch = TrainScheduleManager.instance().getScheduleById(TrainManager.instance().getTrainScheduleActiveId());
if (sch != null) {
timetableName = sch.getName();
}
sch = TrainScheduleManager.instance().getScheduleById(currentSi.getSetoutTrainScheduleId());
if (sch != null) {
currentTimetableName = sch.getName();
}
String mode = Bundle.getMessage("sequential");
if (getScheduleMode() == 1) {
mode = Bundle.getMessage("match");
}
return SCHEDULE + MessageFormat.format(Bundle.getMessage("sequentialMessage"), new Object[] { getScheduleName(), mode, car.toString(), car.getTypeName(), timetableName, car.getRoadName(), car.getLoadName(), currentSi.getTypeName(), currentTimetableName, currentSi.getRoadName(), currentSi.getReceiveLoadName() });
} else {
log.error("ERROR Track " + getName() + " current schedule item is null!");
// NOI18N
return SCHEDULE + " ERROR Track " + getName() + " current schedule item is null!";
}
return OKAY;
}
use of jmri.jmrit.operations.locations.schedules.ScheduleItem in project JMRI by JMRI.
the class Track method searchSchedule.
/*
* Match mode search
*/
private String searchSchedule(Car car) {
if (debugFlag) {
log.debug("Search match for car ({}) type ({}) load ({})", car.toString(), car.getTypeName(), car.getLoadName());
}
if (!car.getScheduleItemId().equals(NONE)) {
ScheduleItem si = getSchedule().getItemById(car.getScheduleItemId());
if (si != null && checkScheduleItem(si, car).equals(OKAY)) {
return OKAY;
}
}
for (int i = 0; i < getSchedule().getSize(); i++) {
ScheduleItem si = getNextScheduleItem();
if (debugFlag) {
log.debug("Item id: ({}) requesting type ({}) load ({}) final dest ({}, {})", si.getId(), si.getTypeName(), si.getReceiveLoadName(), si.getDestinationName(), // NOI18N
si.getDestinationTrackName());
}
String status = checkScheduleItem(si, car);
if (status.equals(OKAY)) {
log.debug("Found item match ({}) car ({}) load ({}) ship ({}) destination ({}, {})", si.getId(), car.toString(), si.getReceiveLoadName(), si.getShipLoadName(), si.getDestinationName(), si.getDestinationTrackName());
// remember which item was a match
car.setScheduleItemId(si.getId());
return OKAY;
} else {
if (debugFlag) {
log.debug("Item id: ({}) status ({})", si.getId(), status);
}
}
}
if (debugFlag) {
log.debug("No Match");
}
// clear the car's schedule id
car.setScheduleItemId(Car.NONE);
return SCHEDULE + " " + Bundle.getMessage("noMatch");
}
use of jmri.jmrit.operations.locations.schedules.ScheduleItem in project JMRI by JMRI.
the class TrainBuilder method generateCarLoadFromStaging.
/**
* Used to generate a car's load from staging. Search for a spur with a
* schedule and load car if possible.
*
* @param car the car
*/
private boolean generateCarLoadFromStaging(Car car) throws BuildFailedException {
if (car.getTrack() == null || !car.getTrack().getTrackType().equals(Track.STAGING) || (!car.getTrack().isAddCustomLoadsAnySpurEnabled() && !car.getTrack().isAddCustomLoadsEnabled()) || !car.getLoadName().equals(CarLoads.instance().getDefaultEmptyName()) || car.getDestination() != null || car.getFinalDestination() != null) {
log.debug(// NOI18N
"No load generation for car ({}) isAddLoadsAnySpurEnabled: " + // NOI18N
(car.getTrack().isAddCustomLoadsAnySpurEnabled() ? "true" : "false") + // NOI18N
", car load ({}) destination ({}) final destination ({})", car.toString(), car.getLoadName(), car.getDestinationName(), // NOI18N
car.getFinalDestinationName());
// if car has a destination or final destination add "no load generated" message to report
if (car.getTrack() != null && car.getTrack().getTrackType().equals(Track.STAGING) && car.getTrack().isAddCustomLoadsAnySpurEnabled() && car.getLoadName().equals(CarLoads.instance().getDefaultEmptyName())) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildCarNoLoadGenerated"), new Object[] { car.toString(), car.getLoadName(), car.getDestinationName(), car.getFinalDestinationName() }));
}
// no load generated for this car
return false;
}
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildSearchTrackNewLoad"), new Object[] { car.toString(), car.getTypeName(), car.getLoadName(), car.getLocationName(), car.getTrackName() }));
// check to see if car type has custom loads
if (CarLoads.instance().getNames(car.getTypeName()).size() == 2) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarNoCustomLoad"), new Object[] { car.toString(), car.getTypeName() }));
return false;
}
if (car.getKernel() != null) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarLeadKernel"), new Object[] { car.toString(), car.getKernelName(), car.getKernel().getSize(), car.getKernel().getTotalLength(), Setup.getLengthUnit().toLowerCase() }));
}
List<Track> tracks = locationManager.getTracksByMoves(Track.SPUR);
log.debug("Found {} spurs", tracks.size());
for (Track track : tracks) {
ScheduleItem si = getScheduleItem(car, track);
if (si == null) {
// no match
continue;
}
// only use tracks serviced by this train?
if (car.getTrack().isAddCustomLoadsEnabled() && _train.getRoute().getLastLocationByName(track.getLocation().getName()) == null) {
continue;
}
// need to set car load so testDestination will work properly
// should be the default empty
String oldCarLoad = car.getLoadName();
car.setLoadName(si.getReceiveLoadName());
String status = car.testDestination(track.getLocation(), track);
if (!status.equals(Track.OKAY) && !status.startsWith(Track.LENGTH)) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildNoDestTrackNewLoad"), new Object[] { track.getLocation().getName(), track.getName(), car.toString(), si.getReceiveLoadName(), status }));
// restore car's load
car.setLoadName(oldCarLoad);
continue;
}
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildTrySpurLoad"), new Object[] { track.getLocation().getName(), track.getName(), car.getLoadName() }));
if (!car.getTrack().acceptsDestination(track.getLocation())) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildDestinationNotServiced"), new Object[] { track.getLocation().getName(), car.getTrackName() }));
// restore car's load
car.setLoadName(oldCarLoad);
continue;
}
if (!track.isSpaceAvailable(car)) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildNoDestTrackSpace"), new Object[] { car.toString(), track.getLocation().getName(), track.getName(), track.getNumberOfCarsInRoute(), track.getReservedInRoute(), Setup.getLengthUnit().toLowerCase(), track.getReservationFactor() }));
// restore car's load
car.setLoadName(oldCarLoad);
continue;
}
car.setFinalDestination(track.getLocation());
car.setFinalDestinationTrack(track);
// try routing car
if (Router.instance().setDestination(car, _train, _buildReport) && car.getDestination() != null) {
// return car with this custom load and destination
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildCreateNewLoadForCar"), new Object[] { car.toString(), si.getReceiveLoadName(), track.getLocation().getName(), track.getName() }));
car.setLoadGeneratedFromStaging(true);
// is car part of kernel?
car.updateKernel();
track.bumpSchedule();
// done, car now has a custom load
return true;
}
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCanNotRouteCar"), new Object[] { car.toString(), si.getReceiveLoadName(), track.getLocation().getName(), track.getName() }));
car.setDestination(null, null);
// restore load and final destination and track
car.setLoadName(oldCarLoad);
car.setFinalDestination(null);
car.setFinalDestinationTrack(null);
}
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildUnableNewLoad"), new Object[] { car.toString() }));
// done, no load generated for this car
return false;
}
use of jmri.jmrit.operations.locations.schedules.ScheduleItem in project JMRI by JMRI.
the class TrainByCarTypeFrame method checkScheduleAttribute.
private boolean checkScheduleAttribute(String attribute, String carType, Car car, Track track) {
Schedule schedule = track.getSchedule();
if (schedule == null) {
return true;
}
// if car is already placed at track, don't check car type and load
if (car != null && car.getTrack() == track) {
return true;
}
List<ScheduleItem> scheduleItems = schedule.getItemsBySequenceList();
for (ScheduleItem si : scheduleItems) {
// check to see if schedule services car type
if (attribute.equals(TYPE) && si.getTypeName().equals(carType)) {
return true;
}
// check to see if schedule services car type and load
if (attribute.equals(LOAD) && si.getTypeName().equals(carType) && (si.getReceiveLoadName().equals(ScheduleItem.NONE) || car == null || si.getReceiveLoadName().equals(car.getLoadName()))) {
return true;
}
// check to see if schedule services car type and road
if (attribute.equals(ROAD) && si.getTypeName().equals(carType) && (si.getRoadName().equals(ScheduleItem.NONE) || car == null || si.getRoadName().equals(car.getRoadName()))) {
return true;
}
// check to see if schedule timetable allows delivery
if (attribute.equals(TIMETABLE) && si.getTypeName().equals(carType) && (si.getSetoutTrainScheduleId().equals("") || TrainManager.instance().getTrainScheduleActiveId().equals(si.getSetoutTrainScheduleId()))) {
return true;
}
// check to see if at least one schedule item can service car
if (attribute.equals(ALL) && si.getTypeName().equals(carType) && (si.getReceiveLoadName().equals(ScheduleItem.NONE) || car == null || si.getReceiveLoadName().equals(car.getLoadName())) && (si.getRoadName().equals(ScheduleItem.NONE) || car == null || si.getRoadName().equals(car.getRoadName())) && (si.getSetoutTrainScheduleId().equals(ScheduleItem.NONE) || TrainManager.instance().getTrainScheduleActiveId().equals(si.getSetoutTrainScheduleId()))) {
return true;
}
}
return false;
}
use of jmri.jmrit.operations.locations.schedules.ScheduleItem in project JMRI by JMRI.
the class ScheduleTest method testScheduleScheduleItems.
// test schedule scheduleitem
public void testScheduleScheduleItems() {
Schedule lts = new Schedule("Test id", "Test Name");
Assert.assertEquals("Location Schedule ScheduleItem id", "Test id", lts.getId());
Assert.assertEquals("Location Schedule ScheduleItem Name", "Test Name", lts.getName());
ScheduleItem ltsi1, ltsi2, ltsi3, ltsi4;
ltsi1 = lts.addItem("New Test Type");
Assert.assertEquals("Location Schedule ScheduleItem Check Type", "New Test Type", ltsi1.getTypeName());
String testid = ltsi1.getId();
ltsi2 = lts.getItemByType("New Test Type");
Assert.assertEquals("Location Schedule ScheduleItem Check Ids", testid, ltsi2.getId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 0", 1, ltsi2.getSequenceId());
ltsi3 = lts.addItem("New Second Test Type");
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 1", 1, ltsi1.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 2", 2, ltsi3.getSequenceId());
lts.moveItemUp(ltsi3);
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 3", 2, ltsi1.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 4", 1, ltsi3.getSequenceId());
ltsi4 = lts.addItem("New Third Test Item", 1);
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 5", 3, ltsi1.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 6", 1, ltsi3.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 7", 2, ltsi4.getSequenceId());
lts.moveItemDown(ltsi3);
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 8", 3, ltsi1.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 9", 2, ltsi3.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 10", 1, ltsi4.getSequenceId());
lts.deleteItem(ltsi3);
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 8", 2, ltsi1.getSequenceId());
Assert.assertEquals("Location Schedule ScheduleItem Check Seq 9", 1, ltsi4.getSequenceId());
}
Aggregations