Create a new Workspace instance, the starting point for managing rules, constants, and their evaluation.
Optionaloptions: Partial<WorkspaceOptions>Optional configuration settings for the workspace.
ProtectedrulesProtecteddependency_ProtectedconstantsProtectedtype_ProtectedtypesProtectedfunctionsProtectedcommandsProtectedoptionsStaticdefaultCreate 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.
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.
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.
an object containing the options to set or update.
Add multiple constants to the workspace. Constants are key-value pairs that can be used in rule evaluation and are accessible across all rules.
a json object containing constant names as keys and their corresponding values.
Add a single constant to the workspace.
the name of the constant.
the value of the constant.
Check if a constant exists in the workspace.
the name of the constant.
true if the constant exists, false otherwise.
Get the value of a constant by its name.
the name of the constant.
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.
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.
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.
a created rule, or rule syntax containing annotations
Optionalsalience: numberthe optional priority of the rule, higher values indicate higher priority. Defaults to 0.
Find a rule by its name.
the name of the rule to find.
the rule with the given name, or undefined if no such rule exists in the workspace.
List all rules currently stored in the workspace.
an unsorted array of all rules in the workspace.
Clear all rules from the workspace. This will effectively create new graphs and remove all existing rules.
Debugging method to get the current depenency graph.
the current Dependency instance representing rules and their dependencies in the workspace.
Debugging method to get the current rete graph.
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.
a created function, or function syntax containing annotations
Debugging method to get the type checker of the workspace, responsible for managing type definitions and performing type checks during rule evaluation.
the TypeChecker instance used by the workspace.
Optionalvalue: unknownCreate 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.
Any input data that needs to be evaluated.
OptionalsharedData: anya new instance of WorkingMemory initialized with the given data and the current workspace.
ProtectedfindUse 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.
an array of flattened data keys from a context.
the context to use while traversing the graph.
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.
the working memory context that contains the current state of data.
an array of applicable rules that can be evaluated against the given context.
ProtectedisCheck if the given context is valid for rule evaluation by performing type checks on the input data.
the working memory context to be evaluated.
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.
the working memory context that contains the current state of data.
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.
the name of the variable to evaluate.
the working memory context that contains the current state of data.
the value of the variable after evaluation, or undefined if the variable cannot be evaluated due to validation errors or exceptions during rule processing.
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.