3 Inference Environments (Basic)
An inference environment encapsulates the state of an executing inference system. This state information includes the following:
Rule network (nodes, data index, goal, index, rule node list)
Assertion index (2 levels) and next assertion id
Exit continuation
Agenda
Current rule, trace flag, and conflict resolution strategy
It is a good practice to create a new inference environment for all of your inference systems. The with-new-inference-environment facilitates this and is used in all of the examples in this document. The primary advantage is to ensure a clean environment for each new execution of the inference system.
The PLT Scheme Inference Collection supports multiple inference environments existing at the same time.
This chapter describes the features of basic simulation environments.
3.1 The inference-environment Structure
The inference-environment structure defines an inference environment.
(struct inference-environment (data-index |
goal-index |
rule-nodes |
exit |
next-assertion-id |
assertion-index |
trace |
agenda |
rule |
strategy |
parent |
rules-fired)) |
data-index : hash-eq? |
goal-index : hash-eq? |
rule-nodes : (listof rule-node?) |
exit : (or/c procedure? false/c) |
next-assertion-id : exact-positive-integer? |
assertion-index : hash-eq? |
trace : boolean? |
agenda : (mlistof rule-instance?) |
rule : (or/c rule-instance? false/c) |
strategy : (one-of/c 'depth 'breadth 'order 'simplicity 'complex 'random) |
parent : (or/c inference-environment? false/c) |
rules-fired : exact-nonnegative-integer? |
See Section 3.3 Current Inference Environment Fields for more information on accessing the fields of the current inference environment.
data-index – an eq? hash table that provides an index of (non-goal) match nodes for each fact kind. This field is maintained internally.
goal-index – an eq? hash table that provides an index of goal match nodes for each fact kind. This field is maintained internally.
rule-nodes – a list of the rule nodes for the activated rule sets. This field is maintained internally.
exit – the continuation to exit the inference currently running in the environment, or false, #f, if no inference is currently running in the environment. This field is used internally.
next-assertion-id – the id to be used for the next assertion. This is auto-incremented each time an assertion is created. This field is maintained internally.
assertion-index – the level-1 assertion index for the environment. Each entry in the level-1 assertion index is a level-2 index. This field is used internally.
trace – if true, #t, information on the current inference (e.g., assertions, deletions, and rule firings) is printed. The default is false, #f. This may be specified by the user.
agenda – a list of the rule instances that are currently eligible to fire. This list is maintained in a priority order. The ordering is controlled by the current conflict resolution strategy. This field is used internally.
rule – the currently executing rule instance, or false, #f, if no rule is currently being executed.
strategy – the conflict resolution strategy used to order the agenda. This determines the order in which rules or equal priority are fired. The available conflict resolution strategies are:
depth – depth first. New rule instances are added to the agenda after all other rule instances of equal (or higher) priority.
breadth – breadth first. New rule instances are added at to the agenda before all of ther rule instance ot equal (or lower) priority.
order – rule order. Rule instances of equal priority on the agenda are ordered according to the order of the rules in their corresponding rule sets.
simplicity – simplest first. Rule instance of equal priority on the agenda are ordered such that the simplest rule instances (i.e., the ones with the fewest preconditions) are first.
complex – most complex first. Rule instance of equal priority on the agenda are ordered such that the most complex rule instances (i.e., the ones with the most preconditions) are first.
#<procedure:schemefont>(random) – random. Rules instances of equal priority on the agenda are in random order.
This may be specified by the user.
parent – the parent inference environment, or false, #f, if this is a top-level inference environment. (See Chapter ???.)
rules-fired – the number of rules instances that have been executed by the current inference.
(make-inference-environemnt [parent]) → inference-environment? |
parent : (or/c inference-environment false/c) = #f |
Returns a new inference environment with the given parent inference environment. The newly created inference environment is empty. That is, there are no rule sets activated, no facts are asserted, user specifiable fields have their default values, and no inference is running in the environment.
default-inference-environment |
Contains the inference environment that is used as the default value for the current inference environment (see Section 3.2).
3.2 Current Inference Environment
The current inference environment is the inference environment that is the current focus for most of the inference calls. It is both thread and continuation specific.
(current-inference-environment) → inference-environment? |
(current-inference-environment env) → void? |
env : inference-environment? |
Gets or sets the current inference environment. A guard procedure ensures that the value of current-inference-environment is indeed an inference environment, as determined by inference-environment?, otherwise a type error is raised.
|
Evaluates its body with the current inference environment bound to the value of env.
|
Evaluates its body with the current inference environment bound to a newly created inference environment. This is the most common way to create and use a new inference environment.
|
Evaluates its body with the current inference environment bound to a newly created inference environment that is a child of the current inference environement.
3.3 Current Inference Environment Fields
The most common way of accessing the current inference environment fields is using the following procedures.
(current-inference-data-index data-index) → void? |
data-index : hash-eq? |
(current-inference-data-index) → hash-eq? |
Sets or gets the data-index field of the current inference environment.
(current-inference-goal-index data-index) → void? |
data-index : hash-eq? |
(current-inference-goal-index) → hash-eq? |
Sets or gets the goal-index field of the current inference environment.
(current-inference-rule-nodes rule-nodes) → void? |
rule-nodes : (listof rule-node?) |
(current-inference-rule-nodes) → (listof rule-node?) |
Sets or gets the rule-nodes field of the current inference environment.
(current-inference-exit exit) → void? |
exit : (or/c procedure? false/c) |
(current-inference-exit) → (one-of/c procedure? false/c) |
Sets or gets the exit field of the current inference environment.
(current-inference-next-assertion-id next-assertion-id) → void? |
next-assertion-id : exact-positive-integer? |
(current-inference-next-assertion-id) |
→ exact-positive-integer? |
Sets or gets the next-assertion-id field of the current inference environment.
(current-inference-assertion-index assertion-index) → void? |
assertion-index : hash-eq? |
(current-inference-assertion-index) → hash-eq? |
Sets or gets the assertion-index field of the current inference environment.
(current-inference-trace trace) → void? |
trace : boolean? |
(current-inference-trace) → boolean? |
Sets or gets the trace field of the current inference environment.
(current-inference-agenda agenda) → void? |
agenda : (mlistof rule-instance?) |
(current-inference-agenda) → (mlistof rule-instance?) |
Sets or gets the agenda field of the current inference environment.
(current-inference-rule rule) → void? |
rule : (or/c rule-instance? false/c) |
(current-inference-rule) → (or/c rule-instance? false/c) |
Sets or gets the rule field of the current inference environment.
(current-inference-strategy strategy) → void? |
strategy : (one-of/c 'depth 'breadth 'order 'simplicity 'complex 'random) |
(current-inference-strategy) |
→ (one-of/c 'depth 'breadth 'order 'simplicity 'complex 'random) |
Sets or gets the strategy field of the current inference environment.
(current-inference-parent parent) → void? |
parent : (or/c inference-environment? false/c) |
(current-inference-parent) |
→ (or/c inference-environment? false/c) |
Sets or gets the parent field of the current inference environment.
(current-inference-rules-fired trace) → void? |
trace : exact-nonnegative-integer? |
(current-inference-rules-fired) → exact-nonnegative-integer? |
Sets or gets the rules-fired field of the current inference environment.