Skip to content

Commit

Permalink
Merge pull request #52 from linas/cle
Browse files Browse the repository at this point in the history
Assorted minor cleanup
  • Loading branch information
linas committed Jun 9, 2016
2 parents 102ac2c + 7671c45 commit b12d83f
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 178 deletions.
111 changes: 21 additions & 90 deletions src/behavior.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@
; HOWTO:
; ------
; Run the main loop:
; (behavior-tree-run)
; (run)
; Pause the main loop:
; (behavior-tree-halt)
; (halt)
;
; TODO:
; -----
; XXX This needs a major redesign, to NOT use behavior trees at the top
; level, but instead to provide a library of suitable actions that can
; be searched over, and then performed when a given situation applies.
; That is, given a certain state vector (typically, a subset of the
; current state), the library is searched to see if there is a behavior
; sequence that can be applied to this situation. If there is no such
; explicit match, then the fuzzy matcher should be employed to find
; something that is at least close. If nothing close is found, then
; either the concept blending code, or a hack of the MOSES knob-turning
; and genetic cross-over code should be used to create new quasi-random
; The current OpenPsi framework allows much more general and flexible
; rules than what are presented below; this geneality should be made
; use of.
;
; A general OpenPsi rule has the form of if(context) then take(action);
; these can contain variables, adn can also be classed into different
; groups based on the demands that they are fulfilling.
;
; The content below consits entirely of actions to nbe taken; the
; contexts are in the `self-model.scm` file. The structure of the
; conexts is fairly rigid; these could probably be loosened to a
; large degree.
;
; The OpenPsi engine could be (should be?) updated to perform fuzzy
; matching on the contexts, to find close or similar contexts, if no
; one exact match can be made. If nothing close is found, then either
; the concept blending code, or a hack of the MOSES knob-turning and
; genetic cross-over code should be used to create new quasi-random
; performance sequences from a bag of likely matches.
;
; Unit testing:
Expand Down Expand Up @@ -719,6 +727,7 @@
))
(TrueLink)
))

(DefineLink
(DefinedPredicate "Keep alive")
(SequentialAnd
Expand All @@ -740,83 +749,5 @@
))
(TrueLink)
))
;; ------------------------------------------------------------------
;; Main loop. Uses tail recursion optimization to form the loop.
(DefineLink
(DefinedPredicate "main loop")
(SatisfactionLink
(SequentialAnd
(SequentialOr
(DefinedPredicate "Skip Interaction?")

(SequentialAnd
(DefinedPredicate "Someone requests interaction?")
(DefinedPredicate "Interaction requested action"))

(SequentialAnd
(DefinedPredicate "Did someone arrive?")
(DefinedPredicate "New arrival sequence"))

(SequentialAnd
(DefinedPredicate "Did someone leave?")
(DefinedPredicate "Someone left action"))

; True, if there is anyone visible.
(SequentialAnd
(DefinedPredicate "Someone visible?")
(DefinedPredicate "Interact with people"))

(DefinedPredicate "Nothing is happening")
(True))

;; XXX FIXME chatbot is disengaged from everything else.
;; The room can be empty, the head is bored or even asleep,
;; but the chatbot is still smiling and yabbering.
;; If interaction is turned-off need keep alive gestures
(SequentialOr
; If the TTS vocalization started (chatbot started talking) ...
(SequentialAnd
(DefinedPredicate "chatbot started talking?")
(DefinedPredicate "Speech started"))

; If the chatbot currently talking ...
(SequentialAnd
(DefinedPredicate "chatbot is talking?")
(DefinedPredicate "Speech ongoing"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot stopped talking?")
(DefinedPredicate "Speech ended"))

(SequentialAnd
(DefinedPredicate "chatbot started listening?")
(DefinedPredicate "Listening started"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot is listening?")
(DefinedPredicate "Listening ongoing"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot stopped listening?")
(DefinedPredicate "Listening ended"))

(SequentialAnd
(DefinedPredicate "Skip Interaction?")
(DefinedPredicate "Keep alive"))

(True)
)

; If ROS is dead, or the continue flag not set, then stop
; running the behavior loop.
(DefinedPredicate "Continue running loop?")
(DefinedPredicate "ROS is running?")

;; Call self -- tail-recurse.
(DefinedPredicate "main loop")
)))

; ----------------------------------------------------------------------
1 change: 1 addition & 0 deletions src/btree-eva-mute.scm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
; (display %load-path)
(add-to-load-path "../src")
(load-from-path "cfg-eva.scm") ;;; <<<=== See, its Eva here!
(load-from-path "old-tree.scm")

;; Call (run) to run the main loop, (halt) to pause the loop.
;; The main loop runs in its own thread.
Expand Down
8 changes: 5 additions & 3 deletions src/btree-eva.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;
; btree-eva.scm
;
; Eva behavior tree (for the Eva blender model animations).
; Eva OpenPsi behaviors (for the Eva blender model animations).
;
; Runs a set of defined behaviors that express Eva's personality.
; This version integrates the OpenCog chatbot.
Expand Down Expand Up @@ -36,16 +36,18 @@
(use-modules (opencog exec)) ; needed for cog-evaluate! in put_atoms.py
(use-modules (opencog eva-model)) ; needed for defines in put_atoms.py
(use-modules (opencog eva-behavior))
(use-modules (opencog openpsi))

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

;; Call (run) to run the main loop, (halt) to pause the loop.
;; The main loop runs in its own thread.
(define (run) (behavior-tree-run))
(define (halt) (behavior-tree-halt))
(define (run) (psi-run))
(define (halt) (psi-halt))

; ---------------------------------------------------------
; Load the chat modules.
Expand Down
23 changes: 3 additions & 20 deletions src/btree-psi.scm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;
; btree-psi.scm
;
; OpenPsi-based Eva behavior action selection (for the Eva blender
; Sophia OpenPsi behavior action selection (for the Sophia blender
; model animations).
;
; Runs a set of defined behaviors that express Eva's personality.
; Runs a set of defined behaviors that express Sophia's personality.
; This version integrates the OpenCog chatbot.
;
; The currently-defined behaviors include acknowledging new people who
Expand Down Expand Up @@ -48,23 +48,6 @@
;; Load the actual psi rules.
(load-from-path "psi-behavior.scm")

; There MUST be a DefinedPredicateNode with exactly the name
; below in order for psi-run to work. Or we could just blow
; that off, and use our own loop...
(define loop-name (string-append psi-prefix-str "loop"))
(DefineLink
(DefinedPredicate loop-name)
(SatisfactionLink
(SequentialAnd
(Evaluation (GroundedPredicate "scm: psi-step")
(ListLink))
(Evaluation (GroundedPredicate "scm: psi-run-continue?")
(ListLink))
; If ROS is dead, or the continue flag not set,
; then stop running the behavior loop.
(DefinedPredicate "ROS is running?")
(DefinedPredicate loop-name))))

;; Call (run) to run the main loop, (halt) to pause the loop.
;; The main loop runs in its own thread.
(define (run) (psi-run))
Expand Down Expand Up @@ -99,7 +82,7 @@

; ---------------------------------------------------------
; Run the hacky garbage collection loop.
; (run-behavior-tree-gc)
(run-behavior-tree-gc)

; Silence the output.
*unspecified*
Expand Down
1 change: 1 addition & 0 deletions src/btree.scm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

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

Expand Down
155 changes: 155 additions & 0 deletions src/old-tree.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
;
; old-tree.scm
;
; Old (deprecated) top-level behavior tree main loop.
;
; This has been supplanted by the newer, OpenPsi-based behavior
; rules, and is temporarily maintained here for backwards
; compaitbility.
;
; HOWTO:
; ------
; Run the main loop:
; (behavior-tree-run)
; Pause the main loop:
; (behavior-tree-halt)
;
;; ------------------------------------------------------------------
;; Main loop. Uses tail recursion optimization to form the loop.
(DefineLink
(DefinedPredicate "main loop")
(SatisfactionLink
(SequentialAnd
(SequentialOr
(DefinedPredicate "Skip Interaction?")

(SequentialAnd
(DefinedPredicate "Someone requests interaction?")
(DefinedPredicate "Interaction requested action"))

(SequentialAnd
(DefinedPredicate "Did someone arrive?")
(DefinedPredicate "New arrival sequence"))

(SequentialAnd
(DefinedPredicate "Did someone leave?")
(DefinedPredicate "Someone left action"))

; True, if there is anyone visible.
(SequentialAnd
(DefinedPredicate "Someone visible?")
(DefinedPredicate "Interact with people"))

(DefinedPredicate "Nothing is happening")
(True))

;; XXX FIXME chatbot is disengaged from everything else.
;; The room can be empty, the head is bored or even asleep,
;; but the chatbot is still smiling and yabbering.
;; If interaction is turned-off need keep alive gestures
(SequentialOr
; If the TTS vocalization started (chatbot started talking) ...
(SequentialAnd
(DefinedPredicate "chatbot started talking?")
(DefinedPredicate "Speech started"))

; If the chatbot currently talking ...
(SequentialAnd
(DefinedPredicate "chatbot is talking?")
(DefinedPredicate "Speech ongoing"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot stopped talking?")
(DefinedPredicate "Speech ended"))

(SequentialAnd
(DefinedPredicate "chatbot started listening?")
(DefinedPredicate "Listening started"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot is listening?")
(DefinedPredicate "Listening ongoing"))

; If the chatbot stopped talking ...
(SequentialAnd
(DefinedPredicate "chatbot stopped listening?")
(DefinedPredicate "Listening ended"))

(SequentialAnd
(DefinedPredicate "Skip Interaction?")
(DefinedPredicate "Keep alive"))

(True)
)

; If ROS is dead, or the continue flag not set, then stop
; running the behavior loop.
(DefinedPredicate "Continue running loop?")
(DefinedPredicate "ROS is running?")

;; Call self -- tail-recurse.
(DefinedPredicate "main loop")
)))

; ----------------------------------------------------------------------
;; Main loop control
(define do-run-loop #t)

(define-public (behavior-tree-run)
"
behavior-tree-run
Run the Eva behavior tree main loop (in a new thread),
Call (behavior-tree-halt) to exit the loop.
"
(set! do-run-loop #t)
(call-with-new-thread
(lambda () (cog-evaluate! (DefinedPredicateNode "main loop")))))

(define-public (behavior-tree-halt)
"
behavior-tree-halt
Tell the Eva behavior tree main loop thread to exit.
"
(set! do-run-loop #f))


(define-public (behavior-tree-running?)
"
behavior-tree-running?
Return #t if the behavior tree is running, else return false.
"
do-run-loop)

(define-public (behavior-tree-loop-count)
"
behavior-tree-loop-count
Return the loop-count of the behavior tree.
"
loop-count)


(define loop-count 0)
(define-public (continue-running-loop) ; public only because its in a GPN
(set! loop-count (+ loop-count 1))

; Print loop count to the screen.
; (if (eq? 0 (modulo loop-count 30))
; (format #t "Main loop: ~a\n" loop-count))

; Pause for one-tenth of a second... 101 millisecs
(usleep 101000)
(if do-run-loop (stv 1 1) (stv 0 1)))

; Return true if the behavior loop should keep running.
(DefineLink
(DefinedPredicate "Continue running loop?")
(Evaluation
(GroundedPredicate "scm:continue-running-loop") (ListLink)))

; ----------------------------------------------------------------------
Loading

0 comments on commit b12d83f

Please sign in to comment.