From 6f2af5f48144ddac4365fa8767941f150e94dc85 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Wed, 10 May 2023 16:36:43 +0200 Subject: [PATCH] Fibonacci --- src/main/java/com/example/AkkaMainSystem.java | 5 +- src/main/java/com/example/C.java | 30 ++++++++++++ src/main/java/com/example/ExampleActor.java | 35 -------------- src/main/java/com/example/Fib.java | 41 ++++++++++++++++ src/main/java/com/example/FibCont.java | 48 +++++++++++++++++++ 5 files changed, 122 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/example/C.java delete mode 100644 src/main/java/com/example/ExampleActor.java create mode 100644 src/main/java/com/example/Fib.java create mode 100644 src/main/java/com/example/FibCont.java diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java index 7ddda70..9b2ba93 100644 --- a/src/main/java/com/example/AkkaMainSystem.java +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -24,10 +24,11 @@ public class AkkaMainSystem extends AbstractBehavior { private Behavior onCreate(Create command) { //#create-actors - ActorRef a = this.getContext().spawn(ExampleActor.create("Alice"), "alice"); + ActorRef c = this.getContext().spawn(C.create(), "c"); + ActorRef fib = this.getContext().spawn(Fib.create(), "fib"); //#create-actors - a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + fib.tell(new Fib.FibMessage(7, c)); return this; } } diff --git a/src/main/java/com/example/C.java b/src/main/java/com/example/C.java new file mode 100644 index 0000000..2ef0199 --- /dev/null +++ b/src/main/java/com/example/C.java @@ -0,0 +1,30 @@ +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 C extends AbstractBehavior { + public static Behavior create() { + return Behaviors.setup(C::new); + } + + + private C(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibCont.FibContMessage.class, this::onFibContMessage) + .build(); + } + + private Behavior onFibContMessage(FibCont.FibContMessage msg) { + getContext().getLog().info("{}", msg.n()); + 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/Fib.java b/src/main/java/com/example/Fib.java new file mode 100644 index 0000000..91a7607 --- /dev/null +++ b/src/main/java/com/example/Fib.java @@ -0,0 +1,41 @@ +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 Fib extends AbstractBehavior { + + public interface Message {}; + + public record FibMessage(int n, ActorRef cust) implements Message { } + + public static Behavior create() { + return Behaviors.setup(Fib::new); + } + + private Fib(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibMessage.class, this::onFibMessage) + .build(); + } + + private Behavior onFibMessage(FibMessage msg) { + if(msg.n <= 1) { + msg.cust.tell(new FibCont.FibContMessage(msg.n)); + } else { + ActorRef cont = this.getContext().spawnAnonymous(FibCont.create(msg.cust)); + this.getContext().getSelf().tell(new FibMessage(msg.n - 1, cont)); + this.getContext().getSelf().tell(new FibMessage(msg.n - 2, cont)); + } + return this; + } +} diff --git a/src/main/java/com/example/FibCont.java b/src/main/java/com/example/FibCont.java new file mode 100644 index 0000000..0d11063 --- /dev/null +++ b/src/main/java/com/example/FibCont.java @@ -0,0 +1,48 @@ +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 FibCont extends AbstractBehavior { + + private boolean flag; + private int store; + + public interface Message {}; + + public record FibContMessage(int n) implements Message { } + + public static Behavior create(ActorRef cust) { + return Behaviors.setup(context -> new FibCont(context, cust)); + } + + private final ActorRef cust; + + private FibCont(ActorContext context, ActorRef cust) { + super(context); + this.cust = cust; + this.flag = false; + this.store = 0; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(FibContMessage.class, this::onFibContMessage) + .build(); + } + + private Behavior onFibContMessage(FibContMessage msg) { + if (this.flag) { + this.cust.tell(new FibContMessage(this.store + msg.n)); + } else { + this.flag = true; + this.store = msg.n; + } + return this; + } +}