View Source Retex (retex v0.1.11)
The algorithm utilizes symbols to create an internal representation of the world.
Each element in the real world is converted into a triple known as a "Working Memory Element" (Retex.Wme.t()
),
represented as {Entity, attribute, attribute_value}.
The world is represented through facts (WMEs) and Rules. A Rule consists of two essential parts: the "given" (right side) and the "then" (left side).
To perform inference, the rule generates a directed graph starting from a common and generic Root node,
which branches out to form leaf nodes. The branches from the Root node correspond to the initial part
of the WME, representing the working memory elements or "Entity". For instance, if we want to
represent a customer's account status as "silver", we would encode it as "{Customer, account_status, silver}".
Alternatively, with the use of a struct, we can achieve the same representation as Retex.Wme.new("Customer", "account status", "silver")
the-struct
The struct
This module also defines a Struct with the following fields:
graph: Graph.new()
: This field is initialized with the value returned by theGraph.new()
function. It is a reference to a directed graph data structure (usinglibgraph
) that represents a network of interconnected nodes and vertices.wmes: %{}
: This field is initialized as an empty map. It is expected to store "working memory elements" (WMEs), which are pieces of information or facts used in the system.agenda: []
: This field is initialized as an empty list. It is intended to store a collection of tasks or items that need to be processed or executed. The agenda typically represents a prioritized queue of pending actions.activations: %{}
: This field is initialized as an empty map. It is used to store information related to the activations of nodes in the network.wme_activations: %{}
: This field is initialized as an empty map. It is similar to theactivations
field but specifically focuses on the activations of working memory elements (WMEs) and can serve as reverse lookup of which facts have activated which no.tokens: MapSet.new()
: This field is initialized with the value returned by theMapSet.new()
function. Will be deprecated soon because it has no use but was a porting from the original paper.bindings: %{}
: This field is initialized as an empty map. It is used to store variable bindings or associations between variables and their corresponding values. This can be useful for tracking and manipulating data within the system.pending_activation: []
: This field is initialized as an empty list. It is likely used to keep track of activations that are pending or awaiting further processing. The exact meaning and usage of these pending activations would depend on the system's design.
Link to this section Summary
Functions
The P node is the production node, just another name of a rule
A production is what is called a Rule in the original Rete paper from C. Forgy
Takes the network itself and a WME struct and tries to activate as many nodes as possible traversing the graph from the Root until each reachable branch executing a series of "tests" (pattern matching) at each node level.
Take each fact of the "given" part of the rule and construct the alpha part of the network destructuring the facts into "Root" -> "Entity" -> "Attribute" -> "Value" (if a node with the same value already exists it will be a noop)
After building the alpha network we will have a list of nodes which are the bottom of the new subnetwork, not connect those two by two. Take the firs two, join them with a new node, that that one node connect it with the next orphan node, keep doing it until all the facts of a rule are connected together and we have one last "Join" node.
The algorithm utilizes symbols to create an internal representation of the world.
Each element in the real world is converted into a triple known as a "Working Memory Element" (Retex.Wme.t()
),
represented as {Entity, attribute, attribute_value}.
Generate a new Retext.Root.t() struct that represents the first node of the network.
An anonymous node that functions just as connector for the type nodes (Retex.Node.Type.t()
)
Link to this section Types
@type action() :: %{given: [Retex.Wme.t()], then: [Retex.Wme.t()]}
@type network_node() :: Retex.Node.Type.t() | Retex.Node.Test.t() | Retex.Node.Select.t() | Retex.Node.PNode.t() | Retex.Node.BetaMemory.t()
Link to this section Functions
@spec add_p_node(Graph.t(), Retex.Node.BetaMemory.t(), action(), [ Retex.Fact.Filter.t() ]) :: Graph.t()
The P node is the production node, just another name of a rule
@spec add_production(t(), %{given: [Retex.Wme.t()], then: action()}) :: t()
A production is what is called a Rule in the original Rete paper from C. Forgy
A production is a map of given and then and each of those fields contains a list of
Retex.Fact.t()
which can be tested against a Retex.Wme.t()
@spec add_token(t(), network_node(), Retex.Wme.t(), map(), [Retex.Token.t()]) :: t()
@spec add_wme(t(), Retex.Wme.t()) :: t()
Takes the network itself and a WME struct and tries to activate as many nodes as possible traversing the graph from the Root until each reachable branch executing a series of "tests" (pattern matching) at each node level.
Each node is tested implementing the activation protocol, so to know if how the test for the node against the WME works check their protocol implementation.
Take each fact of the "given" part of the rule and construct the alpha part of the network destructuring the facts into "Root" -> "Entity" -> "Attribute" -> "Value" (if a node with the same value already exists it will be a noop)
@spec build_beta_network(Graph.t(), [network_node()]) :: {[network_node()], Graph.t()}
After building the alpha network we will have a list of nodes which are the bottom of the new subnetwork, not connect those two by two. Take the firs two, join them with a new node, that that one node connect it with the next orphan node, keep doing it until all the facts of a rule are connected together and we have one last "Join" node.
And example of a graph with only one "and/join" node:
flowchart
2102090852["==100"]
2332826675["==silver"]
2833714732["[{:Discount, :code, 50}]"]
3108351631["Root"]
3726656564["Join"]
3801762854["miles"]
3860425667["Customer"]
3895425755["account_status"]
4112061991["Flight"]
2102090852 --> 3726656564
2332826675 --> 3726656564
3108351631 --> 3860425667
3108351631 --> 4112061991
3726656564 --> 2833714732
3801762854 --> 2102090852
3860425667 --> 3895425755
3895425755 --> 2332826675
4112061991 --> 3801762854
@spec continue_traversal(t(), map(), network_node(), Retex.Wme.t()) :: {t(), map()}
continue_traversal(new_rete, new_bindings, current_node, wme, tokens)
View Source@spec continue_traversal(t(), map(), network_node(), Retex.Wme.t(), [Retex.Token.t()]) :: {t(), map()}
@spec create_activation(t(), network_node(), Retex.Wme.t()) :: t()
@spec deactivate_descendants(t(), network_node()) :: t()
@spec new() :: t()
The algorithm utilizes symbols to create an internal representation of the world.
Each element in the real world is converted into a triple known as a "Working Memory Element" (Retex.Wme.t()
),
represented as {Entity, attribute, attribute_value}.
The world is represented through facts (WMEs) and Rules. A Rule consists of two essential parts: the "given" (right side) and the "then" (left side).
To perform inference, the rule generates a directed graph starting from a common and generic Root node,
which branches out to form leaf nodes. The branches from the Root node correspond to the initial part
of the WME, representing the working memory elements or "Entity". For instance, if we want to
represent a customer's account status as "silver", we would encode it as "{Customer, account_status, silver}".
Alternatively, with the use of a struct, we can achieve the same representation as Retex.Wme.new("Customer", "account status", "silver")
the-struct
The struct
This module also defines a Struct with the following fields:
graph: Graph.new()
: This field is initialized with the value returned by theGraph.new()
function. It is a reference to a directed graph data structure (usinglibgraph
) that represents a network of interconnected nodes and vertices.wmes: %{}
: This field is initialized as an empty map. It is expected to store "working memory elements" (WMEs), which are pieces of information or facts used in the system.agenda: []
: This field is initialized as an empty list. It is intended to store a collection of tasks or items that need to be processed or executed. The agenda typically represents a prioritized queue of pending actions.activations: %{}
: This field is initialized as an empty map. It is used to store information related to the activations of nodes in the network.wme_activations: %{}
: This field is initialized as an empty map. It is similar to theactivations
field but specifically focuses on the activations of working memory elements (WMEs) and can serve as reverse lookup of which facts have activated which no.tokens: MapSet.new()
: This field is initialized with the value returned by theMapSet.new()
function. Will be deprecated soon because it has no use but was a porting from the original paper.bindings: %{}
: This field is initialized as an empty map. It is used to store variable bindings or associations between variables and their corresponding values. This can be useful for tracking and manipulating data within the system.pending_activation: []
: This field is initialized as an empty list. It is likely used to keep track of activations that are pending or awaiting further processing. The exact meaning and usage of these pending activations would depend on the system's design.
@spec propagate_activations(t(), network_node(), Retex.Wme.t(), map()) :: {t(), map()}
propagate_activations(rete, current_node, wme, bindings, new_tokens)
View Source@spec propagate_activations( t(), network_node(), Retex.Wme.t(), map(), [Retex.Token.t()] ) :: {t(), map()}
@spec replace_bindings(Retex.Node.PNode.t(), map()) :: Retex.Node.PNode.t()
@spec root_vertex() :: Retex.Root.t()
Generate a new Retext.Root.t() struct that represents the first node of the network.
An anonymous node that functions just as connector for the type nodes (Retex.Node.Type.t()
)