Loop-Ride of Delivery Cars
This commit is contained in:
parent
d7684ef387
commit
6733ca8768
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -64,7 +64,7 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
|||
Random random = new Random();
|
||||
int index = random.nextInt(customers.size());
|
||||
ActorRef<Customer.Message> addressOf = customers.get(index);
|
||||
getContext().getLog().info("{} asked for an address", msg.nameFragender);
|
||||
getContext().getLog().info("{} asked for an address CAR: {} ", msg.nameFragender, msg.truckReference.path().name());
|
||||
msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference));
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -5,20 +5,38 @@ import akka.actor.typed.Behavior;
|
|||
import akka.actor.typed.javadsl.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
||||
|
||||
public static class Create {
|
||||
}
|
||||
public static class Create {}
|
||||
|
||||
public static Behavior<Create> create() {
|
||||
return Behaviors.setup(AkkaMainSystem::new);
|
||||
}
|
||||
|
||||
// Statische Referenz auf das DistributionCenter
|
||||
private static ActorRef<DistributionCenter.Message> distributionCenter;
|
||||
|
||||
// Statische Methode zum Abrufen der DistributionCenter-Referenz
|
||||
public static ActorRef<DistributionCenter.Message> getDistributionCenter() {
|
||||
return distributionCenter;
|
||||
}
|
||||
|
||||
private AkkaMainSystem(ActorContext<Create> context) {
|
||||
super(context);
|
||||
|
||||
// Erstellen des Adressbuchs
|
||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||
customers.add(getContext().spawn(Customer.create("Alice"), "alice"));
|
||||
customers.add(getContext().spawn(Customer.create("Bob"), "bob"));
|
||||
customers.add(getContext().spawn(Customer.create("Charles"), "charles"));
|
||||
customers.add(getContext().spawn(Customer.create("Derick"), "derick"));
|
||||
ActorRef<AddressBook.Message> addressBook = getContext().spawn(AddressBook.create(customers), "addressBook");
|
||||
|
||||
// Erstellen des Verteilzentrums
|
||||
distributionCenter = getContext().spawn(DistributionCenter.create(addressBook), "distributionCenter");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,35 +45,7 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
}
|
||||
|
||||
private Behavior<Create> onCreate(Create command) {
|
||||
|
||||
// Erstellen des Adressbuchs
|
||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||
// 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)));
|
||||
}*/
|
||||
customers.add(getContext().spawn(Customer.create("Alice"+( 1)) ,"alice"+(1)));
|
||||
customers.add(getContext().spawn(Customer.create("Bob"+( 1)) ,"bob"+(1)));
|
||||
customers.add(getContext().spawn(Customer.create("Charles"+( 1)) ,"charles"+(1)));
|
||||
customers.add(getContext().spawn(Customer.create("Derick"+( 1)) ,"derick"+(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(addressBook), "distributionCenter");
|
||||
/*
|
||||
// Erstellen von vier Wagen mit zufälligen Routen
|
||||
for (int i = 0; i < 4; i++) {
|
||||
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<>();
|
||||
// Beispiel für den Aufruf der ArriveMessage-Nachricht mit korrekten Parametern
|
||||
distributionCenter.tell(new DistributionCenter.ArriveMessage(paketeList, truck));
|
||||
}
|
||||
*/
|
||||
// Additional setup if needed
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -72,6 +72,7 @@ public class Customer extends AbstractBehavior<Customer.Message> {
|
|||
//Wenn 80% getroffen wurden, wird eine Anfrage an das Adressbuch geschickt und die Antwort
|
||||
// an anderer Stelle (onAddressMessage) verarbeitet, um das Paket zu verschicken
|
||||
private Behavior<Message> onPickUpMessage(PickUpMessage msg){
|
||||
getContext().getLog().info("Kunde {}, will kein Paket abschicken CAR: {}", this.name, msg.truckReference.path().name());
|
||||
Random random = new Random();
|
||||
int number = random.nextInt(101);
|
||||
if(number <= 80){
|
||||
|
@ -81,8 +82,6 @@ public class Customer extends AbstractBehavior<Customer.Message> {
|
|||
//Methode um einen Paket zu versenden
|
||||
//über onAddressMessage wird dann Paket aufgenommen
|
||||
onAddressMessage(new AddressMessage(getContext().getSelf(), msg.truckReference));
|
||||
|
||||
|
||||
}else{
|
||||
getContext().getLog().info("Kunde {}, will kein Paket abschicken", this.name);
|
||||
msg.truckReference.tell(new DeliveryCar.PickupResponse(null));
|
||||
|
@ -96,7 +95,7 @@ public class Customer extends AbstractBehavior<Customer.Message> {
|
|||
int index = random.nextInt(gegenstaende.size());
|
||||
String gegenstand = gegenstaende.get(index);
|
||||
Paket paket = new Paket(gegenstand,this.name, adr.addresse);
|
||||
getContext().getLog().info("Paket mit dem Inhalt {} wurde an {} verschickt", paket.inhalt, adr.addresse.path().name());
|
||||
getContext().getLog().info("Paket mit dem Inhalt {} wurde an {} verschickt (CAR: {})", paket.inhalt, adr.addresse.path().name(), adr.truckReference.path().name());
|
||||
adr.truckReference.tell(new DeliveryCar.PickupResponse(paket));
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ import akka.actor.typed.javadsl.Receive;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.example.AkkaMainSystem.getDistributionCenter;
|
||||
|
||||
public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||
|
||||
private final ArrayList<Paket> pakete = new ArrayList<>();
|
||||
|
@ -45,7 +47,8 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
|
||||
private Behavior<Message> onLoadMessage(LoadMessage msg) {
|
||||
pakete.addAll(msg.pakete);
|
||||
checkNextStop();
|
||||
// Nach dem Laden der Pakete zum nächsten Kunden fahren
|
||||
sendPickupMessage();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -63,18 +66,25 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private void checkNextStop() {
|
||||
if (pakete.size() >= maxCapacity) {
|
||||
deliverPackages();
|
||||
} else if (globalIndex >= route.size()) {
|
||||
getContext().getLog().info("globalIndex {}", globalIndex);
|
||||
|
||||
if (globalIndex >= route.size()) {
|
||||
getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück.");
|
||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
||||
// Antwort an das Verteilzentrum senden (die genaue Implementierung hängt von Ihrer Systemarchitektur ab)
|
||||
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());
|
||||
if (pakete.size() >= maxCapacity) {
|
||||
deliverPackages();
|
||||
} else {
|
||||
sendPickupMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Behavior<Message> onStartRoute(StartRoute msg) {
|
||||
globalIndex = 0;
|
||||
|
|
|
@ -65,17 +65,7 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
}
|
||||
}
|
||||
|
||||
public static class LoadMessage implements Message {
|
||||
private final List<Paket> pakete;
|
||||
|
||||
public LoadMessage(List<Paket> pakete) {
|
||||
this.pakete = pakete;
|
||||
}
|
||||
|
||||
public List<Paket> getPakete() {
|
||||
return pakete;
|
||||
}
|
||||
}
|
||||
|
||||
public static class GenerateRoutes implements Message {
|
||||
private final List<ActorRef<Customer.Message>> customers;
|
||||
|
@ -93,7 +83,6 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
public Receive<Message> createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(ArriveMessage.class, this::onArriveMessage)
|
||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(GenerateRoutes.class, this::onGenerateRoutes)
|
||||
.onMessage(StartAllRoutes.class, this::onStartAllRoutes)
|
||||
.build();
|
||||
|
@ -103,7 +92,14 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
|
||||
List<Paket> arrivedPackages = msg.getPakete();
|
||||
lagerraum.addAll(arrivedPackages);
|
||||
getContext().getLog().info("Angekommene Pakete: {}", arrivedPackages);
|
||||
String arrivedPackagesString = "";
|
||||
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
|
||||
int numPackagesToSend = Math.min(3, lagerraum.size());
|
||||
|
@ -111,20 +107,15 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
for (int i = 0; i < numPackagesToSend; i++) {
|
||||
packagesToSend.add(lagerraum.remove(0));
|
||||
}
|
||||
|
||||
msg.getTruck().tell(new DeliveryCar.LoadMessage(packagesToSend));
|
||||
|
||||
msg.getTruck().tell(new DeliveryCar.StartRoute()); // Starte die Route
|
||||
// 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();
|
||||
|
@ -140,19 +131,24 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
}
|
||||
|
||||
// Erstelle vier Lastwagen mit zufälligen Routen
|
||||
for (int i = 0; i < 4; i++) {
|
||||
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);
|
||||
ActorRef<DeliveryCar.Message> deliveryCar = getContext().spawn(DeliveryCar.create(route), "deliveryCar" + (i + 1));
|
||||
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, route.toString());
|
||||
|
||||
String RouteStr = "";
|
||||
for (int j = 0; j < route.size(); j++) {
|
||||
RouteStr += route.get(j).path().name();
|
||||
if (j < route.size() - 1) {
|
||||
RouteStr += ", "; // Fügen Sie ein Trennzeichen hinzu, außer für das letzte Paket
|
||||
}
|
||||
}
|
||||
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i + 1, RouteStr);
|
||||
deliveryCar.tell(new DeliveryCar.StartRoute());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
private Behavior<Message> onStartAllRoutes(StartAllRoutes msg) {
|
||||
for (ActorRef<DeliveryCar.Message> deliveryCar : deliveryCars) {
|
||||
|
|
Loading…
Reference in New Issue