Fixed problem with onArriveMessage:arrivedPackages
This commit is contained in:
parent
ef94068bc3
commit
0e27974ea4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,3 @@
|
|||
// DeliveryCar.java
|
||||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
|
@ -29,6 +28,8 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.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) {
|
||||
super(context);
|
||||
this.route = route;
|
||||
|
@ -46,12 +47,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(StartRoute.class, this::onStartRoute)
|
||||
.onMessage(CheckStop.class, this::onCheckStop)
|
||||
.onMessage(Clear.class, this::onClear)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onLoadMessage(LoadMessage msg) {
|
||||
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
|
||||
return this;
|
||||
}
|
||||
|
@ -59,7 +61,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||
if (rsp.paket != null && rsp.paket.inhalt != null && pakete.size() < maxCapacity) {
|
||||
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
|
||||
return this;
|
||||
|
@ -67,14 +69,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
|
||||
private Behavior<Message> onCheckStop(CheckStop stop) {
|
||||
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());
|
||||
getDistributionCenter().tell(antwort);
|
||||
pakete.clear();
|
||||
globalIndex = 0;
|
||||
} else {
|
||||
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);
|
||||
if (pakete.size() < maxCapacity) {
|
||||
sendPickupMessage(currentStop);
|
||||
|
@ -91,8 +92,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
return this;
|
||||
}
|
||||
|
||||
private Behavior<Message> onClear(Clear clear) {
|
||||
pakete.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// DistributionCenter.java
|
||||
package com.example;
|
||||
|
||||
import akka.actor.typed.ActorRef;
|
||||
|
@ -91,6 +90,7 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
List<Paket> arrivedPackages = msg.getPakete();
|
||||
lagerraum.addAll(arrivedPackages);
|
||||
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
|
||||
int numPackagesToSend = Math.min(3, lagerraum.size());
|
||||
|
|
Loading…
Reference in New Issue