SHARE
TWEET

Untitled

a guest Oct 17th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package modelbasedakka4j;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.requirementsascode.UseCaseModel;
  6. import org.requirementsascode.UseCaseModelBuilder;
  7. import org.requirementsascode.UseCaseModelRunner;
  8.  
  9. import akka.actor.AbstractActor;
  10. import akka.actor.ActorRef;
  11. import akka.actor.ActorSystem;
  12. import akka.actor.Props;
  13. import akka.actor.UntypedAbstractActor;
  14.  
  15. public class ModelBasedAkka4j {
  16.    
  17.     public static void main(String[] args) {
  18.         ActorSystem actorSystem = ActorSystem.create("ModelBasedActorSystem");
  19.  
  20.         UseCaseModelRunner useCaseModelRunner = runnerOf(useCaseModel());
  21.  
  22.         ActorRef modelBasedActor = spawn(actorSystem, ModelBasedActor.class, useCaseModelRunner);
  23.  
  24.         modelBasedActor.tell(new SayHelloWorld(), ActorRef.noSender());
  25.         modelBasedActor.tell(new SayHelloWorld(), ActorRef.noSender());
  26.         modelBasedActor.tell(new SayHelloTo("Sandra"), ActorRef.noSender());
  27.  
  28.         waitForKeyPressed();
  29.  
  30.         actorSystem.terminate();
  31.     }
  32.    
  33.     static UseCaseModelRunner runnerOf(UseCaseModel useCaseModel) {
  34.         UseCaseModelRunner useCaseModelRunner = new UseCaseModelRunner();
  35.         useCaseModelRunner.run(useCaseModel);
  36.         return useCaseModelRunner;
  37.     }
  38.  
  39.     static UseCaseModel useCaseModel() {
  40.         UseCaseModel useCaseModel = UseCaseModelBuilder.newBuilder()
  41.             .useCase("Say hello to world, then user")
  42.                 .basicFlow()
  43.                     .step("S1").user(SayHelloWorld.class).system(shw -> System.out.println("Hello, World."))
  44.                     .step("S2").user(SayHelloTo.class).system(sht -> System.out.println("Hello, " + sht.getName() + "."))
  45.             .build();
  46.         return useCaseModel;
  47.     }
  48.    
  49.     static <T> ActorRef spawn(ActorSystem system, Class<? extends AbstractActor> actorClass, Object... constructorParams) {
  50.         Props props = Props.create(actorClass, constructorParams);
  51.         return system.actorOf(props);
  52.     }
  53.    
  54.     static class ModelBasedActor extends UntypedAbstractActor {
  55.         private UseCaseModelRunner useCaseModelRunner;
  56.        
  57.         public ModelBasedActor(UseCaseModelRunner useCaseModelRunner) {
  58.             this.useCaseModelRunner = useCaseModelRunner;
  59.         }
  60.  
  61.         @Override
  62.         public void onReceive(Object msg) throws Exception {
  63.             useCaseModelRunner.reactTo(msg);
  64.         }
  65.     }
  66.  
  67.     static class SayHelloTo {
  68.         private String name;
  69.  
  70.         public SayHelloTo(String name) {
  71.             this.name = name;
  72.         }
  73.  
  74.         public String getName() {
  75.             return name;
  76.         }
  77.     }
  78.  
  79.     static class SayHelloWorld {
  80.     }
  81.  
  82.     static class AskForHello {
  83.     }
  84.    
  85.     static void waitForKeyPressed() {
  86.         try {
  87.             System.in.read();
  88.         } catch (IOException e) {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92. }
RAW Paste Data
Top