complete TODOs and rewrite AkkaMainSystem

This commit is contained in:
olhag 2024-05-17 01:18:02 +02:00
parent 08f604be37
commit 6493ec8d26
14 changed files with 324 additions and 81 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
.idea/compiler.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="16" />
</component>
</project>

16
.idea/gradle.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

30
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenLocal" />
<option name="name" value="MavenLocal" />
<option name="url" value="file:/$MAVEN_REPOSITORY$/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://repo.akka.io/maven" />
</remote-repository>
</component>
</project>

5
.idea/misc.xml Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="16" project-jdk-type="JavaSDK" />
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -10,29 +10,25 @@ import akka.actor.typed.javadsl.Receive;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
public class AddressBook extends AbstractBehavior<AddressBook.Message> { public class AddressBook extends AbstractBehavior<AddressBook.Message> {
ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>(); ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
public interface Message {} public interface Message {}
//Message, wenn ein Empfänger nach einer Adresse fragt // Nachricht, wenn ein Empfänger nach einer Adresse fragt
public record CustomerMessage(ActorRef<Customer.Message> fragender, String nameFragender,ActorRef<DeliveryCar.Message> truckReference) implements Message{} public static final class CustomerMessage implements Message {
public final ActorRef<Customer.Message> fragender;
public final String nameFragender;
public final ActorRef<DeliveryCar.Message> truckReference;
@Override public CustomerMessage(ActorRef<Customer.Message> fragender, String nameFragender, ActorRef<DeliveryCar.Message> truckReference) {
public Receive<AddressBook.Message> createReceive() { this.fragender = fragender;
return newReceiveBuilder() this.nameFragender = nameFragender;
.onMessage(CustomerMessage.class, this::onCustomerMessage) this.truckReference = truckReference;
.build(); }
} }
//create Methode mit custo = der Liste der Empfänger private AddressBook(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> custo) {
public static Behavior<AddressBook.Message> create(ArrayList<ActorRef<Customer.Message>> custo){
return Behaviors.setup(context -> new AddressBook(context, custo));
}
//Konstruktor von dem Adressbuch, in dem auch jedem Customer direkt eine Hello Nachricht geschickt wird, damit diese
//das Adressbuch auch kennen (um nach Empfängern fragen zu können).
private AddressBook(ActorContext<AddressBook.Message> context, ArrayList<ActorRef<Customer.Message>> custo){
super(context); super(context);
if (!custo.isEmpty()) { if (!custo.isEmpty()) {
this.customers.addAll(custo); this.customers.addAll(custo);
@ -42,11 +38,22 @@ public class AddressBook extends AbstractBehavior<AddressBook.Message> {
} }
} }
public static Behavior<Message> create(ArrayList<ActorRef<Customer.Message>> custo) {
return Behaviors.setup(context -> new AddressBook(context, custo));
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(CustomerMessage.class, this::onCustomerMessage)
.build();
}
// Methode, um einen zufälligen Empfänger auszugeben // Methode, um einen zufälligen Empfänger auszugeben
public Behavior<Message> onCustomerMessage(CustomerMessage msg){ private Behavior<Message> onCustomerMessage(CustomerMessage msg) {
Random random = new Random(); Random random = new Random();
int index = random.nextInt(customers.size()); int index = random.nextInt(customers.size());
var addressOf = customers.get(index); ActorRef<Customer.Message> addressOf = customers.get(index);
getContext().getLog().info("{} asked for an address", msg.nameFragender); getContext().getLog().info("{} asked for an address", msg.nameFragender);
msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference)); msg.fragender.tell(new Customer.AddressMessage(addressOf, msg.truckReference));
return this; return this;

View File

@ -5,7 +5,8 @@ import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.*; import akka.actor.typed.javadsl.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.List;
public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> { public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
@ -25,16 +26,35 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build(); return newReceiveBuilder().onMessage(Create.class, this::onCreate).build();
} }
private Behavior<Create> onCreate(Create command) { private Behavior<Create> onCreate(Create command) {
//#create-actors
ActorRef<Customer.Message> a = this.getContext().spawn(Customer.create("Alice"), "alice"); // Erstellen des Adressbuchs
ActorRef<Customer.Message> b = this.getContext().spawn(Customer.create("Bob"), "bob"); ArrayList<ActorRef<Customer.Message>> customers = new ArrayList<>();
ActorRef<Customer.Message> c = this.getContext().spawn(Customer.create("Charlie"), "charlie"); ActorRef<AddressBook.Message> addressBook = getContext().spawn(AddressBook.create(customers), "addressBook");
ActorRef<Customer.Message> d = this.getContext().spawn(Customer.create("Derik"), "derik");
ActorRef<ExampleTimerActor.Message> timerActor = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); // Erstellen von vier Empfänger*innen
//#create-actors for (int i = 0; i < 4; i++) {
ArrayList<ActorRef<Customer.Message>> actors = new ArrayList<>(Arrays.asList(a,b,c,d)); customers.add(getContext().spawn(Customer.create("Customer" + (i + 1)), "customer" + (i + 1)));
ActorRef<AddressBook.Message> addressBook = this.getContext().spawn(AddressBook.create(actors), "addressbook"); }
// Erstellen des Verteilzentrums
ActorRef<DistributionCenter.Message> distributionCenter = getContext().spawn(DistributionCenter.create(), "distributionCenter");
// Erstellen von vier Wagen mit zufälligen Routen
for (int i = 0; i < 4; i++) {
List<ActorRef<Customer.Message>> route = new ArrayList<>(customers);
Collections.shuffle(route);
ActorRef<DeliveryCar.Message> truck = getContext().spawn(DeliveryCar.create(route), "truck" + (i + 1));
// Erstellen einer leeren Paketliste
List<Paket> paketeList = new ArrayList<>();
paketeList.add(new Paket("Inhalt 1"));
paketeList.add(new Paket("Inhalt 2"));
// Beispiel für den Aufruf der ArriveMessage-Nachricht mit korrekten Parametern
distributionCenter.tell(new DistributionCenter.ArriveMessage(paketeList, truck));
}
return this; return this;
} }
} }

View File

@ -14,16 +14,30 @@ import java.util.Random;
public class Customer extends AbstractBehavior<Customer.Message> { public class Customer extends AbstractBehavior<Customer.Message> {
String name; String name;
ActorRef<AddressBook.Message> adrBook; ActorRef<AddressBook.Message> adrBook;
ArrayList<String> gegenstaende = new ArrayList<>(Arrays.asList("Spülmaschine","Altes CD-Regal", "Goldbarren","20kg Hanteln", "Holzkohlegrill","Blumenerde")); ArrayList<String> gegenstaende = new ArrayList<>(Arrays.asList("Spülmaschine","Altes CD-Regal", "Goldbarren","20kg Hanteln", "Holzkohlegrill","Blumenerde"));
public interface Message {} public interface Message {}
//Delivery Nachricht Typ //Delivery Nachricht Typ
public record DeliveryMessage(Paket paket) implements Message {} public static final class DeliveryMessage implements Message {
public final Paket paket;
public DeliveryMessage(Paket paket) {
this.paket = paket;
}
}
//Pickup Nachricht Typ //Pickup Nachricht Typ
public record PickUpMessage(ActorRef<DeliveryCar.Message> truckReference, String someString) implements Message {} public static final class PickUpMessage implements Message {
public final ActorRef<DeliveryCar.Message> truckReference;
public final String someString;
public PickUpMessage(ActorRef<DeliveryCar.Message> truckReference, String someString) {
this.truckReference = truckReference;
this.someString = someString;
}
}
public record AddressHello(ActorRef<AddressBook.Message> book) implements Message {} public record AddressHello(ActorRef<AddressBook.Message> book) implements Message {}
public record AddressMessage(ActorRef<Customer.Message> addresse, ActorRef<DeliveryCar.Message> truckReference) implements Message {} public record AddressMessage(ActorRef<Customer.Message> addresse, ActorRef<DeliveryCar.Message> truckReference) implements Message {}

View File

@ -4,59 +4,78 @@ 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.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive; import akka.actor.typed.javadsl.Receive;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
//TODO: Timer Aktivitäten müssen hinzugefügt werden
//TODO: Delivery Nachricht für den Fall, dass der Truck ein Paket für den Customer hat, bei dem der Truck gerade ist
//Gedanke: Eventuell mit Overseer wie in Blatt 1 arbeiten für das Einhalten der Routen
//TODO: Pickup Nachricht an den Customer, wenn der Truck noch freie Plätze hat
//TODO: Arrive Nachricht
public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> { public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
ArrayList<Paket> pakete; private final ArrayList<Paket> pakete = new ArrayList<>();
ArrayList<ActorRef<Customer.Message>> route; private final ArrayList<ActorRef<Customer.Message>> route;
private final int maxCapacity = 3;
public record LoadMessage(ArrayList<Paket> pakete) implements Message{}
public interface Message {} public interface Message {}
public record LoadMessage(List<Paket> pakete) implements Message {}
public record PickupResponse(Paket paket) implements Message {} public record PickupResponse(Paket paket) implements Message {}
public record ContinueRoute() implements Message {}
private DeliveryCar(ActorContext<Message> context, ArrayList<ActorRef<Customer.Message>> route) {
super(context);
this.route = route;
}
public static Behavior<Message> create(List<ActorRef<Customer.Message>> route) {
return Behaviors.setup(context -> new DeliveryCar(context, new ArrayList<>(route)));
}
@Override @Override
public Receive<Message> createReceive() { public Receive<Message> createReceive() {
return newReceiveBuilder() return newReceiveBuilder()
.onMessage(LoadMessage.class, this::onLoadMessage)
.onMessage(PickupResponse.class, this::onPickupResponse) .onMessage(PickupResponse.class, this::onPickupResponse)
.onMessage(ContinueRoute.class, msg -> {
deliverPackages();
return Behaviors.same();
})
.build(); .build();
} }
private DeliveryCar(ActorContext<DeliveryCar.Message> context,ArrayList<ActorRef<Customer.Message>> route){ private Behavior<Message> onLoadMessage(LoadMessage msg) {
super(context); pakete.addAll(msg.pakete);
this.route = route; checkNextStop();
}
//Pakete aus dem Verteilzentrum einladen
//TODO: Eventuell das losfahren und handlen, zu welchem Empfänger er fahren muss, mit einem Overseer Actor implementieren
public Behavior<DeliveryCar.Message> onLoadMessage(LoadMessage msg){
if(!msg.pakete.isEmpty()){
this.pakete.addAll(msg.pakete);
}
//vielleicht sowas wie in Blatt 1 Lösung mit einem Overseer Actor und dann einem overseer Attribut im Konstruktor
//this.overseer.tell(new Overseer.LoadedMessage()
return this; return this;
} }
//Customer gibt PickupResponse entweder mit einem Paket oder ohne eins private Behavior<Message> onPickupResponse(PickupResponse rsp) {
//Die Methode kann nur ausgeführt werden, wenn zuvor eine Pickup Nachricht an den Empfänger gesendet wurde und das
//impliziert, dass der Truck noch Platz für weitere Pakete hat
public Behavior<Message> onPickupResponse(PickupResponse rsp){
if(rsp.paket != null){
pakete.add(rsp.paket); pakete.add(rsp.paket);
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size()); getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
} checkNextStop();
//TODO: Fall behandeln, dass kein Paket übergeben wurde (wahrscheinlich einfach Timer setzen)
return this; return this;
} }
private void checkNextStop() {
if (pakete.size() >= maxCapacity || route.isEmpty()) {
getContext().getSelf().tell(new ContinueRoute());
} else {
sendPickupMessage();
}
}
private void sendPickupMessage() {
ActorRef<Customer.Message> nextCustomer = route.remove(0);
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Some string"));
}
private void deliverPackages() {
ActorRef<Customer.Message> nextCustomer = route.get(0);
for (Paket paket : pakete) {
nextCustomer.tell(new Customer.DeliveryMessage(paket));
}
pakete.clear();
}
} }

View File

@ -1,21 +1,62 @@
package com.example; package com.example;
import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior; 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.Receive; import akka.actor.typed.javadsl.Receive;
//TODO: Implementierung dieser Klasse import java.util.ArrayList;
import java.util.List;
public class DistributionCenter extends AbstractBehavior<DistributionCenter.Message> { public class DistributionCenter extends AbstractBehavior<DistributionCenter.Message> {
public DistributionCenter(ActorContext<Message> context) { public static class ArriveMessage implements Message {
private final List<Paket> pakete;
private final ActorRef<DeliveryCar.Message> truck;
public ArriveMessage(List<Paket> pakete, ActorRef<DeliveryCar.Message> truck) {
this.pakete = pakete;
this.truck = truck;
}
public List<Paket> getPakete() {
return pakete;
}
public ActorRef<DeliveryCar.Message> getTruck() {
return truck;
}
}
private DistributionCenter(ActorContext<Message> context) {
super(context); super(context);
} }
interface Message {} interface Message {}
public static Behavior<Message> create() {
return Behaviors.setup(DistributionCenter::new);
}
@Override @Override
public Receive<Message> createReceive() { public Receive<Message> createReceive() {
return newReceiveBuilder().build(); return newReceiveBuilder()
.onMessage(ArriveMessage.class, this::onArriveMessage)
.build();
}
private Behavior<Message> onArriveMessage(ArriveMessage msg) {
// Füge alle Pakete aus der Ankunftsnachricht dem Lagerraum hinzu
List<Paket> pakete = msg.getPakete();
getContext().getLog().info("Angekommene Pakete: {}", pakete);
// Hier kann die weitere Logik für die Behandlung der Pakete implementiert werden
// Beispiel: Senden der Pakete an einen internen Akteur zur weiteren Verarbeitung
// ...
return this;
} }
} }

View File

@ -3,28 +3,61 @@ 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.*;
import akka.io.dns.internal.Message;
import java.util.ArrayList;
import java.util.Random;
import static com.example.AkkaMainSystem.*;
public class ExampleActor extends AbstractBehavior<ExampleActor.Message> { public class ExampleActor extends AbstractBehavior<ExampleActor.Message> {
public interface Message {}; public interface Message {}
public record ExampleMessage(ActorRef<AkkaMainSystem.Create> someReference, String someString) implements Message { } public record ExampleMessage(ActorRef<Create> someReference, String someString) implements Message { }
public record Customer(String name, ActorRef<akka.io.dns.internal.Message> reference) {}
public static Behavior<Message> create(String name) { // Nachricht, um eine Empfängerreferenz hinzuzufügen
return Behaviors.setup(context -> new ExampleActor(context, name)); public static class AddCustomer implements Message {
private final ActorRef<Message> customer;
public AddCustomer(ActorRef<Message> customer) {
this.customer = customer;
}
public ActorRef<Message> getCustomer() {
return customer;
}
} }
private final String name; private final String name;
public static class PrintCustomers implements Message {}
private final ArrayList<ActorRef<Message>> customers; // ArrayList aller Empfänger*innen
private final Random random;
public static class PrintRandomCustomer implements Message {}// Nachricht, um einen zufälligen Kunden auszugeben
private ExampleActor(ActorContext<Message> context, String name) { private ExampleActor(ActorContext<Message> context, String name) {
super(context); super(context);
this.name = name; this.name = name;
this.customers = new ArrayList<>();
this.random = new Random();
}
public static Behavior<Message> create(String name) {
return Behaviors.setup(context -> new ExampleActor(context, name));
} }
@Override @Override
public Receive<Message> createReceive() { public Receive<Message> createReceive() {
return newReceiveBuilder() return newReceiveBuilder()
.onMessage(ExampleMessage.class, this::onExampleMessage) .onMessage(ExampleMessage.class, this::onExampleMessage)
.onMessage(AddCustomer.class, this::onAddCustomer)
.onMessage(PrintCustomers.class, this::onPrintCustomers)
.onMessage(PrintRandomCustomer.class, this::onPrintRandomCustomer)
.build(); .build();
} }
@ -32,4 +65,27 @@ public class ExampleActor extends AbstractBehavior<ExampleActor.Message> {
getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString); getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString);
return this; return this;
} }
private Behavior<Message> onAddCustomer(AddCustomer msg) {
customers.add(msg.getCustomer());
return this;
}
private Behavior<Message> onPrintCustomers(PrintCustomers msg) {
for (ActorRef<Message> customer : customers) {
getContext().getLog().info("Customer: {}", customer.path().name());
}
return this;
}
private Behavior<Message> onPrintRandomCustomer(PrintRandomCustomer msg) {
if (!customers.isEmpty()) {
int randomIndex = random.nextInt(customers.size());
ActorRef<Message> randomCustomer = customers.get(randomIndex);
getContext().getLog().info("Random Customer: {}", randomCustomer.path().name());
} else {
getContext().getLog().info("No customers available.");
}
return this;
}
} }

View File

@ -2,14 +2,24 @@ package com.example;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
public class Paket { public class Paket {
String inhalt; String inhalt;
String absender; String absender;
ActorRef<Customer.Message> empfaenger; ActorRef<Customer.Message> empfaenger;
// Konstruktor, der nur den Inhalt annimmt
Paket(String inhalt) {
this.inhalt = inhalt;
this.absender = null; // Absender und Empfänger können optional sein
this.empfaenger = null;
}
// Konstruktor mit allen Parametern
Paket(String inhalt, String absender, ActorRef<Customer.Message> empfaenger) { Paket(String inhalt, String absender, ActorRef<Customer.Message> empfaenger) {
this.inhalt = inhalt; this.inhalt = inhalt;
this.absender = absender; this.absender = absender;
this.empfaenger = empfaenger; this.empfaenger = empfaenger;
} }
} }