@samatawy/rules
    Preparing search index...

    Class Workspace

    The Workspace class serves as the central hub for managing rules, constants, and their evaluation. It maintains a collection of rules, a graph structure for efficient rule retrieval, and a set of constants that can be used in rule evaluation. The workspace provides methods for adding rules and constants, loading contexts for evaluation, and processing rules against given data.

    Implements

    Index

    Constructors

    Properties

    dependency_graph: DependencyGraph
    rete_graph: ReteGraph
    constants: Record<string, any>
    type_checker: WorkingTypeChecker
    functions: FunctionRegistry
    commands: CommandRegistry

    Methods

    • Create a clone of the current Workspace instance, including all rules, constants, types, and functions. This is useful for creating isolated copies of the workspace for testing, experimentation, or parallel processing without affecting the original workspace. You can safely mutate a cloned workspace without affecting the source, since no references are shared.

      Returns Workspace

      a new Workspace instance that is a deep clone of the current workspace.

    • Get the options currently set for the workspace, which control various aspects of its behavior such as debugging, conflict resolution, and validation strictness.

      Returns WorkspaceOptions

      an object containing the current workspace options.

    • Set or update the options for the workspace. This allows you to configure the behavior of the workspace, such as debugging, conflict resolution, and validation strictness.

      Parameters

      • options: Partial<WorkspaceOptions>

        an object containing the options to set or update.

      Returns void

    • Add multiple constants to the workspace. Constants are key-value pairs that can be used in rule evaluation and are accessible across all rules.

      Parameters

      • constants: Record<string, any>

        a json object containing constant names as keys and their corresponding values.

      Returns void

    • Add a single constant to the workspace.

      Parameters

      • key: string

        the name of the constant.

      • value: any

        the value of the constant.

      Returns void

    • Check if a constant exists in the workspace.

      Parameters

      • key: string

        the name of the constant.

      Returns boolean

      true if the constant exists, false otherwise.

    • Get the value of a constant by its name.

      Parameters

      • key: string

        the name of the constant.

      Returns any

      the value of the constant, or undefined if the constant does not exist.

    • Get all constants currently stored in the workspace as a key-value object.

      Returns Record<string, any>

      an object containing all constants in the workspace, where keys are constant names and values are their corresponding values.

    • Clear all constants from the workspace. This will remove all existing constants and their values, effectively resetting the constants to an empty state.

      Returns void

    • Add a rule to the workspace. The rule can be provided as a string containing the rule syntax, or as an already created AbstractRule instance. If a string is provided, it will be parsed into an AbstractRule using the RuleParser.

      Parameters

      • rule: string | AbstractRule

        a created rule, or rule syntax containing annotations

      • Optionalsalience: number

        the optional priority of the rule, higher values indicate higher priority. Defaults to 0.

      Returns void

    • Find a rule by its name.

      Parameters

      • name: string

        the name of the rule to find.

      Returns AbstractRule | undefined

      the rule with the given name, or undefined if no such rule exists in the workspace.

    • Clear all rules from the workspace. This will effectively create new graphs and remove all existing rules.

      Returns void

    • Debugging method to get the current rete graph.

      Returns ReteGraph

      the current ReteGraph instance representing the optimized rete network for rule evaluation in the workspace.

    • Add a function to the workspace. The function can be provided as a string containing the function syntax, or as an already created FunctionDefinition instance. If a string is provided, it will be parsed into a FunctionDefinition using the FunctionParser.

      Parameters

      • func: string | FunctionDefinition

        a created function, or function syntax containing annotations

      Returns void

    • Debugging method to get the type checker of the workspace, responsible for managing type definitions and performing type checks during rule evaluation.

      Returns TypeChecker

      the TypeChecker instance used by the workspace.

    • Create a working memory to wrap input data and hold outputs and exceptions during rule evaluation. The working memory serves as the context in which rules are evaluated and executed.

      Parameters

      • data: any

        Any input data that needs to be evaluated.

      • OptionalsharedData: any

      Returns WorkingMemory

      a new instance of WorkingMemory initialized with the given data and the current workspace.

    • Use the rete graph to find rules relevant to a given array of input keys.

      N.B. Disabled rules are not returned as relevant, even if their requirements are met, since they will not be executed when processing the context.

      Parameters

      • data_ids: string[]

        an array of flattened data keys from a context.

      • context: WorkingContext

        the context to use while traversing the graph.

      Returns Set<AbstractRule>

      a set of rules relevant to the given data keys.

    • Get all rules that should be executed on the given context. This is done by traversing the rete graph starting from the data nodes that match the keys in the context, and collecting all rules that are reachable and executable based on decision expressions.

      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.

    • Check if the given context is valid for rule evaluation by performing type checks on the input data.

      Parameters

      Returns boolean

      a boolean indicating whether the context is valid for rule evaluation.

    • Evaluate relevant rules against the given context, and execute their consequences. This process is iterative and continues until no new rules become applicable or a maximum iteration limit is reached to prevent infinite loops. In each iteration, all currently applicable rules are evaluated and their executors are collected. Then, all executors are executed in a batch.

      Parameters

      • context: WorkingMemory

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

      Returns boolean

      the final output after processing all applicable rules.

    • Perform backward-chaining style evaluation to determine the value of a specific variable based on the current context and applicable rules.

      Parameters

      • variable: string

        the name of the variable to evaluate.

      • context: WorkingMemory

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

      Returns any

      the value of the variable after evaluation, or undefined if the variable cannot be evaluated due to validation errors or exceptions during rule processing.