Documentation ¶
Overview ¶
Package require implements a general purpose requirements language. The language was initially designed for scheduling systems, but can be used across similar domains where label and predicate matching is needed.
The language provides the base minimum required for representing basic requirements. Overview of symbols:
~ # mark the expression as optional, but preferred ! # negate the expression label # label (re: [^=<]*) str # arbitrary string (re: .*) int # integer (re: -?[0-9]+) = # equality op; < # less-than op; for labels: lexiographic by length.
Overview of syntaxes:
[~] [!] label # label-matching expression [~] [!] {label int} = {str num} # exact value-matching expression [~] [!] {label int} < {str num} # lexiographic/numeric less-than
Examples of well-formed expressions:
healthy # require the label "healthy" ~healthy # prefer the label "healthy" color=blue # require color "blue" !color=green # exclude color "green" !ram<100 # ram is least 100 MiB ~u9n<50 # prefer utilization less than 50%.
Testing compatibility of requirements:
Label and values expression tests work as one would expect. One note is the use of the optional marker: Given the condition "~foo", both "~foo" and "foo" match the expression. Specal handling is given to less-than conditions: Given the condition "bar<100", all conditions "bar<x" where x <= 100 match, and all conditions "bar=y" where y < 100 match.
Lexiographic order is used for strings: Given the condition "baz<fizz", all conditions "baz<x" where bytes.Compare(x, fizz) <= 0 match, and all conditions "baz=y" where bytes.Compare(y, fizz) < 0 match.
Test usage:
var ctx require.Context ctx.Add(s1...) if ctx.Test(s2...) { // all required specs match! }
Scoring candidates:
This provides a integer score useful for comparing multiple options. Score takes a slice of ScoreOptions to provide a means to customize the scoring algorithms used.
Score usage (find the best match):
var ( best = -1 bestScore = require.MinScore ctx require.Context ) ctx.Add(s1...) for i, s2 := range candidates { if score := ctx.Score(s2...); bestScore < score { best = i bestScore = score if threshold <= score { break } } }
Index ¶
Constants ¶
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context contains specs to be tested or scored.
func (*Context) Add ¶
Add adds the specs to the requirement context.
func (Context) Score ¶
Score returns an integer match score of c and s2 which can be compared relative to other specs. The exact score is not meaningful alone. Specs in c2 must have been created at once by Parse or sorted using Sort. Currently the loss function used takes 1 point per unmatched optional condition and uses approximate squared error to compare numeric ranges. It is possible to construct a failing spec which is more favorable than A passing spec, but the loss would need to be close to MaxInt64.
Directories ¶
Path | Synopsis |
---|---|
Package requireutil provides utilities for working with serialized requirement expressions.
|
Package requireutil provides utilities for working with serialized requirement expressions. |