Compare commits

..

1 Commits

Author SHA1 Message Date
Christoph Stahl 6f2af5f481 Fibonacci 2023-05-10 16:36:43 +02:00
6 changed files with 100 additions and 94 deletions

View File

@ -24,11 +24,11 @@ public class AkkaMainSystem extends AbstractBehavior<AkkaMainSystem.Create> {
private Behavior<Create> onCreate(Create command) { private Behavior<Create> onCreate(Create command) {
//#create-actors //#create-actors
ActorRef<FakCont.Message> c = this.getContext().spawn(C.create(), "c"); ActorRef<FibCont.Message> c = this.getContext().spawn(C.create(), "c");
ActorRef<Fak.Message> fak = this.getContext().spawn(Fak.create(), "fak"); ActorRef<Fib.Message> fib = this.getContext().spawn(Fib.create(), "fib");
//#create-actors //#create-actors
fak.tell(new Fak.FakMessage(5, c)); fib.tell(new Fib.FibMessage(7, c));
return this; return this;
} }
} }

View File

@ -6,25 +6,25 @@ import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive; import akka.actor.typed.javadsl.Receive;
public class C extends AbstractBehavior<FakCont.Message> { public class C extends AbstractBehavior<FibCont.Message> {
public static Behavior<FibCont.Message> create() {
public static Behavior<FakCont.Message> create() {
return Behaviors.setup(C::new); return Behaviors.setup(C::new);
} }
private C(ActorContext<FakCont.Message> context) {
private C(ActorContext<FibCont.Message> context) {
super(context); super(context);
} }
@Override @Override
public Receive<FakCont.Message> createReceive() { public Receive<FibCont.Message> createReceive() {
return newReceiveBuilder() return newReceiveBuilder()
.onMessage(FakCont.FakContMessage.class, this::onFakContMessage) .onMessage(FibCont.FibContMessage.class, this::onFibContMessage)
.build(); .build();
} }
private Behavior<FakCont.Message> onFakContMessage(FakCont.FakContMessage msg) { private Behavior<FibCont.Message> onFibContMessage(FibCont.FibContMessage msg) {
getContext().getLog().info("{}", msg.arg()); getContext().getLog().info("{}", msg.n());
return this; return this;
} }
} }

View File

@ -1,41 +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;
public class Fak extends AbstractBehavior<Fak.Message> {
public interface Message {};
public record FakMessage(int val, ActorRef<FakCont.Message> cust) implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(Fak::new);
}
private Fak(ActorContext<Message> context) {
super(context);
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FakMessage.class, this::onFakMessage)
.build();
}
private Behavior<Message> onFakMessage(FakMessage msg) {
if(msg.val == 0) {
msg.cust.tell(new FakCont.FakContMessage(1));
} else {
ActorRef<FakCont.Message> cont = this.getContext().spawnAnonymous(FakCont.create(msg.val, msg.cust));
this.getContext().getSelf().tell(new FakMessage(msg.val - 1, cont));
}
return this;
}
}

View File

@ -1,42 +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;
public class FakCont extends AbstractBehavior<FakCont.Message> {
private final int val;
private final ActorRef<Message> cust;
public interface Message {};
public record FakContMessage(int arg) implements Message { }
public static Behavior<Message> create(int val, ActorRef<Message> cust) {
return Behaviors.setup(context -> new FakCont(context, val, cust));
}
private FakCont(ActorContext<Message> context, int val, ActorRef<Message> cust) {
super(context);
this.val = val;
this.cust = cust;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FakContMessage.class, this::onFakContMessage)
.build();
}
private Behavior<Message> onFakContMessage(FakContMessage msg) {
this.cust.tell(new FakContMessage(this.val * msg.arg));
return this;
}
}

View File

@ -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<Fib.Message> {
public interface Message {};
public record FibMessage(int n, ActorRef<FibCont.Message> cust) implements Message { }
public static Behavior<Message> create() {
return Behaviors.setup(Fib::new);
}
private Fib(ActorContext<Message> context) {
super(context);
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FibMessage.class, this::onFibMessage)
.build();
}
private Behavior<Message> onFibMessage(FibMessage msg) {
if(msg.n <= 1) {
msg.cust.tell(new FibCont.FibContMessage(msg.n));
} else {
ActorRef<FibCont.Message> 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;
}
}

View File

@ -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<FibCont.Message> {
private boolean flag;
private int store;
public interface Message {};
public record FibContMessage(int n) implements Message { }
public static Behavior<Message> create(ActorRef<Message> cust) {
return Behaviors.setup(context -> new FibCont(context, cust));
}
private final ActorRef<Message> cust;
private FibCont(ActorContext<Message> context, ActorRef<Message> cust) {
super(context);
this.cust = cust;
this.flag = false;
this.store = 0;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(FibContMessage.class, this::onFibContMessage)
.build();
}
private Behavior<Message> 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;
}
}