6 Example of creating a "behavior tree" in the AtomSpace.
8 Behavior trees can be thought of as long sequences of nested
9 if-then-else statements; the branch that is taken results in
10 a "behavior" being performed.
12 Behavor trees are usually driven by dynamic data; however, to keep
13 the example simple, the below is static; it simply counts green and
14 red lights, and halts at the first red light.
16 The if-then is implemented via a matching clause with the pattern
17 matcher. When a match is seen, the matcher moves on to the next
21 from opencog.atomspace
import AtomSpace, TruthValue, types, get_type_name
22 from opencog.bindlink
import satisfaction_link
24 from opencog.logger
import Logger, log
27 log.set_level(
'DEBUG')
28 log.info(
"Starting the stop-go demo")
31 atomspace = AtomSpace()
32 set_type_ctor_atomspace(atomspace)
47 if atom == ConceptNode(
"green light"):
48 print "Got a green light..."
51 return TruthValue(1,1)
53 elif atom == ConceptNode(
"red light"):
54 print "Got a red light!"
57 return TruthValue(0,1)
60 print "Oh No!! Car wreck!"
63 return TruthValue(0,0)
69 satisfaction_handle = SatisfactionLink(
72 GroundedPredicateNode(
"py: stop_go"),
74 ConceptNode(
"green light")
78 GroundedPredicateNode(
"py: stop_go"),
80 ConceptNode(
"green light")
84 GroundedPredicateNode(
"py: stop_go"),
86 ConceptNode(
"red light")
90 GroundedPredicateNode(
"py: stop_go"),
92 ConceptNode(
"traffic ticket")
101 print "Number of green lights:", green
102 print "Number of red lights:", red
TruthValuePtr satisfaction_link(AtomSpace *, const Handle &)