diff --git a/.gradle/7.1/executionHistory/executionHistory.bin b/.gradle/7.1/executionHistory/executionHistory.bin index 8ec8121..670278b 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 067e119..51fa88b 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 939e378..a1ca05d 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 6594202..dc5caf0 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 eb87a99..2477e21 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 598d5bc..28c9ac1 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$CustomerMessage.class b/build/classes/java/main/com/example/AddressBook$CustomerMessage.class index ab7f89c..50347a3 100644 Binary files a/build/classes/java/main/com/example/AddressBook$CustomerMessage.class and b/build/classes/java/main/com/example/AddressBook$CustomerMessage.class differ diff --git a/build/classes/java/main/com/example/AddressBook.class b/build/classes/java/main/com/example/AddressBook.class index 36c686e..a4b9f9b 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/AkkaMainSystem.class b/build/classes/java/main/com/example/AkkaMainSystem.class index 545af78..fc8f7cb 100644 Binary files a/build/classes/java/main/com/example/AkkaMainSystem.class and b/build/classes/java/main/com/example/AkkaMainSystem.class differ diff --git a/build/classes/java/main/com/example/DistributionCenter$ArriveMessage.class b/build/classes/java/main/com/example/DistributionCenter$ArriveMessage.class index 3328526..24f9bd0 100644 Binary files a/build/classes/java/main/com/example/DistributionCenter$ArriveMessage.class and b/build/classes/java/main/com/example/DistributionCenter$ArriveMessage.class differ diff --git a/build/classes/java/main/com/example/DistributionCenter.class b/build/classes/java/main/com/example/DistributionCenter.class index 26aa05e..09c0359 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 2039ae2..370b12a 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 68c68a2..22361b7 100644 --- a/src/main/java/com/example/AddressBook.java +++ b/src/main/java/com/example/AddressBook.java @@ -8,10 +8,12 @@ import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Random; public class AddressBook extends AbstractBehavior { - ArrayList> customers = new ArrayList<>(); + private final ArrayList> customers; public interface Message {} @@ -28,24 +30,32 @@ public class AddressBook extends AbstractBehavior { } } - private AddressBook(ActorContext context, ArrayList> custo) { - super(context); - if (!custo.isEmpty()) { - this.customers.addAll(custo); - for (ActorRef cst : this.customers) { - cst.tell(new Customer.AddressHello(getContext().getSelf())); - } + // Nachricht, um die Liste der Kunden abzurufen + public static final class GetCustomers implements Message { + public final ActorRef>> replyTo; + + public GetCustomers(ActorRef>> replyTo) { + this.replyTo = replyTo; } } - public static Behavior create(ArrayList> custo) { - return Behaviors.setup(context -> new AddressBook(context, custo)); + private AddressBook(ActorContext context, ArrayList> customers) { + super(context); + this.customers = new ArrayList<>(customers); + for (ActorRef cst : this.customers) { + cst.tell(new Customer.AddressHello(getContext().getSelf())); + } + } + + public static Behavior create(ArrayList> customers) { + return Behaviors.setup(context -> new AddressBook(context, customers)); } @Override public Receive createReceive() { return newReceiveBuilder() .onMessage(CustomerMessage.class, this::onCustomerMessage) + .onMessage(GetCustomers.class, this::onGetCustomers) .build(); } @@ -58,4 +68,10 @@ public class AddressBook extends AbstractBehavior { msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference)); return this; } + + // Methode, um die Liste der Kunden zu senden + private Behavior onGetCustomers(GetCustomers msg) { + msg.replyTo.tell(Collections.unmodifiableList(customers)); + return this; + } } diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 134e8c2..bb8e684 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -26,35 +26,32 @@ public class AkkaMainSystem extends AbstractBehavior { return newReceiveBuilder().onMessage(Create.class, this::onCreate).build(); } - private Behavior onCreate(Create command) { // Erstellen des Adressbuchs ArrayList> customers = new ArrayList<>(); - ActorRef addressBook = getContext().spawn(AddressBook.create(customers), "addressBook"); - // Erstellen von vier Empfänger*innen for (int i = 0; i < 4; i++) { customers.add(getContext().spawn(Customer.create("Customer" + (i + 1)), "customer" + (i + 1))); } + ActorRef addressBook = getContext().spawn(AddressBook.create(customers), "addressBook"); + + // Nachricht GetCustomers senden, um Kundenreferenzen abzurufen // Erstellen des Verteilzentrums - ActorRef distributionCenter = getContext().spawn(DistributionCenter.create(), "distributionCenter"); - + ActorRef distributionCenter = getContext().spawn(DistributionCenter.create(addressBook), "distributionCenter"); +/* // Erstellen von vier Wagen mit zufälligen Routen for (int i = 0; i < 4; i++) { - List> route = new ArrayList<>(customers); + List route = new ArrayList<>(); // Hier sollte die Liste der Orte sein, nicht der Kundenreferenzen Collections.shuffle(route); ActorRef truck = getContext().spawn(DeliveryCar.create(route), "truck" + (i + 1)); // Erstellen einer leeren Paketliste List paketeList = new ArrayList<>(); - paketeList.add(new Paket("Inhalt 1")); - paketeList.add(new Paket("Inhalt 2")); // Beispiel für den Aufruf der ArriveMessage-Nachricht mit korrekten Parametern distributionCenter.tell(new DistributionCenter.ArriveMessage(paketeList, truck)); - } - +*/ return this; } } diff --git a/src/main/java/com/example/DistributionCenter.java b/src/main/java/com/example/DistributionCenter.java index 64dbd3a..c29fdd1 100644 --- a/src/main/java/com/example/DistributionCenter.java +++ b/src/main/java/com/example/DistributionCenter.java @@ -8,10 +8,41 @@ import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class DistributionCenter extends AbstractBehavior { + private final ActorRef addressBook; + private final List lagerraum = new ArrayList<>(); + + private DistributionCenter(ActorContext context, ActorRef addressBook) { + super(context); + this.addressBook = addressBook; + + // Erhalten Sie die replyTo-Referenz aus dem Kontext + ActorRef>> replyTo = context.spawnAnonymous(Behaviors.>>receive((ctx, message) -> { + if (message instanceof List) { + @SuppressWarnings("unchecked") + List> customers = (List>) message; + ctx.getLog().info("Kundenliste erhalten: {}", customers); + // Nachricht senden, um Routen zu generieren + context.getSelf().tell(new GenerateRoutes(customers)); + return Behaviors.same(); + } + return Behaviors.unhandled(); + })); + + // Anfrage an das AddressBook senden, um die Kundenliste abzurufen + addressBook.tell(new AddressBook.GetCustomers(replyTo)); + } + + public static Behavior create(ActorRef addressBook) { + return Behaviors.setup(context -> new DistributionCenter(context, addressBook)); + } + + interface Message {} + public static class ArriveMessage implements Message { private final List pakete; private final ActorRef truck; @@ -30,33 +61,91 @@ public class DistributionCenter extends AbstractBehavior context) { - super(context); + public static class LoadMessage implements Message { + private final List pakete; + + public LoadMessage(List pakete) { + this.pakete = pakete; + } + + public List getPakete() { + return pakete; + } } - interface Message {} + public static class GenerateRoutes implements Message { + private final List> customers; - public static Behavior create() { - return Behaviors.setup(DistributionCenter::new); + public GenerateRoutes(List> customers) { + this.customers = customers; + } + + public List> getCustomers() { + return customers; + } } @Override public Receive createReceive() { return newReceiveBuilder() .onMessage(ArriveMessage.class, this::onArriveMessage) + .onMessage(LoadMessage.class, this::onLoadMessage) + .onMessage(GenerateRoutes.class, this::onGenerateRoutes) .build(); } private Behavior onArriveMessage(ArriveMessage msg) { // Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu - List pakete = msg.getPakete(); - getContext().getLog().info("Angekommene Pakete: {}", pakete); + List arrivedPackages = msg.getPakete(); + lagerraum.addAll(arrivedPackages); + getContext().getLog().info("Angekommene Pakete: {}", arrivedPackages); - // Hier kann die weitere Logik für die Behandlung der Pakete implementiert werden + // Entferne zufällig 3 Pakete aus dem Lagerraum und sende sie dem angekommenen Wagen + int numPackagesToSend = Math.min(3, lagerraum.size()); + List packagesToSend = new ArrayList<>(); + for (int i = 0; i < numPackagesToSend; i++) { + packagesToSend.add(lagerraum.remove(0)); + } + msg.getTruck().tell(new DeliveryCar.LoadMessage(packagesToSend)); - // Beispiel: Senden der Pakete an einen internen Akteur zur weiteren Verarbeitung - // ... + // Ausgabe, wie viele Pakete nun im Verteilzentrum vorhanden sind + getContext().getLog().info("Pakete im Verteilzentrum nach Laden: {}", lagerraum.size()); return this; } + + private Behavior onLoadMessage(LoadMessage msg) { + // Ausgabe, wie viele Pakete nun im Verteilzentrum vorhanden sind + getContext().getLog().info("Pakete im Verteilzentrum nach Laden: {}", lagerraum.size()); + + return this; + } + + private Behavior onGenerateRoutes(GenerateRoutes msg) { + List> customers = msg.getCustomers(); + generateRoutes(customers); + return this; + } + + private void generateRoutes(List> customers) { + // Überprüfen, ob die Kundenliste null oder leer ist + if (customers == null || customers.isEmpty()) { + getContext().getLog().info("Kundenliste ist leer oder null. Keine Routen generiert."); + return; + } + + // Erstelle vier Lastwagen mit zufälligen Routen + for (int i = 0; i < 4; i++) { + List> route = generateRandomRoute(customers); + getContext().spawn(DeliveryCar.create(route), "deliveryCar" + i); + getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i, route); + } + } + + // Dummy-Methode zum Generieren einer zufälligen Route + private List> generateRandomRoute(List> customers) { + List> route = new ArrayList<>(customers); + Collections.shuffle(route); + return route; + } }