Compare commits
2 Commits
6733ca8768
...
4978d4dccf
Author | SHA1 | Date |
---|---|---|
olhag | 4978d4dccf | |
olhag | 2a60e47cc5 |
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.
|
@ -74,4 +74,4 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
|
||||||
msg.replyTo.tell(Collections.unmodifiableList(customers));
|
msg.replyTo.tell(Collections.unmodifiableList(customers));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,11 +2,9 @@ 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.AbstractBehavior;
|
import akka.actor.typed.javadsl.*;
|
||||||
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;
|
||||||
|
|
||||||
|
@ -17,6 +15,7 @@ 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 {}
|
||||||
|
@ -27,13 +26,16 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
||||||
|
|
||||||
public record StartRoute() implements Message {}
|
public record StartRoute() implements Message {}
|
||||||
|
|
||||||
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
|
public record CheckStop() implements Message {}
|
||||||
|
|
||||||
|
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 -> new DeliveryCar(context, new ArrayList<>(route)));
|
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new DeliveryCar(context, timers, new ArrayList<>(route))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -42,62 +44,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);
|
||||||
// Nach dem Laden der Pakete zum nächsten Kunden fahren
|
scheduleNextStopCheck();
|
||||||
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) {
|
||||||
globalIndex++;
|
scheduleNextStopCheck();
|
||||||
checkNextStop();
|
|
||||||
} else {
|
} else {
|
||||||
if (pakete.size() < maxCapacity) {
|
if (pakete.size() < maxCapacity) {
|
||||||
pakete.add(rsp.paket);
|
pakete.add(rsp.paket);
|
||||||
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
getContext().getLog().info("Current number of packages in the truck: {}", pakete.size());
|
||||||
}
|
}
|
||||||
globalIndex++;
|
scheduleNextStopCheck();
|
||||||
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("Wagen kehrt zum Verteilzentrum zurück.");
|
getContext().getLog().info("Car is returning to the distribution center.");
|
||||||
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("ich bin bei {}", route.get(globalIndex).path().name());
|
getContext().getLog().info("I am at {}", 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;
|
||||||
checkNextStop();
|
scheduleNextStopCheck();
|
||||||
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(), "Wagen kann Paket aufnehmen"));
|
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Car can pick up package"));
|
||||||
} else {
|
} else {
|
||||||
getContext().getLog().info("Keine weiteren Kunden in der Route.");
|
getContext().getLog().info("No more customers in the route.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,9 +111,13 @@ 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--; // Index anpassen, da ein Element entfernt wurde
|
i--; // Adjust index since an element is removed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void scheduleNextStopCheck() {
|
||||||
|
timers.startSingleTimer(new CheckStop(), Duration.ofSeconds(3));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue