String Ausgabe fertig und korrekt

This commit is contained in:
Egor Rutkowski 2024-07-01 18:28:40 +02:00
parent f5f7cd2e7e
commit f0306b96b1
3 changed files with 240 additions and 9 deletions

View File

@ -1,12 +1,24 @@
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 java.util.Objects;
public class Actor extends AbstractBehavior<Actor.Message> {
//TODO: Wartezeit bei der Operation
//Zurückgegebener String von der linken Seite
String leftSide;
//Zurückgegebener String von der rechten Seite
String rightSide;
//Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird
String operationString;
String name;
Expression expression;
@ -14,6 +26,12 @@ public class Actor extends AbstractBehavior<Actor.Message> {
public record StartMessage(Expression expression) implements Message{}
//Antwort von dem linken Kind
public record LeftResponse(String string) implements Message{}
//Antwort von dem rechten Kind
public record RightResponse(String string) implements Message{}
private Actor(ActorContext<Message> context, String name){
super(context);
this.name = name;
@ -27,15 +45,52 @@ public class Actor extends AbstractBehavior<Actor.Message> {
public Receive<Actor.Message> createReceive(){
return newReceiveBuilder()
.onMessage(StartMessage.class, this::onStartMessage)
.onMessage(LeftResponse.class, this::onLeftResponse)
.onMessage(RightResponse.class, this::onRightResponse)
.build();
}
private Behavior<Message> onStartMessage(StartMessage message){
this.expression = message.expression;
//Wenn initial nur Val übergeben wird -> einfach ausgeben
if(expression instanceof Expression.Val){
System.out.println(((Expression.Val) expression).inner());
//Wenn initial nur Val übergeben wird einfach ausgeben ansonsten UnterActors erstellen
if (Objects.requireNonNull(expression) instanceof Expression.Add add) {
this.operationString = "+";
ActorRef<SubActor.Message> LeftSubActor = getContext().spawnAnonymous(SubActor.create());
LeftSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),add.left(), true));
ActorRef<SubActor.Message> RightSubActor = getContext().spawnAnonymous(SubActor.create());
RightSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),add.right(), false));
}
else if (expression instanceof Expression.Sub sub) {
this.operationString = "-";
ActorRef<SubActor.Message> LeftSubActor = getContext().spawnAnonymous(SubActor.create());
LeftSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),sub.left(), true));
ActorRef<SubActor.Message> RightSubActor = getContext().spawnAnonymous(SubActor.create());
RightSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),sub.right(), false));
}
else if (expression instanceof Expression.Mul mul) {
this.operationString = "*";
ActorRef<SubActor.Message> LeftSubActor = getContext().spawnAnonymous(SubActor.create());
LeftSubActor.tell(new SubActor.PrintAndEvaluate(getContext().getSelf(),mul.left(), true));
ActorRef<SubActor.Message> 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());
}
return this;
}
private Behavior<Message> onLeftResponse(LeftResponse response){
this.leftSide = response.string;
if(this.rightSide != null){
System.out.println("(" + leftSide + operationString + rightSide + ")");
}
return this;
}
private Behavior<Message> onRightResponse(RightResponse response){
this.rightSide = response.string;
if(this.leftSide != null){
System.out.println("(" + leftSide + operationString + rightSide + ")");
}
return this;
}

View File

@ -26,16 +26,12 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
}
private Behavior<Create> onCreate(Create command) {
//#create-actors
ActorRef<ExampleActor.Message> a = this.getContext().spawn(ExampleActor.create("Alice"), "alice");
ActorRef<ExampleTimerActor.Message> b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor");
//#create-actors
Expression expression = Expression.generateExpression(6, 9);
Expression testExp = Expression.generateExpression(1,9);
Expression testExp = Expression.generateExpression(4,9);
ActorRef<Actor.Message> test = this.getContext().spawn(Actor.create("Seng"), "Sengmann");
test.tell(new Actor.StartMessage(testExp));
System.out.println(testExp.toString()+ " Wert:" +expression.eval());
System.out.println("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval());
return this;
}
}

View File

@ -0,0 +1,180 @@
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 java.util.Objects;
public class SubActor extends AbstractBehavior<SubActor.Message> {
//TODO: Wartezeit bei den Operationen
Expression expression;
//Zeichen für die Operation (Add, Sub, Mul) welche benutzt wird
String operationString;
//Referenz auf den initialen Actor
ActorRef<Actor.Message> initial;
//Referenz auf den (potenziellen) OberSubActor. Es ist immer entweder initial oder oberActor null.
ActorRef<SubActor.Message> oberActor;
//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 von der rechten Seite
String rightSide;
public interface Message {}
//Message kommt von dem initialen Actor
public record PrintAndEvaluate(ActorRef<Actor.Message> sender,Expression expression, Boolean side) implements Message{ }
//Message kommt von einem anderen SubActor
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)
public record LeftResponse(String string) 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{ }
private SubActor(ActorContext<SubActor.Message> context){
super(context);
}
public static Behavior<SubActor.Message> create(){
return Behaviors.setup(context -> new SubActor(context));
}
@Override
public Receive<SubActor.Message> createReceive(){
return newReceiveBuilder()
.onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate)
.onMessage(SubPrintAndEvaluate.class, this::onSubPrintAndEvaluate)
.onMessage(LeftResponse.class, this::onLeftResponse)
.onMessage(RightResponse.class, this::onRightResponse)
.build();
}
//Die Nachricht von dem initialen Actor wird verarbeitet.
//Dabei wird seine Referenz in @initial gespeichert
public Behavior<Message> onPrintAndEvaluate(PrintAndEvaluate message) {
this.initial = message.sender;
this.expression = message.expression;
this.side = message.side;
//Wenn initial nur Val übergeben wird einfach ausgeben ansonsten UnterActors erstellen
if (Objects.requireNonNull(expression) instanceof Expression.Add add) {
this.operationString = "+";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(), add.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(), add.right(), false));
} else if (expression instanceof Expression.Sub sub) {
this.operationString = "-";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(),sub.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),sub.right(), false));
} else if (expression instanceof Expression.Mul mul) {
this.operationString = "*";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
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())));
}else{
initial.tell(new Actor.RightResponse(String.valueOf(val.inner())));
}
}
return this;
}
//Nachricht von einem SubActor wird verarbeitet.
//Dabei wird seine Referenz in oberActor gespeichert.
public Behavior<Message> onSubPrintAndEvaluate(SubPrintAndEvaluate message){
this.oberActor = message.sender;
this.expression = message.expression;
this.side = message.side;
//Wenn initial nur Val übergeben wird einfach ausgeben ansonsten UnterActors erstellen
if (Objects.requireNonNull(expression) instanceof Expression.Add add) {
this.operationString = "+";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(),add.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),add.right(), false));
}
else if (expression instanceof Expression.Sub sub) {
this.operationString = "-";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(),sub.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
subActorRight.tell(new SubPrintAndEvaluate(getContext().getSelf(),sub.right(), false));
}
else if (expression instanceof Expression.Mul mul) {
this.operationString = "*";
ActorRef<Message> subActorLeft = getContext().spawnAnonymous(SubActor.create());
subActorLeft.tell(new SubPrintAndEvaluate(getContext().getSelf(),mul.left(), true));
ActorRef<Message> subActorRight = getContext().spawnAnonymous(SubActor.create());
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())));
}else{
oberActor.tell(new SubActor.RightResponse(String.valueOf(val.inner())));
}
}
return this;
}
public Behavior<Message> onLeftResponse(LeftResponse response){
this.leftSide = response.string;
if(this.rightSide != 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 + ")"));
}else {
oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")"));
}
}else{
if(side == true){
initial.tell(new Actor.LeftResponse("(" + leftSide + operationString + rightSide + ")"));
}else {
initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")"));
}
}
}
return this;
}
public Behavior<Message> onRightResponse(RightResponse response){
this.rightSide = response.string;
if(this.leftSide != 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 + ")"));
}else {
oberActor.tell(new SubActor.RightResponse("(" + leftSide + operationString + rightSide + ")"));
}
}else {
if(side == true){
initial.tell(new Actor.LeftResponse("(" + leftSide + operationString + rightSide + ")"));
}else {
initial.tell(new Actor.RightResponse("(" + leftSide + operationString + rightSide + ")"));
}
}
}
return this;
}
}