Berechnung der Werte vollständig implementiert

This commit is contained in:
Egor Rutkowski 2024-07-02 14:54:25 +02:00
parent f0306b96b1
commit f193f80acc
3 changed files with 139 additions and 43 deletions

View File

@ -8,14 +8,17 @@ import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive; import akka.actor.typed.javadsl.Receive;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger;
public class Actor extends AbstractBehavior<Actor.Message> { public class Actor extends AbstractBehavior<Actor.Message> {
//TODO: Wartezeit bei der Operation //TODO: Wartezeit bei der Operation
//Zurückgegebener String von der linken Seite //Zurückgegebener String & Integer von der linken Seite
String leftSide; String leftString;
int leftInt;
//Zurückgegebener String von der rechten Seite //Zurückgegebener String & Integer von der rechten Seite
String rightSide; String rightString;
int rightInt;
//Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird //Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird
String operationString; String operationString;
@ -24,13 +27,13 @@ public class Actor extends AbstractBehavior<Actor.Message> {
Expression expression; Expression expression;
public interface Message{} public interface Message{}
public record StartMessage(Expression expression) implements Message{} public record PrintAndEvaluate(Expression expression) implements Message{}
//Antwort von dem linken Kind //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 //Antwort von dem rechten Kind
public record RightResponse(String string) 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){
super(context); super(context);
@ -44,13 +47,13 @@ public class Actor extends AbstractBehavior<Actor.Message> {
@Override @Override
public Receive<Actor.Message> createReceive(){ public Receive<Actor.Message> createReceive(){
return newReceiveBuilder() return newReceiveBuilder()
.onMessage(StartMessage.class, this::onStartMessage) .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)
.build(); .build();
} }
private Behavior<Message> onStartMessage(StartMessage message){ private Behavior<Message> onPrintAndEvaluate(PrintAndEvaluate message){
this.expression = message.expression; this.expression = message.expression;
//Wenn initial nur Val übergeben wird einfach ausgeben ansonsten UnterActors erstellen //Wenn initial nur Val übergeben wird einfach ausgeben ansonsten UnterActors erstellen
if (Objects.requireNonNull(expression) instanceof Expression.Add add) { if (Objects.requireNonNull(expression) instanceof Expression.Add add) {
@ -74,23 +77,38 @@ public class Actor extends AbstractBehavior<Actor.Message> {
ActorRef<SubActor.Message> RightSubActor = getContext().spawnAnonymous(SubActor.create()); ActorRef<SubActor.Message> RightSubActor = getContext().spawnAnonymous(SubActor.create());
RightSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),mul.right(), false)); RightSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),mul.right(), false));
} else if (expression instanceof Expression.Val val) { } else if (expression instanceof Expression.Val val) {
System.out.println(val.inner()); logger.info(val.inner() + "");
} }
return this; return this;
} }
Logger logger = Logger.getLogger(Actor.class.getName());
private Behavior<Message> onLeftResponse(LeftResponse response){ private Behavior<Message> onLeftResponse(LeftResponse response){
this.leftSide = response.string; this.leftString = response.string;
if(this.rightSide != null){ this.leftInt = response.wert;
System.out.println("(" + leftSide + operationString + rightSide + ")"); 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; return this;
} }
private Behavior<Message> onRightResponse(RightResponse response){ private Behavior<Message> onRightResponse(RightResponse response){
this.rightSide = response.string; this.rightString = response.string;
if(this.leftSide != null){ this.rightInt = response.wert;
System.out.println("(" + leftSide + operationString + rightSide + ")"); 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; return this;
} }

View File

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

View File

@ -25,11 +25,13 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
//Linke Seite: true, Rechte Seite: false um zu wissen welche Seite des Ausdrucks berechnet wird. //Linke Seite: true, Rechte Seite: false um zu wissen welche Seite des Ausdrucks berechnet wird.
boolean side; boolean side;
//Zurückgegebener String von der linken Seite //Zurückgegebener String & Integer von der linken Seite
String leftSide; String leftString;
int leftInt;
//Zurückgegebener String von der rechten Seite //Zurückgegebener String & Integer von der rechten Seite
String rightSide; String rightString;
int rightInt;
public interface Message {} public interface Message {}
@ -39,11 +41,11 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
//Message kommt von einem anderen SubActor //Message kommt von einem anderen SubActor
public record SubPrintAndEvaluate(ActorRef<SubActor.Message> sender,Expression expression, Boolean side) implements Message{ } public record SubPrintAndEvaluate(ActorRef<SubActor.Message> sender,Expression expression, Boolean side) implements Message{ }
//Antwort von dem linken Kind (TODO: Später Integer hinzufügen wenn mit den Strings alles funktioniert) //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 (TODO: Später Integer hinzufügen wenn mit den Strings alles funktioniert) //Antwort von dem rechten Kind
public record RightResponse(String string) implements Message{ } public record RightResponse(String string, int wert) implements Message{ }
private SubActor(ActorContext<SubActor.Message> context){ private SubActor(ActorContext<SubActor.Message> context){
super(context); super(context);
@ -90,9 +92,9 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false)); subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false));
} else if (expression instanceof Expression.Val val) { } else if (expression instanceof Expression.Val val) {
if(side == true){ if(side == true){
initial.tell(new Actor.LeftResponse(String.valueOf(val.inner()))); initial.tell(new Actor.LeftResponse(String.valueOf(val.inner()), val.inner()));
}else{ }else{
initial.tell(new Actor.RightResponse(String.valueOf(val.inner()))); initial.tell(new Actor.RightResponse(String.valueOf(val.inner()), val.inner()));
} }
} }
return this; return this;
@ -127,29 +129,67 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false)); subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.right(), false));
} else if (expression instanceof Expression.Val val) { } else if (expression instanceof Expression.Val val) {
if(side == true){ if(side == true){
oberActor.tell(new SubActor.LeftResponse(String.valueOf(val.inner()))); oberActor.tell(new SubActor.LeftResponse(String.valueOf(val.inner()), val.inner()));
}else{ }else{
oberActor.tell(new SubActor.RightResponse(String.valueOf(val.inner()))); oberActor.tell(new SubActor.RightResponse(String.valueOf(val.inner()), val.inner()));
} }
} }
return this; return this;
} }
public Behavior<Message> onLeftResponse(LeftResponse response){ public Behavior<Message> onLeftResponse(LeftResponse response){
this.leftSide = response.string; this.leftString = response.string;
if(this.rightSide != null){ this.leftInt = response.wert;
if(this.rightString != null){
if(oberActor != null){ if(oberActor != null){
//side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt //side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt
if(side == true){ 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{ }else{
oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")")); 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{ }else{
if(side == true){ 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{ }else{
initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")")); 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));
}
} }
} }
} }
@ -157,20 +197,57 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
} }
public Behavior<Message> onRightResponse(RightResponse response){ public Behavior<Message> onRightResponse(RightResponse response){
this.rightSide = response.string; this.rightString = response.string;
if(this.leftSide != null){ this.rightInt = response.wert;
if(this.leftString != null){
if(oberActor != null){ if(oberActor != null){
//side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt //side == true bedeutet, dass dieser Actor das linke Kind ist und es wird LeftResponse geschickt
if(side == true){ 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{ }else{
oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")")); 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 { }else {
if(side == true){ 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{ }else{
initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")")); 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));
}
} }
} }
} }