@samatawy/rules
    Preparing search index...

    Class DependencyGraph

    The DependencyGraph class is responsible for organizing rules based on their required inputs and outputs, allowing for efficient evaluation and execution of rules in the correct order. It builds a graph structure where nodes represent data keys or rules, and edges represent dependencies between them.

    Index

    Constructors

    Properties

    roots: AbstractNode[]

    The root nodes of the graph represent top-level data keys that rules depend on. Only the first level of the graph consists of root nodes, and all other nodes are connected to these roots based on their dependencies.

    dependants: RuleOutputNode[]

    The rules of the graph represent all rules that have been added to the graph, these are linked together by their dependencies.

    dependencies: Record<string, DependencyNode>

    A mapping of data keys to their corresponding DependencyNode in the graph. Used internally to efficiently build the graph and find nodes based on data keys.

    Methods

    • Find a root node in the graph by its key. Root nodes represent top-level data keys that rules depend on.

      Parameters

      • key: string

        the key of the root node to find.

      Returns InputNode | undefined

      the root node with the given key, or undefined if no such node exists.

    • Add a rule to the graph based on its required inputs. The rule is represented as a RuleNode, and it is connected to the nodes representing its required data keys. If multiple rules require the same data key, they are connected to a common CombinationNode to represent the dependency.

      Parameters

      Returns void

      void

    • Get all rules that are applicable to the given context. This is primarily an explanatory feature, and does NOT return the rules that will be executed. This is done by traversing the requirement graph starting from the root nodes that match the keys in the context, and collecting all rules that are reachable and applicable based on their requirements.

      N.B. Applicable rules are not necessarily executable, since they may require certain conditions to be met that are not currently satisfied in the context. Disabled rules are returned as applicable if their requirements are met, but they will not be executed when processing the context.

      Parameters

      • context: WorkingContext

        the working memory context that contains the current state of data.

      Returns AbstractRule[]

      an array of applicable rules that can be evaluated against the given context.