From b5517e43122b002bc2f4c057ea990fe6a411adf4 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Sun, 5 May 2024 13:59:59 +0200 Subject: [PATCH] Blatt 03 --- src/main/java/com/example/AkkaMainSystem.java | 19 +++++--- src/main/java/com/example/ExampleActor.java | 35 --------------- .../java/com/example/ExampleTimerActor.java | 45 ------------------- src/main/java/com/example/Factorial.java | 43 ++++++++++++++++++ src/main/java/com/example/FactorialCont.java | 40 +++++++++++++++++ .../java/com/example/FactorialReceiver.java | 31 +++++++++++++ src/main/java/com/example/Fibonacci.java | 44 ++++++++++++++++++ src/main/java/com/example/FibonacciCont.java | 45 +++++++++++++++++++ .../java/com/example/FibonacciReceiver.java | 32 +++++++++++++ src/main/java/com/example/GCD.java | 40 +++++++++++++++++ src/main/java/com/example/GCDReceiver.java | 35 +++++++++++++++ 11 files changed, 324 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/com/example/ExampleActor.java delete mode 100644 src/main/java/com/example/ExampleTimerActor.java create mode 100644 src/main/java/com/example/Factorial.java create mode 100644 src/main/java/com/example/FactorialCont.java create mode 100644 src/main/java/com/example/FactorialReceiver.java create mode 100644 src/main/java/com/example/Fibonacci.java create mode 100644 src/main/java/com/example/FibonacciCont.java create mode 100644 src/main/java/com/example/FibonacciReceiver.java create mode 100644 src/main/java/com/example/GCD.java create mode 100644 src/main/java/com/example/GCDReceiver.java diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 868351b..5ea9467 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -23,12 +23,21 @@ public class AkkaMainSystem extends AbstractBehavior { } private Behavior onCreate(Create command) { - //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); - ActorRef b = this.getContext().spawn(ExampleTimerActor.create(), "timeractor"); - //#create-actors + // Factorial + var factorialReceiver = this.getContext().spawn(FactorialReceiver.create(), "FactorialReceiver"); + var factorial = this.getContext().spawn(Factorial.create(), "Factorial"); + factorial.tell(new Factorial.FactorialMsg(5, factorialReceiver)); + + // GCD + var gcdReceiver = this.getContext().spawn(GCDReceiver.create(), "GCDReceiver"); + var gcd = this.getContext().spawn(GCD.create(), "GCD"); + gcd.tell(new GCD.GCDMessage(127, 74, gcdReceiver)); + + // Fibonacci + var fibonacciReceiver = this.getContext().spawn(FibonacciReceiver.create(), "FibonacciReceiver"); + var fibonacci = this.getContext().spawn(Fibonacci.create(), "Fibonacci"); + fibonacci.tell(new Fibonacci.FibonacciMessage(7, fibonacciReceiver)); - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); return this; } } diff --git a/src/main/java/com/example/ExampleActor.java b/src/main/java/com/example/ExampleActor.java deleted file mode 100644 index cf56713..0000000 --- a/src/main/java/com/example/ExampleActor.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.example; - -import akka.actor.typed.ActorRef; -import akka.actor.typed.Behavior; -import akka.actor.typed.javadsl.*; - -public class ExampleActor extends AbstractBehavior { - - public interface Message {}; - - public record ExampleMessage(ActorRef someReference, String someString) implements Message { } - - public static Behavior create(String name) { - return Behaviors.setup(context -> new ExampleActor(context, name)); - } - - private final String name; - - private ExampleActor(ActorContext context, String name) { - super(context); - this.name = name; - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/ExampleTimerActor.java b/src/main/java/com/example/ExampleTimerActor.java deleted file mode 100644 index 0bffbea..0000000 --- a/src/main/java/com/example/ExampleTimerActor.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.example; - -import akka.actor.typed.javadsl.TimerScheduler; -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.time.Duration; - - -public class ExampleTimerActor extends AbstractBehavior { - - public interface Message {}; - - - public record ExampleMessage(String someString) implements Message { } - - public static Behavior create() { - return Behaviors.setup(context -> Behaviors.withTimers(timers -> new ExampleTimerActor(context, timers))); - } - - private final TimerScheduler timers; - - private ExampleTimerActor(ActorContext context, TimerScheduler timers) { - super(context); - this.timers = timers; - - Message msg = new ExampleMessage("test123"); - this.timers.startSingleTimer(msg, msg, Duration.ofSeconds(10)); - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I have send myself this message after 10 Seconds: {}", msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/Factorial.java b/src/main/java/com/example/Factorial.java new file mode 100644 index 0000000..53a0d19 --- /dev/null +++ b/src/main/java/com/example/Factorial.java @@ -0,0 +1,43 @@ +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.UUID; + +public class Factorial extends AbstractBehavior { + + public interface Message {}; + + public record FactorialMsg(int val, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(Factorial::new); + } + + private Factorial(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FactorialMsg.class, this::onMsg) + .build(); + } + + private Behavior onMsg(FactorialMsg msg) { + if (msg.val == 0){ + msg.cust.tell(new FactorialCont.FactorialContMsg(1)); + } else { + var cont = this.getContext().spawn(FactorialCont.create(msg.val, msg.cust), UUID.randomUUID().toString()); + this.getContext().getSelf().tell(new FactorialMsg(msg.val - 1, cont)); + } + + return this; + } +} diff --git a/src/main/java/com/example/FactorialCont.java b/src/main/java/com/example/FactorialCont.java new file mode 100644 index 0000000..254ef15 --- /dev/null +++ b/src/main/java/com/example/FactorialCont.java @@ -0,0 +1,40 @@ +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; + +public class FactorialCont extends AbstractBehavior { + + private final ActorRef cust; + private final int val; + + public interface Message {}; + + public record FactorialContMsg(int arg) implements Message { } + + public static Behavior create(int val, ActorRef cust) { + return Behaviors.setup(context -> new FactorialCont(context, val, cust)); + } + + private FactorialCont(ActorContext context, int val, ActorRef cust) { + super(context); + this.val = val; + this.cust = cust; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FactorialContMsg.class, this::onMsg) + .build(); + } + + private Behavior onMsg(FactorialContMsg msg) { + this.cust.tell(new FactorialContMsg(this.val * msg.arg)); + return this; + } +} diff --git a/src/main/java/com/example/FactorialReceiver.java b/src/main/java/com/example/FactorialReceiver.java new file mode 100644 index 0000000..44cbf40 --- /dev/null +++ b/src/main/java/com/example/FactorialReceiver.java @@ -0,0 +1,31 @@ +package com.example; + +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; + +public class FactorialReceiver extends AbstractBehavior { + + public static Behavior create() { + return Behaviors.setup(FactorialReceiver::new); + } + + + private FactorialReceiver(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FactorialCont.FactorialContMsg.class, this::onMsg) + .build(); + } + + private Behavior onMsg(FactorialCont.FactorialContMsg msg) { + this.getContext().getLog().info("Factorial: {}", msg.arg()); + return this; + } +} diff --git a/src/main/java/com/example/Fibonacci.java b/src/main/java/com/example/Fibonacci.java new file mode 100644 index 0000000..06c1b72 --- /dev/null +++ b/src/main/java/com/example/Fibonacci.java @@ -0,0 +1,44 @@ +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.UUID; + +public class Fibonacci extends AbstractBehavior { + + public interface Message {}; + + public record FibonacciMessage(int n, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(Fibonacci::new); + } + + + private Fibonacci(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibonacciMessage.class, this::onExampleMessage) + .build(); + } + + private Behavior onExampleMessage(FibonacciMessage msg) { + if (msg.n <= 1) { + msg.cust.tell(new FibonacciCont.FibonacciContMessage(msg.n)); + } else { + var cont = this.getContext().spawn(FibonacciCont.create(-1, msg.cust), UUID.randomUUID().toString()); + this.getContext().getSelf().tell(new FibonacciMessage(msg.n - 1, cont)); + this.getContext().getSelf().tell(new FibonacciMessage(msg.n - 2, cont)); + } + return this; + } +} diff --git a/src/main/java/com/example/FibonacciCont.java b/src/main/java/com/example/FibonacciCont.java new file mode 100644 index 0000000..9db0515 --- /dev/null +++ b/src/main/java/com/example/FibonacciCont.java @@ -0,0 +1,45 @@ +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; + +public class FibonacciCont extends AbstractBehavior { + + private final ActorRef cust; + + public interface Message {}; + + public record FibonacciContMessage(int result) implements Message { } + + public static Behavior create(int first_value, ActorRef cust) { + return Behaviors.setup(context -> new FibonacciCont(context, first_value, cust)); + } + + private final int first_value; + + private FibonacciCont(ActorContext context, int first_value, ActorRef cust) { + super(context); + this.first_value = first_value; + this.cust = cust; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibonacciContMessage.class, this::onExampleMessage) + .build(); + } + + private Behavior onExampleMessage(FibonacciContMessage msg) { + if (this.first_value == -1) { + return FibonacciCont.create(msg.result, this.cust); + } else { + this.cust.tell(new FibonacciContMessage(msg.result + this.first_value)); + } + return this; + } +} diff --git a/src/main/java/com/example/FibonacciReceiver.java b/src/main/java/com/example/FibonacciReceiver.java new file mode 100644 index 0000000..35e4fcc --- /dev/null +++ b/src/main/java/com/example/FibonacciReceiver.java @@ -0,0 +1,32 @@ +package com.example; + +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; + +public class FibonacciReceiver extends AbstractBehavior { + + + public static Behavior create() { + return Behaviors.setup(FibonacciReceiver::new); + } + + + private FibonacciReceiver(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibonacciCont.FibonacciContMessage.class, this::onExampleMessage) + .build(); + } + + private Behavior onExampleMessage(FibonacciCont.FibonacciContMessage msg) { + getContext().getLog().info("Fibonacci: {}",msg.result()); + return this; + } +} diff --git a/src/main/java/com/example/GCD.java b/src/main/java/com/example/GCD.java new file mode 100644 index 0000000..d0b4936 --- /dev/null +++ b/src/main/java/com/example/GCD.java @@ -0,0 +1,40 @@ +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; + +public class GCD extends AbstractBehavior { + + public interface Message {}; + + public record GCDMessage(int a, int b, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(GCD::new); + } + + + private GCD(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(GCDMessage.class, this::onMsg) + .build(); + } + + private Behavior onMsg(GCDMessage msg) { + if (msg.b == 0) { + msg.cust.tell(new GCDReceiver.GCDResult(msg.a)); + } else { + this.getContext().getSelf().tell(new GCDMessage(msg.b, msg.a % msg.b, msg.cust)); + } + return this; + } +} diff --git a/src/main/java/com/example/GCDReceiver.java b/src/main/java/com/example/GCDReceiver.java new file mode 100644 index 0000000..c1eb06b --- /dev/null +++ b/src/main/java/com/example/GCDReceiver.java @@ -0,0 +1,35 @@ +package com.example; + +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; + +public class GCDReceiver extends AbstractBehavior { + + public interface Message {}; + + public record GCDResult(int result) implements Message { } + + public static Behavior create() { + return Behaviors.setup(GCDReceiver::new); + } + + + private GCDReceiver(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(GCDResult.class, this::onResult) + .build(); + } + + private Behavior onResult(GCDResult msg) { + getContext().getLog().info("GCD: {}", msg.result); + return this; + } +}