csys: env: + proc register; provide special neuron processor for effectors

This commit is contained in:
Helmut Merz 2026-03-08 14:52:33 +01:00
parent bc4aa6f219
commit 3782305853
2 changed files with 30 additions and 28 deletions

View file

@ -10,12 +10,12 @@
(:shape :scopes/shape) (:shape :scopes/shape)
(:util :scopes/util) (:util :scopes/util)
(:alx :alexandria)) (:alx :alexandria))
(:export #:environment #:*environment* #:effectors (:export #:environment #:*environment* #:procs
#:init #:init
#:send #:send-message #:send #:send-message
#:neuron #:synapse #:neuron #:synapse
#:make-neuron #:update-neuron #:make-neuron #:update-neuron
#:handle-action)) #:make-eff-proc #:handle-action))
(in-package :scopes/csys) (in-package :scopes/csys)
@ -25,15 +25,17 @@
((actions :reader actions :initarg :actions :initform (make-hash-table)) ((actions :reader actions :initarg :actions :initform (make-hash-table))
(sensors :reader sensors :initarg :sensors (sensors :reader sensors :initarg :sensors
:initform (make-hash-table :test #'equal)) :initform (make-hash-table :test #'equal))
(effectors :reader effectors :initarg :effectors (procs :reader procs :initarg :procs
:initform (make-hash-table :test #'equal)))) :initform (make-hash-table :test #'equal))))
(defvar *environment* (make-instance 'environment)) (defvar *environment* (make-instance 'environment))
(defun init (zero) (defun init ()
(setf (gethash :sensor (actions *environment*)) #'create-sensor) (setf (gethash :sensor (actions *environment*)) #'create-sensor)
(setf (gethash '(:init :zero) (sensors *environment*)) (list zero)) (setf (gethash :default (procs *environment*)) #'std-proc)
(setf (gethash :default (effectors *environment*)) (list zero))) (let* ((eff-proc (gethash '(:effect :default) (procs *environment*) #'std-proc))
(zero (make-neuron nil :proc eff-proc)))
(setf (gethash '(:init :zero) (sensors *environment*)) (list zero))))
;;;; sensors: neurons receiving messages from environment, addressable via message head ;;;; sensors: neurons receiving messages from environment, addressable via message head
@ -57,10 +59,6 @@
(defun do-log (msg state syns env) (defun do-log (msg state syns env)
(util:lgi msg)) (util:lgi msg))
(defun find-effectors (key &optional (default-key :default) (env *environment*))
(let ((effs (effectors env)))
(gethash key effs (gethash default-key effs))))
;;;; neurons (= behavior factories) and synapses (connection factories) ;;;; neurons (= behavior factories) and synapses (connection factories)
(defun neuron (proc &optional state syns (env *environment*)) (defun neuron (proc &optional state syns (env *environment*))
@ -71,9 +69,10 @@
(lambda (msg) (lambda (msg)
(actor:send rcvr (funcall op msg)))) (actor:send rcvr (funcall op msg))))
(defun make-neuron (syn-target &key (proc #'std-proc) state (defun make-neuron (syn-target &key proc state
(syn-op #'identity) (env *environment*)) (syn-op #'identity) (env *environment*))
(let ((syns (if syn-target (list (synapse syn-target syn-op)) nil))) (let ((proc (or proc (gethash :default (procs env) #'std-proc)))
(syns (if syn-target (list (synapse syn-target syn-op)) nil)))
(actor:create (neuron proc state syns env)))) (actor:create (neuron proc state syns env))))
(defun update-neuron (proc state syns &optional (env *environment*)) (defun update-neuron (proc state syns &optional (env *environment*))
@ -88,6 +87,12 @@
(forward nmsg nsyns) (forward nmsg nsyns)
(update-neuron #'std-proc nst nsyns env))) (update-neuron #'std-proc nst nsyns env)))
(defun make-eff-proc (handler)
(lambda (msg state syns env)
;(util:lgi msg)
(funcall handler msg)
(handle-action msg state syns env :default #'remember)))
(defun forward (msg syns) (defun forward (msg syns)
(dolist (s syns) (dolist (s syns)
(funcall s msg))) (funcall s msg)))

View file

@ -23,15 +23,13 @@
(defclass test-env (csys:environment) (defclass test-env (csys:environment)
((test-suite :reader test-suite :initarg :test-suite))) ((test-suite :reader test-suite :initarg :test-suite)))
(defun probe (msg state syns env) (defun eff-handler (state &optional (env csys:*environment*))
(let ((t:*test-suite* (test-suite env)) (lambda (msg)
(val (shape:data msg)))
(util:lgi msg state) (util:lgi msg state)
(destructuring-bind (msg state syns) (let ((t:*test-suite* (test-suite env))
(csys:handle-action msg state syns env) (val (shape:data msg)))
(unless (consp val) (unless (consp val)
(let ((nst (in-seq val state :remove t))) (setf state (in-seq val state :remove t))))))
(csys:update-neuron #'probe nst syns env))))))
(defun add (msg state syns env) (defun add (msg state syns env)
(list msg (+ (shape:data msg) state) syns env)) (list msg (+ (shape:data msg) state) syns env))
@ -41,21 +39,20 @@
(defun run () (defun run ()
(async:init) (async:init)
(let* ((t:*test-suite* (make-instance 't:test-suite :name "csys")) (let* ((t:*test-suite* (make-instance 't:test-suite :name "csys"))
(csys:*environment* (make-instance 'test-env :test-suite t:*test-suite*)) (csys:*environment* (make-instance 'test-env :test-suite t:*test-suite*)))
(probe (csys:make-neuron nil :proc #'probe :state '(1 3 4 5)
:env csys:*environment*)))
(load (t:test-path "config-csys" "etc")) (load (t:test-path "config-csys" "etc"))
(core:setup-services) (core:setup-services)
(unwind-protect (unwind-protect
(progn (progn
(test-init probe)) (test-init))
(sleep 0.1) (sleep 0.1)
(async:finish) (async:finish)
(t:show-result)))) (t:show-result))))
(deftest test-init (probe) (deftest test-init ()
;(setf (gethash '(:std :d0) (csys:effectors csys:*environment*)) probe) (setf (gethash '(:effect :default) (csys:procs csys:*environment*))
(csys:init probe) (csys:make-eff-proc (eff-handler '(1 3 4 5))))
(csys:init)
(csys:send-message '(:csys :sensor :init :zero) '(:std :s1)) (csys:send-message '(:csys :sensor :init :zero) '(:std :s1))
(csys:send-message '(:csys :sensor :init :zero) '(:std :s2)) (csys:send-message '(:csys :sensor :init :zero) '(:std :s2))
(sleep 0.1) (sleep 0.1)