Überarbeitung von DelieveryCar sowie deren Methoden,
was noch fehlt ist die StartMessage von DistributionCenter aus und die TimeScheduler beim DeliveryCar
This commit is contained in:
parent
50c4920d4b
commit
149f3e7141
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.
|
@ -31,9 +31,13 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
// Erstellen des Adressbuchs
|
||||
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
|
||||
// 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("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
|
||||
|
|
|
@ -77,7 +77,14 @@ public class Customer extends AbstractBehavior<Customer.Message> {
|
|||
if(number <= 80){
|
||||
//truck wird hier übergeben, damit man im nächsten Schritt wieder auf ihn zugreifen kann.
|
||||
this.adrBook.tell(new AddressBook.CustomerMessage(getContext().getSelf(), this.name, msg.truckReference));
|
||||
|
||||
//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));
|
||||
}
|
||||
return this;
|
||||
|
|
|
@ -6,6 +6,7 @@ import akka.actor.typed.javadsl.AbstractBehavior;
|
|||
import akka.actor.typed.javadsl.ActorContext;
|
||||
import akka.actor.typed.javadsl.Behaviors;
|
||||
import akka.actor.typed.javadsl.Receive;
|
||||
import akka.actor.typed.javadsl.TimerScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -15,6 +16,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
private final ArrayList<Paket> pakete = new ArrayList<>();
|
||||
private final ArrayList<ActorRef<Customer.Message>> route;
|
||||
private final int maxCapacity = 3;
|
||||
private int globalIndex = 0;
|
||||
|
||||
public interface Message {}
|
||||
|
||||
|
@ -22,7 +24,9 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
|
||||
public record PickupResponse(Paket paket) implements Message {}
|
||||
|
||||
public record ContinueRoute() implements Message {}
|
||||
public record StartRoute() implements Message {}
|
||||
|
||||
public record RestartRoute() implements Message{}
|
||||
|
||||
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
||||
super(context);
|
||||
|
@ -38,10 +42,8 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
return newReceiveBuilder()
|
||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||
.onMessage(ContinueRoute.class, msg -> {
|
||||
deliverPackages();
|
||||
return Behaviors.same();
|
||||
})
|
||||
.onMessage(StartRoute.class, msg->{
|
||||
onStartRoute();return this;})
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -52,30 +54,81 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
}
|
||||
|
||||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||
if(rsp.paket.inhalt == null){
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
}else {
|
||||
if(pakete.size()==3){
|
||||
checkNextStop();
|
||||
}
|
||||
pakete.add(rsp.paket);
|
||||
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
//Die rekursive Methode,die über alle Addressen iteriert
|
||||
private void checkNextStop() {
|
||||
if (pakete.size() >= maxCapacity || route.isEmpty()) {
|
||||
getContext().getSelf().tell(new ContinueRoute());
|
||||
} else {
|
||||
//Wenn Wagen voll ist, dann wird erstmal geguckt ob man ein Paket liefern kann
|
||||
if (pakete.size() >= maxCapacity) {
|
||||
deliverPackages();
|
||||
checkNextStop();
|
||||
}
|
||||
//Wenn fertig iteriert so geht der Wagen an den Verteilerzentrum
|
||||
if(globalIndex==4){
|
||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete,getContext().getSelf());
|
||||
//Fehlende Antwort an den Verteilerzentrum
|
||||
|
||||
|
||||
//
|
||||
|
||||
}
|
||||
else {
|
||||
//Guckt ob Paket für Kunden da ist und fragt an ob etwas mitgenommen werden muss
|
||||
deliverPackages();
|
||||
sendPickupMessage();
|
||||
}
|
||||
}
|
||||
private Behavior<Message> onStartRoute(){
|
||||
if(globalIndex==0){
|
||||
checkNextStop();
|
||||
return this;
|
||||
}else{
|
||||
if(globalIndex==4) {
|
||||
globalIndex = 0;
|
||||
onStartRoute();
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
//Sendet PickUp Nachricht an den jetztigen Kunden wo der gerade ist.
|
||||
private void sendPickupMessage() {
|
||||
ActorRef<Customer.Message> nextCustomer = route.remove(0);
|
||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Some string"));
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
||||
globalIndex++;
|
||||
}
|
||||
|
||||
|
||||
//Prüft ob Paket im Wagen für den Empfänger ist oder nicht, falls ja wird dieser direkt entfernt
|
||||
private void deliverPackages() {
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(0);
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||
//Iteriert über alle Pakete
|
||||
for (Paket paket : pakete) {
|
||||
//Falls Empfänger vorhanden so wird dieser aus dem Wagen genommen und geht dann weiter
|
||||
if(pakete.get(globalIndex).empfaenger == nextCustomer ) {
|
||||
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
||||
}
|
||||
pakete.clear();
|
||||
pakete.remove(globalIndex);
|
||||
globalIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void removeMessage(Paket paket){
|
||||
pakete.remove(paket);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -137,8 +137,8 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
|||
// 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);
|
||||
getContext().spawn(DeliveryCar.create(route), "deliveryCar" + i+1);
|
||||
getContext().getLog().info("Lieferwagen {} erstellt mit Route: {}", i+1, route.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue