ReteEngine 1.0
A rules engine and builder
Loading...
Searching...
No Matches
ReteProgram.RuleBuilder< TInitial > Class Template Reference

Provides a fluent interface for constructing and configuring rules in a Rete-based rule engine. More...

Public Member Functions

 RuleBuilder (ReteEngine engine, string name)
 Initializes a new instance of the RuleBuilder class with the specified Rete engine and rule name.
RuleBuilder< TInitial > Match< T > (string name, string? debugLabel=null)
 Adds a match condition for facts of the specified type to the rule being built.
RuleBuilder< TInitial > And< T > (string name, Func< Token, T, bool > joinCondition, string? debugLabel=null)
 Adds a join condition to the rule, requiring that both the previous conditions and the specified join condition are satisfied for a fact to match.
RuleBuilder< TInitial > Or< T > (string name, string? debugLabel=null, params Func< Token, T, bool >[] orConditions)
 Adds a logical OR branch to the rule, allowing the rule to match if any of the specified conditions are satisfied for facts of the given type.
RuleBuilder< TInitial > AndNot< T > (string name, Func< Token, T, bool > joinCondition, string? debugLabel=null)
 Adds a negated join condition to the rule, specifying that the rule should only match if there are no facts of type T that satisfy the given join condition.
RuleBuilder< TInitial > Not< T > (string name, Func< Token, T, bool > joinCondition, string? debugLabel=null)
 Adds a "not" condition to the rule, specifying that there must be no facts of type T that satisfy the given join condition for the rule to match.
RuleBuilder< TInitial > Exists< T > (string name, Func< Token, T, bool > joinCondition, string? debugLabel=null)
 Adds an "exists" condition to the rule, specifying that there must be at least one fact of type T that satisfies the given join condition for the rule to match.
RuleBuilder< TInitial > Then (Action< Token > action, int salience=0)
 Adds a terminal action to the rule that will be executed when the rule is triggered. This method finalizes the rule definition by specifying the consequence of the rule. The action will be invoked each time the rule's conditions are satisfied. Salience can be used to control the order in which rules are executed when multiple rules are eligible to fire. Higher salience values indicate higher priority, meaning that rules with greater salience will be executed before those with lower salience when they are both eligible to fire. The default salience is 0, and rules with the same salience will be executed in the order they were added to the agenda. Use this method to specify the action that should be taken when the rule's conditions are met, and optionally assign a salience to influence the execution order of the rule relative to others.
RuleBuilder< TInitial > Trace (string label)
 Adds a trace node to the rule builder pipeline with the specified label, enabling inspection of rule evaluation at this point.
void Assert (object fact)
 Asserts the specified fact into the current context or knowledge base.
RuleBuilder< TInitial > StartWith (AlphaMemory alpha, string factName)
 Begins the rule definition by specifying the initial AlphaMemory node and the fact name to match against.
RuleBuilder< TInitial > JoinWith< TNext > (ReteCore.AlphaMemory nextAlpha, Func< Token, TNext, bool > condition)
 Adds a join node to the rule network that combines the current beta memory with the specified alpha memory using the given join condition.
void Then (Agenda agenda, Action< Token > action, int salience=0)
 Defines the terminal action to execute when the rule is triggered, and adds the rule to the specified agenda with an optional salience.

Properties

string RuleName [get]
 Gets the name of the rule associated with this instance.

Detailed Description

Provides a fluent interface for constructing and configuring rules in a Rete-based rule engine.

Use the RuleBuilder to define the sequence of conditions and actions that make up a rule. The builder supports chaining methods to specify matching patterns, logical combinations (AND/OR), and actions to execute when the rule is triggered. Each method call adds a new node or condition to the rule's execution graph. The builder is not thread-safe and should be used from a single thread during rule construction.

Template Parameters
TInitialThe type of the initial fact or object that the rule operates on.

Member Function Documentation

◆ And< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.And< T > ( string name,
Func< Token, T, bool > joinCondition,
string? debugLabel = null )

Adds a join condition to the rule, requiring that both the previous conditions and the specified join condition are satisfied for a fact to match.

Use this method to combine multiple conditions in a rule using logical AND semantics. The join condition is evaluated for each fact of type T, and only facts that satisfy the condition are considered matches. If a debug label is provided, diagnostic output is written to the console each time the join condition is evaluated.

Template Parameters
TThe type of fact to join with the current rule conditions.
Parameters
nameThe name used to identify the join node within the rule network.
joinConditionA function that determines whether a given fact of type T should be joined with the current token. Returns true if the fact matches the join condition; otherwise, false.
debugLabelAn optional label used for debugging output. If specified, debug information about the join evaluation is written to the console.
Returns
The current RuleBuilder<TInitial> instance, allowing for method chaining.

◆ AndNot< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.AndNot< T > ( string name,
Func< Token, T, bool > joinCondition,
string? debugLabel = null )

Adds a negated join condition to the rule, specifying that the rule should only match if there are no facts of type T that satisfy the given join condition.

Template Parameters
TThe type of fact to which the conditions apply.
Parameters
nameThe name used to identify this branch in the rule for debugging or tracing purposes.
joinConditionA function that determines whether a given fact of type T should be joined with the current token.
debugLabelAn optional label used for debugging output. If specified, debug information about the evaluation of the condition is written to the console.
Returns
The current RuleBuilder<TInitial> instance, allowing further rule configuration.

◆ Assert()

void ReteProgram.RuleBuilder< TInitial >.Assert ( object fact)

Asserts the specified fact into the current context or knowledge base.

Parameters
factThe fact to assert. Cannot be null.

◆ Exists< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Exists< T > ( string name,
Func< Token, T, bool > joinCondition,
string? debugLabel = null )

Adds an "exists" condition to the rule, specifying that there must be at least one fact of type T that satisfies the given join condition for the rule to match.

Template Parameters
TThe type of fact to which the conditions apply.
Parameters
nameThe name used to identify this branch in the rule for debugging or tracing purposes.
joinConditionA function that determines whether a given fact of type T should be joined with the current token.
debugLabelAn optional label used for debugging output. If specified, debug information about the evaluation of the condition is written to the console.
Returns
The current RuleBuilder<TInitial> instance, allowing further rule configuration.

◆ JoinWith< TNext >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.JoinWith< TNext > ( ReteCore.AlphaMemory nextAlpha,
Func< Token, TNext, bool > condition )

Adds a join node to the rule network that combines the current beta memory with the specified alpha memory using the given join condition.

Use this method to extend the rule network by specifying additional join conditions between working memory elements. The join condition is evaluated for each combination of tokens and facts from the respective memories.

Template Parameters
TNextThe type of the facts stored in the alpha memory to be joined.
Parameters
nextAlphaThe alpha memory node to join with the current beta memory. Cannot be null.
conditionA function that determines whether a token from the beta memory and a fact from the alpha memory should be joined. Returns true to join the pair; otherwise, false.
Returns
The current RuleBuilder<TInitial> instance, enabling method chaining.

◆ Match< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Match< T > ( string name,
string? debugLabel = null )

Adds a match condition for facts of the specified type to the rule being built.

Use this method to specify that the rule should match facts of type T. Multiple calls to Match can be chained to build more complex rules. The name parameter is used to reference this match in subsequent rule conditions or actions.

Template Parameters
TThe type of fact to match in the rule.
Parameters
nameThe name used to identify this match condition within the rule. Cannot be null or empty.
debugLabelAn optional label used for debugging purposes. If specified, diagnostic output will be written when the match is added.
Returns
The current RuleBuilder instance, enabling further configuration of the rule.

◆ Not< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Not< T > ( string name,
Func< Token, T, bool > joinCondition,
string? debugLabel = null )

Adds a "not" condition to the rule, specifying that there must be no facts of type T that satisfy the given join condition for the rule to match.

Template Parameters
TThe type of fact to which the conditions apply.
Parameters
nameThe name used to identify this branch in the rule for debugging or tracing purposes.
joinConditionA function that determines whether a given fact of type T should be joined with the current token.
debugLabelAn optional label used for debugging output. If specified, debug information about the evaluation of the condition is written to the console.
Returns
The current RuleBuilder<TInitial> instance, allowing further rule configuration.

◆ Or< T >()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Or< T > ( string name,
string? debugLabel = null,
params Func< Token, T, bool >[] orConditions )

Adds a logical OR branch to the rule, allowing the rule to match if any of the specified conditions are satisfied for facts of the given type.

Each condition in orConditions is evaluated independently against facts of type T . The rule continues if at least one condition is met. This method enables branching logic within a rule, similar to a logical OR operation.

Template Parameters
TThe type of fact to which the conditions apply.
Parameters
nameThe name used to identify this branch in the rule for debugging or tracing purposes.
debugLabelAn optional label used for debugging output. If specified, debug information about the evaluation of each OR condition is written to the console.
orConditionsOne or more predicate functions that define the alternative conditions. The rule matches if any of these predicates return true for a given fact.
Returns
The current RuleBuilder<TInitial> instance, allowing further rule configuration.

◆ RuleBuilder()

ReteProgram.RuleBuilder< TInitial >.RuleBuilder ( ReteEngine engine,
string name )

Initializes a new instance of the RuleBuilder class with the specified Rete engine and rule name.

Parameters
engineThe ReteEngine instance that will be used to build and manage the rule. Cannot be null.
nameThe name to assign to the rule being built. Cannot be null or empty.

◆ StartWith()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.StartWith ( AlphaMemory alpha,
string factName )

Begins the rule definition by specifying the initial AlphaMemory node and the fact name to match against.

Call this method as the first step when constructing a rule. Subsequent calls to JoinWith or other builder methods will extend the rule from this starting point.

Parameters
alphaThe AlphaMemory node that serves as the starting point for the rule's pattern matching.
factNameThe name of the fact to be matched by the initial AlphaMemory node. Cannot be null or empty.
Returns
A RuleBuilder<TInitial> instance configured to continue building the rule from the specified AlphaMemory node.

◆ Then() [1/2]

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Then ( Action< Token > action,
int salience = 0 )

Adds a terminal action to the rule that will be executed when the rule is triggered. This method finalizes the rule definition by specifying the consequence of the rule. The action will be invoked each time the rule's conditions are satisfied. Salience can be used to control the order in which rules are executed when multiple rules are eligible to fire. Higher salience values indicate higher priority, meaning that rules with greater salience will be executed before those with lower salience when they are both eligible to fire. The default salience is 0, and rules with the same salience will be executed in the order they were added to the agenda. Use this method to specify the action that should be taken when the rule's conditions are met, and optionally assign a salience to influence the execution order of the rule relative to others.

Use this method to specify the consequence of the rule. The action will be invoked each time the rule's conditions are satisfied. Salience can be used to control the order in which rules are executed when multiple rules are eligible.

Parameters
actionThe action to execute when the rule fires. The action receives the matched token as its parameter. Cannot be null.
salienceThe priority of the rule when multiple rules are eligible to fire. Higher values indicate higher priority. The default is 0.
Returns
The current RuleBuilder<TInitial> instance, enabling further rule configuration.

◆ Then() [2/2]

void ReteProgram.RuleBuilder< TInitial >.Then ( Agenda agenda,
Action< Token > action,
int salience = 0 )

Defines the terminal action to execute when the rule is triggered, and adds the rule to the specified agenda with an optional salience.

This method finalizes the rule definition by specifying the action to perform when the rule conditions are met. The rule is then registered with the provided agenda. If multiple rules are eligible to fire, those with higher salience values are prioritized.

Parameters
agendaThe agenda to which the rule will be added. Cannot be null.
actionThe action to execute when the rule fires. Receives the token that caused the rule to trigger. Cannot be null.
salienceThe priority of the rule within the agenda. Higher values indicate higher priority. The default is 0.

◆ Trace()

RuleBuilder< TInitial > ReteProgram.RuleBuilder< TInitial >.Trace ( string label)

Adds a trace node to the rule builder pipeline with the specified label, enabling inspection of rule evaluation at this point.

Use trace nodes to monitor or debug the flow of facts through the rule network. Tracing can help diagnose rule behavior or performance issues by providing labeled checkpoints in the evaluation process.

Parameters
labelThe label to associate with the trace node. Used to identify the trace point during rule evaluation.
Returns
The current RuleBuilder<TInitial> instance, allowing for method chaining.

The documentation for this class was generated from the following file:
  • ReteProgram/RuleBuilder.cs