6 : blocksRegistryDelegate(blocksRegistryDelegate) {}
15 for (
const auto& block : blocks) {
16 auto maybeCycle =
hasCycle({.block = block.get(), .port =
nullptr});
25 for (
const auto& block : blocks) {
40 for (
const auto& [source, destinations] :
42 const auto& sourcePortValue = source.block->getPortValue(source.port);
44 for (
const auto& dest : destinations) {
45 if (dest.block == block) {
58 std::optional<std::unordered_set<business_logic::elements::structures::BlocksConnectionSide>>
61 return side.block == predSide.block;
64 std::unordered_set<business_logic::elements::structures::BlocksConnectionSide> cycle = {
65 {.block = side.
block, .port =
nullptr}};
67 for (
const auto& visitedSide :
visited) {
68 cycle.insert(visitedSide);
72 cycle.insert(visitedSide);
88 for (
const auto& [source, destinations] :
90 if (source.block == side.
block) {
91 for (
const auto& dest : destinations) {
93 if (maybeCycle.has_value()) {
94 auto cycle = maybeCycle.value();
96 for (
const auto& visitedSide :
visited) {
97 cycle.insert(visitedSide);
101 cycle.insert(visitedSide);
void calculateValuesFlow()
Calculates the values flowing through the graph.
business_logic::calculations::delegate::IBlocksRegistryDelegate * blocksRegistryDelegate
The delegate that provides the connections registry.
std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > recursionStack
The stack of recursion to detect cycles.
BlocksCalculator(business_logic::calculations::delegate::IBlocksRegistryDelegate *blocksRegistryDelegate)
std::optional< std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > > hasCycle(elements::structures::BlocksConnectionSide side)
Checks if there is a cycle in the graph.
std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > visited
The set of visited blocks to detect cycles.
void calculateBlockValues(business_logic::elements::blocks::BaseBlock *block)
Calculates the values of the dependencies of a block and then the block itself.
std::unordered_set< business_logic::elements::blocks::BaseBlock * > calcVisited
The set of visited blocks to note which blocks have been visited when calculating their values.
The delegate for the blocks registry, allowing to get connections and blocks.
virtual const std::vector< std::shared_ptr< business_logic::elements::blocks::BaseBlock > > & getBlocks() const =0
Gets the blocks.
virtual const std::unordered_map< business_logic::elements::structures::BlocksConnectionSide, std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > > & getConnectionsRegistry() const =0
Gets the connections registry.
The base class for all blocks, containing common functionality and members.
virtual void calculateOutputValues()=0
Calculates the output port values based on input port values.
void setPortValue(const business_logic::elements::structures::Port *port, const FloatingPoint &value)
Sets the value of the port.
Exception thrown when a graph cycle is detected.
Utilities regarding calculation of values for the calculator itself.
A side of a connection between two ports.
business_logic::elements::blocks::BaseBlock * block
The block.