From f193f80acc370ee76fabed385c429f1c98fce584 Mon Sep 17 00:00:00 2001 From: Egor Rutkowski Date: Tue, 2 Jul 2024 14:54:25 +0200 Subject: [PATCH] =?UTF-8?q?Berechnung=20der=20Werte=20vollst=C3=A4ndig=20i?= =?UTF-8?q?mplementiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/Actor.java | 50 ++++--- src/main/java/com/example/AkkaMainSystem.java | 7 +- src/main/java/com/example/SubActor.java | 125 ++++++++++++++---- 3 files changed, 139 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/example/Actor.java b/src/main/java/com/example/Actor.java index 9ad12a2..7bc9c78 100644 --- a/src/main/java/com/example/Actor.java +++ b/src/main/java/com/example/Actor.java @@ -8,14 +8,17 @@ import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; import java.util.Objects; +import java.util.logging.Logger; public class Actor extends AbstractBehavior { //TODO: Wartezeit bei der Operation - //Zurückgegebener String von der linken Seite - String leftSide; + //Zurückgegebener String & Integer von der linken Seite + String leftString; + int leftInt; - //Zurückgegebener String von der rechten Seite - String rightSide; + //Zurückgegebener String & Integer von der rechten Seite + String rightString; + int rightInt; //Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird String operationString; @@ -24,13 +27,13 @@ public class Actor extends AbstractBehavior { Expression expression; public interface Message{} - public record StartMessage(Expression expression) implements Message{} + public record PrintAndEvaluate(Expression expression) implements Message{} //Antwort von dem linken Kind - public record LeftResponse(String string) implements Message{} + public record LeftResponse(String string, int wert) implements Message{} //Antwort von dem rechten Kind - public record RightResponse(String string) implements Message{} + public record RightResponse(String string, int wert) implements Message{} private Actor(ActorContext context, String name){ super(context); @@ -44,13 +47,13 @@ public class Actor extends AbstractBehavior { @Override public Receive createReceive(){ return newReceiveBuilder() - .onMessage(StartMessage.class, this::onStartMessage) + .onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate) .onMessage(LeftResponse.class, this::onLeftResponse) .onMessage(RightResponse.class, this::onRightResponse) .build(); } - private Behavior onStartMessage(StartMessage message){ + private Behavior onPrintAndEvaluate(PrintAndEvaluate message){ this.expression = message.expression; //Wenn initial nur Val übergeben wird → einfach ausgeben ansonsten UnterActors erstellen if (Objects.requireNonNull(expression) instanceof Expression.Add add) { @@ -74,23 +77,38 @@ public class Actor extends AbstractBehavior { ActorRef RightSubActor = getContext().spawnAnonymous(SubActor.create()); RightSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),mul.right(), false)); } else if (expression instanceof Expression.Val val) { - System.out.println(val.inner()); + logger.info(val.inner() + ""); } return this; } + Logger logger = Logger.getLogger(Actor.class.getName()); private Behavior onLeftResponse(LeftResponse response){ - this.leftSide = response.string; - if(this.rightSide != null){ - System.out.println("(" + leftSide + operationString + rightSide + ")"); + 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)); + } } return this; } private Behavior onRightResponse(RightResponse response){ - this.rightSide = response.string; - if(this.leftSide != null){ - System.out.println("(" + leftSide + operationString + rightSide + ")"); + 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)); + } } return this; } diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index a575eec..e9dd534 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -29,9 +29,10 @@ public class AkkaMainSystem extends AbstractBehavior { //#create-actors Expression expression = Expression.generateExpression(6, 9); Expression testExp = Expression.generateExpression(4,9); - ActorRef test = this.getContext().spawn(Actor.create("Seng"), "Sengmann"); - test.tell(new Actor.StartMessage(testExp)); - System.out.println("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval()); + ActorRef computer = this.getContext().spawn(Actor.create("Rechner"), "Rechner"); + computer.tell(new Actor.PrintAndEvaluate(testExp)); + //Vergleich mit dem Output der Berechnung + System.out.println("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval() + " Runtime: " + testExp.runtime()); return this; } } diff --git a/src/main/java/com/example/SubActor.java b/src/main/java/com/example/SubActor.java index 3103216..e6169d7 100644 --- a/src/main/java/com/example/SubActor.java +++ b/src/main/java/com/example/SubActor.java @@ -25,11 +25,13 @@ public class SubActor extends AbstractBehavior { //Linke Seite: true, Rechte Seite: false um zu wissen welche Seite des Ausdrucks berechnet wird. boolean side; - //Zurückgegebener String von der linken Seite - String leftSide; + //Zurückgegebener String & Integer von der linken Seite + String leftString; + int leftInt; - //Zurückgegebener String von der rechten Seite - String rightSide; + //Zurückgegebener String & Integer von der rechten Seite + String rightString; + int rightInt; public interface Message {} @@ -39,11 +41,11 @@ public class SubActor extends AbstractBehavior { //Message kommt von einem anderen SubActor public record SubPrintAndEvaluate(ActorRef sender,Expression expression, Boolean side) implements Message{ } - //Antwort von dem linken Kind (TODO: Später Integer hinzufügen wenn mit den Strings alles funktioniert) - public record LeftResponse(String string) implements Message{ } + //Antwort von dem linken Kind + public record LeftResponse(String string, int wert) implements Message{ } - //Antwort von dem rechten Kind (TODO: Später Integer hinzufügen wenn mit den Strings alles funktioniert) - public record RightResponse(String string) implements Message{ } + //Antwort von dem rechten Kind + public record RightResponse(String string, int wert) implements Message{ } private SubActor(ActorContext context){ super(context); @@ -90,9 +92,9 @@ public class SubActor extends AbstractBehavior { subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false)); } else if (expression instanceof Expression.Val val) { if(side == true){ - initial.tell(new Actor.LeftResponse(String.valueOf(val.inner()))); + initial.tell(new Actor.LeftResponse(String.valueOf(val.inner()), val.inner())); }else{ - initial.tell(new Actor.RightResponse(String.valueOf(val.inner()))); + initial.tell(new Actor.RightResponse(String.valueOf(val.inner()), val.inner())); } } return this; @@ -127,29 +129,67 @@ public class SubActor extends AbstractBehavior { subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false)); } else if (expression instanceof Expression.Val val) { if(side == true){ - oberActor.tell(new SubActor.LeftResponse(String.valueOf(val.inner()))); + oberActor.tell(new SubActor.LeftResponse(String.valueOf(val.inner()), val.inner())); }else{ - oberActor.tell(new SubActor.RightResponse(String.valueOf(val.inner()))); + oberActor.tell(new SubActor.RightResponse(String.valueOf(val.inner()), val.inner())); } } return this; } public Behavior onLeftResponse(LeftResponse response){ - this.leftSide = response.string; - if(this.rightSide != null){ + 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){ - oberActor.tell(new SubActor.LeftResponse("(" + leftSide + operationString + rightSide + ")")); + 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 { - oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")")); + 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){ - initial.tell(new Actor.LeftResponse("(" + leftSide + operationString + rightSide + ")")); + 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 { - initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")")); + 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)); + } } } } @@ -157,20 +197,57 @@ public class SubActor extends AbstractBehavior { } public Behavior onRightResponse(RightResponse response){ - this.rightSide = response.string; - if(this.leftSide != null){ + 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){ - oberActor.tell(new SubActor.LeftResponse("(" + leftSide + operationString + rightSide + ")")); + 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 { - oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")")); + 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){ - initial.tell(new Actor.LeftResponse("(" + leftSide + operationString + rightSide + ")")); + 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 { - initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")")); + 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)); + } } } }