Compare commits
No commits in common. "4978d4dccfbae559dc92d375fee0afb43c0a4282" and "6733ca87686faaced903d10119791f9ea618222b" have entirely different histories.
4978d4dccf
...
6733ca8768
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,9 +2,11 @@ package com.example;
|
||||||
|
|
||||||
import akka.actor.typed.ActorRef;
|
import akka.actor.typed.ActorRef;
|
||||||
import akka.actor.typed.Behavior;
|
import akka.actor.typed.Behavior;
|
||||||
import akka.actor.typed.javadsl.*;
|
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 java.time.Duration;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -15,7 +17,6 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
private final ArrayList<Paket> pakete = new ArrayList<>();
|
private final ArrayList<Paket> pakete = new ArrayList<>();
|
||||||
private final ArrayList<ActorRef<Customer.Message>> route;
|
private final ArrayList<ActorRef<Customer.Message>> route;
|
||||||
private final int maxCapacity = 3;
|
private final int maxCapacity = 3;
|
||||||
private final TimerScheduler<Message> timers;
|
|
||||||
private int globalIndex = 0;
|
private int globalIndex = 0;
|
||||||
|
|
||||||
public interface Message {}
|
public interface Message {}
|
||||||
|
@ -26,16 +27,13 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
|
|
||||||
public record StartRoute() implements Message {}
|
public record StartRoute() implements Message {}
|
||||||
|
|
||||||
public record CheckStop() implements Message {}
|
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
||||||
|
|
||||||
private DeliveryCar(ActorContext<Message> context, TimerScheduler<Message> timers, ArrayList<ActorRef<Customer.Message>> route) {
|
|
||||||
super(context);
|
super(context);
|
||||||
this.route = route;
|
this.route = route;
|
||||||
this.timers = timers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Behavior<Message> create(List<ActorRef<Customer.Message>> route) {
|
public static Behavior<Message> create(List<ActorRef<Customer.Message>> route) {
|
||||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new DeliveryCar(context, timers, new ArrayList<>(route))));
|
return Behaviors.setup(context -> new DeliveryCar(context, new ArrayList<>(route)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -44,62 +42,62 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||||
.onMessage(StartRoute.class, this::onStartRoute)
|
.onMessage(StartRoute.class, this::onStartRoute)
|
||||||
.onMessage(CheckStop.class, this::onCheckStop)
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Message> onLoadMessage(LoadMessage msg) {
|
private Behavior<Message> onLoadMessage(LoadMessage msg) {
|
||||||
pakete.addAll(msg.pakete);
|
pakete.addAll(msg.pakete);
|
||||||
scheduleNextStopCheck();
|
// Nach dem Laden der Pakete zum nächsten Kunden fahren
|
||||||
|
sendPickupMessage();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||||
if (rsp.paket == null || rsp.paket.inhalt == null) {
|
if (rsp.paket == null || rsp.paket.inhalt == null) {
|
||||||
scheduleNextStopCheck();
|
globalIndex++;
|
||||||
|
checkNextStop();
|
||||||
} else {
|
} else {
|
||||||
if (pakete.size() < maxCapacity) {
|
if (pakete.size() < maxCapacity) {
|
||||||
pakete.add(rsp.paket);
|
pakete.add(rsp.paket);
|
||||||
getContext().getLog().info("Current number of packages in the truck: {}", pakete.size());
|
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
||||||
}
|
}
|
||||||
scheduleNextStopCheck();
|
globalIndex++;
|
||||||
|
checkNextStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
private void checkNextStop() {
|
||||||
|
getContext().getLog().info("globalIndex {}", globalIndex);
|
||||||
|
|
||||||
private Behavior<Message> onCheckStop(CheckStop stop) {
|
|
||||||
if (globalIndex >= route.size()) {
|
if (globalIndex >= route.size()) {
|
||||||
getContext().getLog().info("Car is returning to the distribution center.");
|
getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück.");
|
||||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
||||||
getDistributionCenter().tell(antwort);
|
getDistributionCenter().tell(antwort);
|
||||||
|
// Setze globalIndex auf 0, damit der Wagen weitermachen kann
|
||||||
globalIndex = 0;
|
globalIndex = 0;
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
getContext().getLog().info("I am at {}", route.get(globalIndex).path().name());
|
getContext().getLog().info("ich bin bei {}", route.get(globalIndex).path().name());
|
||||||
if (pakete.size() >= maxCapacity) {
|
if (pakete.size() >= maxCapacity) {
|
||||||
deliverPackages();
|
deliverPackages();
|
||||||
} else {
|
} else {
|
||||||
sendPickupMessage();
|
sendPickupMessage();
|
||||||
}
|
}
|
||||||
globalIndex++;
|
|
||||||
}
|
}
|
||||||
scheduleNextStopCheck();
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Behavior<Message> onStartRoute(StartRoute msg) {
|
private Behavior<Message> onStartRoute(StartRoute msg) {
|
||||||
globalIndex = 0;
|
globalIndex = 0;
|
||||||
scheduleNextStopCheck();
|
checkNextStop();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPickupMessage() {
|
private void sendPickupMessage() {
|
||||||
if (globalIndex < route.size()) {
|
if (globalIndex < route.size()) {
|
||||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Car can pick up package"));
|
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
||||||
} else {
|
} else {
|
||||||
getContext().getLog().info("No more customers in the route.");
|
getContext().getLog().info("Keine weiteren Kunden in der Route.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,13 +109,9 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
if (paket.empfaenger == nextCustomer) {
|
if (paket.empfaenger == nextCustomer) {
|
||||||
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
||||||
pakete.remove(i);
|
pakete.remove(i);
|
||||||
i--; // Adjust index since an element is removed
|
i--; // Index anpassen, da ein Element entfernt wurde
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleNextStopCheck() {
|
|
||||||
timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue