Skip to content

Commit

Permalink
Merge pull request #3374 from matsim-org/kmt_freightCleanupCode
Browse files Browse the repository at this point in the history
Freight contrib: code maintenance
  • Loading branch information
kt86 authored Jul 17, 2024
2 parents f8119af + f59e4d6 commit 31d2c0d
Show file tree
Hide file tree
Showing 44 changed files with 214 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void startTag(String name, Attributes atts, Stack<String> context) {
// do nothing
break ;
default:
logger.warn("Unexpected value while reading in. This field will be ignored: " + name);
logger.warn("Unexpected value while reading in. This field will be ignored: {}", name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void startTag(String name, Attributes atts, Stack<String> context) {
// do nothing
break ;
default:
logger.warn("Unexpected value while reading in. This field will be ignored: " + name);
logger.warn("Unexpected value while reading in. This field will be ignored: {}", name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public void readFile( String filename ){
try {
reader.readFile( filename );
} catch (Exception e) {
log.warn("### Exception found while trying to read CarrierPlan: Message: " + e.getMessage() + " ; cause: " + e.getCause() + " ; class " + e.getClass());
log.warn("### Exception found while trying to read CarrierPlan: Message: {} ; cause: {} ; class {}", e.getMessage(), e.getCause(), e.getClass());
if (e.getCause().getMessage().contains("cvc-elt.1")) { // "Cannot find the declaration of element" -> exception comes most probably because no validation information was found
log.warn("read with validation = true failed. Try it again without validation... filename: " + filename);
log.warn("read with validation = true failed. Try it again without validation... filename: {}", filename);
reader.setValidating(false);
reader.readFile(filename);
} else { //other problem: e.g. validation does not work, because of missing validation file.
Expand All @@ -72,9 +72,9 @@ public void readURL( URL url ){
try {
reader.readURL(url);
} catch (Exception e) {
log.warn("### Exception found while trying to read CarrierPlan: Message: " + e.getMessage() + " ; cause: " + e.getCause() + " ; class " + e.getClass());
log.warn("### Exception found while trying to read CarrierPlan: Message: {} ; cause: {} ; class {}", e.getMessage(), e.getCause(), e.getClass());
if (e.getCause().getMessage().contains("cvc-elt.1")) { // "Cannot find the declaration of element" -> exception comes most probably because no validation information was found
log.warn("read with validation = true failed. Try it again without validation... url: " + url.toString());
log.warn("read with validation = true failed. Try it again without validation... url: {}", url.toString());
reader.setValidating(false);
reader.readURL(url);
} else { //other problem: e.g. validation does not work, because of missing validation file.
Expand All @@ -89,7 +89,7 @@ public void readStream( InputStream inputStream ){
reader.setValidating(false);
reader.parse(inputStream);
} catch (Exception e) {
log.warn("### Exception found while trying to read CarrierPlan: Message: " + e.getMessage() + " ; cause: " + e.getCause() + " ; class " + e.getClass());
log.warn("### Exception found while trying to read CarrierPlan: Message: {} ; cause: {} ; class {}", e.getMessage(), e.getCause(), e.getClass());
throw e;
}
}
Expand All @@ -110,7 +110,7 @@ private static final class CarriersPlanReader extends MatsimXmlParser {
public void startTag(final String name, final Attributes attributes, final Stack<String> context) {
if ( CARRIERS.equalsIgnoreCase( name ) ) {
String str = attributes.getValue( "xsi:schemaLocation" );
log.info("Found following schemeLocation in carriers definition file: " + str);
log.info("Found following schemeLocation in carriers definition file: {}", str);
if (str == null){
log.warn("Carrier plans file does not contain a valid xsd header. Using CarrierPlanReaderV2.");
delegate = new CarrierPlanXmlParserV2( carriers, carrierVehicleTypes ) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,22 @@ public void write(String filename) {
}
}

private void writeRootElement() throws UncheckedIOException, IOException {
private void writeRootElement() throws UncheckedIOException {
List<Tuple<String, String>> atts = new ArrayList<>();
atts.add(createTuple(XMLNS, MatsimXmlWriter.MATSIM_NAMESPACE));
atts.add(createTuple(XMLNS + ":xsi", DEFAULTSCHEMANAMESPACELOCATION));
atts.add(createTuple("xsi:schemaLocation", MATSIM_NAMESPACE + " " + DEFAULT_DTD_LOCATION + "carriersDefinitions_v2.1.xsd"));
this.writeStartTag(CARRIERS, atts);
}

private void startCarrier(Carrier carrier, BufferedWriter writer)
throws IOException {
private void startCarrier(Carrier carrier, BufferedWriter writer) {
this.writeStartTag(CARRIER, List.of(
createTuple(ID, carrier.getId().toString())), false, true
);
attributesWriter.writeAttributes("\t\t", writer, carrier.getAttributes(), false);
}

private void writeVehiclesAndTheirTypes(Carrier carrier)throws IOException {
private void writeVehiclesAndTheirTypes(Carrier carrier) {
this.writeStartTag(CAPABILITIES, List.of(
createTuple(FLEET_SIZE, carrier.getCarrierCapabilities().getFleetSize().toString())
));
Expand All @@ -142,7 +141,7 @@ private void writeVehiclesAndTheirTypes(Carrier carrier)throws IOException {
this.writeEndTag(CAPABILITIES);
}

private void writeShipments(Carrier carrier, BufferedWriter writer) throws IOException {
private void writeShipments(Carrier carrier, BufferedWriter writer) {
if(carrier.getShipments().isEmpty()) return;
this.writeStartTag(SHIPMENTS, null);
for (CarrierShipment s : carrier.getShipments().values()) {
Expand Down Expand Up @@ -173,7 +172,7 @@ private void writeShipment(CarrierShipment s, Id<CarrierShipment> shipmentId, bo
);
}

private void writeServices(Carrier carrier, BufferedWriter writer) throws IOException {
private void writeServices(Carrier carrier, BufferedWriter writer) {
if(carrier.getServices().isEmpty()) return;
this.writeStartTag(SERVICES, null);
for (CarrierService s : carrier.getServices().values()) {
Expand Down Expand Up @@ -283,8 +282,7 @@ else if (tourElement instanceof Tour.ShipmentBasedActivity act) {
createTuple(SHIPMENT_ID, act.getShipment().getId().toString())), true
);
if (!carrier.getShipments().containsKey(act.getShipment().getId())) {
logger.error("Shipment with id " + act.getShipment().getId().toString() + " is contained in the carriers plan, " +
"but not available in the list of shipments. Carrier with carrierId: " + carrier.getId());
logger.error("Shipment with id {} is contained in the carriers plan, but not available in the list of shipments. Carrier with carrierId: {}", act.getShipment().getId().toString(), carrier.getId());
}
}
else if (tourElement instanceof Tour.ServiceActivity act) {
Expand All @@ -293,8 +291,7 @@ else if (tourElement instanceof Tour.ServiceActivity act) {
createTuple(SERVICE_ID, act.getService().getId().toString())), true
);
if (!carrier.getServices().containsKey(act.getService().getId())) {
logger.error("service with id " + act.getService().getId().toString() + " is contained in the carriers plan, " +
"but not available in the list of services. Carrier with carrierId: " + carrier.getId());
logger.error("service with id {} is contained in the carriers plan, but not available in the list of services. Carrier with carrierId: {}", act.getService().getId().toString(), carrier.getId());
}
}
}
Expand All @@ -308,11 +305,11 @@ else if (tourElement instanceof Tour.ServiceActivity act) {
this.writeEndTag(PLANS);
}

private void endCarrier() throws IOException {
private void endCarrier() {
this.writeEndTag(CARRIER);
}

private void writeEndElement() throws IOException {
private void writeEndElement() {
this.writeEndTag(CARRIERS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static Builder newInstance(Id<CarrierShipment> id, Id<Link> from, Id<Link
}

Id<CarrierShipment> id;
Id<Link> from;
Id<Link> to;
int size;
final Id<Link> from;
final Id<Link> to;
final int size;
TimeWindow pickTW = TimeWindow.newInstance(0.0, Integer.MAX_VALUE);
TimeWindow delTW = TimeWindow.newInstance(0.0, Integer.MAX_VALUE);
double pickServiceTime = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private CarrierVehicleType(){} // do not instantiate
*
*/
public static class Builder {
VehicleType delegate ;
final VehicleType delegate ;

/**
* Returns a new instance of builder initialized with the typeId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void readFile( String filename ){
reader.setValidating(true) ;
reader.readFile( filename );
} catch (Exception e) {
log.warn("### Exception: Message=" + e.getMessage() + " ; cause=" + e.getCause() + " ; class=" + e.getClass());
log.warn("### Exception: Message={} ; cause={} ; class={}", e.getMessage(), e.getCause(), e.getClass());
if (e.getCause().getMessage().contains("cvc-elt.1")) { // "Cannot find the declaration of element" -> exception comes most probably because no validation information was found
log.warn("read with validation = true failed. Try it again without validation. filename: " + filename);
log.warn("read with validation = true failed. Try it again without validation. filename: {}", filename);
reader.setValidating(false);
reader.readFile(filename);
} else { //other problem: e.g. validation does not work, because of missing validation file.
Expand All @@ -78,11 +78,11 @@ public void readURL( URL url ){
reader.setValidating(true) ;
reader.readURL(url);
} catch (Exception e) {
log.warn("### Exception: Message=" + e.getMessage() );
log.warn("### Exception: Cause=" + e.getCause() );
log.warn("### Exception: Class=" + e.getClass() );
log.warn("### Exception: Message={}", e.getMessage());
log.warn("### Exception: Cause={}", e.getCause());
log.warn("### Exception: Class={}", e.getClass());
if (e.getCause().getMessage().contains("cvc-elt.1.a")) { // "Cannot find the declaration of element" -> exception comes most probably because no validation information was found
log.warn("read with validation = true failed. Try it again without validation... url: " + url.toString());
log.warn("read with validation = true failed. Try it again without validation... url: {}", url.toString());
reader.setValidating(false);
reader.readURL(url);
} else { //other problem: e.g. validation does not work, because of missing validation file.
Expand All @@ -97,7 +97,8 @@ public void readStream( InputStream inputStream ){
reader.setValidating(true) ;
reader.parse( inputStream ) ;
} catch (Exception e)
{log.warn("### Exception found while trying to read Carrier Vehicle Type: Message: " + e.getMessage() + " ; cause: " + e.getCause() + " ; class " + e.getClass());
{
log.warn("### Exception found while trying to read Carrier Vehicle Type: Message: {} ; cause: {} ; class {}", e.getMessage(), e.getCause(), e.getClass());
if (e.getCause().getMessage().contains("cvc-elt.1.a")) { // "Cannot find the declaration of element" -> exception comes most probably because no validation information was found
log.warn("read with validation = true failed. Try it again without validation... ");
reader.setValidating(false);
Expand All @@ -121,10 +122,10 @@ private static final class CarrierVehicleTypeParser extends MatsimXmlParser {

@Override
public void startTag(final String name, final Attributes attributes, final Stack<String> context) {
log.debug("Reading start tag. name: " + name + " , attributes: " + attributes.toString() + " , context: " + context);
log.debug("Reading start tag. name: {} , attributes: {} , context: {}", name, attributes.toString(), context);
if ( "vehicleTypes".equalsIgnoreCase( name ) ) {
String str = attributes.getValue( "xsi:schemaLocation" );
log.info("Found following schemeLocation in carriers definition file: " + str);
log.info("Found following schemeLocation in carriers definition file: {}", str);
if (str == null){
log.warn( "No validation information found. Using ReaderV1." );
delegate = new CarrierVehicleTypeReaderV1( vehicleTypes );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void addCarrier(Carrier carrier) {
if(!carriers.containsKey(carrier.getId())){
carriers.put(carrier.getId(), carrier);
}
else log.warn("carrier " + carrier.getId() + " already exists");
else log.warn("carrier {} already exists", carrier.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class CarriersUtils {

Expand Down Expand Up @@ -237,10 +236,10 @@ public static Carriers createShipmentVRPCarrierFromServiceVRPSolution(Carriers c
Carriers carriersWithShipments = new Carriers();
for (Carrier carrier : carriers.getCarriers().values()) {
Carrier carrierWS = createCarrier(carrier.getId());
if (carrier.getShipments().size() > 0) {
if (!carrier.getShipments().isEmpty()) {
copyShipments(carrierWS, carrier);
}
if (carrier.getServices().size() > 0) {
if (!carrier.getServices().isEmpty()) {
createShipmentsFromServices(carrierWS, carrier);
}
carrierWS.setCarrierCapabilities(carrier.getCarrierCapabilities()); // vehicles and other carrierCapabilities
Expand Down Expand Up @@ -314,7 +313,7 @@ public static void loadCarriersAccordingToFreightConfig(Scenario scenario) {
*/
private static void copyShipments(Carrier carrierWS, Carrier carrier) {
for (CarrierShipment carrierShipment : carrier.getShipments().values()) {
log.debug("Copy CarrierShipment: " + carrierShipment.toString());
log.debug("Copy CarrierShipment: {}", carrierShipment.toString());
addShipment(carrierWS, carrierShipment);
}
}
Expand Down Expand Up @@ -349,7 +348,7 @@ private static void createShipmentsFromServices(Carrier carrierWS, Carrier carri
}
}
for (CarrierService carrierService : carrier.getServices().values()) {
log.debug("Converting CarrierService to CarrierShipment: " + carrierService.getId());
log.debug("Converting CarrierService to CarrierShipment: {}", carrierService.getId());
CarrierShipment carrierShipment = CarrierShipment.Builder
.newInstance(Id.create(carrierService.getId().toString(), CarrierShipment.class),
depotServiceIsDeliveredFrom.get(carrierService.getId()), carrierService.getLocationLinkId(),
Expand Down Expand Up @@ -513,7 +512,7 @@ private static void addSkill(Attributes attributes, String skill) {
List<String> skills = convertSkillsAttributeToList(attributes);
if (!skills.contains(skill)) {
String skillString;
if (skills.size() == 0) {
if (skills.isEmpty()) {
skillString = skill;
} else {
skillString = attributes.getAttribute(ATTR_SKILLS) + "," + skill;
Expand Down Expand Up @@ -556,7 +555,7 @@ private static List<String> convertSkillsAttributeToList(Attributes attributes)
}

private static void setSkills(Attributes attributes, Set<String> skills) {
if (skills.size() != 0) {
if (!skills.isEmpty()) {
Iterator<String> skillIterator = skills.iterator();
StringBuilder skillString = new StringBuilder(skillIterator.next());
while (skillIterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ public boolean equals(Object o){
@Override
public int hashCode(){
int result = 59;
long startLong = Double.doubleToLongBits(start);
int startHash = (int) (startLong^(startLong>>>32));
int startHash = Double.hashCode(start);
result = 31 * result + startHash;
long endLong = Double.doubleToLongBits(end);
int endHash = (int) (endLong^(endLong>>>32));
int endHash = Double.hashCode(end);
result = 31 * result + endHash;
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Leg createLeg(Route route, double dep_time, double transportTime) {
@Deprecated
public Builder insertLegAtBeginning(Leg leg) {
Gbl.assertNotNull(leg);
tourElements.add(0,leg);
tourElements.addFirst(leg);
return this;
}

Expand All @@ -183,7 +183,7 @@ public Builder schedulePickupAtBeginning(CarrierShipment shipment) {
}
// assertLastElementIsLeg();
Pickup pickup = createPickup(shipment);
tourElements.add(0, pickup);
tourElements.addFirst(pickup);
// previousElementIsActivity = true;
return this;
}
Expand All @@ -197,7 +197,7 @@ public Builder schedulePickupAtBeginning(CarrierShipment shipment) {
*/
public void schedulePickup(CarrierShipment shipment) {
Gbl.assertNotNull(shipment);
logger.debug("Pickup to get scheduled: " + shipment);
logger.debug("Pickup to get scheduled: {}", shipment);
boolean wasNew = openPickups.add(shipment);
if (!wasNew) {
throw new IllegalStateException("Trying to deliver something which was already picked up.");
Expand All @@ -223,8 +223,8 @@ private void assertLastElementIsLeg() {
*/
public void scheduleDelivery(CarrierShipment shipment) {
Gbl.assertNotNull(shipment);
logger.debug("Delivery to get scheduled: " + shipment);
logger.debug("OpenPickups: " + openPickups);
logger.debug("Delivery to get scheduled: {}", shipment);
logger.debug("OpenPickups: {}", openPickups);
boolean wasOpen = openPickups.remove(shipment);
if (!wasOpen) {
throw new IllegalStateException("Trying to deliver something which was not picked up.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void scoreSelectedPlan() {
}
scoringFunction.finish();
final double score = scoringFunction.getScore();
log.warn("score=" + score);
log.warn("score={}", score);
carrier.getSelectedPlan().setScore( score );
}
void handleEvent(Event event, Id<Person> driverId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,21 @@ final class CarrierDriverAgent{
this.carrier = carrier;
this.events = events;
this.carrierEventCreators = carrierEventCreators;
log.debug( "creating CarrierDriverAgent with driverId=" + driverId );
log.debug("creating CarrierDriverAgent with driverId={}", driverId);
this.driverId = driverId;
this.scheduledTour = tour;
}

void handleAnEvent(Event event){
// the event comes to here from CarrierAgent#handleEvent only for events concerning this driver

if( event instanceof PersonArrivalEvent ){
handleEvent( (PersonArrivalEvent) event);
} else if( event instanceof PersonDepartureEvent ){
handleEvent( (PersonDepartureEvent) event );
} else if( event instanceof LinkEnterEvent ){
handleEvent( (LinkEnterEvent) event );
} else if( event instanceof ActivityEndEvent ){
handleEvent( (ActivityEndEvent) event );
} else if( event instanceof ActivityStartEvent ){
handleEvent( (ActivityStartEvent) event );
} else{
createAdditionalEvents( event, null, scheduledTour, driverId, planElementCounter);
switch (event) {
case PersonArrivalEvent personArrivalEvent -> handleEvent(personArrivalEvent);
case PersonDepartureEvent personDepartureEvent -> handleEvent(personDepartureEvent);
case LinkEnterEvent linkEnterEvent -> handleEvent(linkEnterEvent);
case ActivityEndEvent activityEndEvent -> handleEvent(activityEndEvent);
case ActivityStartEvent activityStartEvent -> handleEvent(activityStartEvent);
case null, default -> createAdditionalEvents(event, null, scheduledTour, driverId, planElementCounter);
}
}

Expand All @@ -119,8 +114,8 @@ private void handleEvent( PersonArrivalEvent event ){
currentRoute = null;
} else{
Id<Link> startLink;
if( currentRoute.size() != 0 ){
startLink = currentRoute.get( 0 );
if(!currentRoute.isEmpty()){
startLink = currentRoute.getFirst();
} else{
startLink = event.getLinkId();
}
Expand Down Expand Up @@ -163,7 +158,7 @@ private void handleEvent( ActivityEndEvent event ){

createAdditionalEvents( event, currentActivity, scheduledTour, driverId, planElementCounter );

log.debug( "handling activity end event=" + event );
log.debug("handling activity end event={}", event);
if( CarrierConstants.START.equals( event.getActType() ) ){
planElementCounter += 1;
return;
Expand Down
Loading

0 comments on commit 31d2c0d

Please sign in to comment.