Timer fixed and no locks

This commit is contained in:
olhag 2024-05-19 13:12:44 +02:00
parent 4978d4dccf
commit ef94068bc3
17 changed files with 37 additions and 65 deletions

Binary file not shown.

View File

@ -1,3 +1,4 @@
// DeliveryCar.java
package com.example; package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
@ -50,74 +51,61 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
private Behavior<Message> onLoadMessage(LoadMessage msg) { private Behavior<Message> onLoadMessage(LoadMessage msg) {
pakete.addAll(msg.pakete); pakete.addAll(msg.pakete);
scheduleNextStopCheck(); getContext().getLog().info("Geladen {} Pakete in den LKW.", msg.pakete.size());
scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds
return this; return this;
} }
private Behavior<Message> onPickupResponse(PickupResponse rsp) { private Behavior<Message> onPickupResponse(PickupResponse rsp) {
if (rsp.paket == null || rsp.paket.inhalt == null) { if (rsp.paket != null && rsp.paket.inhalt != null && pakete.size() < maxCapacity) {
scheduleNextStopCheck(); pakete.add(rsp.paket);
} else { getContext().getLog().info("Ein Paket abgeholt. Aktuelle Anzahl der Pakete im LKW: {}", pakete.size());
if (pakete.size() < maxCapacity) {
pakete.add(rsp.paket);
getContext().getLog().info("Current number of packages in the truck: {}", pakete.size());
}
scheduleNextStopCheck();
} }
scheduleNextStopCheck(Duration.ofSeconds(1)); // Move to the next stop after 1 second
return this; return this;
} }
private Behavior<Message> onCheckStop(CheckStop stop) { private Behavior<Message> onCheckStop(CheckStop stop) {
if (globalIndex >= route.size()) { if (globalIndex >= route.size()) {
getContext().getLog().info("Car is returning to the distribution center."); getContext().getLog().info("Der LKW kehrt zum Verteilzentrum zurück.");
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 {
getContext().getLog().info("I am at {}", route.get(globalIndex).path().name()); ActorRef<Customer.Message> currentStop = route.get(globalIndex);
if (pakete.size() >= maxCapacity) { getContext().getLog().info("Ich bin bei {}", currentStop.path().name());
deliverPackages(); deliverPackages(currentStop);
} else { if (pakete.size() < maxCapacity) {
sendPickupMessage(); sendPickupMessage(currentStop);
} }
globalIndex++; globalIndex++;
scheduleNextStopCheck(pakete.size() >= maxCapacity ? Duration.ofSeconds(1) : Duration.ofSeconds(3));
} }
scheduleNextStopCheck();
return this; return this;
} }
private Behavior<Message> onStartRoute(StartRoute msg) { private Behavior<Message> onStartRoute(StartRoute msg) {
globalIndex = 0; globalIndex = 0;
scheduleNextStopCheck(); scheduleNextStopCheck(Duration.ofSeconds(3)); // Start checking the first stop after 3 seconds
return this; return this;
} }
private void sendPickupMessage() { private void sendPickupMessage(ActorRef<Customer.Message> customer) {
if (globalIndex < route.size()) { customer.tell(new Customer.PickUpMessage(getContext().getSelf(), "LKW kann Paket abholen"));
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Car can pick up package"));
} else {
getContext().getLog().info("No more customers in the route.");
}
} }
private void deliverPackages() { private void deliverPackages(ActorRef<Customer.Message> customer) {
if (globalIndex < route.size()) { pakete.removeIf(paket -> {
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex); if (paket.empfaenger.equals(customer)) {
for (int i = 0; i < pakete.size(); i++) { customer.tell(new Customer.DeliveryMessage(paket));
Paket paket = pakete.get(i); return true;
if (paket.empfaenger == nextCustomer) {
nextCustomer.tell(new Customer.DeliveryMessage(paket));
pakete.remove(i);
i--; // Adjust index since an element is removed
}
} }
} return false;
});
} }
private void scheduleNextStopCheck() { private void scheduleNextStopCheck(Duration duration) {
timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3)); timers.startSingleTimer(new CheckStop(), duration);
} }
} }

View File

@ -1,3 +1,4 @@
// DistributionCenter.java
package com.example; package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
@ -19,7 +20,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
private final List<Paket> lagerraum = new ArrayList<>(); private final List<Paket> lagerraum = new ArrayList<>();
private final List<ActorRef<DeliveryCar.Message>> deliveryCars = new ArrayList<>(); private final List<ActorRef<DeliveryCar.Message>> deliveryCars = new ArrayList<>();
private DistributionCenter(ActorContext<Message> context, ActorRef<AddressBook.Message> addressBook) { private DistributionCenter(ActorContext<Message> context, ActorRef<AddressBook.Message> addressBook) {
super(context); super(context);
this.addressBook = addressBook; this.addressBook = addressBook;
@ -65,8 +65,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
} }
} }
public static class GenerateRoutes implements Message { public static class GenerateRoutes implements Message {
private final List<ActorRef<Customer.Message>> customers; private final List<ActorRef<Customer.Message>> customers;
@ -92,14 +90,7 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu // Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
List<Paket> arrivedPackages = msg.getPakete(); List<Paket> arrivedPackages = msg.getPakete();
lagerraum.addAll(arrivedPackages); lagerraum.addAll(arrivedPackages);
String arrivedPackagesString = ""; getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackages.size(), lagerraum.size());
for (int i = 0; i < arrivedPackages.size(); i++) {
arrivedPackagesString += arrivedPackages.get(i).inhalt;
if (i < arrivedPackages.size() - 1) {
arrivedPackagesString += ", "; // Fügen Sie ein Trennzeichen hinzu, außer für das letzte Paket
}
}
getContext().getLog().info("Angekommene Pakete: {} Anz Packeten in Lager: {}", arrivedPackagesString, lagerraum.size());
// 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());
@ -115,8 +106,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
return this; return this;
} }
private Behavior<Message> onGenerateRoutes(GenerateRoutes msg) { private Behavior<Message> onGenerateRoutes(GenerateRoutes msg) {
List<ActorRef<Customer.Message>> customers = msg.getCustomers(); List<ActorRef<Customer.Message>> customers = msg.getCustomers();
generateRoutes(customers); generateRoutes(customers);
@ -124,32 +113,28 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
} }
private void generateRoutes(List<ActorRef<Customer.Message>> customers) { private void generateRoutes(List<ActorRef<Customer.Message>> customers) {
// Überprüfen, ob die Kundenliste null oder leer ist
if (customers == null || customers.isEmpty()) { if (customers == null || customers.isEmpty()) {
getContext().getLog().info("Kundenliste ist leer oder null. Keine Routen generiert."); getContext().getLog().info("Kundenliste ist leer oder null. Keine Routen generiert.");
return; return;
} }
// Erstelle vier Lastwagen mit zufälligen Routen for (int i = 0; i < 4; i++) { // Erstelle vier Lieferwagen
for (int i = 0; i < 2; i++) {
/*List<ActorRef<Customer.Message>> route = generateRandomRoute(customers);
getContext().spawn(DeliveryCar.create(route), "deliveryCar" + i+1);
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i+1, route.toString());*/
List<ActorRef<Customer.Message>> route = generateRandomRoute(customers); List<ActorRef<Customer.Message>> route = generateRandomRoute(customers);
ActorRef<DeliveryCar.Message> deliveryCar = getContext().spawn(DeliveryCar.create(route), "deliveryCar" + (i + 1)); ActorRef<DeliveryCar.Message> deliveryCar = getContext().spawn(DeliveryCar.create(route), "deliveryCar" + (i + 1));
deliveryCars.add(deliveryCar);
String RouteStr = ""; StringBuilder routeStr = new StringBuilder();
for (int j = 0; j < route.size(); j++) { for (int j = 0; j < route.size(); j++) {
RouteStr += route.get(j).path().name(); routeStr.append(route.get(j).path().name());
if (j < route.size() - 1) { if (j < route.size() - 1) {
RouteStr += ", "; // Fügen Sie ein Trennzeichen hinzu, außer für das letzte Paket routeStr.append(", ");
} }
} }
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, RouteStr); getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, routeStr);
deliveryCar.tell(new DeliveryCar.StartRoute()); deliveryCar.tell(new DeliveryCar.StartRoute());
} }
} }
private Behavior<Message> onStartAllRoutes(StartAllRoutes msg) { private Behavior<Message> onStartAllRoutes(StartAllRoutes msg) {
for (ActorRef<DeliveryCar.Message> deliveryCar : deliveryCars) { for (ActorRef<DeliveryCar.Message> deliveryCar : deliveryCars) {
deliveryCar.tell(new DeliveryCar.StartRoute()); deliveryCar.tell(new DeliveryCar.StartRoute());
@ -157,10 +142,9 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
return this; return this;
} }
// Dummy-Methode zum Generieren einer zufälligen Route
private List<ActorRef<Customer.Message>> generateRandomRoute(List<ActorRef<Customer.Message>> customers) { private List<ActorRef<Customer.Message>> generateRandomRoute(List<ActorRef<Customer.Message>> customers) {
List<ActorRef<Customer.Message>> route = new ArrayList<>(customers); List<ActorRef<Customer.Message>> route = new ArrayList<>(customers);
Collections.shuffle(route); Collections.shuffle(route);
return route; return route;
} }
} }