cl-scopes/csys/csys.lisp

48 lines
1.6 KiB
Common Lisp

;;;; cl-scopes/csys - concurrent cybernetic communication systems
(defpackage :scopes/csys
(:use :common-lisp)
(:local-nicknames (:actor :scopes/core/actor)
(:async :scopes/util/async)
(:config :scopes/config)
(:core :scopes/core)
(:message :scopes/core/message)
(:shape :scopes/shape)
(:util :scopes/util)
(:alx :alexandria))
(:export #:send #:*sensors* #:neuron
#:nprint))
(in-package :scopes/csys)
(defvar *sensors* (make-hash-table :test #'equal))
(defun send (msg)
(if (eq (shape:head-value msg :domain) :csys)
(dolist (sn (find-create-sensors msg))
(actor:send sn msg))
(core:handle-message core:*root* msg)))
(defun find-create-sensors (msg)
(let* ((key (cddr (shape:head msg)))
(sns (gethash key *sensors*)))
(format t "~&~a ~a" key sns)
(or sns
(let* ((mx (message:create
(list :csys :sensor (shape:head-value msg :class))))
(hdlrs (core:select mx (core:actions core:*root*))))
(if hdlrs
(let ((tsks (mapcar #'actor:create hdlrs)))
(setf (gethash key *sensors*) tsks))
(util:lgw "no action selected" msg))))))
;;;; neurons (= behavior generators)
(defun neuron (proc &optional state syns env)
(lambda (msg) (funcall proc msg state syns env)))
;;;; predefined neuron processors
(defun nprint (msg state syns env)
(util:lgi msg state)
(actor:become (neuron #'nprint (shape:data msg) syns env)))