Search in sources :

Example 11 with TrainSchedule

use of jmri.jmrit.operations.trains.timetable.TrainSchedule in project JMRI by JMRI.

the class ApplyTimetableAction method doAction.

@Override
public void doAction() {
    if (getAutomationItem() != null) {
        TrainSchedule ts = TrainScheduleManager.instance().getScheduleById(TrainManager.instance().getTrainScheduleActiveId());
        if (ts != null) {
            for (Train train : TrainManager.instance().getTrainsByIdList()) {
                train.setBuildEnabled(ts.containsTrainId(train.getId()));
            }
        }
        finishAction(ts != null);
    }
}
Also used : TrainSchedule(jmri.jmrit.operations.trains.timetable.TrainSchedule) Train(jmri.jmrit.operations.trains.Train)

Example 12 with TrainSchedule

use of jmri.jmrit.operations.trains.timetable.TrainSchedule 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;
}
Also used : ScheduleItem(jmri.jmrit.operations.locations.schedules.ScheduleItem) TrainSchedule(jmri.jmrit.operations.trains.timetable.TrainSchedule) Schedule(jmri.jmrit.operations.locations.schedules.Schedule) TrainSchedule(jmri.jmrit.operations.trains.timetable.TrainSchedule)

Example 13 with TrainSchedule

use of jmri.jmrit.operations.trains.timetable.TrainSchedule in project JMRI by JMRI.

the class TrainBuilder method removeAndListCars.

/**
     * Remove unwanted cars from the car list. Remove cars that don't have a
     * track assignment, and check that the car can be serviced by this train.
     * Lists all cars available to train by location.
     */
private void removeAndListCars() throws BuildFailedException {
    // add line when in very detailed report mode
    addLine(_buildReport, SEVEN, BLANK_LINE);
    addLine(_buildReport, SEVEN, Bundle.getMessage("buildRemoveCars"));
    boolean showCar = true;
    int carListSize = _carList.size();
    for (_carIndex = 0; _carIndex < _carList.size(); _carIndex++) {
        Car car = _carList.get(_carIndex);
        // only show the first 100 cars removed
        if (showCar && carListSize - _carList.size() == DISPLAY_CAR_LIMIT_100) {
            showCar = false;
            addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildOnlyFirstXXXCars"), new Object[] { DISPLAY_CAR_LIMIT_100, Bundle.getMessage("Type") }));
        }
        // remove cars that don't have a track assignment
        if (car.getTrack() == null) {
            addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildErrorRsNoLoc"), new Object[] { car.toString(), car.getLocationName() }));
            _carList.remove(car);
            _carIndex--;
            continue;
        }
        // remove cars that have been reported as missing
        if (car.isLocationUnknown()) {
            addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarLocUnknown"), new Object[] { car.toString(), car.getLocationName(), car.getTrackName() }));
            if (car.getTrack().equals(_departStageTrack)) {
                throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorLocationUnknown"), new Object[] { car.getLocationName(), car.getTrackName(), car.toString() }));
            }
            _carList.remove(car);
            _carIndex--;
            continue;
        }
        // remove cars that are out of service
        if (car.isOutOfService()) {
            addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarOutOfService"), new Object[] { car.toString(), car.getLocationName(), car.getTrackName() }));
            if (car.getTrack().equals(_departStageTrack)) {
                throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorLocationOutOfService"), new Object[] { car.getLocationName(), car.getTrackName(), car.toString() }));
            }
            _carList.remove(car);
            _carIndex--;
            continue;
        }
        // remove cars with FRED that have a destination that isn't the terminal
        if (car.hasFred() && car.getDestination() != null && car.getDestination() != _terminateLocation) {
            addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildExcludeCarWrongDest"), new Object[] { car.toString(), car.getTypeName(), car.getDestinationName() }));
            _carList.remove(car);
            _carIndex--;
            continue;
        }
        // remove cabooses that have a destination that isn't the terminal, no caboose changes in the train's route
        if (car.isCaboose() && car.getDestination() != null && car.getDestination() != _terminateLocation && _train.getSecondLegOptions() == Train.NO_CABOOSE_OR_FRED && _train.getThirdLegOptions() == Train.NO_CABOOSE_OR_FRED) {
            addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildExcludeCarWrongDest"), new Object[] { car.toString(), car.getTypeName(), car.getDestinationName() }));
            _carList.remove(car);
            _carIndex--;
            continue;
        }
        // is car at interchange?
        if (car.getTrack().getTrackType().equals(Track.INTERCHANGE)) {
            // don't service a car at interchange and has been dropped off by this train
            if (car.getTrack().getPickupOption().equals(Track.ANY) && car.getLastRouteId().equals(_train.getRoute().getId())) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarDropByTrain"), new Object[] { car.toString(), _train.getRoute().getName(), car.getLocationName(), car.getTrackName() }));
                _carList.remove(car);
                _carIndex--;
                continue;
            }
        }
        if (car.getTrack().getTrackType().equals(Track.INTERCHANGE) || car.getTrack().getTrackType().equals(Track.SPUR)) {
            if (car.getTrack().getPickupOption().equals(Track.TRAINS) || car.getTrack().getPickupOption().equals(Track.EXCLUDE_TRAINS)) {
                if (car.getTrack().acceptsPickupTrain(_train)) {
                    log.debug("Car ({}) can be picked up by this train", car.toString());
                } else {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarByTrain"), new Object[] { car.toString(), car.getTrack().getTrackTypeName(), car.getLocationName(), car.getTrackName() }));
                    _carList.remove(car);
                    _carIndex--;
                    continue;
                }
            } else if (car.getTrack().getPickupOption().equals(Track.ROUTES) || car.getTrack().getPickupOption().equals(Track.EXCLUDE_ROUTES)) {
                if (car.getTrack().acceptsPickupRoute(_train.getRoute())) {
                    log.debug("Car ({}) can be picked up by this route", car.toString());
                } else {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarByRoute"), new Object[] { car.toString(), car.getTrack().getTrackTypeName(), car.getLocationName(), car.getTrackName() }));
                    _carList.remove(car);
                    _carIndex--;
                    continue;
                }
            }
        }
        // checked in the routine checkDepartureStagingTrack().
        if (_departStageTrack == null || car.getTrack() != _departStageTrack) {
            if (!_train.acceptsRoadName(car.getRoadName())) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarWrongRoad"), new Object[] { car.toString(), car.getTypeName(), car.getRoadName() }));
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            if (!_train.acceptsTypeName(car.getTypeName())) {
                if (showCar) {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarWrongType"), new Object[] { car.toString(), car.getTypeName() }));
                }
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            if (!car.isCaboose() && !car.isPassenger() && !_train.acceptsLoad(car.getLoadName(), car.getTypeName())) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarLoadAtLoc"), new Object[] { car.toString(), car.getTypeName(), car.getLoadName() }));
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            if (!_train.acceptsOwnerName(car.getOwner())) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarOwnerAtLoc"), new Object[] { car.toString(), car.getOwner(), (car.getLocationName() + ", " + car.getTrackName()) }));
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            if (!_train.acceptsBuiltDate(car.getBuilt())) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarBuiltAtLoc"), new Object[] { car.toString(), car.getBuilt(), (car.getLocationName() + ", " + car.getTrackName()) }));
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            // remove cars with FRED if not needed by train
            if (car.hasFred() && (_train.getRequirements() & Train.FRED) == 0) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarWithFredAtLoc"), new Object[] { car.toString(), car.getTypeName(), (car.getLocationName() + ", " + car.getTrackName()) }));
                // remove this car from the list
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            // does car have a wait count?
            if (car.getWait() > 0 && _train.services(car)) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarWait"), new Object[] { car.toString(), car.getTypeName(), car.getLocationName(), car.getTrackName(), car.getWait() }));
                // decrement wait count
                car.setWait(car.getWait() - 1);
                // a car's load changes when the wait count reaches 0
                String oldLoad = car.getLoadName();
                if (car.getTrack().getTrackType().equals(Track.SPUR)) {
                    // has the wait count reached 0?
                    car.updateLoad();
                }
                String newLoad = car.getLoadName();
                if (!oldLoad.equals(newLoad)) {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarLoadChangedWait"), new Object[] { car.toString(), car.getTypeName(), oldLoad, newLoad }));
                }
                _carList.remove(car);
                _carIndex--;
                continue;
            }
            if (!car.getPickupScheduleId().equals(Car.NONE)) {
                if (TrainManager.instance().getTrainScheduleActiveId().equals(TrainSchedule.ANY) || car.getPickupScheduleId().equals(TrainManager.instance().getTrainScheduleActiveId())) {
                    car.setPickupScheduleId(Car.NONE);
                } else {
                    TrainSchedule sch = TrainScheduleManager.instance().getScheduleById(car.getPickupScheduleId());
                    if (sch != null) {
                        addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarSchedule"), new Object[] { car.toString(), car.getTypeName(), car.getLocationName(), car.getTrackName(), sch.getName() }));
                        _carList.remove(car);
                        _carIndex--;
                        continue;
                    }
                }
            }
        }
    }
    // adjust car list to only have cars from one staging track
    if (_departStageTrack != null) {
        int numCarsFromStaging = 0;
        _numOfBlocks = new Hashtable<String, Integer>();
        // add line when in very detailed report mode
        addLine(_buildReport, SEVEN, BLANK_LINE);
        addLine(_buildReport, SEVEN, Bundle.getMessage("buildRemoveCarsStaging"));
        for (_carIndex = 0; _carIndex < _carList.size(); _carIndex++) {
            Car car = _carList.get(_carIndex);
            if (car.getLocationName().equals(_departLocation.getName())) {
                if (car.getTrackName().equals(_departStageTrack.getName())) {
                    numCarsFromStaging++;
                    // don't block cabooses, cars with FRED, or passenger. Only block lead cars in kernel
                    if (!car.isCaboose() && !car.hasFred() && !car.isPassenger() && (car.getKernel() == null || car.getKernel().isLead(car))) {
                        log.debug("Car {} last location id: {}", car.toString(), car.getLastLocationId());
                        Integer number = 1;
                        if (_numOfBlocks.containsKey(car.getLastLocationId())) {
                            number = _numOfBlocks.get(car.getLastLocationId()) + 1;
                            _numOfBlocks.remove(car.getLastLocationId());
                        }
                        _numOfBlocks.put(car.getLastLocationId(), number);
                    }
                } else {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarAtLoc"), new Object[] { car.toString(), (car.getLocationName() + ", " + car.getTrackName()) }));
                    _carList.remove(car);
                    _carIndex--;
                }
            }
        }
        // show how many cars are departing from staging
        // add line when in detailed report mode
        addLine(_buildReport, FIVE, BLANK_LINE);
        addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildDepartingStagingCars"), new Object[] { _departStageTrack.getLocation().getName(), _departStageTrack.getName(), numCarsFromStaging }));
        // and list them
        for (Car car : _carList) {
            if (car.getTrack() == _departStageTrack) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildStagingCarAtLoc"), new Object[] { car.toString(), car.getTypeName(), car.getLoadName() }));
            }
        }
        // error if all of the cars from staging aren't available
        if (numCarsFromStaging != _departStageTrack.getNumberCars()) {
            throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNotAllCars"), new Object[] { _departStageTrack.getName(), Integer.toString(_departStageTrack.getNumberCars() - numCarsFromStaging) }));
        }
        log.debug("Staging departure track ({}) has {} cars and {} blocks", _departStageTrack.getName(), numCarsFromStaging, // NOI18N
        _numOfBlocks.size());
    }
    // show how many cars were found
    // add line when in detailed report mode
    addLine(_buildReport, FIVE, BLANK_LINE);
    addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildFoundCars"), new Object[] { Integer.toString(_carList.size()), _train.getName() }));
    // only show cars once using the train's route
    List<String> locationNames = new ArrayList<String>();
    for (RouteLocation rl : _train.getRoute().getLocationsBySequenceList()) {
        if (locationNames.contains(rl.getName())) {
            continue;
        }
        locationNames.add(rl.getName());
        if (rl.getLocation().isStaging()) {
            addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarsInStaging"), new Object[] { rl.getName() }));
        } else {
            addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarsAtLocation"), new Object[] { rl.getName() }));
        }
        // now go through the car list and remove non-lead cars in kernels, destinations that aren't part of this
        // route
        int carCount = 0;
        for (_carIndex = 0; _carIndex < _carList.size(); _carIndex++) {
            Car car = _carList.get(_carIndex);
            if (!car.getLocationName().equals(rl.getName())) {
                continue;
            }
            // only print out the first DISPLAY_CAR_LIMIT cars for each location
            if (carCount < DISPLAY_CAR_LIMIT_50) {
                if (car.getLoadPriority().equals(CarLoad.PRIORITY_LOW)) {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarAtLocWithMoves"), new Object[] { car.toString(), car.getTypeName(), (car.getLocationName() + ", " + car.getTrackName()), car.getMoves() }));
                } else {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarAtLocWithMovesPriority"), new Object[] { car.toString(), car.getTypeName(), (car.getLocationName() + ", " + car.getTrackName()), car.getMoves(), car.getLoadPriority() }));
                }
            }
            if (carCount == DISPLAY_CAR_LIMIT_50) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildOnlyFirstXXXCars"), new Object[] { carCount, rl.getName() }));
            }
            carCount++;
            // use only the lead car in a kernel for building trains
            if (car.getKernel() != null) {
                if (car.getKernel().isLead(car)) {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarLeadKernel"), new Object[] { car.toString(), car.getKernelName(), car.getKernel().getSize(), car.getKernel().getTotalLength(), Setup.getLengthUnit().toLowerCase() }));
                } else {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarPartOfKernel"), new Object[] { car.toString(), car.getKernelName(), car.getKernel().getSize(), car.getKernel().getTotalLength(), Setup.getLengthUnit().toLowerCase() }));
                }
                checkKernel(car);
                if (!car.getKernel().isLead(car)) {
                    // remove this car from the list
                    _carList.remove(car);
                    _carIndex--;
                    continue;
                }
            }
            if (_train.equals(car.getTrain())) {
                addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildCarAlreadyAssigned"), new Object[] { car.toString() }));
            }
            // does car have a destination that is part of this train's route?
            if (car.getDestination() != null) {
                addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCarHasAssignedDest"), new Object[] { car.toString(), (car.getDestinationName() + ", " + car.getDestinationTrackName()) }));
                RouteLocation rld = _train.getRoute().getLastLocationByName(car.getDestinationName());
                if (rld == null) {
                    addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildExcludeCarDestNotPartRoute"), new Object[] { car.toString(), car.getDestinationName(), _train.getRoute().getName() }));
                    // build failure if car departing staging
                    if (car.getLocation().equals(_departLocation) && _departStageTrack != null) {
                        // routine.
                        throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorCarNotPartRoute"), new Object[] { car.toString() }));
                    }
                    // remove this car from the list
                    _carList.remove(car);
                    _carIndex--;
                }
            }
        }
        // add line when in detailed report mode
        addLine(_buildReport, SEVEN, BLANK_LINE);
    }
    return;
}
Also used : Car(jmri.jmrit.operations.rollingstock.cars.Car) TrainSchedule(jmri.jmrit.operations.trains.timetable.TrainSchedule) ArrayList(java.util.ArrayList) RouteLocation(jmri.jmrit.operations.routes.RouteLocation)

Aggregations

TrainSchedule (jmri.jmrit.operations.trains.timetable.TrainSchedule)13 RouteLocation (jmri.jmrit.operations.routes.RouteLocation)4 Train (jmri.jmrit.operations.trains.Train)3 ArrayList (java.util.ArrayList)2 ActivateTimetableAction (jmri.jmrit.operations.automation.actions.ActivateTimetableAction)2 Location (jmri.jmrit.operations.locations.Location)2 Car (jmri.jmrit.operations.rollingstock.cars.Car)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Automation (jmri.jmrit.operations.automation.Automation)1 AutomationItem (jmri.jmrit.operations.automation.AutomationItem)1 BuildTrainAction (jmri.jmrit.operations.automation.actions.BuildTrainAction)1 GotoAction (jmri.jmrit.operations.automation.actions.GotoAction)1 MoveTrainAction (jmri.jmrit.operations.automation.actions.MoveTrainAction)1 RunAutomationAction (jmri.jmrit.operations.automation.actions.RunAutomationAction)1