Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Sep 21, 2023
1 parent c85847f commit 2e4a660
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
package com.fluxtion.example.cookbook.lottery;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.example.cookbook.lottery.nodes.LotteryGameNode;
import com.fluxtion.runtime.EventProcessor;
import com.fluxtion.compiler.extern.spring.FluxtionSpring;
import com.fluxtion.example.cookbook.lottery.api.LotteryGame;
import com.fluxtion.example.cookbook.lottery.api.Ticket;
import com.fluxtion.example.cookbook.lottery.api.TicketStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.function.Consumer;

/**
* A simple lottery game to demonstrate event processing logic built with Fluxtion
*/
@Slf4j
public class LotteryApp {

public void start(){
EventProcessor<?> lotteryGame = Fluxtion.interpret(new LotteryGameNode());
// lotteryGame.getExportedService()
private static LotteryGame lotteryGame;
private static TicketStore ticketStore;

public static void main(String[] args) {
start(LotteryApp::ticketReceipt, LotteryApp::lotteryResult);
//try and buy a ticket - store is closed
ticketStore.buyTicket(new Ticket(12_65_56));

//open store and buy ticket
ticketStore.openStore();
ticketStore.buyTicket(new Ticket(12_65_56));
ticketStore.buyTicket(new Ticket(36_58_58));
ticketStore.buyTicket(new Ticket(73_00_12));

//bad numbers
ticketStore.buyTicket(new Ticket(25));

//close the store and run the lottery
ticketStore.closeStore();

//try and buy a ticket - store is closed
ticketStore.buyTicket(new Ticket(12_65_56));

//run the lottery
lotteryGame.selectWinningTicket();
}

public static void start(Consumer<String> ticketReceiptHandler, Consumer<String> resultsPublisher){
var lotteryEventProcessor = FluxtionSpring.interpret(
new ClassPathXmlApplicationContext("com/fluxtion/example/cookbook/lottery/spring-lottery.xml"));
lotteryEventProcessor.init();
lotteryGame = lotteryEventProcessor.getExportedService();
ticketStore = lotteryEventProcessor.getExportedService();
lotteryGame.setResultPublisher(resultsPublisher);
ticketStore.setTicketSalesPublisher(ticketReceiptHandler);
lotteryEventProcessor.start();
}

public static void ticketReceipt(String receipt){
log.info(receipt);
}

public static void lotteryResult(String receipt){
log.info(receipt);
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fluxtion.example.cookbook.lottery.api;

import java.util.function.Consumer;

/**
* Service that runs the lottery, picks a ticket and publishes the results to a supplied Consumer.
* * If no resultPublisher is set the LotteryGame should fail with an exception at startup
*/
public interface LotteryGame {

void selectWinningTicket();

void setResultPublisher(Consumer<String> resultPublisher);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fluxtion.example.cookbook.lottery.api;

import java.util.UUID;

public record Ticket(int number, UUID id) {

public Ticket(int number){
this(number, UUID.randomUUID());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fluxtion.example.cookbook.lottery.api;

import java.util.function.Consumer;

/**
* Ticket store for lottery tickets. The store must be open before tickets can be bought.
* If no ticketSalesPublisher is set the TicketStore should fail with an exception
*/
public interface TicketStore {
boolean buyTicket(Ticket ticket);
void openStore();
void closeStore();
void setTicketSalesPublisher(Consumer<String> ticketSalesPublisher);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
package com.fluxtion.example.cookbook.lottery.nodes;

import com.fluxtion.example.cookbook.lottery.LotteryGame;
import com.fluxtion.example.cookbook.lottery.api.Ticket;
import com.fluxtion.example.cookbook.lottery.api.LotteryGame;
import com.fluxtion.runtime.annotations.ExportService;
import com.fluxtion.runtime.annotations.OnTrigger;

import com.fluxtion.runtime.annotations.Start;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class LotteryGameNode implements @ExportService LotteryGame {
@Slf4j
@RequiredArgsConstructor
public class LotteryGameNode implements
@ExportService LotteryGame {

private final TicketStoreNode ticketStoreNode;
private final Supplier<Ticket> ticketSupplier;
private final transient List<Ticket> ticketsBought = new ArrayList<>();
private Consumer<String> resultPublisher;

public LotteryGameNode(){
this(new TicketStoreNode());
}

public LotteryGameNode(TicketStoreNode ticketStoreNode) {
this.ticketStoreNode = ticketStoreNode;
}

@Override
public void setResultPublisher(Consumer<String> resultPublisher) {
this.resultPublisher = resultPublisher;
}

@Start
public void start(){
Objects.requireNonNull(resultPublisher, "must set a results publisher before starting the lottery game");
log.info("started");
}

@OnTrigger
public boolean newTicketSale(){
public boolean processNewTicketSale() {
ticketsBought.add(ticketSupplier.get());
log.info("tickets sold:{}", ticketsBought.size());
return false;
}

@Override
public void pickTicket() {

public void selectWinningTicket() {
if(ticketsBought.isEmpty()){
log.info("no tickets bought - no winning ticket");
}else {
Collections.shuffle(ticketsBought);
log.info("WINNING ticket {}", ticketsBought.get(0));
}
ticketsBought.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
package com.fluxtion.example.cookbook.lottery.nodes;

import com.fluxtion.example.cookbook.lottery.Ticket;
import com.fluxtion.example.cookbook.lottery.TicketStore;
import com.fluxtion.example.cookbook.lottery.api.Ticket;
import com.fluxtion.example.cookbook.lottery.api.TicketStore;
import com.fluxtion.runtime.annotations.ExportService;
import com.fluxtion.runtime.annotations.NoPropagateFunction;
import com.fluxtion.runtime.annotations.Start;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class TicketStoreNode implements @ExportService TicketStore {
@Slf4j
public class TicketStoreNode implements
Supplier<Ticket>,
@ExportService TicketStore {

private boolean storeOpen;
private Consumer<String> receiptReceiver;
private Consumer<String> ticketSalesPublisher;
private Ticket ticket;

@Override
@NoPropagateFunction
public void setTicketSalesPublisher(Consumer<String> receiptReceiver) {
this.receiptReceiver = receiptReceiver;
public void setTicketSalesPublisher(Consumer<String> ticketSalesPublisher) {
this.ticketSalesPublisher = ticketSalesPublisher;
}

@Start
public void start(){
Objects.requireNonNull(receiptReceiver, "must have a receipt publisher set");
public void start() {
Objects.requireNonNull(ticketSalesPublisher, "must have a ticketSalesPublisher set");
storeOpen = false;
}

@Override
public boolean buyTicket(Ticket ticket) {
if(storeOpen){
if (ticket.number() < 9_99_99 | ticket.number() > 99_99_99) {
ticketSalesPublisher.accept("invalid numbers " + ticket);
this.ticket = null;
} else if (storeOpen) {
ticketSalesPublisher.accept("good luck with " + ticket);
this.ticket = ticket;
} else {
ticketSalesPublisher.accept("store shut - no tickets can be bought");
this.ticket = null;
}
return storeOpen;
return this.ticket != null;
}

public Ticket lastTicketSold(){
@Override
public Ticket get() {
return ticket;
}

@Override
@NoPropagateFunction
public void openStore() {
log.info("store opened");
storeOpen = true;
}

@Override
@NoPropagateFunction
public void closeStore() {
log.info("store closed");
storeOpen = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="lotteryGame" class="com.fluxtion.example.cookbook.lottery.nodes.LotteryGameNode">
<constructor-arg ref="ticketStore"/>
</bean>

<bean id="ticketStore" class="com.fluxtion.example.cookbook.lottery.nodes.TicketStoreNode">
</bean>

</beans>

0 comments on commit 2e4a660

Please sign in to comment.