forked from opencog/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop_go.py
executable file
·103 lines (87 loc) · 3.05 KB
/
stop_go.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env python
#
# stop_go.py
#
"""
Example of creating a "behavior tree" in the AtomSpace.
Behavior trees can be thought of as long sequences of nested
if-then-else statements; the branch that is taken results in
a "behavior" being performed.
Behavor trees are usually driven by dynamic data; however, to keep
the example simple, the below is static; it simply counts green and
red lights, and halts at the first red light.
The if-then is implemented via a matching clause with the pattern
matcher. When a match is seen, the matcher moves on to the next
clause.
"""
from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name
from opencog.exec import execute_atom
from opencog.type_constructors import *
from opencog.logger import Logger, log
# Logging will be written to opencog.log in the current directory.
log.set_level('DEBUG')
log.info("Starting the stop-go demo")
# The atomspace where everything will live.
atomspace = AtomSpace()
set_default_atomspace(atomspace)
# The callback counts the number fo red and green lights.
# It returns a TruthValue of TRUE for green lights and FALSE for the
# red lights. FALSE is interpreted as a mismatch (failure to satisfy)
# by the pattner matcher, and thus, the pattern matcher will backtrack
# and sarch for a different solution. Since the example below contains
# no variables, it will just backtrack to the start, and then report
# non-satisfiability (which is what we want, when we get a red light).
green = 0
red = 0
def stop_go(atom):
if atom == ConceptNode("green light"):
print("Got a green light...")
global green
green += 1
return TruthValue(1,1)
elif atom == ConceptNode("red light"):
print("Got a red light!")
global red
red += 1
return TruthValue(0,1)
else:
print("Oh No!! Car wreck!")
assert(false)
return TruthValue(0,0)
# This is the pattern that the pattern matcher attempts to ground.
# It consists of two green lights, which evaluate to true, followed
# by a red light, which halts the satsifiability search. The last
# term is thus never encountered. Which is a good thing.
satisfaction_handle = SatisfactionLink(
SequentialAndLink(
EvaluationLink(
GroundedPredicateNode("py: stop_go"),
ListLink(
ConceptNode("green light")
)
),
EvaluationLink(
GroundedPredicateNode("py: stop_go"),
ListLink(
ConceptNode("green light")
)
),
EvaluationLink(
GroundedPredicateNode("py: stop_go"),
ListLink(
ConceptNode("red light")
)
),
EvaluationLink(
GroundedPredicateNode("py: stop_go"),
ListLink(
ConceptNode("traffic ticket")
)
)
)
)
# Perform the actual satisfiability search.
result = execute_atom(atomspace, satisfaction_handle)
print("Satisfaction query result:", result)
print("Number of green lights:", green)
print("Number of red lights:", red)