Class RuleEngine
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
RuleEngine(RuleEvaluator, ILink, IReadOnlyDictionary<string, Func<bool, ValueTask>>?, Func<TransportMessage, float>?)
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
rulesRuleEvaluatorThe rules, loaded.
linkILinkThe link readings arrive on and actions publish over.
actuatorsIReadOnlyDictionary<string, Func<bool, ValueTask>>The outputs the rules drive, each under the name the rule file gives it.
decodeFunc<TransportMessage, float>Reads a message as a reading; its text as a number unless given.
RuleEngine(string, ILink, IReadOnlyDictionary<string, Func<bool, ValueTask>>?, Func<TransportMessage, float>?)
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
rulesstringThe rule file's text.
linkILinkThe link readings arrive on and actions publish over.
actuatorsIReadOnlyDictionary<string, Func<bool, ValueTask>>The outputs the rules drive, each under the name the rule file gives it.
decodeFunc<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
Evaluator
Gets the rules, deciding each reading as the engine hands it over.
public RuleEvaluator Evaluator { get; }
Property Value
Topics
Gets the topics the rules watch, each once, in name order.
public IReadOnlyList<string> Topics { get; }
Property Value
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
rulestringThe rule's name.
Returns
- bool?
The state, or
nullfor 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
onFiredFunc<IReadOnlyList<RuleFired>, ValueTask>Hears each message that set or cleared a rule, with what fired.
onErrorFunc<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.
messagesint?How many messages to handle before returning; until cancelled unless given.
pollTimeSpan?How long each wait for a message lasts before the token is checked again.
cancellationTokenCancellationTokenStops 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
timeoutTimeSpanHow long to wait.
Returns
- Task<IReadOnlyList<RuleFired>>
What fired, which is empty when the message set or cleared nothing, or
nullwhen no message arrived in time or the link has ended.
Exceptions
- PamojaException
A reading on a watched topic is not a finite number.