From 2fca2c201c56d6aee7ccdfd870e4b35f301340e9 Mon Sep 17 00:00:00 2001 From: Egor Rutkowski Date: Tue, 2 Jul 2024 15:44:40 +0200 Subject: [PATCH] =?UTF-8?q?TimerScheduler=20eingebaut.=20Funktionalit?= =?UTF-8?q?=C3=A4t=20komplett=20fertig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/Actor.java | 46 +++-- src/main/java/com/example/AkkaMainSystem.java | 5 +- src/main/java/com/example/SubActor.java | 175 +++++++----------- 3 files changed, 99 insertions(+), 127 deletions(-) diff --git a/src/main/java/com/example/Actor.java b/src/main/java/com/example/Actor.java index 7bc9c78..d0e55de 100644 --- a/src/main/java/com/example/Actor.java +++ b/src/main/java/com/example/Actor.java @@ -6,12 +6,16 @@ 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 { - //TODO: Wartezeit bei der Operation + + //Timer für die Berechnung + final TimerScheduler timer; //Zurückgegebener String & Integer von der linken Seite String leftString; int leftInt; @@ -27,6 +31,8 @@ public class Actor extends AbstractBehavior { Expression expression; public interface Message{} + public record Compute() implements Message{} + public record PrintAndEvaluate(Expression expression) implements Message{} //Antwort von dem linken Kind @@ -35,13 +41,14 @@ public class Actor extends AbstractBehavior { //Antwort von dem rechten Kind public record RightResponse(String string, int wert) implements Message{} - private Actor(ActorContext context, String name){ + private Actor(ActorContext context, String name, TimerScheduler timer){ super(context); this.name = name; + this.timer = timer; } public static Behavior create(String name){ - return Behaviors.setup(context -> new Actor(context, name)); + return Behaviors.setup(context -> Behaviors.withTimers(timers -> new Actor(context, name, timers))); } @Override @@ -50,6 +57,7 @@ public class Actor extends AbstractBehavior { .onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate) .onMessage(LeftResponse.class, this::onLeftResponse) .onMessage(RightResponse.class, this::onRightResponse) + .onMessage(Compute.class, this::onComputeMessage) .build(); } @@ -83,32 +91,36 @@ public class Actor extends AbstractBehavior { } 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 onLeftResponse(LeftResponse response){ this.leftString = response.string; this.leftInt = response.wert; if(this.rightString != null){ - 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)); - } + this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1)); } 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 onRightResponse(RightResponse response){ this.rightString = response.string; this.rightInt = response.wert; if(this.leftString != null){ - 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)); - } + this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1)); + } + return this; + } + + private Behavior 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; } diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index e9dd534..b2baacc 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -28,11 +28,10 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors Expression expression = Expression.generateExpression(6, 9); - Expression testExp = Expression.generateExpression(4,9); ActorRef 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 - 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; } } diff --git a/src/main/java/com/example/SubActor.java b/src/main/java/com/example/SubActor.java index e6169d7..97594ad 100644 --- a/src/main/java/com/example/SubActor.java +++ b/src/main/java/com/example/SubActor.java @@ -2,15 +2,17 @@ package com.example; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; -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; public class SubActor extends AbstractBehavior { - //TODO: Wartezeit bei den Operationen + + + //Timer, um bei den Berechnungen eine Sekunde lang zu warten + TimerScheduler timer; + Expression expression; //Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird @@ -47,12 +49,16 @@ public class SubActor extends AbstractBehavior { //Antwort von dem rechten Kind public record RightResponse(String string, int wert) implements Message{ } - private SubActor(ActorContext context){ + //Nachricht an sich selbst, um nach einer Sekunde Wartezeit das Ergebnis zu berechnen + public record Compute() implements Message{} + + private SubActor(ActorContext context, TimerScheduler timer){ super(context); + this.timer = timer; } public static Behavior create(){ - return Behaviors.setup(context -> new SubActor(context)); + return Behaviors.setup(context -> Behaviors.withTimers(timers -> new SubActor(context,timers))); } @Override @@ -62,6 +68,7 @@ public class SubActor extends AbstractBehavior { .onMessage(SubPrintAndEvaluate.class, this::onSubPrintAndEvaluate) .onMessage(LeftResponse.class, this::onLeftResponse) .onMessage(RightResponse.class, this::onRightResponse) + .onMessage(Compute.class, this::onComputeMessage) .build(); } @@ -100,7 +107,7 @@ public class SubActor extends AbstractBehavior { 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. public Behavior onSubPrintAndEvaluate(SubPrintAndEvaluate message){ this.oberActor = message.sender; @@ -141,57 +148,7 @@ public class SubActor extends AbstractBehavior { this.leftString = response.string; this.leftInt = response.wert; if(this.rightString != null){ - 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)); - } - } - } + timer.startSingleTimer(new Compute(), Duration.ofSeconds(1)); } return this; } @@ -200,58 +157,62 @@ public class SubActor extends AbstractBehavior { this.rightString = response.string; this.rightInt = response.wert; if(this.leftString != null){ - 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)); - } + timer.startSingleTimer(new Compute(), Duration.ofSeconds(1)); + } + return this; + } + + private Behavior 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(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)); - } + } 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; } - }