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 akka.actor.typed.javadsl.Receive;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
||||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
private final ArrayList<ActorRef<Customer.Message>> customers;
|
||||||
|
|
||||||
public interface Message {}
|
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
|
||||||
super(context);
|
public static final class GetCustomers implements Message {
|
||||||
if (!custo.isEmpty()) {
|
public final ActorRef<List<ActorRef<Customer.Message>>> replyTo;
|
||||||
this.customers.addAll(custo);
|
|
||||||
for (ActorRef<Customer.Message> cst : this.customers) {
|
public GetCustomers(ActorRef<List<ActorRef<Customer.Message>>> replyTo) {
|
||||||
cst.tell(new Customer.AddressHello(getContext().getSelf()));
|
this.replyTo = replyTo;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Behavior<Message> create(ArrayList<ActorRef<Customer.Message>> custo) {
|
private AddressBook(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> customers) {
|
||||||
return Behaviors.setup(context -> new AddressBook(context, custo));
|
super(context);
|
||||||
|
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>> customers) {
|
||||||
|
return Behaviors.setup(context -> new AddressBook(context, customers));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Receive<Message> createReceive() {
|
public Receive<Message> createReceive() {
|
||||||
return newReceiveBuilder()
|
return newReceiveBuilder()
|
||||||
.onMessage(CustomerMessage.class, this::onCustomerMessage)
|
.onMessage(CustomerMessage.class, this::onCustomerMessage)
|
||||||
|
.onMessage(GetCustomers.class, this::onGetCustomers)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,4 +68,10 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
||||||
msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference));
|
msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference));
|
||||||
return this;
|
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();
|
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Behavior<Create> onCreate(Create command) {
|
private Behavior<Create> onCreate(Create command) {
|
||||||
|
|
||||||
// Erstellen des Adressbuchs
|
// Erstellen des Adressbuchs
|
||||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||||
ActorRef<AddressBook.Message> addressBook = getContext().spawn(AddressBook.create(customers), "addressBook");
|
|
||||||
|
|
||||||
// Erstellen von vier Empfänger*innen
|
// Erstellen von vier Empfänger*innen
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
customers.add(getContext().spawn(Customer.create("Customer" + (i + 1)), "customer" + (i + 1)));
|
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
|
// 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
|
// Erstellen von vier Wagen mit zufälligen Routen
|
||||||
for (int i = 0; i < 4; i++) {
|
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);
|
Collections.shuffle(route);
|
||||||
ActorRef<DeliveryCar.Message> truck = getContext().spawn(DeliveryCar.create(route), "truck" + (i + 1));
|
ActorRef<DeliveryCar.Message> truck = getContext().spawn(DeliveryCar.create(route), "truck" + (i + 1));
|
||||||
// Erstellen einer leeren Paketliste
|
// Erstellen einer leeren Paketliste
|
||||||
List<Paket> paketeList = new ArrayList<>();
|
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
|
// Beispiel für den Aufruf der ArriveMessage-Nachricht mit korrekten Parametern
|
||||||
distributionCenter.tell(new DistributionCenter.ArriveMessage(paketeList, truck));
|
distributionCenter.tell(new DistributionCenter.ArriveMessage(paketeList, truck));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,41 @@ import akka.actor.typed.javadsl.Behaviors;
|
||||||
import akka.actor.typed.javadsl.Receive;
|
import akka.actor.typed.javadsl.Receive;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class DistributionCenter extends AbstractBehavior<DistributionCenter.Message> {
|
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 {
|
public static class ArriveMessage implements Message {
|
||||||
private final List<Paket> pakete;
|
private final List<Paket> pakete;
|
||||||
private final ActorRef<DeliveryCar.Message> truck;
|
private final ActorRef<DeliveryCar.Message> truck;
|
||||||
|
@ -30,33 +61,91 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DistributionCenter(ActorContext<Message> context) {
|
public static class LoadMessage implements Message {
|
||||||
super(context);
|
private final List<Paket> pakete;
|
||||||
|
|
||||||
|
public LoadMessage(List<Paket> pakete) {
|
||||||
|
this.pakete = pakete;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Paket> getPakete() {
|
||||||
|
return pakete;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Message {}
|
public static class GenerateRoutes implements Message {
|
||||||
|
private final List<ActorRef<Customer.Message>> customers;
|
||||||
|
|
||||||
public static Behavior<Message> create() {
|
public GenerateRoutes(List<ActorRef<Customer.Message>> customers) {
|
||||||
return Behaviors.setup(DistributionCenter::new);
|
this.customers = customers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ActorRef<Customer.Message>> getCustomers() {
|
||||||
|
return customers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Receive<Message> createReceive() {
|
public Receive<Message> createReceive() {
|
||||||
return newReceiveBuilder()
|
return newReceiveBuilder()
|
||||||
.onMessage(ArriveMessage.class, this::onArriveMessage)
|
.onMessage(ArriveMessage.class, this::onArriveMessage)
|
||||||
|
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||||
|
.onMessage(GenerateRoutes.class, this::onGenerateRoutes)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Message> onArriveMessage(ArriveMessage msg) {
|
private Behavior<Message> onArriveMessage(ArriveMessage msg) {
|
||||||
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
|
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
|
||||||
List<Paket> pakete = msg.getPakete();
|
List<Paket> arrivedPackages = msg.getPakete();
|
||||||
getContext().getLog().info("Angekommene Pakete: {}", pakete);
|
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;
|
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