Documentation
¶
Overview ¶
Package stdlib is jaala's standard vocabulary: the predicates every host gets unless it composes its own set. Strings registers the string tests under str (str.contains, str.prefix, str.suffix, str.glob, str.match), Numbers registers num.value and num.unit, which split a number from its unit, Absent registers absent at the root, and Register registers all three.
Reach builds a transitive-closure predicate from a host's own walk: it runs from whichever end a query binds, refuses a call that binds neither (through its Modes), and cites each answer's path in order, which a Witness keeps. The host keeps its edge semantics; every host gets the same binding, refusal and path behaviour.
CompileGlob and CompilePattern are the compilers behind str.glob and str.match, exported so Go code that must match the same patterns (agni's profiles match nets to signals both in queries and in Go) shares one translation rather than keeping a twin that could drift.
stdlib imports ns and never an engine, so a host's fact layer that may not import one can still register the standard predicates. The engine, jaala/datalog, does not import stdlib either.
Index ¶
- func Absent(v *ns.Vocabulary) error
- func Arithmetic(v *ns.Vocabulary) error
- func CompileGlob(pattern string) (*regexp.Regexp, error)
- func CompilePattern(pattern string) (*regexp.Regexp, error)
- func EdgePaths(step EdgeStep, extra ...ns.ArgType) ns.Builtin
- func Hops(step Step, extra ...ns.ArgType) ns.Builtin
- func ListValues(v *ns.Vocabulary) error
- func Numbers(v *ns.Vocabulary) error
- func PathValues(v *ns.Vocabulary) error
- func Paths(step Step, extra ...ns.ArgType) ns.Builtin
- func Reach(walk Walk, extra ...ns.ArgType) ns.Builtin
- func Register(v *ns.Vocabulary) error
- func Strings(v *ns.Vocabulary) error
- func Units(v *ns.Vocabulary) error
- type EdgeStep
- type Step
- type Walk
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Absent ¶
func Absent(v *ns.Vocabulary) error
Absent registers absent(?x) at the root, the only way to ASK about a field the source did not state. Before Value.Absent existed such a field bound to the empty string, so it was not merely hard to select, it was indistinguishable from one that was stated as "". Its negation is the useful half as often as not: `not absent(?min)` reads "this row states a lower bound". It is not a string test, since it asks about any value, which is why it is not under str.
It also registers coalesce(?a, ?b, ?v) at the root, which binds ?v to ?a unless ?a is absent, and to ?b otherwise (#284). Both inputs must be bound, absent counting as bound, so a value that may be absent (a min over nothing, a field the source left out) can fall back to another; more fallbacks nest, coalesce(?a, coalesce(?b, ?c)). Over two absent values it binds absent, which is a value, rather than answering no row.
func Arithmetic ¶ added in v0.1.37
func Arithmetic(v *ns.Vocabulary) error
Arithmetic registers num.add, num.sub, num.mul, num.div and num.neg, the functions a query's arithmetic is written in (jaala#5): ?y = ?a * (?b + ?c) runs as num.add(?b, ?c, ?t), num.mul(?a, ?t, ?y). Each is a relation of its operands and result, so its modes run it in every direction it can: num.mul(?i, ?r, ?v) finds ?v from ?i and ?r, or ?r from ?v and ?i (Ohm's law, whichever is missing). Units follow the vocabulary's table: a sum needs one unit (a bare number takes the other side's), a product multiplies them, so amps times ohms is V once V is defined as A*ohm. A value that isn't a number, a sum of two units, and a division by zero have no result, so the call gives no row, as a comparison of unlike units holds for none.
func CompileGlob ¶
CompileGlob compiles pattern as a glob with SQLite GLOB's semantics, memoized: `*` matches any run of characters, `?` exactly one, and `[...]` one character of a class (`[abc]`, `[a-z]`, `[^abc]`). Unlike CompilePattern the match is WHOLE-STRING and case-sensitive, which is what a glob conventionally means. A literal `[` is written `[[]`, so a bus name like DATA[1:0] is matched as `DATA[[]1:0]`. An unclosed `[` is an error rather than a pattern that matches nothing.
It translates to a regexp rather than deferring to path.Match because path.Match's `*` does not cross `/`, and hierarchical net names contain `/` (a sub-sheet local is `/amp1/DATA0`), so path.Match would silently under-match exactly the names a multi-instance interface is named with. SQLite's dialect is the one Declaire's walk selectors use, so a pattern means the same in a host's SQL and in a query.
func CompilePattern ¶
CompilePattern compiles pattern as an RE2 regular expression, memoized. The match is UNANCHORED (Go's regexp semantics): `_H` matches any name containing `_H`, so a caller that means "the whole name" writes `^...$`. That is deliberate — a regex predicate that silently anchored would surprise anyone who knows RE2, and anchoring is one character. An invalid pattern is an error here rather than a silent non-match, so a typo surfaces instead of reading as "nothing matched".
func EdgePaths ¶ added in v0.1.79
EdgePaths is Paths over a step whose edges have identities: ?path names its edges too ({"nodes": [...], "edges": [...]}), a trail never takes one edge twice, and two parallel edges make two paths. agni's parts between two nets are such edges.
func Hops ¶ added in v0.1.79
Hops builds a bounded reachability predicate, reach(?from, ?to, extra..., ?within, ?hops), from a host's step (#281). Where Reach hands the whole walk to the host, Hops walks breadth first itself, so it can promise what Reach can't:
- ?within is an input, the most steps a path may take, and the walk stops there: no step is asked past it, so a radius of 2 costs two levels whatever lies beyond. It must be a whole number of at least 1.
- ?hops is the fewest steps from ?from to ?to, and each answer cites one path of that length, its steps in order from ?from to ?to (a backward walk's reversed to read that way).
- It runs from whichever end a body binds, as Reach does, and with both bound stops at ?to.
- ?from reaches itself only through a cycle, at the cycle's length, as a recursive rule's closure does.
The extra arguments must be bound; extra gives their types. Arguments are labelled from, to, argN, within and hops.
func ListValues ¶ added in v0.1.79
func ListValues(v *ns.Vocabulary) error
ListValues registers the functions under list that read a list value (jaala#283), the text the list aggregate writes (ns.ListText), or one written in a query: list.member, list.length and list.at. Each reads its list from the text alone, so it works on a list from any rule, a Source's column or a host's binding. Indexes count from 0, and list.at's negative index from the end. A value that isn't a list, absent included, gives no row: which values reach a function depends on the plan, so an error there would make one evaluator fail where another answers (as path.* found, jaala#280).
func Numbers ¶ added in v0.1.32
func Numbers(v *ns.Vocabulary) error
Numbers registers the number predicates under num: num.value, which binds a number without its unit, and num.unit, which binds the unit. They are how a query says what it means when the engine won't guess: a min, max or sum over numbers in different units is absent (jaala#80), so a query that wants the raw numbers strips the units with num.value, and one that wants a result per unit groups by num.unit.
func PathValues ¶ added in v0.1.79
func PathValues(v *ns.Vocabulary) error
PathValues registers the functions under path that read a path value (jaala#280), the text graph.paths, Paths and EdgePaths bind (ns.Path): path.length, path.node, path.step, path.edge, path.nodes and path.edges. Each reads its path from the text alone, so it works on a path from any of them, carried through rules or bound by a host. Positions count from 0. A value that isn't a path, absent included, gives no row, and neither do a path's edges when it names none: which values reach a function depends on the plan (a caller's constant reaches it through demand), so an error there would make one evaluator fail where another answers.
func Paths ¶ added in v0.1.79
Paths builds a path predicate, paths(?from, ?to, extra..., mode, within, ?path), over a host's step (jaala#280): ?path is each path from ?from to ?to of 1 to within steps (within 0 for no bound) that mode allows, as a list of its nodes (ns.Path), which path.* reads. The modes are graph.paths's:
- walk: anything may repeat, so within must bound it;
- trail: no step from one node to another twice;
- acyclic: no node twice;
- simple: no node twice, but a path may end where it started, closing a cycle;
- shortest: one path of the fewest steps per pair, the one whose nodes come first, step by step.
It runs from whichever end a body binds, as Hops does, and each answer cites its steps in order. It asks for each node's steps once per call and stops when emit does, so a limit stops the walk. The extra arguments must be bound; extra gives their types. Arguments are labelled from, to, argN, mode, within and path.
func Reach ¶ added in v0.1.10
Reach builds a transitive-closure predicate, reach(?from, ?to, extra...), from a host's walk, so each host writes only its edge semantics and gets the rest the same way:
- It runs from whichever end a body binds: out from ?from when that is bound, back from ?to when only ?to is. Its Modes say so, so the planner schedules it from the bound end and a body that binds neither is refused at validation, naming the predicate.
- With both ends bound it walks out from ?from and stops at ?to.
- Each answer cites its path's steps in order from ?from to ?to (a backward walk's path is reversed to read that way), which a Witness keeps in that order.
The extra arguments must be bound; extra gives their types. Arguments are labelled from, to and argN; set Labels, Types and Doc on the result to name them for a host's catalogue.
func Register ¶
func Register(v *ns.Vocabulary) error
Register registers everything this package provides: Strings, Numbers, Arithmetic, Units, Absent, PathValues and ListValues. A host that wants other names, or only some of it, calls those itself.
func Strings ¶
func Strings(v *ns.Vocabulary) error
Strings registers the string predicates under str: the tests str.contains, str.prefix and str.suffix, the two pattern tests str.glob (see CompileGlob) and str.match (see CompilePattern), str.distance, which binds the edit distance between two strings, and the functions that build text (#284): str.concat, str.replace, str.lower, str.upper, str.capitalize, str.split and str.find.
func Units ¶ added in v0.1.37
func Units(v *ns.Vocabulary) error
Units registers the SI prefixes (T, G, M, k, m, u and µ, n, p), the seven SI base units (kg, m, s, A, K, mol, cd), the gram as a thousandth of a kg, and the electrical units decomposed into them: V = kg*m^2/(A*s^3), ohm (and Ω) = V/A, W = V*A, J = W*s, C = A*s, F = C/V, H = V*s/A, and Hz = 1/s (jaala#205). Since every one is a product of base units, two spellings of one dimension are one unit, so a host naming N = kg*m/s^2 finds N*m is J. kg is written through V (WriteBaseThrough), so a unit with no name is written as it was before the decomposition, in volts: V^2, V/m, ohm/s. A host adds its own with Vocabulary.AddUnit, and a symbol nobody defines is a unit of its own. The table folds a query's literals (3300mV is 3.3 V) and num.*'s results; a Source's values are folded by it only when the host also calls Vocabulary.FoldSourceUnits (jaala#303).
Types ¶
type EdgeStep ¶ added in v0.1.79
type EdgeStep func(ctx context.Context, node ns.Value, out bool, extra []ns.Value, each func(next, edge ns.Value, cites []string) error) error
An EdgeStep is a Step whose edges have identities: it calls each once per edge with the node it leads to, the edge (a part, a call site, a fact's id), and the edge's citations.
type Step ¶ added in v0.1.79
type Step func(ctx context.Context, node ns.Value, out bool, extra []ns.Value, each func(next ns.Value, cites []string) error) error
A Step lists one node's neighbours: out along edges (out true) or back against them, calling each once per edge with the node it leads to and the edge's citations. The host decides what an edge is, as for a Walk, and extra holds the step's other arguments. A Step should stop and return ctx.Err() when ctx ends, and return each's error as is.
type Walk ¶ added in v0.1.10
type Walk func(ctx context.Context, start ns.Value, out bool, extra []ns.Value, visit func(node ns.Value, path []string) error) error
A Walk visits what is reachable from start, out along edges (out true) or back against them, calling visit once per node reached with its path from start: the citations of the steps taken, in walk order. The host decides what an edge is (which relation, which kinds, ties, what not to walk through); extra holds the walk's other arguments, such as the edge kinds to follow. A Walk should stop and return ctx.Err() when ctx ends, and return visit's error as is.