Table of Contents

Class RuleEngine

Namespace
Pamoja.Profile
Assembly
Pamoja.Profile.dll

Runs a rule file off a link.

public sealed class RuleEngine : IDisposable
Inheritance
RuleEngine
Implements
Inherited Members

Remarks

ListenAsync() checks that every output a rule drives was given and subscribes to every watched topic; each StepAsync(TimeSpan) handles one message, switching the outputs by name and publishing over the same link; and RunAsync(Func<IReadOnlyList<RuleFired>, ValueTask>?, Func<Exception, ValueTask>?, int?, TimeSpan?, CancellationToken) repeats it until cancelled. The engine owns the evaluator it loads from a file's text, and disposing it releases that.

Constructors

Assembles an engine around an evaluator already loaded, which the caller keeps.

public RuleEngine(RuleEvaluator rules, ILink link, IReadOnlyDictionary<string, Func<bool, ValueTask>>? actuators = null, Func<TransportMessage, float>? decode = null)

Parameters

rules RuleEvaluator

The rules, loaded.

link ILink

The link readings arrive on and actions publish over.

actuators IReadOnlyDictionary<string, Func<bool, ValueTask>>

The outputs the rules drive, each under the name the rule file gives it.

decode Func<TransportMessage, float>

Reads a message as a reading; its text as a number unless given.

Assembles an engine from a rule file's text.

public RuleEngine(string rules, ILink link, IReadOnlyDictionary<string, Func<bool, ValueTask>>? actuators = null, Func<TransportMessage, float>? decode = null)

Parameters

rules string

The rule file's text.

link ILink

The link readings arrive on and actions publish over.

actuators IReadOnlyDictionary<string, Func<bool, ValueTask>>

The outputs the rules drive, each under the name the rule file gives it.

decode Func<TransportMessage, float>

Reads a message as a reading; its text as a number unless given.

Exceptions

PamojaException

The rule file is refused, with the rule and the reason.

Properties

Actuators

Gets the outputs the rules drive, each once, in name order.

public IReadOnlyList<string> Actuators { get; }

Property Value

IReadOnlyList<string>

Evaluator

Gets the rules, deciding each reading as the engine hands it over.

public RuleEvaluator Evaluator { get; }

Property Value

RuleEvaluator

Topics

Gets the topics the rules watch, each once, in name order.

public IReadOnlyList<string> Topics { get; }

Property Value

IReadOnlyList<string>

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

IsSet(string)

Says whether a rule's condition holds.

public bool? IsSet(string rule)

Parameters

rule string

The rule's name.

Returns

bool?

The state, or null for a name no rule has.

ListenAsync()

Checks the engine was given every output a rule drives, and subscribes to every watched topic.

public Task ListenAsync()

Returns

Task

A task that completes once the engine is listening.

Exceptions

PamojaException

A rule drives an output the engine was not given, naming it.

RunAsync(Func<IReadOnlyList<RuleFired>, ValueTask>?, Func<Exception, ValueTask>?, int?, TimeSpan?, CancellationToken)

Handles messages until cancelled.

public Task RunAsync(Func<IReadOnlyList<RuleFired>, ValueTask>? onFired = null, Func<Exception, ValueTask>? onError = null, int? messages = null, TimeSpan? poll = null, CancellationToken cancellationToken = default)

Parameters

onFired Func<IReadOnlyList<RuleFired>, ValueTask>

Hears each message that set or cleared a rule, with what fired.

onError Func<Exception, ValueTask>

Hears a message that could not be judged or an action that failed. The loop carries on with the next message; without one, the failure ends the loop and is thrown.

messages int?

How many messages to handle before returning; until cancelled unless given.

poll TimeSpan?

How long each wait for a message lasts before the token is checked again.

cancellationToken CancellationToken

Stops the loop between messages.

Returns

Task

A task that completes once cancelled, the messages run out, or the link ends.

StepAsync(TimeSpan)

Waits a limited time for one message and runs every rule that watches its topic.

public Task<IReadOnlyList<RuleFired>?> StepAsync(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait.

Returns

Task<IReadOnlyList<RuleFired>>

What fired, which is empty when the message set or cleared nothing, or null when no message arrived in time or the link has ended.

Exceptions

PamojaException

A reading on a watched topic is not a finite number.