-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c85847f
commit 2e4a660
Showing
10 changed files
with
168 additions
and
56 deletions.
There are no files selected for viewing
63 changes: 57 additions & 6 deletions
63
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/LotteryApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
9 changes: 0 additions & 9 deletions
9
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/LotteryGame.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/Ticket.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/TicketStore.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/api/LotteryGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
10 changes: 10 additions & 0 deletions
10
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/api/Ticket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/api/TicketStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
49 changes: 34 additions & 15 deletions
49
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/nodes/LotteryGameNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
37 changes: 26 additions & 11 deletions
37
cookbook/src/main/java/com/fluxtion/example/cookbook/lottery/nodes/TicketStoreNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
cookbook/src/main/resources/com/fluxtion/example/cookbook/lottery/spring-lottery.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |