Liking cljdoc? Tell your friends :D

clara.rules.compiler

This namespace is for internal use and may move in the future. This is the Clara rules compiler, translating raw data structures into compiled versions and functions. Most users should use only the clara.rules namespace.

This namespace is for internal use and may move in the future.
This is the Clara rules compiler, translating raw data structures into compiled versions and functions.
Most users should use only the clara.rules namespace.
raw docstring

*compile-ctx*clj

source

add-production-load-orderclj

(add-production-load-order productions)

Adds ::rule-load-order to metadata of productions. Custom DSL's may need to use this if creating a session in Clojure without calling mk-session below.

Adds ::rule-load-order to metadata of productions. Custom DSL's may need to use this if
creating a session in Clojure without calling mk-session below.
sourceraw docstring

alpha-roots-wrapclj

(alpha-roots-wrap fact-type roots)
source

analyze-conditionclj

(analyze-condition condition)

Inputs: [condition :- schema/Condition] Returns: {:bound #{sc/Symbol}, :unbound #{sc/Symbol}, :condition schema/Condition, :is-accumulator sc/Bool}

Inputs: [condition :- schema/Condition]
Returns: {:bound #{sc/Symbol}, :unbound #{sc/Symbol}, :condition schema/Condition, :is-accumulator sc/Bool}
sourceraw docstring

BetaNodeclj

These nodes exist in the beta network.

These nodes exist in the beta network.
sourceraw docstring

build-networkclj

(build-network id-to-node
               beta-roots
               alpha-fns
               productions
               fact-type-fn
               ancestors-fn
               activation-group-sort-fn
               activation-group-fn
               expr-fn-lookup)

Inputs: [id-to-node :- schema/MutableLongHashMap beta-roots alpha-fns productions fact-type-fn ancestors-fn activation-group-sort-fn activation-group-fn expr-fn-lookup] Returns: Rulebase

Constructs the network from compiled beta tree and condition functions.

Inputs: [id-to-node :- schema/MutableLongHashMap beta-roots alpha-fns productions fact-type-fn ancestors-fn activation-group-sort-fn activation-group-fn expr-fn-lookup]
Returns: Rulebase

Constructs the network from compiled beta tree and condition functions.
sourceraw docstring

build-rule-nodeclj

(build-rule-node production)

Inputs: [production :- schema/Production] Returns: schema/ProductionNode

Inputs: [production :- schema/Production]
Returns: schema/ProductionNode
sourceraw docstring

clear-compiler-cache!clj

(clear-compiler-cache!)

Clears the default compiler cache of re-usable compiled expressions, so any subsequent expressions will be re-compiled. This is intended for use by tooling or during testing; most users can simply specify the :compiler-cache false option when creating sessions.

Clears the default compiler cache of re-usable compiled expressions, so any
subsequent expressions will be re-compiled. This is intended for use by tooling
or during testing; most users can simply specify the :compiler-cache false
option when creating sessions.
sourceraw docstring

clear-session-cache!clj

(clear-session-cache!)

Clears the cache of reusable Clara sessions, so any subsequent sessions will be re-compiled from the rule definitions. This is intended for use by tooling or specialized needs; most users can simply specify the :cache false option when creating sessions.

Clears the cache of reusable Clara sessions, so any subsequent sessions
will be re-compiled from the rule definitions. This is intended for use
by tooling or specialized needs; most users can simply specify the :cache false
option when creating sessions.
sourceraw docstring

compile-accumclj

(compile-accum node-id node-type accum env)

Used to create accumulators that take the environment into account.

Used to create accumulators that take the environment into account.
sourceraw docstring

compile-actionclj

(compile-action node-id binding-keys rhs env)

Compile the right-hand-side action of a rule, returning a function to execute it.

Compile the right-hand-side action of a rule, returning a function to execute it.
sourceraw docstring

compile-action-handlerclj

(compile-action-handler action-name binding-keys rhs env)
source

compile-alpha-nodesclj

(compile-alpha-nodes alpha-nodes expr-fn-lookup)

Inputs: [alpha-nodes :- [schema/AlphaNode] expr-fn-lookup :- schema/NodeFnLookup] Returns: [{:id sc/Int, :type sc/Any, :alpha-fn schema/Function, (sc/optional-key :env) #:sc{Keyword sc/Any}, :children [sc/Num]}]

Inputs: [alpha-nodes :- [schema/AlphaNode] expr-fn-lookup :- schema/NodeFnLookup]
Returns: [{:id sc/Int, :type sc/Any, :alpha-fn schema/Function, (sc/optional-key :env) #:sc{Keyword sc/Any}, :children [sc/Num]}]
sourceraw docstring

compile-conditionclj

(compile-condition type
                   node-id
                   destructured-fact
                   constraints
                   result-binding
                   env)

Returns a function definition that can be used in alpha nodes to test the condition.

Returns a function definition that can be used in alpha nodes to test the condition.
sourceraw docstring

compile-exprsclj

(compile-exprs key->expr options)

Inputs: [key->expr :- schema/NodeExprLookup options :- #:sc{Keyword sc/Any}] Returns: schema/NodeFnLookup

Takes a map in the form produced by extract-exprs and evaluates the values(expressions) of the map in a batched manner. This allows the eval calls to be more effecient, rather than evaluating each expression on its own. See #381 for more details.

Inputs: [key->expr :- schema/NodeExprLookup options :- #:sc{Keyword sc/Any}]
Returns: schema/NodeFnLookup

Takes a map in the form produced by extract-exprs and evaluates the values(expressions) of the map in a batched manner.
 This allows the eval calls to be more effecient, rather than evaluating each expression on its own.
 See #381 for more details.
sourceraw docstring

compile-join-filterclj

(compile-join-filter node-id
                     node-type
                     {:keys [type constraints args] :as unification-condition}
                     ancestor-bindings
                     element-bindings
                     env)

Compiles to a predicate function that ensures the given items can be unified. Returns a ready-to-eval function that accepts the following:

  • a token from the parent node
  • the fact
  • a map of bindings from the fact, which was typically computed on the alpha side
  • an environment

The function created here returns truthy if the given fact satisfies the criteria.

Compiles to a predicate function that ensures the given items can be unified. Returns a ready-to-eval
function that accepts the following:

* a token from the parent node
* the fact
* a map of bindings from the fact, which was typically computed on the alpha side
* an environment

The function created here returns truthy if the given fact satisfies the criteria.
sourceraw docstring

compile-testclj

(compile-test node-id constraints env)
source

compile-test-handlerclj

(compile-test-handler node-id constraints env)
source

condition-to-nodeclj

(condition-to-node condition env parent-bindings)

Inputs: [condition :- schema/Condition env :- (sc/maybe #:sc{Keyword sc/Any}) parent-bindings :- #{sc/Keyword}] Returns: schema/ConditionNode

Converts a condition to a node structure.

Inputs: [condition :- schema/Condition env :- (sc/maybe #:sc{Keyword sc/Any}) parent-bindings :- #{sc/Keyword}]
Returns: schema/ConditionNode

Converts a condition to a node structure.
sourceraw docstring

condition-typeclj

(condition-type condition)

Returns the type of a single condition that has been transformed to disjunctive normal form. The types are: :negation, :accumulator, :test, :exists, and :join

Returns the type of a single condition that has been transformed
to disjunctive normal form. The types are: :negation, :accumulator, :test, :exists, and :join
sourceraw docstring

create-ancestors-fnclj

(create-ancestors-fn {:keys [ancestors-fn hierarchy]})
source

create-get-alphas-fnclj

(create-get-alphas-fn fact-type-fn ancestors-fn alpha-roots)

Returns a function that given a sequence of facts, returns a map associating alpha nodes with the facts they accept.

Returns a function that given a sequence of facts,
returns a map associating alpha nodes with the facts they accept.
sourceraw docstring

default-compiler-cacheclj

source

default-session-cacheclj

source

effective-typeclj

source

extract-exprsclj

(extract-exprs beta-graph alpha-graph)

Inputs: [beta-graph :- schema/BetaGraph alpha-graph :- [schema/AlphaNode]] Returns: schema/NodeExprLookup

Walks the Alpha and Beta graphs and extracts the expressions that will be used in the construction of the final network. The extracted expressions are stored by their key, [<node-id> <field-key>], this allows for the function to be retrieved after it has been compiled.

Note: The keys of the map returned carry the metadata that can be used during evaluation. This metadata will contain, if available, the compile context, file and ns. This metadata is not stored on the expression itself because it will contain forms that will fail to eval.

Inputs: [beta-graph :- schema/BetaGraph alpha-graph :- [schema/AlphaNode]]
Returns: schema/NodeExprLookup

Walks the Alpha and Beta graphs and extracts the expressions that will be used in the construction of the final network.
 The extracted expressions are stored by their key, [<node-id> <field-key>], this allows for the function to be retrieved
 after it has been compiled.

 Note: The keys of the map returned carry the metadata that can be used during evaluation. This metadata will contain,
 if available, the compile context, file and ns. This metadata is not stored on the expression itself because it will
 contain forms that will fail to eval.
sourceraw docstring

field-name->accessors-usedclj

(field-name->accessors-used type constraints)

Returns a map of field name to accessors for any field names of type used in the constraints.

Returns a map of field name to accessors for any field names of type used
in the constraints.
sourceraw docstring

flatten-expressionclj

(flatten-expression expression)

Flattens expression as clojure.core/flatten does, except will flatten anything that is a collection rather than specifically sequential.

Flattens expression as clojure.core/flatten does, except will flatten
anything that is a collection rather than specifically sequential.
sourceraw docstring

forms-per-eval-defaultclj

The default max number of forms that will be evaluated together as a single batch. 5000 is chosen here due to the way that clojure will evaluate the vector of forms extracted from the nodes. The limiting factor here is the max java method size (64KiB), clojure will compile each form in the vector down into its own class file and generate another class file that will reference each of the other functions and wrap them in a vector inside a static method. For example,

(eval [(fn one [_] ...) (fn two [_] ...)]) would generate 3 classes.

some_namespace$eval1234 some_namespace$eval1234$one_1234 some_namespace$eval1234$two_1235

some_namespace$eval1234$one_1234 and some_namespace$eval1234$two_1235 contian the implementation of the functions, where some_namespace$eval1234 will contain two methods, invoke and invokeStatic. The invokeStatic method breaks down into something similar to a single create array call followed by 2 array set calls with new invocations on the 2 classes the method then returns a new vector created from the array.

5000 is lower than the absolute max to allow for modifications to how clojure compiles without needing to modify this. The current limit should be 5471, this is derived from the following opcode investigation:

Array creation: 5B Creating and populating the first 6 elements of the array: 60B Creating and populating the next 122 elements of the array: 1,342B Creating and populating the next 5343 elements of the array: 64,116B Creating the vector and the return statement: 4B

This sums to 65,527B just shy of the 65,536B method size limit.

The default max number of forms that will be evaluated together as a single batch.
5000 is chosen here due to the way that clojure will evaluate the vector of forms extracted from the nodes.
The limiting factor here is the max java method size (64KiB), clojure will compile each form in the vector down into
its own class file and generate another class file that will reference each of the other functions and wrap them in
a vector inside a static method. For example,

(eval [(fn one [_] ...) (fn two [_] ...)])
would generate 3 classes.

some_namespace$eval1234
some_namespace$eval1234$one_1234
some_namespace$eval1234$two_1235

some_namespace$eval1234$one_1234 and some_namespace$eval1234$two_1235 contian the implementation of the functions,
where some_namespace$eval1234 will contain two methods, invoke and invokeStatic.
The invokeStatic method breaks down into something similar to a single create array call followed by 2 array set calls
with new invocations on the 2 classes the method then returns a new vector created from the array.

5000 is lower than the absolute max to allow for modifications to how clojure compiles without needing to modify this.
The current limit should be 5471, this is derived from the following opcode investigation:

Array creation:                                                    5B
Creating and populating the first 6 elements of the array:        60B
Creating and populating the next 122 elements of the array:    1,342B
Creating and populating the next 5343 elements of the array:  64,116B
Creating the vector and the return statement:                      4B

This sums to 65,527B just shy of the 65,536B method size limit.
sourceraw docstring

get-fieldsclj

source

IClaraSourcecljprotocol

load-sourceclj

(load-source source)
source

load-source*clj

(load-source* source)

loads the rules, queries and hierarchies from a source if it implements IClaraSource, or navigates inside collections to recursively load from vectors, lists, sets, seqs.

loads the rules, queries and hierarchies from a source if it implements `IClaraSource`, or navigates inside
collections to recursively load from vectors, lists, sets, seqs.
sourceraw docstring

map->Rulebaseclj

(map->Rulebase m23701)

Factory function for class Rulebase, taking a map of keywords to field values, but not much slower than ->x like the clojure.core version. (performance is fixed in Clojure 1.7, so this should eventually be removed.)

Factory function for class Rulebase, taking a map of keywords to field values, but not much
slower than ->x like the clojure.core version.
(performance is fixed in Clojure 1.7, so this should eventually be removed.)
sourceraw docstring

mk-node-fn-nameclj

(mk-node-fn-name node-type node-id fn-type)

A simple helper function to maintain a consistent pattern for naming anonymous functions in the rulebase.

node-type - expected to align with one of the types of nodes defined in clara.rules.engine, and node-type->abbreviated-type. node-id - expected to be an integer fn-type - an identifier for what the function means to the node

fn-type is required as some nodes might have multiple functions associated to them, ex. Accumulator nodes containing filter functions.

A simple helper function to maintain a consistent pattern for naming anonymous functions in the rulebase.

node-type - expected to align with one of the types of nodes defined in clara.rules.engine, and node-type->abbreviated-type.
node-id - expected to be an integer
fn-type - an identifier for what the function means to the node

fn-type is required as some nodes might have multiple functions associated to them, ex. Accumulator nodes containing
filter functions.
sourceraw docstring

mk-sessionclj

(mk-session sources-and-options)

Creates a new session using the given rule source. The resulting session is immutable, and can be used with insert, retract, fire-rules, and query functions.

Creates a new session using the given rule source. The resulting session
is immutable, and can be used with insert, retract, fire-rules, and query functions.
sourceraw docstring

mk-session*clj

(mk-session* productions options)

Inputs: [productions :- #{schema/Production} options :- #:sc{Keyword sc/Any}]

Compile the rules into a rete network and return the given session.

Inputs: [productions :- #{schema/Production} options :- #:sc{Keyword sc/Any}]

Compile the rules into a rete network and return the given session.
sourceraw docstring

omit-compile-ctx-defaultclj

During construction of the Session there is data maintained such that if the underlying expressions fail to compile then this data can be used to explain the failure and the constraints of the rule who's expression is being evaluated. The default behavior will be to discard this data, as there will be no use unless the session will be serialized and deserialized into a dissimilar environment, ie function or symbols might be unresolvable. In those sorts of scenarios it would be possible to construct the original Session with the omit-compile-ctx flag set to false, then the compile context should aid in debugging the compilation failure on deserialization.

During construction of the Session there is data maintained such that if the underlying expressions fail to compile
then this data can be used to explain the failure and the constraints of the rule who's expression is being evaluated.
The default behavior will be to discard this data, as there will be no use unless the session will be serialized and
deserialized into a dissimilar environment, ie function or symbols might be unresolvable. In those sorts of scenarios
it would be possible to construct the original Session with the `omit-compile-ctx` flag set to false, then the compile
context should aid in debugging the compilation failure on deserialization.
sourceraw docstring

production-load-order-compclj

(production-load-order-comp a b)
source

safe-getclj

(safe-get m k)

A helper function for retrieving a given key from the provided map. If the key doesn't exist within the map this function will throw an exception.

A helper function for retrieving a given key from the provided map. If the key doesn't exist within the map this
function will throw an exception.
sourceraw docstring

sort-conditionsclj

(sort-conditions conditions)

Inputs: [conditions :- [schema/Condition]] Returns: [schema/Condition]

Performs a topologic sort of conditions to ensure variables needed by child conditions are bound.

Inputs: [conditions :- [schema/Condition]]
Returns: [schema/Condition]

Performs a topologic sort of conditions to ensure variables needed by
 child conditions are bound.
sourceraw docstring

strict-map->Rulebaseclj

(strict-map->Rulebase m23702 & [drop-extra-keys?__21722__auto__])

Factory function for class Rulebase, taking a map of keywords to field values. All keys are required, and no extra keys are allowed. Even faster than map->

Factory function for class Rulebase, taking a map of keywords to field values.  All keys are required, and no extra keys are allowed.  Even faster than map->
sourceraw docstring

to-alpha-graphclj

(to-alpha-graph beta-graph create-id-fn)

Inputs: [beta-graph :- schema/BetaGraph create-id-fn :- IFn] Returns: [schema/AlphaNode]

Returns a sequence of [condition-fn, [node-ids]] tuples to represent the alpha side of the network.

Inputs: [beta-graph :- schema/BetaGraph create-id-fn :- IFn]
Returns: [schema/AlphaNode]

Returns a sequence of [condition-fn, [node-ids]] tuples to represent the alpha side of the network.
sourceraw docstring

to-beta-graphclj

(to-beta-graph productions create-id-fn)

Inputs: [productions :- #{schema/Production} create-id-fn :- IFn] Returns: schema/BetaGraph

Produces a description of the beta network.

Inputs: [productions :- #{schema/Production} create-id-fn :- IFn]
Returns: schema/BetaGraph

Produces a description of the beta network.
sourceraw docstring

to-dnfclj

(to-dnf expression)

Convert a lhs expression to disjunctive normal form.

Convert a lhs expression to disjunctive normal form.
sourceraw docstring

try-evalclj

(try-eval expr)

Evals the given expr. If an exception is thrown, it is caught and an ex-info exception is thrown with more details added. Uses compile-ctx for additional contextual info to add to the exception details.

Evals the given `expr`.  If an exception is thrown, it is caught and an
ex-info exception is thrown with more details added.  Uses *compile-ctx*
for additional contextual info to add to the exception details.
sourceraw docstring

validate-names-uniqueclj

(validate-names-unique productions)

Checks that all productions included in the session have unique names, throwing an exception if duplicates are found.

Checks that all productions included in the session have unique names,
throwing an exception if duplicates are found.
sourceraw docstring

variables-as-keywordsclj

(variables-as-keywords expression)

Returns a set of the symbols in the given s-expression that start with '?' as keywords

Returns a set of the symbols in the given s-expression that start with '?' as keywords
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close