Skip to content

Commit b12d83f

Browse files
committed
Merge pull request #52 from linas/cle
Assorted minor cleanup
2 parents 102ac2c + 7671c45 commit b12d83f

8 files changed

+214
-178
lines changed

src/behavior.scm

+21-90
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,30 @@
1212
; HOWTO:
1313
; ------
1414
; Run the main loop:
15-
; (behavior-tree-run)
15+
; (run)
1616
; Pause the main loop:
17-
; (behavior-tree-halt)
17+
; (halt)
1818
;
1919
; TODO:
2020
; -----
21-
; XXX This needs a major redesign, to NOT use behavior trees at the top
22-
; level, but instead to provide a library of suitable actions that can
23-
; be searched over, and then performed when a given situation applies.
24-
; That is, given a certain state vector (typically, a subset of the
25-
; current state), the library is searched to see if there is a behavior
26-
; sequence that can be applied to this situation. If there is no such
27-
; explicit match, then the fuzzy matcher should be employed to find
28-
; something that is at least close. If nothing close is found, then
29-
; either the concept blending code, or a hack of the MOSES knob-turning
30-
; and genetic cross-over code should be used to create new quasi-random
21+
; The current OpenPsi framework allows much more general and flexible
22+
; rules than what are presented below; this geneality should be made
23+
; use of.
24+
;
25+
; A general OpenPsi rule has the form of if(context) then take(action);
26+
; these can contain variables, adn can also be classed into different
27+
; groups based on the demands that they are fulfilling.
28+
;
29+
; The content below consits entirely of actions to nbe taken; the
30+
; contexts are in the `self-model.scm` file. The structure of the
31+
; conexts is fairly rigid; these could probably be loosened to a
32+
; large degree.
33+
;
34+
; The OpenPsi engine could be (should be?) updated to perform fuzzy
35+
; matching on the contexts, to find close or similar contexts, if no
36+
; one exact match can be made. If nothing close is found, then either
37+
; the concept blending code, or a hack of the MOSES knob-turning and
38+
; genetic cross-over code should be used to create new quasi-random
3139
; performance sequences from a bag of likely matches.
3240
;
3341
; Unit testing:
@@ -719,6 +727,7 @@
719727
))
720728
(TrueLink)
721729
))
730+
722731
(DefineLink
723732
(DefinedPredicate "Keep alive")
724733
(SequentialAnd
@@ -740,83 +749,5 @@
740749
))
741750
(TrueLink)
742751
))
743-
;; ------------------------------------------------------------------
744-
;; Main loop. Uses tail recursion optimization to form the loop.
745-
(DefineLink
746-
(DefinedPredicate "main loop")
747-
(SatisfactionLink
748-
(SequentialAnd
749-
(SequentialOr
750-
(DefinedPredicate "Skip Interaction?")
751-
752-
(SequentialAnd
753-
(DefinedPredicate "Someone requests interaction?")
754-
(DefinedPredicate "Interaction requested action"))
755-
756-
(SequentialAnd
757-
(DefinedPredicate "Did someone arrive?")
758-
(DefinedPredicate "New arrival sequence"))
759-
760-
(SequentialAnd
761-
(DefinedPredicate "Did someone leave?")
762-
(DefinedPredicate "Someone left action"))
763-
764-
; True, if there is anyone visible.
765-
(SequentialAnd
766-
(DefinedPredicate "Someone visible?")
767-
(DefinedPredicate "Interact with people"))
768-
769-
(DefinedPredicate "Nothing is happening")
770-
(True))
771-
772-
;; XXX FIXME chatbot is disengaged from everything else.
773-
;; The room can be empty, the head is bored or even asleep,
774-
;; but the chatbot is still smiling and yabbering.
775-
;; If interaction is turned-off need keep alive gestures
776-
(SequentialOr
777-
; If the TTS vocalization started (chatbot started talking) ...
778-
(SequentialAnd
779-
(DefinedPredicate "chatbot started talking?")
780-
(DefinedPredicate "Speech started"))
781-
782-
; If the chatbot currently talking ...
783-
(SequentialAnd
784-
(DefinedPredicate "chatbot is talking?")
785-
(DefinedPredicate "Speech ongoing"))
786-
787-
; If the chatbot stopped talking ...
788-
(SequentialAnd
789-
(DefinedPredicate "chatbot stopped talking?")
790-
(DefinedPredicate "Speech ended"))
791-
792-
(SequentialAnd
793-
(DefinedPredicate "chatbot started listening?")
794-
(DefinedPredicate "Listening started"))
795-
796-
; If the chatbot stopped talking ...
797-
(SequentialAnd
798-
(DefinedPredicate "chatbot is listening?")
799-
(DefinedPredicate "Listening ongoing"))
800-
801-
; If the chatbot stopped talking ...
802-
(SequentialAnd
803-
(DefinedPredicate "chatbot stopped listening?")
804-
(DefinedPredicate "Listening ended"))
805-
806-
(SequentialAnd
807-
(DefinedPredicate "Skip Interaction?")
808-
(DefinedPredicate "Keep alive"))
809-
810-
(True)
811-
)
812-
813-
; If ROS is dead, or the continue flag not set, then stop
814-
; running the behavior loop.
815-
(DefinedPredicate "Continue running loop?")
816-
(DefinedPredicate "ROS is running?")
817-
818-
;; Call self -- tail-recurse.
819-
(DefinedPredicate "main loop")
820-
)))
821752

822753
; ----------------------------------------------------------------------

src/btree-eva-mute.scm

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
; (display %load-path)
4141
(add-to-load-path "../src")
4242
(load-from-path "cfg-eva.scm") ;;; <<<=== See, its Eva here!
43+
(load-from-path "old-tree.scm")
4344

4445
;; Call (run) to run the main loop, (halt) to pause the loop.
4546
;; The main loop runs in its own thread.

src/btree-eva.scm

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;
22
; btree-eva.scm
33
;
4-
; Eva behavior tree (for the Eva blender model animations).
4+
; Eva OpenPsi behaviors (for the Eva blender model animations).
55
;
66
; Runs a set of defined behaviors that express Eva's personality.
77
; This version integrates the OpenCog chatbot.
@@ -36,16 +36,18 @@
3636
(use-modules (opencog exec)) ; needed for cog-evaluate! in put_atoms.py
3737
(use-modules (opencog eva-model)) ; needed for defines in put_atoms.py
3838
(use-modules (opencog eva-behavior))
39+
(use-modules (opencog openpsi))
3940

4041
; Load the Eva personality configuration.
4142
; (display %load-path)
4243
(add-to-load-path "../src")
4344
(load-from-path "cfg-eva.scm") ;;; <<<=== See, its Eva here!
45+
(load-from-path "psi-behavior.scm")
4446

4547
;; Call (run) to run the main loop, (halt) to pause the loop.
4648
;; The main loop runs in its own thread.
47-
(define (run) (behavior-tree-run))
48-
(define (halt) (behavior-tree-halt))
49+
(define (run) (psi-run))
50+
(define (halt) (psi-halt))
4951

5052
; ---------------------------------------------------------
5153
; Load the chat modules.

src/btree-psi.scm

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
;
22
; btree-psi.scm
33
;
4-
; OpenPsi-based Eva behavior action selection (for the Eva blender
4+
; Sophia OpenPsi behavior action selection (for the Sophia blender
55
; model animations).
66
;
7-
; Runs a set of defined behaviors that express Eva's personality.
7+
; Runs a set of defined behaviors that express Sophia's personality.
88
; This version integrates the OpenCog chatbot.
99
;
1010
; The currently-defined behaviors include acknowledging new people who
@@ -48,23 +48,6 @@
4848
;; Load the actual psi rules.
4949
(load-from-path "psi-behavior.scm")
5050

51-
; There MUST be a DefinedPredicateNode with exactly the name
52-
; below in order for psi-run to work. Or we could just blow
53-
; that off, and use our own loop...
54-
(define loop-name (string-append psi-prefix-str "loop"))
55-
(DefineLink
56-
(DefinedPredicate loop-name)
57-
(SatisfactionLink
58-
(SequentialAnd
59-
(Evaluation (GroundedPredicate "scm: psi-step")
60-
(ListLink))
61-
(Evaluation (GroundedPredicate "scm: psi-run-continue?")
62-
(ListLink))
63-
; If ROS is dead, or the continue flag not set,
64-
; then stop running the behavior loop.
65-
(DefinedPredicate "ROS is running?")
66-
(DefinedPredicate loop-name))))
67-
6851
;; Call (run) to run the main loop, (halt) to pause the loop.
6952
;; The main loop runs in its own thread.
7053
(define (run) (psi-run))
@@ -99,7 +82,7 @@
9982

10083
; ---------------------------------------------------------
10184
; Run the hacky garbage collection loop.
102-
; (run-behavior-tree-gc)
85+
(run-behavior-tree-gc)
10386

10487
; Silence the output.
10588
*unspecified*

src/btree.scm

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
;; Call (run) to run the main loop, (halt) to pause the loop.
6363
;; The main loop runs in its own thread.
64+
(load-from-path "old-tree.scm")
6465
(define (run) (behavior-tree-run))
6566
(define (halt) (behavior-tree-halt))
6667

src/old-tree.scm

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
;
2+
; old-tree.scm
3+
;
4+
; Old (deprecated) top-level behavior tree main loop.
5+
;
6+
; This has been supplanted by the newer, OpenPsi-based behavior
7+
; rules, and is temporarily maintained here for backwards
8+
; compaitbility.
9+
;
10+
; HOWTO:
11+
; ------
12+
; Run the main loop:
13+
; (behavior-tree-run)
14+
; Pause the main loop:
15+
; (behavior-tree-halt)
16+
;
17+
;; ------------------------------------------------------------------
18+
;; Main loop. Uses tail recursion optimization to form the loop.
19+
(DefineLink
20+
(DefinedPredicate "main loop")
21+
(SatisfactionLink
22+
(SequentialAnd
23+
(SequentialOr
24+
(DefinedPredicate "Skip Interaction?")
25+
26+
(SequentialAnd
27+
(DefinedPredicate "Someone requests interaction?")
28+
(DefinedPredicate "Interaction requested action"))
29+
30+
(SequentialAnd
31+
(DefinedPredicate "Did someone arrive?")
32+
(DefinedPredicate "New arrival sequence"))
33+
34+
(SequentialAnd
35+
(DefinedPredicate "Did someone leave?")
36+
(DefinedPredicate "Someone left action"))
37+
38+
; True, if there is anyone visible.
39+
(SequentialAnd
40+
(DefinedPredicate "Someone visible?")
41+
(DefinedPredicate "Interact with people"))
42+
43+
(DefinedPredicate "Nothing is happening")
44+
(True))
45+
46+
;; XXX FIXME chatbot is disengaged from everything else.
47+
;; The room can be empty, the head is bored or even asleep,
48+
;; but the chatbot is still smiling and yabbering.
49+
;; If interaction is turned-off need keep alive gestures
50+
(SequentialOr
51+
; If the TTS vocalization started (chatbot started talking) ...
52+
(SequentialAnd
53+
(DefinedPredicate "chatbot started talking?")
54+
(DefinedPredicate "Speech started"))
55+
56+
; If the chatbot currently talking ...
57+
(SequentialAnd
58+
(DefinedPredicate "chatbot is talking?")
59+
(DefinedPredicate "Speech ongoing"))
60+
61+
; If the chatbot stopped talking ...
62+
(SequentialAnd
63+
(DefinedPredicate "chatbot stopped talking?")
64+
(DefinedPredicate "Speech ended"))
65+
66+
(SequentialAnd
67+
(DefinedPredicate "chatbot started listening?")
68+
(DefinedPredicate "Listening started"))
69+
70+
; If the chatbot stopped talking ...
71+
(SequentialAnd
72+
(DefinedPredicate "chatbot is listening?")
73+
(DefinedPredicate "Listening ongoing"))
74+
75+
; If the chatbot stopped talking ...
76+
(SequentialAnd
77+
(DefinedPredicate "chatbot stopped listening?")
78+
(DefinedPredicate "Listening ended"))
79+
80+
(SequentialAnd
81+
(DefinedPredicate "Skip Interaction?")
82+
(DefinedPredicate "Keep alive"))
83+
84+
(True)
85+
)
86+
87+
; If ROS is dead, or the continue flag not set, then stop
88+
; running the behavior loop.
89+
(DefinedPredicate "Continue running loop?")
90+
(DefinedPredicate "ROS is running?")
91+
92+
;; Call self -- tail-recurse.
93+
(DefinedPredicate "main loop")
94+
)))
95+
96+
; ----------------------------------------------------------------------
97+
;; Main loop control
98+
(define do-run-loop #t)
99+
100+
(define-public (behavior-tree-run)
101+
"
102+
behavior-tree-run
103+
104+
Run the Eva behavior tree main loop (in a new thread),
105+
Call (behavior-tree-halt) to exit the loop.
106+
"
107+
(set! do-run-loop #t)
108+
(call-with-new-thread
109+
(lambda () (cog-evaluate! (DefinedPredicateNode "main loop")))))
110+
111+
(define-public (behavior-tree-halt)
112+
"
113+
behavior-tree-halt
114+
115+
Tell the Eva behavior tree main loop thread to exit.
116+
"
117+
(set! do-run-loop #f))
118+
119+
120+
(define-public (behavior-tree-running?)
121+
"
122+
behavior-tree-running?
123+
124+
Return #t if the behavior tree is running, else return false.
125+
"
126+
do-run-loop)
127+
128+
(define-public (behavior-tree-loop-count)
129+
"
130+
behavior-tree-loop-count
131+
132+
Return the loop-count of the behavior tree.
133+
"
134+
loop-count)
135+
136+
137+
(define loop-count 0)
138+
(define-public (continue-running-loop) ; public only because its in a GPN
139+
(set! loop-count (+ loop-count 1))
140+
141+
; Print loop count to the screen.
142+
; (if (eq? 0 (modulo loop-count 30))
143+
; (format #t "Main loop: ~a\n" loop-count))
144+
145+
; Pause for one-tenth of a second... 101 millisecs
146+
(usleep 101000)
147+
(if do-run-loop (stv 1 1) (stv 0 1)))
148+
149+
; Return true if the behavior loop should keep running.
150+
(DefineLink
151+
(DefinedPredicate "Continue running loop?")
152+
(Evaluation
153+
(GroundedPredicate "scm:continue-running-loop") (ListLink)))
154+
155+
; ----------------------------------------------------------------------

0 commit comments

Comments
 (0)