|
DetectorGraph
2.0
|
Sophisticated Vending Machine example using a Lag-based feedback loop. More...
Go to the source code of this file.
Sophisticated Vending Machine example using a Lag-based feedback loop.
You see, here things got a bit out of hand. All I was supposed to do was to code a single representative and fun example. But then I got legitimately nerd-snipped [1] and had to go all the way.
This example provides a vending machine algorithm that keeps track of:
This example shows a concrete example of how to deal with a Large TopicState - large enough that one wouldn't any unnecessary copies of it. This is the case for the Look-Up Table generated by ChangeAlgo. That table is conveyed inside the ChangeAvailable TopicState to allow for efficient access to properties of that TopicState - namely to check whether change for a given amount can be given. The way this is accomplished is by using a shared_ptr to wrap the heap-allocated Look-Up Table.
The graph uses 6 detectors and 14 TopicStates to encode the different logic and data signals.
The 'public' API to this graph is composed of: Inputs:
The graph below shows the relationships between the topics (rectangles) and detectors (ellipses). Note that this graph can be automatically generated for any instance of DetectorGraph::Graph using DetectorGraph::GraphAnalyzer.
Note that this entire application in contained in a single file for the sake of unity as an example. In real-world scenarios the suggested pattern is to split the code into:
detectorgraph/
include/
fancyvendingmachine.hpp (FancyVendingMachine header)
src/
fancyvendingmachine.hpp (FancyVendingMachine implementation)
detectors/
include/
UserBalanceDetector.hpp
SaleProcessor.hpp
ProductStockManager.hpp
CoinBankManager.hpp
FinancesReportDetector.hpp
src/
UserBalanceDetector.cpp
SaleProcessor.cpp
ProductStockManager.cpp
CoinBankManager.cpp
FinancesReportDetector.cpp
topicstates/
include/
CoinInserted.hpp
SelectedProduct.hpp
SaleProcessed.hpp
RefillProduct.hpp
PriceUpdate.hpp
StockState.hpp
UserBalance.hpp
MoneyBackButton.hpp
ReturnChange.hpp
FinancesReport.hpp
RefillChange.hpp
ReleaseCoins.hpp
ChangeAvailable.hpp
CoinType.hpp (enum and GetStr - common core datatypes used
across multiple topicstates are also commonly
stored here)
ProductIdType.hpp
src/
ChangeAvailable.cpp (TopicStates implementations, as needed)
CoinType.cpp (e.g GetCoinTypeStr())
ProductIdType.cpp
utils/ (For utility modules/algos, as needed)
include/
ChangeAlgo.hpp
src/
ChangeAlgo.cpp
Some projects that use Protocol Buffers for its TopicStates may have:
proto/
some_data.proto
Although this example makes heavy use of C++11, that is NOT a requirement for DetectorGraph applications.
Definition in file fancyvendingmachine.cpp.
1.8.13