diff --git a/.gradle/7.1/executionHistory/executionHistory.bin b/.gradle/7.1/executionHistory/executionHistory.bin index b896371..a6c89ef 100644 Binary files a/.gradle/7.1/executionHistory/executionHistory.bin and b/.gradle/7.1/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.1/executionHistory/executionHistory.lock b/.gradle/7.1/executionHistory/executionHistory.lock index 5829810..b99f05d 100644 Binary files a/.gradle/7.1/executionHistory/executionHistory.lock and b/.gradle/7.1/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.1/fileHashes/fileHashes.bin b/.gradle/7.1/fileHashes/fileHashes.bin index 3f0eedc..1117e74 100644 Binary files a/.gradle/7.1/fileHashes/fileHashes.bin and b/.gradle/7.1/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.1/fileHashes/fileHashes.lock b/.gradle/7.1/fileHashes/fileHashes.lock index db69a43..f00e982 100644 Binary files a/.gradle/7.1/fileHashes/fileHashes.lock and b/.gradle/7.1/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index e57ca00..450cc19 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/checksums/checksums.lock b/.gradle/checksums/checksums.lock index 28c9ac1..c590545 100644 Binary files a/.gradle/checksums/checksums.lock and b/.gradle/checksums/checksums.lock differ diff --git a/build/classes/java/main/com/example/AddressBook.class b/build/classes/java/main/com/example/AddressBook.class index a4b9f9b..ef0e26a 100644 Binary files a/build/classes/java/main/com/example/AddressBook.class and b/build/classes/java/main/com/example/AddressBook.class differ diff --git a/build/classes/java/main/com/example/Customer.class b/build/classes/java/main/com/example/Customer.class index 8653b7a..6731e3f 100644 Binary files a/build/classes/java/main/com/example/Customer.class and b/build/classes/java/main/com/example/Customer.class differ diff --git a/build/classes/java/main/com/example/DeliveryCar$LoadMessage.class b/build/classes/java/main/com/example/DeliveryCar$LoadMessage.class index 3018329..1987861 100644 Binary files a/build/classes/java/main/com/example/DeliveryCar$LoadMessage.class and b/build/classes/java/main/com/example/DeliveryCar$LoadMessage.class differ diff --git a/build/classes/java/main/com/example/DeliveryCar$PickupResponse.class b/build/classes/java/main/com/example/DeliveryCar$PickupResponse.class index 0ee5351..6a5bf7c 100644 Binary files a/build/classes/java/main/com/example/DeliveryCar$PickupResponse.class and b/build/classes/java/main/com/example/DeliveryCar$PickupResponse.class differ diff --git a/build/classes/java/main/com/example/DeliveryCar.class b/build/classes/java/main/com/example/DeliveryCar.class index 6569f68..41bbc19 100644 Binary files a/build/classes/java/main/com/example/DeliveryCar.class and b/build/classes/java/main/com/example/DeliveryCar.class differ diff --git a/build/classes/java/main/com/example/DistributionCenter$GenerateRoutes.class b/build/classes/java/main/com/example/DistributionCenter$GenerateRoutes.class index 2740fae..d209414 100644 Binary files a/build/classes/java/main/com/example/DistributionCenter$GenerateRoutes.class and b/build/classes/java/main/com/example/DistributionCenter$GenerateRoutes.class differ diff --git a/build/classes/java/main/com/example/DistributionCenter$LoadMessage.class b/build/classes/java/main/com/example/DistributionCenter$LoadMessage.class deleted file mode 100644 index 0527f4b..0000000 Binary files a/build/classes/java/main/com/example/DistributionCenter$LoadMessage.class and /dev/null differ diff --git a/build/classes/java/main/com/example/DistributionCenter.class b/build/classes/java/main/com/example/DistributionCenter.class index fbdba7e..0938943 100644 Binary files a/build/classes/java/main/com/example/DistributionCenter.class and b/build/classes/java/main/com/example/DistributionCenter.class differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index b6333b9..03bfb24 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/com/example/AddressBook.java b/src/main/java/com/example/AddressBook.java index 3eea797..244cb7f 100644 --- a/src/main/java/com/example/AddressBook.java +++ b/src/main/java/com/example/AddressBook.java @@ -74,4 +74,4 @@ public class AddressBook extends AbstractBehavior { msg.replyTo.tell(Collections.unmodifiableList(customers)); return this; } -} +} \ No newline at end of file diff --git a/src/main/java/com/example/DeliveryCar.java b/src/main/java/com/example/DeliveryCar.java index dc021e7..135e7ec 100644 --- a/src/main/java/com/example/DeliveryCar.java +++ b/src/main/java/com/example/DeliveryCar.java @@ -2,11 +2,9 @@ package com.example; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; -import akka.actor.typed.javadsl.AbstractBehavior; -import akka.actor.typed.javadsl.ActorContext; -import akka.actor.typed.javadsl.Behaviors; -import akka.actor.typed.javadsl.Receive; +import akka.actor.typed.javadsl.*; +import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -17,6 +15,7 @@ public class DeliveryCar extends AbstractBehavior { private final ArrayList pakete = new ArrayList<>(); private final ArrayList> route; private final int maxCapacity = 3; + private final TimerScheduler timers; private int globalIndex = 0; public interface Message {} @@ -27,13 +26,16 @@ public class DeliveryCar extends AbstractBehavior { public record StartRoute() implements Message {} - private DeliveryCar(ActorContext context, ArrayList> route) { + public record CheckStop() implements Message {} + + private DeliveryCar(ActorContext context, TimerScheduler timers, ArrayList> route) { super(context); this.route = route; + this.timers = timers; } public static Behavior create(List> route) { - return Behaviors.setup(context -> new DeliveryCar(context, new ArrayList<>(route))); + return Behaviors.setup(context -> Behaviors.withTimers(timers -> new DeliveryCar(context, timers, new ArrayList<>(route)))); } @Override @@ -42,62 +44,62 @@ public class DeliveryCar extends AbstractBehavior { .onMessage(PickupResponse.class, this::onPickupResponse) .onMessage(LoadMessage.class, this::onLoadMessage) .onMessage(StartRoute.class, this::onStartRoute) + .onMessage(CheckStop.class, this::onCheckStop) .build(); } private Behavior onLoadMessage(LoadMessage msg) { pakete.addAll(msg.pakete); - // Nach dem Laden der Pakete zum nächsten Kunden fahren - sendPickupMessage(); + scheduleNextStopCheck(); return this; } private Behavior onPickupResponse(PickupResponse rsp) { if (rsp.paket == null || rsp.paket.inhalt == null) { - globalIndex++; - checkNextStop(); + scheduleNextStopCheck(); } else { if (pakete.size() < maxCapacity) { pakete.add(rsp.paket); - getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size()); + getContext().getLog().info("Current number of packages in the truck: {}", pakete.size()); } - globalIndex++; - checkNextStop(); + scheduleNextStopCheck(); } + return this; } - private void checkNextStop() { - getContext().getLog().info("globalIndex {}", globalIndex); + private Behavior onCheckStop(CheckStop stop) { if (globalIndex >= route.size()) { - getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück."); + getContext().getLog().info("Car is returning to the distribution center."); DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf()); getDistributionCenter().tell(antwort); - // Setze globalIndex auf 0, damit der Wagen weitermachen kann globalIndex = 0; - return; } else { - getContext().getLog().info("ich bin bei {}", route.get(globalIndex).path().name()); + getContext().getLog().info("I am at {}", route.get(globalIndex).path().name()); if (pakete.size() >= maxCapacity) { deliverPackages(); } else { sendPickupMessage(); } + globalIndex++; } + scheduleNextStopCheck(); + return this; } + private Behavior onStartRoute(StartRoute msg) { globalIndex = 0; - checkNextStop(); + scheduleNextStopCheck(); return this; } private void sendPickupMessage() { if (globalIndex < route.size()) { ActorRef nextCustomer = route.get(globalIndex); - nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen")); + nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Car can pick up package")); } else { - getContext().getLog().info("Keine weiteren Kunden in der Route."); + getContext().getLog().info("No more customers in the route."); } } @@ -109,9 +111,13 @@ public class DeliveryCar extends AbstractBehavior { if (paket.empfaenger == nextCustomer) { nextCustomer.tell(new Customer.DeliveryMessage(paket)); pakete.remove(i); - i--; // Index anpassen, da ein Element entfernt wurde + i--; // Adjust index since an element is removed } } } } + + private void scheduleNextStopCheck() { + timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3)); + } }