Erstelle Verteilzentrum mit vier Wagen, von denen jeder eine zufällige Route hat.
This commit is contained in:
parent
1e0683bfea
commit
b4ae4d671a
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.
|
@ -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<AddressBook.Message> {
|
||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||
private final ArrayList<ActorRef<Customer.Message>> customers;
|
||||
|
||||
public interface Message {}
|
||||
|
||||
|
@ -28,24 +30,32 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
|||
}
|
||||
}
|
||||
|
||||
private AddressBook(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> custo) {
|
||||
// Nachricht, um die Liste der Kunden abzurufen
|
||||
public static final class GetCustomers implements Message {
|
||||
public final ActorRef<List<ActorRef<Customer.Message>>> replyTo;
|
||||
|
||||
public GetCustomers(ActorRef<List<ActorRef<Customer.Message>>> replyTo) {
|
||||
this.replyTo = replyTo;
|
||||
}
|
||||
}
|
||||
|
||||
private AddressBook(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> customers) {
|
||||
super(context);
|
||||
if (!custo.isEmpty()) {
|
||||
this.customers.addAll(custo);
|
||||
this.customers = new ArrayList<>(customers);
|
||||
for (ActorRef<Customer.Message> cst : this.customers) {
|
||||
cst.tell(new Customer.AddressHello(getContext().getSelf()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Behavior<Message> create(ArrayList<ActorRef<Customer.Message>> custo) {
|
||||
return Behaviors.setup(context -> new AddressBook(context, custo));
|
||||
public static Behavior<Message> create(ArrayList<ActorRef<Customer.Message>> customers) {
|
||||
return Behaviors.setup(context -> new AddressBook(context, customers));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(CustomerMessage.class, this::onCustomerMessage)
|
||||
.onMessage(GetCustomers.class, this::onGetCustomers)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -58,4 +68,10 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
|||
msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference));
|
||||
return this;
|
||||
}
|
||||
|
||||
// Methode, um die Liste der Kunden zu senden
|
||||
private Behavior<Message> onGetCustomers(GetCustomers msg) {
|
||||
msg.replyTo.tell(Collections.unmodifiableList(customers));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,35 +26,32 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build();
|
||||
}
|
||||
|
||||
|
||||
private Behavior<Create> onCreate(Create command) {
|
||||
|
||||
// Erstellen des Adressbuchs
|
||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||
ActorRef<AddressBook.Message> 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.Message> addressBook = getContext().spawn(AddressBook.create(customers), "addressBook");
|
||||
|
||||
// Nachricht GetCustomers senden, um Kundenreferenzen abzurufen
|
||||
|
||||
// Erstellen des Verteilzentrums
|
||||
ActorRef<DistributionCenter.Message> distributionCenter = getContext().spawn(DistributionCenter.create(), "distributionCenter");
|
||||
|
||||
ActorRef<DistributionCenter.Message> distributionCenter = getContext().spawn(DistributionCenter.create(addressBook), "distributionCenter");
|
||||
/*
|
||||
// Erstellen von vier Wagen mit zufälligen Routen
|
||||
for (int i = 0; i < 4; i++) {
|
||||
List<ActorRef<Customer.Message>> route = new ArrayList<>(customers);
|
||||
List<String> route = new ArrayList<>(); // Hier sollte die Liste der Orte sein, nicht der Kundenreferenzen
|
||||
Collections.shuffle(route);
|
||||
ActorRef<DeliveryCar.Message> truck = getContext().spawn(DeliveryCar.create(route), "truck" + (i + 1));
|
||||
// Erstellen einer leeren Paketliste
|
||||
List<Paket> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<DistributionCenter.Message> {
|
||||
|
||||
private final ActorRef<AddressBook.Message> addressBook;
|
||||
private final List<Paket> lagerraum = new ArrayList<>();
|
||||
|
||||
private DistributionCenter(ActorContext<Message> context, ActorRef<AddressBook.Message> addressBook) {
|
||||
super(context);
|
||||
this.addressBook = addressBook;
|
||||
|
||||
// Erhalten Sie die replyTo-Referenz aus dem Kontext
|
||||
ActorRef<List<ActorRef<Customer.Message>>> replyTo = context.spawnAnonymous(Behaviors.<List<ActorRef<Customer.Message>>>receive((ctx, message) -> {
|
||||
if (message instanceof List) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ActorRef<Customer.Message>> customers = (List<ActorRef<Customer.Message>>) 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<Message> create(ActorRef<AddressBook.Message> addressBook) {
|
||||
return Behaviors.setup(context -> new DistributionCenter(context, addressBook));
|
||||
}
|
||||
|
||||
interface Message {}
|
||||
|
||||
public static class ArriveMessage implements Message {
|
||||
private final List<Paket> pakete;
|
||||
private final ActorRef<DeliveryCar.Message> truck;
|
||||
|
@ -30,33 +61,91 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
}
|
||||
}
|
||||
|
||||
private DistributionCenter(ActorContext<Message> context) {
|
||||
super(context);
|
||||
public static class LoadMessage implements Message {
|
||||
private final List<Paket> pakete;
|
||||
|
||||
public LoadMessage(List<Paket> pakete) {
|
||||
this.pakete = pakete;
|
||||
}
|
||||
|
||||
interface Message {}
|
||||
public List<Paket> getPakete() {
|
||||
return pakete;
|
||||
}
|
||||
}
|
||||
|
||||
public static Behavior<Message> create() {
|
||||
return Behaviors.setup(DistributionCenter::new);
|
||||
public static class GenerateRoutes implements Message {
|
||||
private final List<ActorRef<Customer.Message>> customers;
|
||||
|
||||
public GenerateRoutes(List<ActorRef<Customer.Message>> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
public List<ActorRef<Customer.Message>> getCustomers() {
|
||||
return customers;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(ArriveMessage.class, this::onArriveMessage)
|
||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(GenerateRoutes.class, this::onGenerateRoutes)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Behavior<Message> onArriveMessage(ArriveMessage msg) {
|
||||
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
|
||||
List<Paket> pakete = msg.getPakete();
|
||||
getContext().getLog().info("Angekommene Pakete: {}", pakete);
|
||||
List<Paket> 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<Paket> 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<Message> 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<Message> onGenerateRoutes(GenerateRoutes msg) {
|
||||
List<ActorRef<Customer.Message>> customers = msg.getCustomers();
|
||||
generateRoutes(customers);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void generateRoutes(List<ActorRef<Customer.Message>> 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<ActorRef<Customer.Message>> 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<ActorRef<Customer.Message>> generateRandomRoute(List<ActorRef<Customer.Message>> customers) {
|
||||
List<ActorRef<Customer.Message>> route = new ArrayList<>(customers);
|
||||
Collections.shuffle(route);
|
||||
return route;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue