Table of Contents

Interface IContext

Namespace
NRules.RuleModel
Assembly
NRules.RuleModel.dll

Rules engine execution context. Can be used by rules to interact with the rules engine, i.e. insert, update, retract facts.

public interface IContext
Extension Methods

Properties

CancellationToken

Enables cooperative cancellation of the rules execution cycle.

CancellationToken CancellationToken { get; }

Property Value

CancellationToken

Match

Current rule match.

IMatch Match { get; }

Property Value

IMatch

Rule

Current rule definition.

IRuleDefinition Rule { get; }

Property Value

IRuleDefinition

Methods

GetLinked(object)

Retrieves a fact linked to the current rule activation by key.

object GetLinked(object key)

Parameters

key object

Key for the linked fact.

Returns

object

Linked fact if it exists, null otherwise.

GetLinkedKeys()

Retrieves keys of facts linked to the current rule activation.

IEnumerable<object> GetLinkedKeys()

Returns

IEnumerable<object>

Keys for linked facts.

Halt()

Halts rules execution. The engine continues execution of the current rule and exits the execution cycle.

void Halt()

Insert(object)

Inserts a new fact to the rules engine memory.

void Insert(object fact)

Parameters

fact object

Fact to add.

Exceptions

ArgumentException

If fact already exists in working memory.

InsertAll(IEnumerable<object>)

Inserts new facts to the rules engine memory.

void InsertAll(IEnumerable<object> facts)

Parameters

facts IEnumerable<object>

Facts to add.

Exceptions

ArgumentException

If any fact already exists in working memory.

InsertAllLinked(IEnumerable<KeyValuePair<object, object>>)

Inserts new facts and links them to the current rule activation. The facts will be automatically retracted if this activation is removed.

void InsertAllLinked(IEnumerable<KeyValuePair<object, object>> keyedFacts)

Parameters

keyedFacts IEnumerable<KeyValuePair<object, object>>

Keyed facts to insert. Keys must be unique for a given rule.

InsertLinked(object, object)

Inserts a new fact and links it to the current rule activation. The fact will be automatically retracted if this activation is removed.

void InsertLinked(object key, object fact)

Parameters

key object

Key for the linked fact. Must be unique for a given rule.

fact object

Fact to insert.

Resolve(Type)

Resolves a registered service (normally via an IoC container).

object Resolve(Type serviceType)

Parameters

serviceType Type

Type of service to resolve.

Returns

object

Service instance.

Retract(object)

Removes existing fact from the rules engine memory.

void Retract(object fact)

Parameters

fact object

Fact to remove.

Exceptions

ArgumentException

If fact does not exist in working memory.

RetractAll(IEnumerable<object>)

Removes existing facts from the rules engine memory.

void RetractAll(IEnumerable<object> facts)

Parameters

facts IEnumerable<object>

Facts to remove.

Exceptions

ArgumentException

If any fact does not exist in working memory.

RetractAllLinked(IEnumerable<KeyValuePair<object, object>>)

Retracts existing facts that are linked to the current rule activation.

void RetractAllLinked(IEnumerable<KeyValuePair<object, object>> keyedFacts)

Parameters

keyedFacts IEnumerable<KeyValuePair<object, object>>

Keyed facts to retract. Keys must be unique for a given rule.

Remarks

Linked facts are retracted automatically, when activation is deleted, but this method can be used in complex scenarios, when linked facts need to be retracted explicitly, prior to activation getting deleted.

RetractLinked(object, object)

Retracts existing fact that's linked to the current rule activation.

void RetractLinked(object key, object fact)

Parameters

key object

Key for the linked fact. Must be unique for a given rule.

fact object

Fact to retract.

Remarks

Linked facts are retracted automatically, when activation is deleted, but this method can be used in complex scenarios, when linked facts need to be retracted explicitly, prior to activation getting deleted.

TryInsert(object)

Inserts a fact to the rules engine memory if the fact does not exist.

bool TryInsert(object fact)

Parameters

fact object

Fact to add.

Returns

bool

Whether the fact was inserted or not.

TryRetract(object)

Removes a fact from the rules engine memory if the fact exists.

bool TryRetract(object fact)

Parameters

fact object

Fact to remove.

Returns

bool

Whether the fact was retracted or not.

TryUpdate(object)

Updates a fact in the rules engine memory if the fact exists.

bool TryUpdate(object fact)

Parameters

fact object

Fact to update.

Returns

bool

Whether the fact was updated or not.

Update(object)

Updates existing fact in the rules engine memory.

void Update(object fact)

Parameters

fact object

Fact to update.

Exceptions

ArgumentException

If fact does not exist in working memory.

UpdateAll(IEnumerable<object>)

Updates existing facts in the rules engine memory.

void UpdateAll(IEnumerable<object> facts)

Parameters

facts IEnumerable<object>

Facts to update.

Exceptions

ArgumentException

If any fact does not exist in working memory.

UpdateAllLinked(IEnumerable<KeyValuePair<object, object>>)

Updates existing facts that are linked to the current rule activation.

void UpdateAllLinked(IEnumerable<KeyValuePair<object, object>> keyedFacts)

Parameters

keyedFacts IEnumerable<KeyValuePair<object, object>>

Keyed facts to update. Keys must be unique for a given rule.

UpdateLinked(object, object)

Updates existing fact that's linked to the current rule activation.

void UpdateLinked(object key, object fact)

Parameters

key object

Key for the linked fact. Must be unique for a given rule.

fact object

Fact to update.