revert 2fca2c201c
revert TimerScheduler eingebaut. Funktionalität komplett fertig
This commit is contained in:
parent
f9c07cebd9
commit
4366a22761
|
@ -6,16 +6,12 @@ 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 akka.actor.typed.javadsl.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Actor extends AbstractBehavior<Actor.Message> {
|
||||
|
||||
//Timer für die Berechnung
|
||||
final TimerScheduler<Message> timer;
|
||||
//TODO: Wartezeit bei der Operation
|
||||
//Zurückgegebener String & Integer von der linken Seite
|
||||
String leftString;
|
||||
int leftInt;
|
||||
|
@ -31,8 +27,6 @@ public class Actor extends AbstractBehavior<Actor.Message> {
|
|||
Expression expression;
|
||||
public interface Message{}
|
||||
|
||||
public record Compute() implements Message{}
|
||||
|
||||
public record PrintAndEvaluate(Expression expression) implements Message{}
|
||||
|
||||
//Antwort von dem linken Kind
|
||||
|
@ -41,14 +35,13 @@ public class Actor extends AbstractBehavior<Actor.Message> {
|
|||
//Antwort von dem rechten Kind
|
||||
public record RightResponse(String string, int wert) implements Message{}
|
||||
|
||||
private Actor(ActorContext<Message> context, String name, TimerScheduler<Message> timer){
|
||||
private Actor(ActorContext<Message> context, String name){
|
||||
super(context);
|
||||
this.name = name;
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
public static Behavior<Actor.Message> create(String name){
|
||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new Actor(context, name, timers)));
|
||||
return Behaviors.setup(context -> new Actor(context, name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -57,7 +50,6 @@ public class Actor extends AbstractBehavior<Actor.Message> {
|
|||
.onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate)
|
||||
.onMessage(LeftResponse.class, this::onLeftResponse)
|
||||
.onMessage(RightResponse.class, this::onRightResponse)
|
||||
.onMessage(Compute.class, this::onComputeMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -91,36 +83,32 @@ public class Actor extends AbstractBehavior<Actor.Message> {
|
|||
}
|
||||
|
||||
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){
|
||||
this.leftString = response.string;
|
||||
this.leftInt = response.wert;
|
||||
if(this.rightString != null){
|
||||
this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1));
|
||||
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;
|
||||
}
|
||||
|
||||
//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){
|
||||
this.rightString = response.string;
|
||||
this.rightInt = response.wert;
|
||||
if(this.leftString != null){
|
||||
this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
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));
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -28,10 +28,11 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
private Behavior<Create> onCreate(Create command) {
|
||||
//#create-actors
|
||||
Expression expression = Expression.generateExpression(6, 9);
|
||||
Expression testExp = Expression.generateExpression(4,9);
|
||||
ActorRef<Actor.Message> computer = this.getContext().spawn(Actor.create("Rechner"), "Rechner");
|
||||
computer.tell(new Actor.PrintAndEvaluate(expression));
|
||||
computer.tell(new Actor.PrintAndEvaluate(testExp));
|
||||
//Vergleich mit dem Output der Berechnung
|
||||
System.out.println("SOLL: "+expression+ " Wert:" +expression.eval() + " Runtime: " + expression.runtime());
|
||||
System.out.println("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval() + " Runtime: " + testExp.runtime());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,15 @@ package com.example;
|
|||
|
||||
import akka.actor.typed.ActorRef;
|
||||
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.Objects;
|
||||
|
||||
public class SubActor extends AbstractBehavior<SubActor.Message> {
|
||||
|
||||
|
||||
//Timer, um bei den Berechnungen eine Sekunde lang zu warten
|
||||
TimerScheduler<Message> timer;
|
||||
|
||||
//TODO: Wartezeit bei den Operationen
|
||||
Expression expression;
|
||||
|
||||
//Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird
|
||||
|
@ -49,16 +47,12 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
//Antwort von dem rechten Kind
|
||||
public record RightResponse(String string, int wert) implements Message{ }
|
||||
|
||||
//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){
|
||||
private SubActor(ActorContext<SubActor.Message> context){
|
||||
super(context);
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
public static Behavior<SubActor.Message> create(){
|
||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new SubActor(context,timers)));
|
||||
return Behaviors.setup(context -> new SubActor(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,7 +62,6 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
.onMessage(SubPrintAndEvaluate.class, this::onSubPrintAndEvaluate)
|
||||
.onMessage(LeftResponse.class, this::onLeftResponse)
|
||||
.onMessage(RightResponse.class, this::onRightResponse)
|
||||
.onMessage(Compute.class, this::onComputeMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -107,7 +100,7 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
return this;
|
||||
}
|
||||
|
||||
//Nachricht von dem SubActor, von welchem dieser SubActor erstellt wurde, wird verarbeitet.
|
||||
//Nachricht von einem SubActor wird verarbeitet.
|
||||
//Dabei wird seine Referenz in oberActor gespeichert.
|
||||
public Behavior<Message> onSubPrintAndEvaluate(SubPrintAndEvaluate message){
|
||||
this.oberActor = message.sender;
|
||||
|
@ -148,7 +141,57 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
this.leftString = response.string;
|
||||
this.leftInt = response.wert;
|
||||
if(this.rightString != null){
|
||||
timer.startSingleTimer(new Compute(), Duration.ofSeconds(1));
|
||||
if(oberActor != null){
|
||||
//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;
|
||||
}
|
||||
|
@ -157,62 +200,58 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
this.rightString = response.string;
|
||||
this.rightInt = response.wert;
|
||||
if(this.leftString != null){
|
||||
timer.startSingleTimer(new Compute(), Duration.ofSeconds(1));
|
||||
if(oberActor != null){
|
||||
//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;
|
||||
}
|
||||
|
||||
private Behavior<Message> onComputeMessage(Compute response) {
|
||||
if (oberActor != null) {
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue