-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An initial forray into backward chaining.
Implements BackwardFilterNode and BackwardExtremumNode. No thought has yet been put into integrating these with the @rule macro. For now, such nodes will need tto be created and connected by hand.
- Loading branch information
1 parent
18e85a9
commit 32d3d3e
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Some backwad chaining functionality | ||
|
||
export BackwardChaining, BackwardFilterNode, BackwardExtremumNode | ||
|
||
|
||
""" | ||
BackwardChaining is the abstract supertype for all backward chaining | ||
Rete nodes. | ||
""" | ||
abstract type BackwardChaining <: AbstractReteNode end | ||
|
||
|
||
function receive(::BackwardChaining, fact) | ||
# no-op | ||
end | ||
|
||
|
||
""" | ||
BackwardFilterNode(filter_function, label) | ||
When `askc`ed, passes through from its inputs only those facts whuich | ||
satisfy `predicate`. | ||
""" | ||
struct BackwardFilterNode <: BackwardChaining | ||
inputs::Set{AbstractReteNode} | ||
outputs::Set{AbstractReteNode} | ||
predicate | ||
label | ||
|
||
function BackwardFilterNode(predicate, label) | ||
new(Set{AbstractReteNode}(), | ||
Set{AbstractReteNode}(), | ||
predicate, label) | ||
end | ||
end | ||
|
||
function askc(continuation, node::BackwardFilterNode) | ||
for input in node.inputs | ||
askc(input) do fact | ||
if node.predicate(fact) | ||
continuation(fact) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
""" | ||
BackwardExtremumNode(comparison, extractor, label) | ||
When `askc`ed, provides one value: the fact with the most extreme | ||
value (based on `comparison`) of `extractor` applied to each input | ||
fact at the time `askc` was called. | ||
""" | ||
struct BackwardExtremumNode <: BackwardChaining | ||
inputs::Set{AbstractReteNode} | ||
outputs::Set{AbstractReteNode} | ||
comparison | ||
extractor | ||
label | ||
|
||
function BackwardExtremumNode(comparison, extractor, label) | ||
new(Set{AbstractReteNode}(), | ||
Set{AbstractReteNode}(), | ||
comparison, extractor, label) | ||
end | ||
end | ||
|
||
function askc(continuation, node::BackwardExtremumNode) | ||
extremum = nothing | ||
for input in node.inputs | ||
askc(input) do fact | ||
if extremum === nothing || node.comparison(node.extractor(fact), | ||
extremum) | ||
extremum = fact | ||
end | ||
end | ||
end | ||
continuation(extremum) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
@testset "test BackwardFilterNode" begin | ||
root = ReteRootNode("root") | ||
ints = ensure_IsaMemoryNode(root, Int) | ||
connect(root, ints) | ||
even = BackwardFilterNode(iseven, "even Ints") | ||
connect(ints, even) | ||
for i in 1:10 | ||
receive(root, i) | ||
end | ||
filtered = collecting() do c | ||
askc(c, even) | ||
end | ||
@test sort(filtered) == [2, 4, 6, 8, 10] | ||
end | ||
|
||
@testset "test BackwardExtremumNode" begin | ||
root = ReteRootNode("root") | ||
ints = ensure_IsaMemoryNode(root, Int) | ||
connect(root, ints) | ||
maxval = BackwardExtremumNode(>, identity, "greatest int") | ||
connect(ints, maxval) | ||
for i in 1:10 | ||
receive(root, i) | ||
end | ||
extreme = collecting() do c | ||
askc(c, maxval) | ||
end | ||
@test extreme == [10] | ||
end | ||
|