fixed version of DeliveryCar: no problems with index
This commit is contained in:
parent
16291e0e11
commit
d7684ef387
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.
|
@ -58,4 +58,4 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
||||||
*/
|
*/
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -17,4 +17,4 @@ public class AkkaStart {
|
||||||
messageMain.terminate();
|
messageMain.terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -106,4 +106,4 @@ public class Customer extends AbstractBehavior<Customer.Message> {
|
||||||
getContext().getLog().info("Ich habe ein Paket von {} erhalten mit dem Inhalt: {}", pkt.paket.absender, pkt.paket.inhalt);
|
getContext().getLog().info("Ich habe ein Paket von {} erhalten mit dem Inhalt: {}", pkt.paket.absender, pkt.paket.inhalt);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,6 @@ import akka.actor.typed.javadsl.AbstractBehavior;
|
||||||
import akka.actor.typed.javadsl.ActorContext;
|
import akka.actor.typed.javadsl.ActorContext;
|
||||||
import akka.actor.typed.javadsl.Behaviors;
|
import akka.actor.typed.javadsl.Behaviors;
|
||||||
import akka.actor.typed.javadsl.Receive;
|
import akka.actor.typed.javadsl.Receive;
|
||||||
import akka.actor.typed.javadsl.TimerScheduler;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -25,7 +24,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
public record PickupResponse(Paket paket) implements Message {}
|
public record PickupResponse(Paket paket) implements Message {}
|
||||||
|
|
||||||
public record StartRoute() implements Message {}
|
public record StartRoute() implements Message {}
|
||||||
|
|
||||||
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
||||||
super(context);
|
super(context);
|
||||||
this.route = route;
|
this.route = route;
|
||||||
|
@ -40,9 +39,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
return newReceiveBuilder()
|
return newReceiveBuilder()
|
||||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
.onMessage(StartRoute.class, this::onStartRoute)
|
||||||
.onMessage(StartRoute.class, msg->{
|
|
||||||
onStartRoute();return this;})
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,88 +50,58 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||||
if(rsp.paket.inhalt == null){
|
if (rsp.paket == null || rsp.paket.inhalt == null) {
|
||||||
globalIndex++;
|
globalIndex++;
|
||||||
checkNextStop();
|
checkNextStop();
|
||||||
}else {
|
} else {
|
||||||
if(pakete.size()==3){
|
if (pakete.size() < maxCapacity) {
|
||||||
checkNextStop();
|
pakete.add(rsp.paket);
|
||||||
|
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
||||||
}
|
}
|
||||||
pakete.add(rsp.paket);
|
|
||||||
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
|
||||||
globalIndex++;
|
globalIndex++;
|
||||||
checkNextStop();
|
checkNextStop();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
//Die rekursive Methode,die über alle Addressen iteriert
|
|
||||||
private void checkNextStop() {
|
private void checkNextStop() {
|
||||||
//Wenn Wagen voll ist, dann wird erstmal geguckt ob man ein Paket liefern kann
|
|
||||||
if (pakete.size() >= maxCapacity) {
|
if (pakete.size() >= maxCapacity) {
|
||||||
deliverPackages();
|
deliverPackages();
|
||||||
checkNextStop();
|
} else if (globalIndex >= route.size()) {
|
||||||
}
|
getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück.");
|
||||||
//Wenn fertig iteriert so geht der Wagen an den Verteilerzentrum
|
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
||||||
if(globalIndex==4){
|
// Antwort an das Verteilzentrum senden (die genaue Implementierung hängt von Ihrer Systemarchitektur ab)
|
||||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete,getContext().getSelf());
|
} else {
|
||||||
//ActorRef<DistributionCenter.Message> antwort1;
|
|
||||||
// antwort1.tell(antwort);
|
|
||||||
|
|
||||||
//Fehlende Antwort an den Verteilerzentrum
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
//Guckt ob Paket für Kunden da ist und fragt an ob etwas mitgenommen werden muss
|
|
||||||
deliverPackages();
|
|
||||||
sendPickupMessage();
|
sendPickupMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Behavior<Message> onStartRoute(){
|
|
||||||
/*if(globalIndex==0){
|
|
||||||
checkNextStop();
|
|
||||||
return this;
|
|
||||||
|
|
||||||
}else{
|
private Behavior<Message> onStartRoute(StartRoute msg) {
|
||||||
if(globalIndex==4) {
|
|
||||||
globalIndex = 0;
|
|
||||||
onStartRoute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;*/
|
|
||||||
globalIndex = 0;
|
globalIndex = 0;
|
||||||
checkNextStop();
|
checkNextStop();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sendet PickUp Nachricht an den jetztigen Kunden wo der gerade ist.
|
|
||||||
private void sendPickupMessage() {
|
private void sendPickupMessage() {
|
||||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
if (globalIndex < route.size()) {
|
||||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||||
globalIndex++;
|
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
||||||
|
} else {
|
||||||
|
getContext().getLog().info("Keine weiteren Kunden in der Route.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Prüft ob Paket im Wagen für den Empfänger ist oder nicht, falls ja wird dieser direkt entfernt
|
|
||||||
private void deliverPackages() {
|
private void deliverPackages() {
|
||||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
if (globalIndex < route.size()) {
|
||||||
//Iteriert über alle Pakete
|
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||||
for (Paket paket : pakete) {
|
for (int i = 0; i < pakete.size(); i++) {
|
||||||
//Falls Empfänger vorhanden so wird dieser aus dem Wagen genommen und geht dann weiter
|
Paket paket = pakete.get(i);
|
||||||
if(pakete.get(globalIndex).empfaenger == nextCustomer ) {
|
if (paket.empfaenger == nextCustomer) {
|
||||||
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
||||||
pakete.remove(globalIndex);
|
pakete.remove(i);
|
||||||
// globalIndex++;
|
i--; // Index anpassen, da ein Element entfernt wurde
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void removeMessage(Paket paket){
|
|
||||||
pakete.remove(paket);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -167,4 +167,4 @@ public class DistributionCenter extends AbstractBehavior<DistributionCenter.Mess
|
||||||
Collections.shuffle(route);
|
Collections.shuffle(route);
|
||||||
return route;
|
return route;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -22,4 +22,3 @@ public class Paket {
|
||||||
this.empfaenger = empfaenger;
|
this.empfaenger = empfaenger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue