<![CDATA[Systems, People & Technology]]>https://t-pl.ioRSS for NodeThu, 30 May 2024 07:20:08 GMT60<![CDATA[DDD Aggregates: Processes, State Machines and Transducers]]>https://t-pl.io/ddd-aggregates-processes-state-machines-and-transducershttps://t-pl.io/ddd-aggregates-processes-state-machines-and-transducersSun, 02 Jan 2022 10:52:42 GMT<![CDATA[<p>This blog post is the first one in a series of three posts in which we try to derive a mathematical model for process-oriented Domain-Driven Design (DDD) Aggregates.</p><h2 id="heading-the-need-for-process-oriented-models">The need for process-oriented models</h2><p>In order to understand this need we need to dive into complex systems theory. Dont worry, we will not dive deep!</p><p>Not surprisingly software is not an isolated artifact. It must be embedded into the socio-technical context of the people that use and produce it, paired with the constant interactions of the environment. What we need to know is how complex systems show the behaviors that we, as system designers, try to capture in useful abstractions.</p><blockquote><p><strong>Organizations are dynamic, hierarchically structured entities. Such dynamism is reflected in the emergence of significant events at every organizational level.</strong></p><p><a target="_blank" href="https://www.researchgate.net/publication/272427231_Event_System_Theory_An_Event-Oriented_Approach_to_the_Organizational_Sciences">[Morgeson F., Mitchell T., Liu D. 2015. Event System Theory: An Event-Oriented Approach To The Origanisational Sciences]</a></p></blockquote><p>We will call these patterns of events <strong>processes</strong>. These processes emerge in systems to achieve something, be it selling books online or providing restaurants with seat reservations. A large research topic termed process theories is trying to understand how patterns of events lead to positive outcomes. Ultimately they are <strong>stories about what happened and who did what when that is, events, activities, and choices ordered over time</strong> <a target="_blank" href="https://www.jstor.org/stable/259349?seq=1#page_scan_tab_contents">[Langley, A. 1999. Strategies for theorizing from process data]</a>.</p><h3 id="heading-the-rise-of-collaborative-temporal-modelling">The rise of collaborative temporal modelling</h3><p>Over the past years new practices to capture these processes emerged. Event Storming, Story Mapping or Domain Story Telling are all examples of approaches to recognize that understanding the repeating patterns of events can give us a great leverage in designing working solutions. These results regularly have a much larger overlap with the organizational reality than traditional static modelling techniques.</p><p>Temporal modelling also had an effect on the tactical design choices that experienced DDD practitioners make when working with Aggregates. Aggregates are then defined as a combination of <strong>Commands</strong> (stimuli from the environment), <strong>Behaviors</strong> (reactions to incoming stimuli that can change the systems response mechanisms) and <strong>Events</strong> (system signals in response to changes in system state). The Event Sourcing pattern for example builds on these fundamental design choices to make the time-ordered hence temporal patterns of events the central element of its design.</p><p><strong>If we can define some Aggregates by time-ordered series of events, then it follows that these Aggregates are also processes.</strong></p><h2 id="heading-rediscovering-finite-state-machines">Rediscovering Finite-State Machines</h2><p>Modelling processes is nothing new in computing. And <strong>automata theory</strong> seems especially interesting:</p><blockquote><p>A Finite-State Machine (FSM) formulation is used to describe the processes during which information or tasks move from one state to another for action, according to a set of rules. The states consists of the smallest amount of information that together with the knowledge of the input can determine the output.</p></blockquote><p>The terms Automaton and Machine are used interchangeably, so when we talk about FSA, we also mean FSM.</p><p>Translated into our process terminology, FSMs process a stream of system stimuli (Commands), enforcing invariants and changing the system state (Behaviors).</p><p>The following figure (Fig. 2) shows how the temporal nature of FSMs is driven by the time-ordered and unidirectional stream of Commands.</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641031481149/N6f766oxh.jpeg" alt="1 Qfbo0Jmfxwoy9tujEhdZ7g.jpg" /></p><p>Since we want to derive a mathematical model, lets look at the definition of an Finite-State Machine using our previously derived process terminology. An FSM is a 5-tuple</p><p>\[A = \Big(S, C, \delta\colon S \times C \to S, S_0\colon S_0 \in S, F\colon F \subset S \Big)\]</p><p>where:</p><ul><li><strong>S</strong> is a non-empty, finite set of states</li><li><strong>C</strong> is the <strong>Command language</strong>, a non-empty, finite set of Commands</li><li><strong></strong> is the set of <strong>Behaviors</strong> a relation between a state and Command to a state</li><li><strong>S</strong> is the initial state where S is an element of S</li><li><strong>F</strong> is the non-empty, finite set of final states where <strong>F</strong> is a subset of <strong>S</strong></li></ul><p>Given this definition, we will need to derive the command and state alphabets from the domain experts knowledge and then connect the dots via our Behavior functions.</p><h3 id="heading-the-universe-is-a-messy-place">The universe is a messy place</h3><p>We love to look for deterministic solutions to problems that are complex and thus non-deterministic by nature. DDD as a modelling approach embraces unexpected behaviors of systems which is one of the major reasons why I believe applying DDD thinking to complex problems is almost always a good choice. This means that responses to system stimuli are often different depending on the current situation. Imagine yourself as a call-center agent that is suddenly advised to re-route calls for specific topics to supervisors. The world is just not predictable.Non-deterministic Finite-State Auatomata (NDFA)</p><p>In order to deal with this messy world automata theory offers two distinct types of FSAs that can express these differences Non-Deterministic (NDFA) and Deterministic (DFA) FSAs. Differences between DFA and NDFAs are subtle. While DFAs have exactly one transition for each state and command pair, NDFAs can have multiple, or even none. Mathematically, they are both equivalent. You can always transform a DFA into an NDFA and back again.</p><p>But they do differ between the amount of complexity required to represent them. An NDFA with n states can be transformed into a DFA with up to 2 states. <strong>For a decently complicated NDFA of 10 states we would need up to 1024 states in a DFA to represent it!</strong></p><blockquote><p><strong>When using NDFAs you trade determinism for simplicity!</strong></p></blockquote><h3 id="heading-where-are-the-events">Where are the Events?</h3><p>If we recollect the definition of the FSA which was defined by the Command and state languages paired with the Behaviors one might recognize that there is currently no way to signal anything back to the systems. So, where are the events?</p><p>It seems like a plain NDFA is not enough to represent our processes. Can automata theory provide another concept that we can use in this case?</p><p>Spoiler, yes it can!</p><h2 id="heading-introducing-finite-state-transducers-fsts">Introducing Finite-State Transducers (FSTs)</h2><p>Lets compare the following schematic diagram of an FST (Fig. 4) with the previous one of an FSM (Fig. 2) and see if we can spot a difference:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641115365141/UcZxMwTlj.jpeg" alt="1 oecW_d-OOCvYrzEUd4Awsg.jpg" /></p><p>We now have an actual output of Events that correspond to the states and Commands that were fed in. Exactly what we need to send our signals out into the world to wreak havoc! Again, lets have a look at the formal definition of an <strong>Finite-State Transducer</strong>. It is a 7-tuple</p><p>\[A = \Big(S, C, E, \delta\colon S \times C \to S, \omega\colon S \times C \to E, S_0\colon S_0 \subset S, F\colon F \subset S \Big)\]</p><p>where:</p><ul><li><strong>S</strong> is a non-empty, finite set of states</li><li><strong>C</strong> is the <strong>Command language</strong>, a non-empty, finite set of Commands</li><li><strong>E</strong> is the <strong>Event language</strong>, a non-empty, finite set of Events</li><li><strong></strong> is the set of <strong>Behaviors</strong> a relation of a state and Command pair to a state of form <strong> : S x C S</strong></li><li><strong></strong> is the <strong>output relation</strong> of a state and Command pair to an Event of form <strong> : S x C E</strong></li><li><strong>S</strong> is the non-empty, finite set of initial states where <strong>S S</strong> (S is a subset of S)</li><li><strong>F</strong> is the non-empty, finite set of final states where <strong>F S</strong> (F is a subset of S)</li></ul><p>Now that we have Commands, Behaviors and Events we can model our processes as non-deterministic FSTs. Lets get to action and use the math in an example scenario!</p><h3 id="heading-a-short-excursion-on-the-output-relation-w-omega">A short excursion on the output relation (omega)</h3><p>I quickly want to mention that automata theory differs between two major types of transducers the <a target="_blank" href="https://en.wikipedia.org/wiki/Moore_machine"><strong>Moore</strong></a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Mealy_machine"><strong>Mealy</strong></a> types.</p><p>They are only different in the form of their output relation . Our transducer definition above is a Mealy Transducer, because its output relation depends on the state and Command as inputs, while a Moore Transducer's output relation would only depend on the state, and not the Command.</p><ul><li><strong>Moore</strong> form: <strong> : S E</strong> (Event generation depends only on state)</li><li><strong>Mealy</strong> form: <strong> : S x C E</strong> (Event generation depends on Commands and state combined)</li></ul><h2 id="heading-example-designing-a-user-registration-process">Example: Designing a User Registration Process</h2><p>Lets assume we have had a great Event Storming session and are now trying to apply the math to the crunched knowledge of a specific problem a User Registration process. We have to define the Commands, States, Behaviors and the Events that should be written to the output in response to a specific Behavior. Since we have chosen Event Storming as our collaboration method of choice we'll start with the Events.</p><h3 id="heading-speaking-the-event-language">Speaking the Event language!</h3><p>We start with building our Event language.</p><ul><li><p><strong>Confirmation was sent</strong></p><p>After having started the registration we want to make sure that the customer really has access to the provided email address, hence we send out a confirmation via email with a confirmation link.</p></li><li><p><strong>Confirmation resent</strong></p><p>When the confirmation has expired we want to enable our customers to easily resend the confirmation, maybe the previous mail was caught by a spam filter.</p></li><li><p><strong>Account was confirmed</strong></p><p>When the confirmation was completed by the customer we say that the account is now confirmed.</p></li><li><p><strong>Account was deleted through a GDPR request</strong></p><p>We are bound by European laws to delete all personal data.</p></li></ul><p><em>It follows that:</em></p><p>\[E = \{\small{ConfirmationSent},\small{ConfirmationResent},\small{AccountConfirmed},\small{AccountDeleted}\}\]</p><h3 id="heading-thou-command-shall-be-my-duty">Thou Command shall be my duty!</h3><p>We have designed the following Commands analogous to our Events:</p><ul><li><strong>Start registration</strong></li><li><strong>Confirm account</strong></li><li><strong>Resend confirmation</strong></li><li><strong>Fulfill GDPR request</strong></li></ul><p><em>It follows that:</em></p><p>\[C = \{\small{StartRegistration},\small{ConfirmAccount},\small{ResendConfirmation},\small{FulfillGDPRRequest}\}\]</p><h3 id="heading-designing-the-states">Designing the States</h3><p>Remember that we have a principle that should always be on our mind when designing State:</p><blockquote><p>The states should consist of the smallest amount of information that together with the knowledge of the input can determine the output.</p></blockquote><p>Our goal is to find the minimum required amount of information to enable suitable reactions. Here less is always more.</p><p>This example is not focusing on managing data, but it is possible to define properties on states this will not have an effect on the general formulas.</p><ul><li><p><strong>Potential customer</strong></p><p>This is our initial state \(PotentialCustomer \in S_0\). Any unregistered customer is a potential!</p></li><li><p><strong>Requires confirmation</strong></p></li><li><strong>Active</strong></li><li><p><strong>Deleted</strong></p><p>This is a final state \(Deleted \in F\)</p></li></ul><p><em>It follows that:</em></p><p>\[S = \{\small{PotentialCustomer},\small{RequiresConfirmation},\small{Active},\small{Deleted}\}\]</p><p>and</p><p>$$S_0 = \{\\\small{PotentialCustomer}\}$$</p><p>and</p><p>$$F = \{\\\small{Deleted}\}$$</p><h3 id="heading-how-should-we-react">How should we react?</h3><p>The last thing we need is to define our Behavior () and Event () functions.</p><pre><code class="lang-text"> : S x C S = { (PotentialCustomer, StartRegistration) RequiresConfirmation, (RequiresConfirmation, ResendConfirmation) RequiresConfirmation, (RequiresConfirmation, ConfirmAccount) Confirmed, (Active, FulfillGDPRRequest) Deleted, (RequiresConfirmation, FulfillGDPRRequest) Deleted}</code></pre><p>and</p><pre><code class="lang-text"> : S x C E = { (PotentialCustomer, StartRegistration) ConfirmationSent, (RequiresConfirmation, ResendConfirmation) ConfirmationResent, (RequiresConfirmation, ConfirmAccount) AccountConfirmed, (Active, FulfillGDPRRequest) AccountDeleted, (RequiresConfirmation, FulfillGDPRRequest) ,}</code></pre><p>The here is special. It says that there is no output for this specific Command-State pair. We will learn more about and ways we can model handling errors in the next post of this series.</p><h3 id="heading-construction-of-our-transducer-aggregate">Construction of our Transducer Aggregate!</h3><p>Now we can finally construct our User Registration Aggregate (or rather non-deterministic Finite-State Transducer) as:</p><p>\[T = \Big(S, C, E, \delta, \omega, \{PotentialCustomer\}, \{Deleted\} \Big)\]</p><p>and send the following Commands to it:</p><p>$$C = \{\small{StartRegistration},\small{ResendConfirmation},\small{ConfirmAccount},\small{FulfillGDPRRequest}\}$$</p><p>In response we will receive the following Events:</p><p>$$E = \{\small{ConfirmationSent},\small{ConfirmationResent},\small{AccountConfirmed},\small{AccountDeleted}\}$$</p><h4 id="heading-exercise">Exercise</h4><p>Given the following commands:</p><p>$$C = \{\small{StartRegistration},\small{ResendConfirmation},\small{FulfillGDPRRequest}\}$$</p><ol><li>Which sequence of events would we get?</li><li>Which state is produced?</li></ol><h2 id="heading-conclusions">Conclusions</h2><ul><li>We have learned that process-oriented, temporal models are a great match when dealing with the behaviors of complex systems and how collaborative modelling techniques have shaped our understanding of these systems.</li><li>We looked into the past of computing advances to rediscover automata theory as a means to model processes, explored different types of machines (FSA, DFA, NDFA, FST) and mapped these to desired properties of temporal Aggregate design.</li><li>We put the math to action and designed a an User Registration Aggregate as a concrete example.</li></ul><h2 id="heading-whats-next">Whats next?</h2><p>In the next post of the series we will cover the following topics:</p><ul><li>Learning about <em>Accpetors</em> and <em>Sequencers</em></li><li>Exploring generic operations on FSAs like <em>unions</em>, <em>concatenations</em> and <em>projections</em>.</li><li>Using these operations to naturally transform our current User Registration example into an event-sourced model.</li></ul><h2 id="heading-personal-note">Personal note</h2><p>These posts are a try to summarize my personal thoughts about this topic for quite some time. I encourage everyone to challenge me on these ideas, and I am the last one to claim that this is the right way to think about these problems.</p><p>The hope I have is that readers have fun following me through my thinking process!</p><p>Thank you.</p><p><em>This article was originally published on Medium, but unfortunately I don't want to support or use Medium as a publishing platform. Hence I am moving my blog posts to hashnode. The original article can be found <a target="_blank" href="https://medium.com/@tPl0ch/domain-driven-design-a-mathematical-model-for-process-oriented-aggregates-pt-i-6aecc8957865">here</a>.</em></p>]]>https://cdn.hashnode.com/res/hashnode/image/unsplash/aQYgUYwnCsM/upload/v1641121050901/_cvnyJNXU.jpeg