use of jmri.jmrit.operations.locations.Location in project JMRI by JMRI.
the class RouteManager method copyRouteLocation.
private void copyRouteLocation(Route newRoute, RouteLocation rl, RouteLocation rlNext, boolean invert) {
Location loc = LocationManager.instance().getLocationByName(rl.getName());
RouteLocation rlNew = newRoute.addLocation(loc);
// now copy the route location objects we want
rlNew.setMaxCarMoves(rl.getMaxCarMoves());
rlNew.setWait(rl.getWait());
rlNew.setDepartureTime(rl.getDepartureTime());
rlNew.setComment(rl.getComment());
if (!invert) {
rlNew.setDropAllowed(rl.isDropAllowed());
rlNew.setPickUpAllowed(rl.isPickUpAllowed());
rlNew.setGrade(rl.getGrade());
rlNew.setTrainDirection(rl.getTrainDirection());
rlNew.setMaxTrainLength(rl.getMaxTrainLength());
} else {
// flip set outs and pick ups
rlNew.setDropAllowed(rl.isPickUpAllowed());
rlNew.setPickUpAllowed(rl.isDropAllowed());
// invert train directions
int oldDirection = rl.getTrainDirection();
if (oldDirection == RouteLocation.NORTH) {
rlNew.setTrainDirection(RouteLocation.SOUTH);
} else if (oldDirection == RouteLocation.SOUTH) {
rlNew.setTrainDirection(RouteLocation.NORTH);
} else if (oldDirection == RouteLocation.EAST) {
rlNew.setTrainDirection(RouteLocation.WEST);
} else if (oldDirection == RouteLocation.WEST) {
rlNew.setTrainDirection(RouteLocation.EAST);
}
// get the max length between location
if (rlNext == null) {
log.error("Can not copy route, oldNextRl is null!");
return;
}
rlNew.setMaxTrainLength(rlNext.getMaxTrainLength());
}
rlNew.setTrainIconX(rl.getTrainIconX());
rlNew.setTrainIconY(rl.getTrainIconY());
}
use of jmri.jmrit.operations.locations.Location in project JMRI by JMRI.
the class TrainBuilder method generateCarLoadStagingToStaging.
/**
* Tries to place a custom load in the car that is departing staging, and
* may terminate to staging. Tries to create a custom load that will be
* accepted by the train's terminal if the terminal is staging. Otherwise,
* any staging track is searched for that will accept this car and a custom
* load.
*
* @param car the car
*/
private boolean generateCarLoadStagingToStaging(Car car) throws BuildFailedException {
if (car.getTrack() == null || !car.getTrack().getTrackType().equals(Track.STAGING) || !car.getTrack().isAddCustomLoadsAnyStagingTrackEnabled() || !car.getLoadName().equals(CarLoads.instance().getDefaultEmptyName()) || car.getDestination() != null || car.getFinalDestination() != null) {
log.debug(// NOI18N
"No load generation for car ({}) isAddCustomLoadsAnyStagingTrackEnabled: " + // NOI18N
(car.getTrack().isAddCustomLoadsAnyStagingTrackEnabled() ? "true" : "false") + // NOI18N
", car load ({}) destination ({}) final destination ({})", car.toString(), car.getLoadName(), car.getDestinationName(), car.getFinalDestinationName());
return false;
}
List<Track> tracks = locationManager.getTracks(Track.STAGING);
// log.debug("Found {} staging tracks for load generation", tracks.size());
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTryStagingToStaging"), new Object[] { car.toString(), tracks.size() }));
// list of locations that can't be reached by the router
List<Location> locationsNotReachable = new ArrayList<Location>();
while (tracks.size() > 0) {
// pick a track randomly
int rnd = (int) (Math.random() * tracks.size());
Track track = tracks.get(rnd);
tracks.remove(track);
log.debug("Try staging track ({}, {})", track.getLocation().getName(), track.getName());
// find a staging track that isn't at the departure
if (track.getLocation() == _departLocation) {
log.debug("Don't use departure location ({})", track.getLocation().getName());
continue;
}
if (!_train.isAllowThroughCarsEnabled() && track.getLocation() == _terminateLocation) {
log.debug("Through cars to location ({}) not allowed", track.getLocation().getName());
continue;
}
if (locationsNotReachable.contains(track.getLocation())) {
log.debug("Location ({}) not reachable", track.getLocation().getName());
continue;
}
if (!car.getTrack().acceptsDestination(track.getLocation())) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildDestinationNotServiced"), new Object[] { track.getLocation().getName(), car.getTrackName() }));
locationsNotReachable.add(track.getLocation());
continue;
}
if (_terminateStageTrack != null && track.getLocation() == _terminateStageTrack.getLocation()) {
log.debug("Train doesn't terminate to staging track ({}) at terminal ({})", track.getName(), track.getLocation().getName());
continue;
}
// the following method sets the load generated from staging boolean
if (generateLoadCarDepartingAndTerminatingIntoStaging(car, track)) {
// test to see if destination is reachable by this train
if (Router.instance().setDestination(car, _train, _buildReport) && car.getDestination() != null) {
// done, car has a custom load and a final destination
return true;
}
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildStagingTrackNotReachable"), new Object[] { track.getLocation().getName(), track.getName(), car.getLoadName() }));
// return car to original state
car.setLoadName(CarLoads.instance().getDefaultEmptyName());
car.setLoadGeneratedFromStaging(false);
car.setFinalDestination(null);
car.updateKernel();
// couldn't route to this staging location
locationsNotReachable.add(track.getLocation());
}
}
// No staging tracks reachable, try the track the train is terminating to
if (_train.isAllowThroughCarsEnabled() && _terminateStageTrack != null && car.getTrack().acceptsDestination(_terminateStageTrack.getLocation()) && generateLoadCarDepartingAndTerminatingIntoStaging(car, _terminateStageTrack)) {
return true;
}
return false;
}
use of jmri.jmrit.operations.locations.Location in project JMRI by JMRI.
the class TrainBuilder method setLocoDestination.
private boolean setLocoDestination(Engine engine, RouteLocation rl, RouteLocation rld, Track terminateTrack) {
// is there a staging track?
if (terminateTrack != null) {
String status = engine.testDestination(terminateTrack.getLocation(), terminateTrack);
if (status.equals(Track.OKAY)) {
addEngineToTrain(engine, rl, rld, terminateTrack);
// done
return true;
} else {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCanNotDropEngineToTrack"), new Object[] { engine.toString(), terminateTrack.getName(), status, terminateTrack.getTrackTypeName() }));
}
// find a destination track for this engine
} else {
Location destination = rld.getLocation();
List<Track> destTracks = destination.getTrackByMovesList(null);
if (destTracks.size() == 0) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildNoTracksAtDestination"), new Object[] { rld.getName() }));
}
for (Track track : destTracks) {
if (!checkDropTrainDirection(engine, rld, track)) {
continue;
}
String status = engine.testDestination(destination, track);
if (status.equals(Track.OKAY)) {
addEngineToTrain(engine, rl, rld, track);
// done
return true;
} else {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildCanNotDropEngineToTrack"), new Object[] { engine.toString(), track.getName(), status, track.getTrackTypeName() }));
}
}
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildCanNotDropEngToDest"), new Object[] { engine.toString(), rld.getName() }));
}
// not able to set loco's destination
return false;
}
use of jmri.jmrit.operations.locations.Location in project JMRI by JMRI.
the class TrainBuilder method blockCarsFromStaging.
/**
* Block cars departing staging. No guarantee that cars departing staging
* can be blocked by destination. By using the pick up location id, this
* routine tries to find destinations that are willing to accepts all of the
* cars that were "blocked" together when they were picked up. Rules: The
* route must allow set outs at the destination. The route must allow the
* correct number of set outs. The destination must accept all cars in the
* pick up block.
*
*/
private void blockCarsFromStaging() throws BuildFailedException {
if (_departStageTrack == null || !_departStageTrack.isBlockCarsEnabled()) {
return;
}
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("blockDepartureHasBlocks"), new Object[] { _departStageTrack.getName(), _numOfBlocks.size() }));
Enumeration<String> en = _numOfBlocks.keys();
while (en.hasMoreElements()) {
String locId = en.nextElement();
int numCars = _numOfBlocks.get(locId);
String locName = "";
Location l = locationManager.getLocationById(locId);
if (l != null) {
locName = l.getName();
}
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("blockFromHasCars"), new Object[] { locId, locName, numCars }));
if (_numOfBlocks.size() < 2) {
addLine(_buildReport, SEVEN, Bundle.getMessage("blockUnable"));
return;
}
}
blockByLocationMoves();
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("blockDone"), new Object[] { _departStageTrack.getName() }));
}
use of jmri.jmrit.operations.locations.Location in project JMRI by JMRI.
the class AutomationManagerTest method testCopyAutomation.
/**
* Creates an automation with 5 items, and checks to see if all items
* are copied correctly.
*/
public void testCopyAutomation() {
AutomationManager manager = AutomationManager.instance();
Assert.assertNotNull("test creation", manager);
Automation automation = manager.newAutomation("TestAutomation");
automation.setComment("test comment for automation");
Assert.assertEquals(1, manager.getSize());
AutomationItem item1 = automation.addItem();
item1.setAction(new BuildTrainAction());
item1.setTrain(new Train("trainId", "trainName1"));
item1.setMessage("item1 OK message");
item1.setMessageFail("item1 fail message");
item1.setHaltFailureEnabled(false);
AutomationItem item2 = automation.addItem();
item2.setAction(new GotoAction());
item2.setGotoAutomationItem(item1);
AutomationItem item3 = automation.addItem();
item3.setAction(new MoveTrainAction());
item3.setTrain(new Train("trainId", "trainName2"));
item3.setRouteLocation(new RouteLocation("id", new Location("id", "testLocationName")));
AutomationItem item4 = automation.addItem();
item4.setAction(new ActivateTimetableAction());
TrainSchedule trainSchedule = TrainScheduleManager.instance().newSchedule("train schedule name");
item4.setOther(trainSchedule);
AutomationItem item5 = automation.addItem();
item5.setAction(new RunAutomationAction());
Automation automationToRun = manager.newAutomation("TestAutomation2");
item5.setOther(automationToRun);
item5.setMessage("item5 OK message");
item5.setMessageFail("item5 fail message");
item5.setHaltFailureEnabled(false);
Automation copy = manager.copyAutomation(automation, "Copy");
Assert.assertNotNull("test automation creation", copy);
// There are now three automations
Assert.assertEquals("The number of automations", 3, manager.getSize());
Assert.assertEquals("The number of items", 5, copy.getSize());
Assert.assertEquals(copy.getComment(), automation.getComment());
AutomationItem copyItem1 = copy.getItemBySequenceId(1);
Assert.assertEquals("1st item is build train", copyItem1.getActionName(), item1.getActionName());
Assert.assertNotNull(copyItem1.getTrain());
Assert.assertNull(copyItem1.getGotoAutomationItem());
Assert.assertNull(copyItem1.getTrainSchedule());
Assert.assertNull(copyItem1.getRouteLocation());
Assert.assertEquals(copyItem1.getTrain(), item1.getTrain());
Assert.assertEquals("item1 OK message", copyItem1.getMessage());
Assert.assertEquals("item1 fail message", copyItem1.getMessageFail());
Assert.assertNull(copyItem1.getAutomationToRun());
Assert.assertFalse(copyItem1.isHaltFailureEnabled());
AutomationItem copyItem2 = copy.getItemBySequenceId(2);
Assert.assertEquals("2nd item is goto", copyItem2.getActionName(), item2.getActionName());
Assert.assertNull(copyItem2.getTrain());
Assert.assertNotNull(copyItem2.getGotoAutomationItem());
Assert.assertNull(copyItem2.getTrainSchedule());
Assert.assertNull(copyItem2.getRouteLocation());
Assert.assertEquals(copyItem2.getGotoAutomationItem().getActionName(), item2.getGotoAutomationItem().getActionName());
Assert.assertNull(copyItem2.getAutomationToRun());
Assert.assertEquals("", copyItem2.getMessage());
Assert.assertEquals("", copyItem2.getMessageFail());
Assert.assertTrue(copyItem2.isHaltFailureEnabled());
AutomationItem copyItem3 = copy.getItemBySequenceId(3);
Assert.assertEquals("3rd item is move train", copyItem3.getActionName(), item3.getActionName());
Assert.assertNotNull(copyItem3.getTrain());
Assert.assertNull(copyItem3.getGotoAutomationItem());
Assert.assertNull(copyItem3.getTrainSchedule());
Assert.assertNotNull(copyItem3.getRouteLocation());
Assert.assertEquals(copyItem3.getTrain(), item3.getTrain());
Assert.assertEquals(copyItem3.getRouteLocation(), item3.getRouteLocation());
Assert.assertNull(copyItem3.getAutomationToRun());
Assert.assertEquals("", copyItem3.getMessage());
Assert.assertEquals("", copyItem3.getMessageFail());
Assert.assertTrue(copyItem3.isHaltFailureEnabled());
AutomationItem copyItem4 = copy.getItemBySequenceId(4);
Assert.assertEquals("4th item is activate train schedule", copyItem4.getActionName(), item4.getActionName());
Assert.assertNull(copyItem4.getTrain());
Assert.assertNull(copyItem4.getGotoAutomationItem());
Assert.assertNull(copyItem4.getRouteLocation());
Assert.assertNotNull(copyItem4.getTrainSchedule());
Assert.assertEquals(trainSchedule, copyItem4.getTrainSchedule());
Assert.assertNull(copyItem4.getAutomationToRun());
Assert.assertEquals("", copyItem4.getMessage());
Assert.assertEquals("", copyItem4.getMessageFail());
Assert.assertTrue(copyItem4.isHaltFailureEnabled());
AutomationItem copyItem5 = copy.getItemBySequenceId(5);
Assert.assertEquals("5th item is run automation", copyItem5.getActionName(), item5.getActionName());
Assert.assertNull(copyItem5.getTrain());
Assert.assertNull(copyItem5.getGotoAutomationItem());
Assert.assertNull(copyItem5.getRouteLocation());
Assert.assertNull(copyItem5.getTrainSchedule());
Assert.assertNotNull(copyItem5.getAutomationToRun());
Assert.assertEquals(automationToRun, copyItem5.getAutomationToRun());
Assert.assertEquals("item5 OK message", copyItem5.getMessage());
Assert.assertEquals("item5 fail message", copyItem5.getMessageFail());
Assert.assertFalse(copyItem5.isHaltFailureEnabled());
}
Aggregations