use of jmri.jmrit.operations.routes.Route in project JMRI by JMRI.
the class TrainBuilderTest method testMaxEngines.
public void testMaxEngines() {
// This test uses the maximum length of a train in route
Setup.setMaxTrainLength(1000);
Train train = tmanager.newTrain("TestMaxEngines");
train.setNumberEngines(Train.AUTO);
Route route = rmanager.newRoute("AutoEngineTest");
train.setRoute(route);
Location A = lmanager.newLocation("A");
Location B = lmanager.newLocation("B");
Location C = lmanager.newLocation("C");
Track At = A.addTrack("track", Track.SPUR);
Track Bt = B.addTrack("track", Track.SPUR);
Track Ct = C.addTrack("track", Track.SPUR);
At.setLength(300);
Bt.setLength(300);
Ct.setLength(300);
RouteLocation rA = route.addLocation(A);
RouteLocation rB = route.addLocation(B);
RouteLocation rC = route.addLocation(C);
rA.setMaxCarMoves(12);
rB.setMaxCarMoves(12);
rC.setMaxCarMoves(12);
// 2.5% grade!
rB.setGrade(2.5);
Engine e1 = emanager.newEngine("E", "1");
e1.setModel("GP40");
Engine e2 = emanager.newEngine("E", "2");
e2.setModel("GP40");
Engine e3 = emanager.newEngine("E", "3");
e3.setModel("GP40");
Engine e4 = emanager.newEngine("E", "4");
e4.setModel("GP40");
e1.setLocation(A, At);
e2.setLocation(A, At);
e3.setLocation(A, At);
e4.setLocation(A, At);
Consist c = emanager.newConsist("c");
e1.setConsist(c);
e2.setConsist(c);
e3.setConsist(c);
e4.setConsist(c);
// limit the maximum to three engines
Setup.setMaxNumberEngines(3);
train.reset();
new TrainBuilder().build(train);
Assert.assertFalse("Train should not build, needs four engines, three is the maximum allowed", train.isBuilt());
// remove one engine from consist, train should build
c.delete(e4);
train.reset();
new TrainBuilder().build(train);
Assert.assertTrue("Train should build, three engines available", train.isBuilt());
}
use of jmri.jmrit.operations.routes.Route in project JMRI by JMRI.
the class PrintLocationsAction method getSetOutTrains.
private String getSetOutTrains(Track track) {
if (track.getDropOption().equals(Track.ANY)) {
return TAB + TAB + Bundle.getMessage("SetOutAllTrains") + NEW_LINE;
}
StringBuffer buf;
int charCount = 0;
String[] ids = track.getDropIds();
if (track.getDropOption().equals(Track.TRAINS) || track.getDropOption().equals(Track.EXCLUDE_TRAINS)) {
String trainType = Bundle.getMessage("TrainsSetOutTrack");
if (track.getDropOption().equals(Track.EXCLUDE_TRAINS)) {
trainType = Bundle.getMessage("ExcludeTrainsSetOutTrack");
}
buf = new StringBuffer(TAB + TAB + trainType + NEW_LINE + TAB + TAB);
for (String id : ids) {
Train train = TrainManager.instance().getTrainById(id);
if (train == null) {
log.info("Could not find a train for id: " + id + " track (" + track.getName() + ")");
continue;
}
charCount += train.getName().length() + 2;
if (charCount > charactersPerLine - 2 * TAB_LENGTH) {
buf.append(NEW_LINE + TAB + TAB);
charCount = train.getName().length() + 2;
}
buf.append(train.getName() + ", ");
}
} else {
String routeType = Bundle.getMessage("RoutesSetOutTrack");
if (track.getDropOption().equals(Track.EXCLUDE_ROUTES)) {
routeType = Bundle.getMessage("ExcludeRoutesSetOutTrack");
}
buf = new StringBuffer(TAB + TAB + routeType + NEW_LINE + TAB + TAB);
for (String id : ids) {
Route route = RouteManager.instance().getRouteById(id);
if (route == null) {
log.info("Could not find a route for id: " + id + " location (" + track.getLocation().getName() + ") track (" + track.getName() + // NOI18N
")");
continue;
}
charCount += route.getName().length() + 2;
if (charCount > charactersPerLine - 2 * TAB_LENGTH) {
buf.append(NEW_LINE + TAB + TAB);
charCount = route.getName().length() + 2;
}
buf.append(route.getName() + ", ");
}
}
if (buf.length() > 2) {
// remove trailing separators
buf.setLength(buf.length() - 2);
}
buf.append(NEW_LINE);
return buf.toString();
}
use of jmri.jmrit.operations.routes.Route in project JMRI by JMRI.
the class CarManager method getAvailableTrainList.
/**
* Return a list available cars (no assigned train or car already assigned
* to this train) on a route, cars are ordered least recently moved to most
* recently moved.
* @param train The Train to use.
*
* @return List of cars with no assigned train on a route
*/
public List<Car> getAvailableTrainList(Train train) {
List<Car> out = new ArrayList<Car>();
Route route = train.getRoute();
if (route == null) {
return out;
}
// get a list of locations served by this route
List<RouteLocation> routeList = route.getLocationsBySequenceList();
// don't include RollingStock at route destination
RouteLocation destination = null;
if (routeList.size() > 1) {
destination = routeList.get(routeList.size() - 1);
// include all cars
for (int i = 0; i < routeList.size() - 1; i++) {
if (destination.getName().equals(routeList.get(i).getName())) {
// include cars at destination
destination = null;
break;
}
}
// pickup allowed at destination? Don't include cars in staging
if (destination != null && destination.isPickUpAllowed() && destination.getLocation() != null && !destination.getLocation().isStaging()) {
// include cars at destination
destination = null;
}
}
// get rolling stock by priority and then by moves
List<Car> sortByPriority = sortByPriority(getByMovesList());
// now build list of available RollingStock for this route
for (Car car : sortByPriority) {
// only use RollingStock with a location
if (car.getLocation() == null) {
continue;
}
RouteLocation rl = route.getLastLocationByName(car.getLocationName());
// assigned train is this one
if (rl != null && rl != destination && (car.getTrain() == null || train.equals(car.getTrain()))) {
out.add(car);
}
}
return out;
}
use of jmri.jmrit.operations.routes.Route in project JMRI by JMRI.
the class TrainBuilder method checkEngineHP.
/**
* Checks to see if the engine assigned to the train has the appropriate HP.
* If the train's HP requirements are significantly higher or lower than the
* engine that was assigned, the program will search for a more appropriate
* engine, and assign that engine to the train.
*
* The HP calculation is based on a minimum train speed of 36 MPH. The
* formula HPT x 12 / % Grade = Speed, is used to determine the horsepower
* required. Speed is fixed at 36 MPH. For example a 1% grade requires a
* minimum of 3 HPT.
*/
private void checkEngineHP() throws BuildFailedException {
if (!_train.getNumberEngines().equals(Train.AUTO_HPT) || Setup.getHorsePowerPerTon() == 0 || _departStageTrack != null)
return;
// there should be at lease one engine assigned to this train
Engine leadEngine = _train.getLeadEngine();
if (leadEngine == null)
// TODO throw an exception
return;
addLine(_buildReport, ONE, BLANK_LINE);
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildDetermineHpNeeded"), new Object[] { leadEngine.toString(), leadEngine.getHp(), Setup.getHorsePowerPerTon() }));
// now determine the HP needed for this train
int hpNeeded = 0;
int hpAvailable = 0;
Route route = _train.getRoute();
if (route != null) {
boolean helper = false;
for (RouteLocation rl : route.getLocationsBySequenceList()) {
if ((_train.getSecondLegOptions() == Train.HELPER_ENGINES && rl == _train.getSecondLegStartLocation()) || (_train.getThirdLegOptions() == Train.HELPER_ENGINES && rl == _train.getThirdLegStartLocation())) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("AddHelpersAt"), new Object[] { rl.getName() }));
helper = true;
}
if ((_train.getSecondLegOptions() == Train.HELPER_ENGINES && rl == _train.getSecondLegEndLocation()) || (_train.getThirdLegOptions() == Train.HELPER_ENGINES && rl == _train.getThirdLegEndLocation())) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("RemoveHelpersAt"), new Object[] { rl.getName() }));
helper = false;
}
if (helper) {
// ignore HP needed when helpers are assigned to the train
continue;
}
// check for a change of engines in the train's route
if (((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES && rl == _train.getSecondLegStartLocation()) || ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES && rl == _train.getThirdLegStartLocation())) {
log.debug("Loco change at ({})", rl.getName());
// done
break;
}
if (_train.getTrainHorsePower(rl) > hpAvailable)
hpAvailable = _train.getTrainHorsePower(rl);
int weight = rl.getTrainWeight();
int hpRequired = (int) ((36 * rl.getGrade() / 12) * weight);
if (hpRequired < Setup.getHorsePowerPerTon() * weight)
// minimum HPT
hpRequired = Setup.getHorsePowerPerTon() * weight;
if (hpRequired > hpNeeded) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildReportTrainHpNeeds"), new Object[] { weight, _train.getNumberCarsInTrain(rl), rl.getGrade(), rl.getName(), rl.getId(), hpRequired }));
hpNeeded = hpRequired;
}
}
}
if (hpNeeded > hpAvailable) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildAssignedHpNotEnough"), new Object[] { leadEngine.toString(), hpAvailable, hpNeeded }));
findNewEngine(hpNeeded, leadEngine);
} else if (hpAvailable > 2 * hpNeeded) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildAssignedHpTooMuch"), new Object[] { leadEngine.toString(), hpAvailable, hpNeeded }));
findNewEngine(hpNeeded, leadEngine);
} else {
log.debug("Keeping engine ({}) it meets the train's HP requirement", leadEngine.toString());
}
}
use of jmri.jmrit.operations.routes.Route in project JMRI by JMRI.
the class Train method getTrainLength.
/**
* Gets the train's length at the route location specified
*
* @param routeLocation The route location
* @return The train length at the route location
*/
public int getTrainLength(RouteLocation routeLocation) {
int length = 0;
Route route = getRoute();
if (route != null) {
for (RouteLocation rl : route.getLocationsBySequenceList()) {
for (RollingStock rs : EngineManager.instance().getList(this)) {
if (rs.getRouteLocation() == rl) {
length += rs.getTotalLength();
}
if (rs.getRouteDestination() == rl) {
length += -rs.getTotalLength();
}
}
for (RollingStock rs : CarManager.instance().getList(this)) {
if (rs.getRouteLocation() == rl) {
length += rs.getTotalLength();
}
if (rs.getRouteDestination() == rl) {
length += -rs.getTotalLength();
}
}
if (rl == routeLocation) {
break;
}
}
}
return length;
}
Aggregations