Nodora 0.4.2
dotnet add package Nodora --version 0.4.2
NuGet\Install-Package Nodora -Version 0.4.2
<PackageReference Include="Nodora" Version="0.4.2" />
<PackageVersion Include="Nodora" Version="0.4.2" />
<PackageReference Include="Nodora" />
paket add Nodora --version 0.4.2
#r "nuget: Nodora, 0.4.2"
#:package Nodora@0.4.2
#addin nuget:?package=Nodora&version=0.4.2
#tool nuget:?package=Nodora&version=0.4.2
nodora-dotnet
.NET bindings for the Nodora rule engine.
Install
dotnet add package Nodora
The package targets netstandard2.0, and every release is tested on .NET 8 and
.NET 10.
Prebuilt runtimes
The native engine ships inside the package under runtimes/<rid>/native and is
loaded automatically at run time. Prebuilt for:
linux-x64linux-arm64osx-x64osx-arm64win-x64
Other platforms build from source (needs Go 1.24+ with cgo), see Development.
Usage
using NodoraEngine;
using var engine = new Engine();
var ruleset = engine.Compile(@"
rule AdultCheck {
out is_adult = input.age >= 18
}
");
using var evaluator = engine.CreateEvaluator(ruleset);
var result = evaluator.Evaluate("AdultCheck", new { age = 21 });
Console.WriteLine(result.Outputs["is_adult"].GetBoolean()); // True
Evaluate takes any object; it is serialized to JSON with
System.Text.Json and becomes the rule's input. Outputs are returned as
JsonElement values you read with the usual accessors
(GetBoolean(), GetInt32(), GetString(), …).
Signals emitted by a rule are returned in the result. You can read the raw list directly:
foreach (var signal in result.EmittedSignals)
{
Console.WriteLine($"{signal.Name}({string.Join(", ", signal.Args)})");
}
Or subscribe listeners to specific signals with a SignalDispatcher, so you
don't have to switch over each signal by name:
var signals = new SignalDispatcher();
signals.On("Greet", s => Console.WriteLine($"Greetings, {s.Arg<string>(0)}!"));
signals.OnAny(s => logger.Debug($"signal {s.Name}")); // catch-all
var result = evaluator.Evaluate("AdultCheck", new { age = 21, name = "Alice" });
signals.Dispatch(result);
On and OnAny return an IDisposable you can dispose to unsubscribe.
Arg<T>(index) deserializes a positional argument to the type you expect.
Dispatch is synchronous: the decision (rule outputs) is already returned before you dispatch, so keep handlers fast. For a slow or best-effort effect such as sending an email, have the handler hand the work off to a queue or outbox and return, rather than blocking in the handler.
A precompiled ruleset (e.g. the output of nodora compile) can be loaded
without recompiling the source:
var ruleset = Ruleset.FromJson(rulesetJson);
Engines and evaluators own native handles, so dispose them when you are done (a
using statement is the easiest way). Both are thread-safe and meant to be
reused: most applications need a single engine, and one evaluator per ruleset.
Evaluators keep working after their engine is disposed.
Custom functions
Rules can call functions implemented in .NET. The signature is inferred from the delegate, and the compiler checks calls against it:
engine.Register("risk", "score", (double amount, int? age) =>
age < 25 ? amount / 50 : amount / 100);
var ruleset = engine.Compile(@"
rule Review {
out flagged = risk::score(input.amount, input.age) > 10
}
");
Register functions before compiling the rulesets that use them. Parameters of a
nullable value type (int?) are optional. If any other argument is undefined, the
function is not called and the result is undefined.
For more control over the signature, such as optional strings, union types or more
than four parameters, describe it with a FunctionSpec:
engine.Register(
new FunctionSpec("risk", "label")
{
Parameters =
{
new FunctionParameter("id", NodoraType.AnyOf(NodoraType.String, NodoraType.Number)),
new FunctionParameter("country", NodoraType.String) { IsOptional = true },
},
ReturnType = NodoraType.String,
},
args => $"{args[0]}-{args.GetOrDefault<string>(1) ?? "??"}");
Arguments and results are converted with System.Text.Json. Return
Undefined.Value for a result that is undefined rather than null. If the function
throws, the evaluation fails with a NodoraException that has the exception as its
InnerException.
Arguments and results cross the native boundary as JSON on every call, so the cost grows with their size. Pass only the fields the function needs rather than large objects or arrays.
Functions are called on the thread that evaluates the rule, possibly on several threads at once, so keep them fast and thread-safe. Asynchronous functions are not supported. Functions are never called while compiling.
Functions belong to the engine they are registered in, and are provided by the engine a ruleset is evaluated with. To test a ruleset, evaluate it with an engine that has fakes:
using var fakes = new Engine().Register("risk", "score", (double amount, int? age) => 42);
using var evaluator = fakes.CreateEvaluator(ruleset);
API
| Member | Purpose |
|---|---|
Engine.Compile(source) |
Compile Nodora source to a ruleset. |
Engine.CreateEvaluator(ruleset) |
Build a reusable evaluator. |
Engine.Register(...) |
Register a custom function. |
FunctionSpec, FunctionParameter, NodoraType |
Describe a custom function. |
FunctionArguments, Undefined |
Implement a custom function. |
Ruleset.FromJson(json) |
Load a precompiled ruleset. |
Ruleset.ToJson() |
Serialize the ruleset back to JSON. |
Evaluator.Evaluate(rule, input) |
Run a rule against any serializable input. |
Evaluator.EvaluateJson(rule, json) |
Run a rule against a raw JSON input. |
EvaluationResult { Outputs, EmittedSignals } |
Named outputs and emitted signals. |
SignalDispatcher |
Route emitted signals to named listeners. |
NodoraException |
Thrown on compile/evaluation errors. |
Development
The managed library builds without the native engine, but running tests or the example requires it. Building the engine needs a Go 1.24+ toolchain with cgo (a working C compiler):
make native # build the native engine into runtimes/<host-rid>/native
make test # build the engine, then run the test suite
make example # run the example
On Linux the engine is wrapped by native/linux.c, which keeps the Go and
.NET runtimes from crashing each other during garbage collection. Building it
also needs objcopy (binutils). On Windows it is wrapped by
native/windows.c, which keeps .NET threads out of Go code: there, .NET
9+ can crash the process when it suspends a thread for garbage collection.
On Windows, make native runs build/build-native.ps1; you can also run that
script directly. It requires a MinGW-w64 gcc on PATH for cgo.
To produce the NuGet package locally (with whatever runtimes you have built):
make pack # -> dist/Nodora.<version>.nupkg
CI (.github/workflows/build.yml) builds the native engine for every supported
RID and packs them all into a single cross-platform package on tag pushes.
License
Apache-2.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Json (>= 6.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.