<commit_before># TaskCat.Automaton
Json driven finite state machine.

Essentially the need of this could've been simplified using a simple finite state machine. Since this was a part of TaskCat, we figured we need something tailor made for us. A lot of systems are using JSON not only as the primary data interchange format but also the primary storage format now a days. Thanks goes to the document driven databases like MongoDb (esssentially MongoDb uses BSON, but meh!). Handling a system that essentially works with workflows gets tougher and tougher since the real life workflows are not easily predictable, sometimes not even understood up until a certain time has invested into development.

TaskCat.Automaton is a another finite state machine but the transition conditions for this state machines are defined by JSONPatch defined in RFC-6902. The reason behind this was to trigger a document state from one state to another using JSONPatch on the document. Although TaskCat.Automaton doesn't modify the actual document but still it is indeed helpful when you can just ask a state machine to actuate upon any condition you can define through a JSONPatch on any JSON document.

The sample state machine diagram used in the unit tests looks like this. If `createNewTarget` property of the machine is set to `true` then the node will create a new node in the chain rather than coming back to itself. TaskCat.Automaton currently clones the definition to create a new node as this is generative. So, your system doesn't have to have all the nodes to begin with. Our current plan is to make sure that it only takes document driven by a JsonSchema and generates default documents (nodes) from that schema so no system has to know all the nodes right in front and you only save what you have traversed.

![Sample FSM](https://g.gravizo.com/g?
 digraph G {
   pickup -> pickup [style=bold, label="FAILED (change variant to retry)"];
   pickup -> delivery [label="COMPLETED"];
   delivery -> delivery [style=bold, label="FAILED (change variant to retry)"];
   delivery -> ReturnToSeller [label="RETURNED"];
   ReturnToSeller -> ReturnToSeller [style=bold, label="FAILED (change variant to retry)"];
   ReturnToSeller -> ReturnToWarehouse [style=bold, label="FAILED"];
   ReturnToWarehouse -> ReturnToSeller [label="COMPLETED"];
   ReturnToWarehouse -> ReturnToWarehouse [style=bold, label="FAILED (change variant to retry)"];
   ReturnToWarehouse -> resolved [label="COMPLETED"];
   delivery -> ReturnToWarehouse [label="RETURNED"];
   resolved [shape=box,style=filled,color=".7 .3 1.0"];
   delivery -> resolved [label="COMPLETED"];
   ReturnToSeller -> resolved [label="COMPLETED"];
 }
)

The simple state machine based on this graph can be found over https://github.com/thehoneymad/TaskCat.Automaton/blob/master/TaskCat.Automaton/TaskCat.Automaton.Tests/SampleFSM.json<commit_msg>Remove spacing to show the diagram properly<commit_after># TaskCat.Automaton
Json driven finite state machine.

Essentially the need of this could've been simplified using a simple finite state machine. Since this was a part of TaskCat, we figured we need something tailor made for us. A lot of systems are using JSON not only as the primary data interchange format but also the primary storage format now a days. Thanks goes to the document driven databases like MongoDb (esssentially MongoDb uses BSON, but meh!). Handling a system that essentially works with workflows gets tougher and tougher since the real life workflows are not easily predictable, sometimes not even understood up until a certain time has invested into development.

TaskCat.Automaton is a another finite state machine but the transition conditions for this state machines are defined by JSONPatch defined in RFC-6902. The reason behind this was to trigger a document state from one state to another using JSONPatch on the document. Although TaskCat.Automaton doesn't modify the actual document but still it is indeed helpful when you can just ask a state machine to actuate upon any condition you can define through a JSONPatch on any JSON document.

The sample state machine diagram used in the unit tests looks like this. If `createNewTarget` property of the machine is set to `true` then the node will create a new node in the chain rather than coming back to itself. TaskCat.Automaton currently clones the definition to create a new node as this is generative. So, your system doesn't have to have all the nodes to begin with. Our current plan is to make sure that it only takes document driven by a JsonSchema and generates default documents (nodes) from that schema so no system has to know all the nodes right in front and you only save what you have traversed.

![Sample FSM](https://g.gravizo.com/g?%20digraph%20G%20{%20pickup%20-%3E%20pickup%20[style=bold,%20label=%22FAILED%20(change%20variant%20to%20retry)%22];%20pickup%20-%3E%20delivery%20[label=%22COMPLETED%22];%20delivery%20-%3E%20delivery%20[style=bold,%20label=%22FAILED%20(change%20variant%20to%20retry)%22];%20delivery%20-%3E%20ReturnToSeller%20[label=%22RETURNED%22];%20ReturnToSeller%20-%3E%20ReturnToSeller%20[style=bold,%20label=%22FAILED%20(change%20variant%20to%20retry)%22];%20ReturnToSeller%20-%3E%20ReturnToWarehouse%20[style=bold,%20label=%22FAILED%22];%20ReturnToWarehouse%20-%3E%20ReturnToSeller%20[label=%22COMPLETED%22];%20ReturnToWarehouse%20-%3E%20ReturnToWarehouse%20[style=bold,%20label=%22FAILED%20(change%20variant%20to%20retry)%22];%20ReturnToWarehouse%20-%3E%20resolved%20[label=%22COMPLETED%22];%20delivery%20-%3E%20ReturnToWarehouse%20[label=%22RETURNED%22];%20resolved%20[shape=box,style=filled,color=%22.7%20.3%201.0%22];%20delivery%20-%3E%20resolved%20[label=%22COMPLETED%22];%20ReturnToSeller%20-%3E%20resolved%20[label=%22COMPLETED%22];%20}
)

The simple state machine based on this graph can be found over https://github.com/thehoneymad/TaskCat.Automaton/blob/master/TaskCat.Automaton/TaskCat.Automaton.Tests/SampleFSM.json
{'fraction_non_alphanumeric': 0.07844121090483358, 'fraction_numerical': 0.039136979427997994, 'mean_word_length': 5.865671641791045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2159020', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1632, 'n_words': 696}