TimerScheduler eingebaut. Funktionalität komplett fertig

This commit is contained in:
Egor Rutkowski 2024-07-02 15:44:40 +02:00
parent f193f80acc
commit 2fca2c201c
3 changed files with 99 additions and 127 deletions

View File

@ -6,12 +6,16 @@ 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.*;
import java.time.Duration;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger; import java.util.logging.Logger;
public class Actor extends AbstractBehavior<Actor.Message> { public class Actor extends AbstractBehavior<Actor.Message> {
//TODO: Wartezeit bei der Operation
//Timer für die Berechnung
final TimerScheduler<Message> timer;
//Zurückgegebener String & Integer von der linken Seite //Zurückgegebener String & Integer von der linken Seite
String leftString; String leftString;
int leftInt; int leftInt;
@ -27,6 +31,8 @@ public class Actor extends AbstractBehavior<Actor.Message> {
Expression expression; Expression expression;
public interface Message{} public interface Message{}
public record Compute() implements Message{}
public record PrintAndEvaluate(Expression expression) implements Message{} public record PrintAndEvaluate(Expression expression) implements Message{}
//Antwort von dem linken Kind //Antwort von dem linken Kind
@ -35,13 +41,14 @@ public class Actor extends AbstractBehavior<Actor.Message> {
//Antwort von dem rechten Kind //Antwort von dem rechten Kind
public record RightResponse(String string, int wert) implements Message{} public record RightResponse(String string, int wert) implements Message{}
private Actor(ActorContext<Message> context, String name){ private Actor(ActorContext<Message> context, String name, TimerScheduler<Message> timer){
super(context); super(context);
this.name = name; this.name = name;
this.timer = timer;
} }
public static Behavior<Actor.Message> create(String name){ public static Behavior<Actor.Message> create(String name){
return Behaviors.setup(context -> new Actor(context, name)); return Behaviors.setup(context -> Behaviors.withTimers(timers -> new Actor(context, name, timers)));
} }
@Override @Override
@ -50,6 +57,7 @@ public class Actor extends AbstractBehavior<Actor.Message> {
.onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate) .onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate)
.onMessage(LeftResponse.class, this::onLeftResponse) .onMessage(LeftResponse.class, this::onLeftResponse)
.onMessage(RightResponse.class, this::onRightResponse) .onMessage(RightResponse.class, this::onRightResponse)
.onMessage(Compute.class, this::onComputeMessage)
.build(); .build();
} }
@ -83,32 +91,36 @@ public class Actor extends AbstractBehavior<Actor.Message> {
} }
Logger logger = Logger.getLogger(Actor.class.getName()); Logger logger = Logger.getLogger(Actor.class.getName());
//Antwort von dem rechten Kind. Wenn beide Seiten geantwortet haben, wird eine Sekunde gewartet und dann das Ergebnis
//berechnet in Compute()
private Behavior<Message> onLeftResponse(LeftResponse response){ private Behavior<Message> onLeftResponse(LeftResponse response){
this.leftString = response.string; this.leftString = response.string;
this.leftInt = response.wert; this.leftInt = response.wert;
if(this.rightString != null){ if(this.rightString != null){
if(operationString.equals("+")){ this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1));
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt + rightInt));
} else if (operationString.equals("-")) {
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt - rightInt));
}else{
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt * rightInt));
}
} }
return this; return this;
} }
//Antwort von dem rechten Kind. Wenn beide Seiten geantwortet haben, wird eine Sekunde gewartet und dann das Ergebnis
//berechnet in Compute()
private Behavior<Message> onRightResponse(RightResponse response){ private Behavior<Message> onRightResponse(RightResponse response){
this.rightString = response.string; this.rightString = response.string;
this.rightInt = response.wert; this.rightInt = response.wert;
if(this.leftString != null){ if(this.leftString != null){
if(operationString.equals("+")){ this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1));
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt + rightInt)); }
} else if (operationString.equals("-")) { return this;
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt - rightInt)); }
}else{
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt * rightInt)); private Behavior<Message> onComputeMessage(Compute response){
} if(operationString.equals("+")){
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt + rightInt));
} else if (operationString.equals("-")) {
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt - rightInt));
}else{
logger.info("(" + leftString + operationString + rightString + ") Wert: " + (leftInt * rightInt));
} }
return this; return this;
} }

View File

@ -28,11 +28,10 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
private Behavior<Create> onCreate(Create command) { private Behavior<Create> onCreate(Create command) {
//#create-actors //#create-actors
Expression expression = Expression.generateExpression(6, 9); Expression expression = Expression.generateExpression(6, 9);
Expression testExp = Expression.generateExpression(4,9);
ActorRef<Actor.Message> computer = this.getContext().spawn(Actor.create("Rechner"), "Rechner"); ActorRef<Actor.Message> computer = this.getContext().spawn(Actor.create("Rechner"), "Rechner");
computer.tell(new Actor.PrintAndEvaluate(testExp)); computer.tell(new Actor.PrintAndEvaluate(expression));
//Vergleich mit dem Output der Berechnung //Vergleich mit dem Output der Berechnung
System.out.println("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval() + " Runtime: " + testExp.runtime()); System.out.println("SOLL: "+expression+ " Wert:" +expression.eval() + " Runtime: " + expression.runtime());
return this; return this;
} }
} }

View File

@ -2,15 +2,17 @@ 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.Objects; import java.util.Objects;
public class SubActor extends AbstractBehavior<SubActor.Message> { public class SubActor extends AbstractBehavior<SubActor.Message> {
//TODO: Wartezeit bei den Operationen
//Timer, um bei den Berechnungen eine Sekunde lang zu warten
TimerScheduler<Message> timer;
Expression expression; Expression expression;
//Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird //Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird
@ -47,12 +49,16 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
//Antwort von dem rechten Kind //Antwort von dem rechten Kind
public record RightResponse(String string, int wert) implements Message{ } public record RightResponse(String string, int wert) implements Message{ }
private SubActor(ActorContext<SubActor.Message> context){ //Nachricht an sich selbst, um nach einer Sekunde Wartezeit das Ergebnis zu berechnen
public record Compute() implements Message{}
private SubActor(ActorContext<SubActor.Message> context, TimerScheduler<Message> timer){
super(context); super(context);
this.timer = timer;
} }
public static Behavior<SubActor.Message> create(){ public static Behavior<SubActor.Message> create(){
return Behaviors.setup(context -> new SubActor(context)); return Behaviors.setup(context -> Behaviors.withTimers(timers -> new SubActor(context,timers)));
} }
@Override @Override
@ -62,6 +68,7 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
.onMessage(SubPrintAndEvaluate.class, this::onSubPrintAndEvaluate) .onMessage(SubPrintAndEvaluate.class, this::onSubPrintAndEvaluate)
.onMessage(LeftResponse.class, this::onLeftResponse) .onMessage(LeftResponse.class, this::onLeftResponse)
.onMessage(RightResponse.class, this::onRightResponse) .onMessage(RightResponse.class, this::onRightResponse)
.onMessage(Compute.class, this::onComputeMessage)
.build(); .build();
} }
@ -100,7 +107,7 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
return this; return this;
} }
//Nachricht von einem SubActor wird verarbeitet. //Nachricht von dem SubActor, von welchem dieser SubActor erstellt wurde, wird verarbeitet.
//Dabei wird seine Referenz in oberActor gespeichert. //Dabei wird seine Referenz in oberActor gespeichert.
public Behavior<Message> onSubPrintAndEvaluate(SubPrintAndEvaluate message){ public Behavior<Message> onSubPrintAndEvaluate(SubPrintAndEvaluate message){
this.oberActor = message.sender; this.oberActor = message.sender;
@ -141,57 +148,7 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
this.leftString = response.string; this.leftString = response.string;
this.leftInt = response.wert; this.leftInt = response.wert;
if(this.rightString != null){ if(this.rightString != null){
if(oberActor != null){ timer.startSingleTimer(new Compute(), Duration.ofSeconds(1));
//side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt
if(side == true){
if(operationString.equals("+")){
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt));
} else if (operationString.equals("-")) {
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
}else{
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
}
}else {
if(operationString.equals("+")){
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt));
} else if (operationString.equals("-")) {
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
}else{
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
}
}
}else{
if(side == true){
if(operationString.equals("+")){
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt));
} else if (operationString.equals("-")) {
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
}else{
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
}
}else {
if(operationString.equals("+")){
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt));
} else if (operationString.equals("-")) {
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
}else{
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
}
}
}
} }
return this; return this;
} }
@ -200,58 +157,62 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
this.rightString = response.string; this.rightString = response.string;
this.rightInt = response.wert; this.rightInt = response.wert;
if(this.leftString != null){ if(this.leftString != null){
if(oberActor != null){ timer.startSingleTimer(new Compute(), Duration.ofSeconds(1));
//side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt }
if(side == true){ return this;
if(operationString.equals("+")){ }
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt)); private Behavior<Message> onComputeMessage(Compute response) {
} else if (operationString.equals("-")) { if (oberActor != null) {
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")", //side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt
leftInt - rightInt)); if (side == true) {
}else{ if (operationString.equals("+")) {
oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")", oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt)); leftInt + rightInt));
} } else if (operationString.equals("-")) {
}else { oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
if(operationString.equals("+")){ leftInt - rightInt));
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")", } else {
leftInt + rightInt)); oberActor.tell(new LeftResponse("(" + leftString + operationString + rightString + ")",
} else if (operationString.equals("-")) { leftInt * rightInt));
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
}else{
oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
}
} }
}else { } else {
if(side == true){ if (operationString.equals("+")) {
if(operationString.equals("+")){ oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")", leftInt + rightInt));
leftInt + rightInt)); } else if (operationString.equals("-")) {
} else if (operationString.equals("-")) { oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")", leftInt - rightInt));
leftInt - rightInt)); } else {
}else{ oberActor.tell(new RightResponse("(" + leftString + operationString + rightString + ")",
initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")", leftInt * rightInt));
leftInt * rightInt)); }
} }
}else { } else {
if(operationString.equals("+")){ if (side == true) {
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")", if (operationString.equals("+")) {
leftInt + rightInt)); initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
} else if (operationString.equals("-")) { leftInt + rightInt));
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")", } else if (operationString.equals("-")) {
leftInt - rightInt)); initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
}else{ leftInt - rightInt));
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")", } else {
leftInt * rightInt)); initial.tell(new Actor.LeftResponse("(" + leftString + operationString + rightString + ")",
} leftInt * rightInt));
}
} else {
if (operationString.equals("+")) {
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt + rightInt));
} else if (operationString.equals("-")) {
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt - rightInt));
} else {
initial.tell(new Actor.RightResponse("(" + leftString + operationString + rightString + ")",
leftInt * rightInt));
} }
} }
} }
return this; return this;
} }
} }