more csys refactoring (still in progress) with scope class, action handlers

This commit is contained in:
Helmut Merz 2026-07-12 07:20:31 +02:00
parent 8471173a99
commit a20170368b
3 changed files with 40 additions and 16 deletions

View file

@ -6,7 +6,7 @@
(:shape :scopes/shape) (:shape :scopes/shape)
(:util :scopes/util)) (:util :scopes/util))
(:export #:message-meta #:message #:create (:export #:message-meta #:message #:create
#:action-key #:object-key)) #:action #:addr #:categ #:domain))
(in-package :scopes/core/message) (in-package :scopes/core/message)
@ -29,11 +29,17 @@
(defmethod actor:content ((msg message)) (defmethod actor:content ((msg message))
(list (shape:head-plist msg) (shape:data msg))) (list (shape:head-plist msg) (shape:data msg)))
(defun action-key (msg) (defun action (msg)
(let ((head (shape:head msg))) (let ((head (shape:head msg)))
(list (car head) (cadr head)))) (list (car head) (cadr head))))
(defun object-key (msg) (defun addr (msg)
(let ((head (shape:head msg))) (let ((head (shape:head msg)))
(cons (car head) (cddr head)))) (cons (car head) (cddr head))))
(defun categ (msg)
(let ((head (shape:head msg)))
(list (car head) (caddr head))))
(defun domain (msg)
(car (shape:head msg)))

View file

@ -18,29 +18,38 @@
;;;; scope: information a neuron has access to ;;;; scope: information a neuron has access to
(defclass scope () (defclass scope ()
((value :reader value :initarg :value :initform 0) ((value :accessor value :initarg :value :initform 0)
(stage :reader stage :initform :initial) (params :accessor params :initarg :params :initform nil)
(syns :reader syns :initform nil) (categ :accessor categ :initarg :categ :initform '(:csys :c00))
(stage :accessor stage :initform :initial)
(proc :accessor proc :initarg :proc :initform #'std-proc)
(program :reader program :initarg :program :initform nil) (program :reader program :initarg :program :initform nil)
(actions :reader actions :initarg :actions :initform nil) (actions :reader actions :initarg :actions :initform nil)
(syns :accessor syns :initform nil)
(environ :reader environ :initarg :environ))) (environ :reader environ :initarg :environ)))
(defun scope (&optional (program #'std-proc) &rest args) (defun scope (&optional (program #'std-proc) &rest args)
(apply #'make-instance 'scope :program program args)) (apply #'make-instance 'scope :program program args))
(defun reset-scope (scope &rest args)
(apply #'scope (program scope) :environ (environ scope) args))
;;;; neurons (= async tasks) and synapses (= connections) ;;;; neurons (= async tasks) and synapses (= connections)
(defun neuron (scope) (defun neuron (scope)
(actor:create (actor:create (process scope)))
(lambda (msg) (funcall msg scope))))
(defun synapse (rcvr &optional (op #'identity)) (defun synapse (rcvr &optional (op #'identity))
(lambda (msg) (lambda (msg)
(actor:send rcvr (funcall op msg)))) (actor:send rcvr (funcall op msg))))
(defun update (scope) (defun update (scope)
(actor:become (actor:become (process scope)))
(lambda (msg) (funcall msg scope))))
;;;; message handlers, proc steps
(defun process (scope)
(lambda (msg) (funcall (proc scope) msg scope)))
(defun std-proc (msg scope) (defun std-proc (msg scope)
;(util:lgi msg state syns env) ;(util:lgi msg state syns env)
@ -49,29 +58,39 @@
(forward nmsg (syns nscope)) (forward nmsg (syns nscope))
(update nscope))) (update nscope)))
(defun create (msg scope)
;;;; effector procs for pseudo-neurons embedded in environment (let ((new (neuron (reset scope))))
(notify-created msg new scope)))
(defun do-log (msg scope) (defun do-log (msg scope)
(util:lgi msg)) (util:lgi msg))
;;;; helper / utility funtions (defun notify (msg scope)
(actor:send (environ scope) msg))
(defun forward (msg syns) (defun forward (msg syns)
(dolist (s syns) (dolist (s syns)
(funcall s msg))) (funcall s msg)))
(defun handle-action (msg scope &key (default #'no-op)) (defun handle-action (msg scope &key (default #'no-op))
(let* ((key (message:action-key msg)) (let* ((key (message:action msg))
;(act (select-action msg state default)) ;(act (select-action msg state default))
(act (gethash key (actions scope) default))) (act (gethash key (actions scope) default)))
(funcall act msg scope))) (funcall act msg scope)))
(defun notify-created (msg new scope)
(let* ((head (list (car (shape:head msg)) :created))
(data (list :new new :old actor:*self*)) ; todo: update existing data
(msg1 (message:create head :data data)))
(notify msg1 scope))
)
;;;; predefined neuron actions ;;;; predefined neuron actions
(defun no-op (msg scope) (defun no-op (msg scope)
(list msg scope)) (list msg scope))
(defun remember (msg scope) (defun remember (msg scope)
(setf (value scope) (shape:data-value msg :value))
;(list msg (make-neuron-state (shape:data msg) syns)) ;(list msg (make-neuron-state (shape:data msg) syns))
(list msg (shape:data msg) syns)) (list msg scope))

View file

@ -68,7 +68,6 @@
) )
(deftest x-test-init () (deftest x-test-init ()
(setup-test-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)