src/main/java/com/example/Actor.java gelöscht
This commit is contained in:
parent
2fca2c201c
commit
fb249b94a9
|
@ -1,127 +0,0 @@
|
||||||
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;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class Actor extends AbstractBehavior<Actor.Message> {
|
|
||||||
|
|
||||||
//Timer für die Berechnung
|
|
||||||
final TimerScheduler<Message> timer;
|
|
||||||
//Zurückgegebener String & Integer von der linken Seite
|
|
||||||
String leftString;
|
|
||||||
int leftInt;
|
|
||||||
|
|
||||||
//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;
|
|
||||||
|
|
||||||
String name;
|
|
||||||
Expression expression;
|
|
||||||
public interface Message{}
|
|
||||||
|
|
||||||
public record Compute() implements Message{}
|
|
||||||
|
|
||||||
public record PrintAndEvaluate(Expression expression) implements Message{}
|
|
||||||
|
|
||||||
//Antwort von dem linken Kind
|
|
||||||
public record LeftResponse(String string, int wert) implements Message{}
|
|
||||||
|
|
||||||
//Antwort von dem rechten Kind
|
|
||||||
public record RightResponse(String string, int wert) implements Message{}
|
|
||||||
|
|
||||||
private Actor(ActorContext<Message> context, String name, TimerScheduler<Message> timer){
|
|
||||||
super(context);
|
|
||||||
this.name = name;
|
|
||||||
this.timer = timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Behavior<Actor.Message> create(String name){
|
|
||||||
return Behaviors.setup(context -> Behaviors.withTimers(timers -> new Actor(context, name, timers)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Receive<Actor.Message> createReceive(){
|
|
||||||
return newReceiveBuilder()
|
|
||||||
.onMessage(PrintAndEvaluate.class, this::onPrintAndEvaluate)
|
|
||||||
.onMessage(LeftResponse.class, this::onLeftResponse)
|
|
||||||
.onMessage(RightResponse.class, this::onRightResponse)
|
|
||||||
.onMessage(Compute.class, this::onComputeMessage)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
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) {
|
|
||||||
logger.info(val.inner() + "");
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Message> onLeftResponse(LeftResponse response){
|
|
||||||
this.leftString = response.string;
|
|
||||||
this.leftInt = response.wert;
|
|
||||||
if(this.rightString != null){
|
|
||||||
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<Message> onRightResponse(RightResponse response){
|
|
||||||
this.rightString = response.string;
|
|
||||||
this.rightInt = response.wert;
|
|
||||||
if(this.leftString != null){
|
|
||||||
this.timer.startSingleTimer(new Compute(),Duration.ofSeconds(1));
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Behavior<Message> 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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue