Documentation
¶
Overview ¶
Package eval is Verdict's evaluator: it turns a parsed DMN model into a prepared program and then executes decisions, decision services and boxed expressions against caller inputs, recording a trace as it goes.
Index ¶
- type Config
- type Context
- func (ec *Context) Budget() *feel.Budget
- func (ec *Context) Derive(bindings map[string]any) *Context
- func (ec *Context) Diagnose(d diag.Diagnostic)
- func (ec *Context) FEEL() *feel.Evaluator
- func (ec *Context) Go() context.Context
- func (ec *Context) Trace() trace.Writer
- func (ec *Context) Vars() map[string]any
- type Matcher
- func (m *Matcher) Admits(clause int, value any) (admits, decidable bool)
- func (m *Matcher) Compile(text string) (*feel.Expr, error)
- func (m *Matcher) EvalStatic(x *feel.Expr) (any, bool)
- func (m *Matcher) InputCount() int
- func (m *Matcher) InputValues(clause int) string
- func (m *Matcher) Match(values []any) ([]int, error)
- func (m *Matcher) OutputExprs(rule int) []*feel.Expr
- func (m *Matcher) RuleCount() int
- func (m *Matcher) Test(rule, clause int) *feel.UnaryTest
- type NodeEvaluator
- type NodeEvaluatorFunc
- type Program
- type Request
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// FEEL is the expression evaluator. Required. It carries the evaluation
// limits — the iteration budget among them (feel.WithMaxIterations) — so
// there is no second copy here that could disagree with it. Evaluate
// draws one shared budget of that size per request (FEEL.NewBudget), and
// every expression and unary test the request evaluates, decision tables
// and boxed expressions alike, charges it.
FEEL *feel.Evaluator
// Bridge answers agentDecision nodes. Nil means agent decisions fail, which
// their failure policy then handles.
Bridge agent.Bridge
// TraceMode selects how much of an evaluation is recorded.
TraceMode trace.Mode
// RedactPaths name bindings whose values are replaced in the trace.
RedactPaths []string
// Strict makes the loader reject models whose static analysis reports
// error-severity findings, and makes ambiguous hit policies fatal.
Strict bool
// DefaultMaxLatency bounds an agent decision that declares no policy of its
// own. Zero means unbounded, which is almost never what a caller wants; the
// engine's own default is applied before Config reaches here.
DefaultMaxLatency time.Duration
// Parallel evaluates independent decisions in the same dependency layer
// concurrently.
Parallel bool
// Memoize caches the value of referentially transparent nodes on the hash of
// their inputs, within a single evaluation. Agent decisions are never
// memoised.
Memoize bool
// MaxDepth bounds recursive decision-service invocation.
MaxDepth int
// Clock supplies timestamps. Nil means time.Now.
Clock func() time.Time
// NodeEvaluators overrides the built-in handling of a boxed-expression kind.
NodeEvaluators map[model.Kind]NodeEvaluator
}
Config is everything the evaluator needs that is not the model itself.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is the state one node evaluation sees: the Go context, the visible names, the trace writer for the node, and the shared diagnostic sink.
A Context is derived, never mutated in place by a child: entering a nested scope produces a new Context whose vars map shadows the parent's. That is what makes concurrent evaluation of independent decisions safe without a lock around the variable map.
func (*Context) Derive ¶
Derive returns a Context with additional bindings layered over this one. Passing a nil or empty map returns the receiver unchanged.
func (*Context) Diagnose ¶
func (ec *Context) Diagnose(d diag.Diagnostic)
Diagnose lets a custom NodeEvaluator contribute a diagnostic.
func (*Context) FEEL ¶
FEEL returns the expression evaluator, so a custom NodeEvaluator can compile and evaluate expressions with the same dialect and function libraries. Pass Budget to its EvalBudget so those expressions draw on the request's iteration budget too.
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matcher exposes a prepared decision table's rule-matching machinery to the static analyser.
Analysis runs the *same* unary tests through the *same* evaluator that evaluation uses, rather than reimplementing DMN's matching rules over the table's source text. That is what keeps the gap and overlap report honest: it cannot drift from what the engine will actually do at runtime.
func (*Matcher) Admits ¶
Admits reports whether a value is inside the domain an input clause declares through its allowed-value list, and whether there was a decidable list to ask.
The analyser probes each boundary and the points either side of it, which for a range-valued list means probing just outside the declared domain. A value the modeller has said cannot occur is not a gap, so those probes are dropped rather than reported — otherwise a table that covers its whole declared domain could never be gap-free, and --strict would be unusable.
func (*Matcher) Compile ¶
Compile parses an expression with the program's dialect, so the analyser can interpret a declared inputValues list with the same grammar the table uses.
func (*Matcher) EvalStatic ¶
EvalStatic evaluates an expression with no model variables in scope. It returns ok=false when the expression depends on runtime data, which is the analyser's signal that it cannot decide the question statically.
func (*Matcher) InputCount ¶
InputCount reports how many input clauses the table has.
func (*Matcher) InputValues ¶
InputValues returns the compiled allowed-value list of an input clause, which the analyser uses as that clause's domain when the modeller declared one.
func (*Matcher) Match ¶
Match reports which rules match a point in the table's input space, in rule order. Values are FEEL values parallel to the input clauses.
func (*Matcher) OutputExprs ¶
OutputExprs returns a rule's compiled output expressions.
type NodeEvaluator ¶
type NodeEvaluator interface {
// Evaluate produces the expression's value. The scope holds every name
// currently visible, already converted to FEEL values.
Evaluate(ec *Context, expr model.Expression) (any, error)
}
NodeEvaluator evaluates one boxed-expression kind. Registering an evaluator for a kind replaces the built-in handling for that kind, which is the extension point behind the engine's WithNodeEvaluator option.
type NodeEvaluatorFunc ¶
type NodeEvaluatorFunc func(ec *Context, expr model.Expression) (any, error)
NodeEvaluatorFunc adapts a function to NodeEvaluator.
func (NodeEvaluatorFunc) Evaluate ¶
func (f NodeEvaluatorFunc) Evaluate(ec *Context, expr model.Expression) (any, error)
Evaluate implements NodeEvaluator.
type Program ¶
type Program struct {
Defs *model.Definitions
Graph *model.Graph
// contains filtered or unexported fields
}
Program is a model prepared for evaluation: the DRG indexed, every expression compiled, every prompt template parsed.
Preparation is where a model's expression errors surface. Doing it once at load time rather than on first evaluation means a model that will fail is rejected at deploy, not at 3am.
func Prepare ¶
func Prepare(defs *model.Definitions, cfg Config) (*Program, []diag.Diagnostic, error)
Prepare compiles a parsed model. It returns the program together with any diagnostics; an error means the model cannot be evaluated at all — a cyclic DRG, or, in strict mode, any error-severity diagnostic.
func (*Program) Diagnostics ¶
func (p *Program) Diagnostics() []diag.Diagnostic
Diagnostics returns the preparation diagnostics.
func (*Program) Matcher ¶
func (p *Program) Matcher(t *model.DecisionTable) (*Matcher, bool)
Matcher builds a matcher for a prepared decision table.
func (*Program) TopLevelDecisions ¶
TopLevelDecisions lists the decisions no other decision requires — the natural outputs of the model, and the default entry point.
type Request ¶
type Request struct {
// Decisions names the decisions to evaluate, by ID or name. Empty means
// every decision that nothing else in the model depends on — the DRG's
// top-level outputs.
Decisions []string
// Service names a decision service to evaluate instead of Decisions.
Service string
// Inputs are the caller-supplied values, keyed by input-data name.
Inputs map[string]any
}
Request selects what to evaluate.
type Result ¶
type Result struct {
// Outputs are the values of the evaluated entry points, keyed by decision
// output name.
Outputs map[string]any
// Trace is the execution record. Nil when tracing is off.
Trace *trace.Trace
// Diagnostics are the model's load-time diagnostics plus anything the
// evaluation itself reported.
Diagnostics []diag.Diagnostic
// Duration is the wall time of the evaluation.
Duration time.Duration
}
Result is the outcome of one evaluation.