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

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 :- #:sc{Int sc/Any} beta-roots alpha-fns productions fact-type-fn ancestors-fn activation-group-sort-fn activation-group-fn expr-fn-lookup]

Constructs the network from compiled beta tree and condition functions.

Inputs: [id-to-node :- #:sc{Int sc/Any} beta-roots alpha-fns productions fact-type-fn ancestors-fn activation-group-sort-fn activation-group-fn expr-fn-lookup]

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

build-token-assignmentclj

(build-token-assignment binding-key)

A helper function to build variable assignment forms for tokens.

A helper function to build variable assignment forms for tokens.
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

cljs-nsclj

(cljs-ns)

Returns the ClojureScript namespace being compiled during Clojurescript compilation.

Returns the ClojureScript namespace being compiled during Clojurescript compilation.
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-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 sc/Any, (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 sc/Any, (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 partition-size)

Inputs: [key->expr :- schema/NodeExprLookup partition-size :- sc/Int] 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 partition-size :- sc/Int]
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)
source

compiling-cljs?clj

(compiling-cljs?)

Return true if we are currently generating cljs code. Useful because cljx does not provide a hook for conditional macro expansion.

Return true if we are currently generating cljs code.  Useful because cljx does not
provide a hook for conditional macro expansion.
sourceraw docstring

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

effective-typeclj

(effective-type type)
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

(get-fields type)

Returns a map of field name to a symbol representing the function used to access it.

Returns a map of field name to a symbol representing the function used to access it.
sourceraw docstring

get-namespace-infoclj

(get-namespace-info namespace)

Get metadata about the given namespace.

Get metadata about the given namespace.
sourceraw docstring

IRuleSourcecljprotocol

load-rulesclj

(load-rules source)
source

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

resolve-cljs-symclj

(resolve-cljs-sym ns-sym sym)

Resolves a ClojureScript symbol in the given namespace.

Resolves a ClojureScript symbol in the given namespace.
sourceraw docstring

Rulebaseclj

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

t-map->Rulebaseclj

source

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