Berechnung der Werte vollständig implementiert
This commit is contained in:
parent
f0306b96b1
commit
f193f80acc
|
@ -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<Actor.Message> {
|
||||
//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<Actor.Message> {
|
|||
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<Message> context, String name){
|
||||
super(context);
|
||||
|
@ -44,13 +47,13 @@ public class Actor extends AbstractBehavior<Actor.Message> {
|
|||
@Override
|
||||
public Receive<Actor.Message> 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<Message> onStartMessage(StartMessage message){
|
||||
private Behavior<Message> 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<Actor.Message> {
|
|||
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());
|
||||
logger.info(val.inner() + "");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Logger logger = Logger.getLogger(Actor.class.getName());
|
||||
private Behavior<Message> 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<Message> 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;
|
||||
}
|
||||
|
|
|
@ -29,9 +29,10 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
|
|||
//#create-actors
|
||||
Expression expression = Expression.generateExpression(6, 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("SOLL: "+testExp.toString()+ " Wert:" +testExp.eval());
|
||||
ActorRef<Actor.Message> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
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<SubActor.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 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<SubActor.Message> context){
|
||||
super(context);
|
||||
|
@ -90,9 +92,9 @@ public class SubActor extends AbstractBehavior<SubActor.Message> {
|
|||
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<SubActor.Message> {
|
|||
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<Message> 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 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{
|
||||
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.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){
|
||||
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 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 {
|
||||
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.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue