Documentation
¶
Overview ¶
Package engine contains an implementation of the semi-naive evaluation strategy for datalog programs. It computes the fixpoint of the consequence operator incrementally by applying rules to known facts, taking care of consequences of already seen facts only once, until no new facts have been discovered.
Index ¶
- func EvalProgram(programInfo *analysis.ProgramInfo, store factstore.FactStore, ...) error
- func EvalProgramNaive(program []ast.Clause, store factstore.SimpleInMemoryStore) error
- func EvalTransform(head ast.Atom, transform ast.Transform, input []ast.ConstSubstList, ...) error
- type EvalOption
- type EvalOptions
- type ExternalPredicateCallback
- type InclusionChecker
- type QueryContext
- func (q QueryContext) EvalExternalQuery(query ast.Atom, mode []ast.ArgMode, ext ExternalPredicateCallback, ...) error
- func (q QueryContext) EvalPremise(premise ast.Term, subst unionfind.UnionFind) ([]unionfind.UnionFind, error)
- func (q QueryContext) EvalQuery(query ast.Atom, mode []ast.ArgMode, uf unionfind.UnionFind, ...) error
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EvalProgram ¶
func EvalProgram(programInfo *analysis.ProgramInfo, store factstore.FactStore, options ...EvalOption) error
EvalProgram evaluates a given program on the given facts, modifying the fact store in the process. Deprecated: use EvalStratifiedProgramWithStats instead.
func EvalProgramNaive ¶
func EvalProgramNaive(program []ast.Clause, store factstore.SimpleInMemoryStore) error
EvalProgramNaive evaluates a given program on the given facts, modifying the fact store in the process.
Types ¶
type EvalOption ¶
type EvalOption func(*EvalOptions)
EvalOption affects the way the evaluation is performed.
func WithCreatedFactLimit ¶
func WithCreatedFactLimit(limit int) EvalOption
WithCreatedFactLimit is an evaluation option that limits the maximum number of facts created during evaluation.
func WithExternalPredicates ¶ added in v0.4.0
func WithExternalPredicates( callbacks map[ast.PredicateSym]ExternalPredicateCallback) EvalOption
WithExternalPredicates allows the user to provide callbacks for external predicates.
type EvalOptions ¶
type EvalOptions struct {
// contains filtered or unexported fields
}
EvalOptions are used to configure the evaluation.
type ExternalPredicateCallback ¶ added in v0.4.0
type ExternalPredicateCallback interface {
// If true, the engine will pass any subgoals of the clause that mention output variables
// to ExecuteQuery. Otherwise, the pushdown argument will be empty.
ShouldPushdown() bool
ShouldQuery(inputs []ast.Constant, filters []ast.BaseTerm, pushdown []ast.Term) bool
ExecuteQuery(inputs []ast.Constant, filters []ast.BaseTerm, pushdown []ast.Term,
cb func([]ast.BaseTerm)) error
}
ExternalPredicateCallback is used to query external data sources.
An atom `mydb(input1, ..., inputN, OutputVar1, ..., OutputVarN)` is evaluated as follows:
- the engine checks whether the fact store contains any facts of the shape `mydb(input1, ..., inputN, _, ..., _)`. If so, we use those for evaluation.
- if no facts were found, the engine calls `ShouldQuery(input1, ..., inputN)` if false, evaluation continues.
- if true, the engine calls `Query(input1, ..., inputN, filter1, ..., filterM)` and expects outputs callback. Every output tuple gets added as `mydb(input1, ..., inputN, output1, ..., outputN)` fact to the store, and continues evaluation. if ExecuteQuery returns an error, evaluation fails with that error.
If tuples (input1, ..., inputN) are known to yield empty results, the implementation can keep track of that and prevent an unnecessary call to `ExecuteQuery`. filters contains the arguments that are output positions which are either variables or constants that are used to match the position (filters proper). The implementation may use the constant filter arguments for filter-pushdown, but is also free to ignore them. In any case, when constant filter arguments are present, only matching facts will be added to the store.
type InclusionChecker ¶
type InclusionChecker struct {
// contains filtered or unexported fields
}
InclusionChecker checks inclusion constraints. It does not check type bounds.
func NewInclusionChecker ¶
func NewInclusionChecker(decls map[ast.PredicateSym]ast.Decl) (*InclusionChecker, error)
NewInclusionChecker returns a new InclusionChecker.
func NewInclusionCheckerFromDesugared ¶
func NewInclusionCheckerFromDesugared(decls map[ast.PredicateSym]*ast.Decl) *InclusionChecker
NewInclusionCheckerFromDesugared returns a new InclusionChecker. The declarations must be in desugared form.
type QueryContext ¶ added in v0.2.0
type QueryContext struct {
PredToRules map[ast.PredicateSym][]ast.Clause
PredToDecl map[ast.PredicateSym]*ast.Decl
Store factstore.ReadOnlyFactStore
ExternalPredicates map[ast.PredicateSym]ExternalPredicateCallback
}
QueryContext groups data needed for evaluating a query top-down (backward chaining).
func (QueryContext) EvalExternalQuery ¶ added in v0.4.0
func (q QueryContext) EvalExternalQuery(query ast.Atom, mode []ast.ArgMode, ext ExternalPredicateCallback, pushdown []ast.Term, cb func(fact ast.Atom) error) error
EvalExternalQuery evaluates an external query. See ExternalPredicateCallback for more details.
func (QueryContext) EvalPremise ¶ added in v0.2.0
func (q QueryContext) EvalPremise(premise ast.Term, subst unionfind.UnionFind) ([]unionfind.UnionFind, error)
EvalPremise evaluates a single premise top-down. This is similar to PROLOG style SLD resolution: even though we have negated atoms, they are treated them as lookups (stratified semantics).
func (QueryContext) EvalQuery ¶ added in v0.2.0
func (q QueryContext) EvalQuery(query ast.Atom, mode []ast.ArgMode, uf unionfind.UnionFind, cb func(fact ast.Atom) error) error
EvalQuery evaluates a query top-down, according to mode and union-find-subst. The mode must consist only of ArgModeInput (+) and ArgModeOutput (-). For every input, query.Args[i] is either a constant or a variable that is in the domain of subst.
type Stats ¶
type Stats struct {
Strata [][]ast.PredicateSym
Duration []time.Duration
PredToStratum map[ast.PredicateSym]int
}
Stats represents strata and their running times.
func EvalProgramWithStats ¶
func EvalProgramWithStats(programInfo *analysis.ProgramInfo, store factstore.FactStore, options ...EvalOption) (Stats, error)
EvalProgramWithStats evaluates a given program on the given facts, modifying the fact store in the process. Deprecated: use EvalStratifiedProgramWithStats instead.
func EvalStratifiedProgramWithStats ¶
func EvalStratifiedProgramWithStats(programInfo *analysis.ProgramInfo, strata []analysis.Nodeset, predToStratum map[ast.PredicateSym]int, store factstore.FactStore, options ...EvalOption) (Stats, error)
EvalStratifiedProgramWithStats evaluates a given stratified program on the given facts, modifying the fact store in the process.