Fixed problem with onArriveMessage:arrivedPackages

This commit is contained in:
olhag 2024-05-19 14:18:53 +02:00
parent ef94068bc3
commit 0e27974ea4
17 changed files with 14 additions and 8 deletions

View File

@ -1,4 +1,3 @@
// DeliveryCar.java
package com.example; package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
@ -29,6 +28,8 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
public record CheckStop() implements Message {} public record CheckStop() implements Message {}
public record Clear() implements Message {} // Clear all packages in the delivery car
private DeliveryCar(ActorContext<Message> context, TimerScheduler<Message> timers, ArrayList<ActorRef<Customer.Message>> route) { private DeliveryCar(ActorContext<Message> context, TimerScheduler<Message> timers, ArrayList<ActorRef<Customer.Message>> route) {
super(context); super(context);
this.route = route; this.route = route;
@ -46,12 +47,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
.onMessage(LoadMessage.class, this::onLoadMessage) .onMessage(LoadMessage.class, this::onLoadMessage)
.onMessage(StartRoute.class, this::onStartRoute) .onMessage(StartRoute.class, this::onStartRoute)
.onMessage(CheckStop.class, this::onCheckStop) .onMessage(CheckStop.class, this::onCheckStop)
.onMessage(Clear.class, this::onClear)
.build(); .build();
} }
private Behavior<Message> onLoadMessage(LoadMessage msg) { private Behavior<Message> onLoadMessage(LoadMessage msg) {
pakete.addAll(msg.pakete); pakete.addAll(msg.pakete);
getContext().getLog().info("Geladen {} Pakete in den LKW.", msg.pakete.size()); getContext().getLog().info("Laden: {} Pakete wurden in den LKW geladen.", msg.pakete.size());
scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds
return this; return this;
} }
@ -59,7 +61,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
private Behavior<Message> onPickupResponse(PickupResponse rsp) { private Behavior<Message> onPickupResponse(PickupResponse rsp) {
if (rsp.paket != null && rsp.paket.inhalt != null && pakete.size() < maxCapacity) { if (rsp.paket != null && rsp.paket.inhalt != null && pakete.size() < maxCapacity) {
pakete.add(rsp.paket); pakete.add(rsp.paket);
getContext().getLog().info("Ein Paket abgeholt. Aktuelle Anzahl der Pakete im LKW: {}", pakete.size()); getContext().getLog().info("Abholung: Ein Paket wurde aufgenommen. Aktuelle Anzahl der Pakete im LKW: {}", pakete.size());
} }
scheduleNextStopCheck(Duration.ofSeconds(1)); // Move to the next stop after 1 second scheduleNextStopCheck(Duration.ofSeconds(1)); // Move to the next stop after 1 second
return this; return this;
@ -67,14 +69,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
private Behavior<Message> onCheckStop(CheckStop stop) { private Behavior<Message> onCheckStop(CheckStop stop) {
if (globalIndex >= route.size()) { if (globalIndex >= route.size()) {
getContext().getLog().info("Der LKW kehrt zum Verteilzentrum zurück."); getContext().getLog().info("Rückkehr: Der LKW kehrt zum Verteilzentrum zurück mit {} Paketen.", pakete.size());
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf()); DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
getDistributionCenter().tell(antwort); getDistributionCenter().tell(antwort);
pakete.clear();
globalIndex = 0; globalIndex = 0;
} else { } else {
ActorRef<Customer.Message> currentStop = route.get(globalIndex); ActorRef<Customer.Message> currentStop = route.get(globalIndex);
getContext().getLog().info("Ich bin bei {}", currentStop.path().name()); getContext().getLog().info("Aktuelle Haltestelle: {}", currentStop.path().name());
deliverPackages(currentStop); deliverPackages(currentStop);
if (pakete.size() < maxCapacity) { if (pakete.size() < maxCapacity) {
sendPickupMessage(currentStop); sendPickupMessage(currentStop);
@ -91,8 +92,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
return this; return this;
} }
private Behavior<Message> onClear(Clear clear) {
pakete.clear();
return this;
}
private void sendPickupMessage(ActorRef<Customer.Message> customer) { private void sendPickupMessage(ActorRef<Customer.Message> customer) {
customer.tell(new Customer.PickUpMessage(getContext().getSelf(), "LKW kann Paket abholen")); customer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Der LKW kann das Paket abholen."));
} }
private void deliverPackages(ActorRef<Customer.Message> customer) { private void deliverPackages(ActorRef<Customer.Message> customer) {

View File

@ -1,4 +1,3 @@
// DistributionCenter.java
package com.example; package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
@ -91,6 +90,7 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
List<Paket> arrivedPackages = msg.getPakete(); List<Paket> arrivedPackages = msg.getPakete();
lagerraum.addAll(arrivedPackages); lagerraum.addAll(arrivedPackages);
getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackages.size(), lagerraum.size()); getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackages.size(), lagerraum.size());
msg.getTruck().tell(new DeliveryCar.Clear());
// Entferne zufällig 3 Pakete aus dem Lagerraum und sende sie dem angekommenen Wagen // Entferne zufällig 3 Pakete aus dem Lagerraum und sende sie dem angekommenen Wagen
int numPackagesToSend = Math.min(3, lagerraum.size()); int numPackagesToSend = Math.min(3, lagerraum.size());