OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stop_go.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 # stop_go.py
4 #
5 """
6 Example of creating a "behavior tree" in the AtomSpace.
7 
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.
11 
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.
15 
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
18 clause.
19 """
20 
21 from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name
22 from opencog.bindlink import satisfaction_link
23 from opencog.type_constructors import *
24 from opencog.logger import Logger, log
25 
26 # Logging will be written to opencog.log in the current directory.
27 log.set_level('DEBUG')
28 log.info("Starting the stop-go demo")
29 
30 # The atomspace where everything will live.
31 atomspace = AtomSpace()
32 set_type_ctor_atomspace(atomspace)
33 
34 
35 # The callback counts the number fo red and green lights.
36 # It returns a TruthValue of TRUE for green lights and FALSE for the
37 # red lights. FALSE is interpreted as a mismatch (failure to satisfy)
38 # by the pattner matcher, and thus, the pattern matcher will backtrack
39 # and sarch for a different solution. Since the example below contains
40 # no variables, it will just backtrack to the start, and then report
41 # non-satisfiability (which is what we want, when we get a red light).
42 green = 0
43 red = 0
44 
45 def stop_go(atom):
46 
47  if atom == ConceptNode("green light"):
48  print "Got a green light..."
49  global green
50  green += 1
51  return TruthValue(1,1)
52 
53  elif atom == ConceptNode("red light"):
54  print "Got a red light!"
55  global red
56  red += 1
57  return TruthValue(0,1)
58 
59  else:
60  print "Oh No!! Car wreck!"
61  assert(false)
62 
63  return TruthValue(0,0)
64 
65 # This is the pattern that the pattern matcher attempts to ground.
66 # It consists of two green lights, which evaluate to true, followed
67 # by a red light, which halts the satsifiability search. The last
68 # term is thus never encountered. Which is a good thing.
69 satisfaction_handle = SatisfactionLink(
70  SequentialAndLink(
71  EvaluationLink(
72  GroundedPredicateNode("py: stop_go"),
73  ListLink(
74  ConceptNode("green light")
75  )
76  ),
77  EvaluationLink(
78  GroundedPredicateNode("py: stop_go"),
79  ListLink(
80  ConceptNode("green light")
81  )
82  ),
83  EvaluationLink(
84  GroundedPredicateNode("py: stop_go"),
85  ListLink(
86  ConceptNode("red light")
87  )
88  ),
89  EvaluationLink(
90  GroundedPredicateNode("py: stop_go"),
91  ListLink(
92  ConceptNode("traffic ticket")
93  )
94  )
95  )
96 ).h
97 
98 # Perform the actual satisfiability search.
99 result = satisfaction_link(atomspace, satisfaction_handle)
100 
101 print "Number of green lights:", green
102 print "Number of red lights:", red
TruthValuePtr satisfaction_link(AtomSpace *, const Handle &)
Definition: Satisfier.cc:67