cl-scopes/csys/csys.lisp

77 lines
2.2 KiB
Common Lisp

;;;; cl-scopes/csys - concurrent cybernetic communication systems
(defpackage :scopes/csys
(:use :common-lisp)
(:local-nicknames (:actor :scopes/core/actor)
(:config :scopes/config)
(:message :scopes/core/message)
(:shape :scopes/shape)
(:util :scopes/util)
(:alx :alexandria))
(:export #:scope #:environ
#:send #:send-message
#:neuron #:synapse #:std-proc #:update
#:handle-action))
(in-package :scopes/csys)
;;;; scope: information a neuron has access to
(defclass scope ()
((value :reader value :initarg :value :initform 0)
(stage :reader stage :initform :initial)
(syns :reader syns :initform nil)
(program :reader program :initarg :program :initform nil)
(actions :reader actions :initarg :actions :initform nil)
(environ :reader environ :initarg :environ)))
(defun scope (&optional (program #'std-proc) &rest args)
(apply #'make-instance 'scope :program program args))
;;;; neurons (= async tasks) and synapses (= connections)
(defun neuron (scope)
(actor:create
(lambda (msg) (funcall msg scope))))
(defun synapse (rcvr &optional (op #'identity))
(lambda (msg)
(actor:send rcvr (funcall op msg))))
(defun update (scope)
(actor:become
(lambda (msg) (funcall msg scope))))
(defun std-proc (msg scope)
;(util:lgi msg state syns env)
(destructuring-bind (nmsg nscope)
(handle-action msg scope :default #'remember)
(forward nmsg (syns nscope))
(update nscope)))
;;;; effector procs for pseudo-neurons embedded in environment
(defun do-log (msg scope)
(util:lgi msg))
;;;; helper / utility funtions
(defun forward (msg syns)
(dolist (s syns)
(funcall s msg)))
(defun handle-action (msg scope &key (default #'no-op))
(let* ((key (message:action-key msg))
;(act (select-action msg state default))
(act (gethash key (actions scope) default)))
(funcall act msg scope)))
;;;; predefined neuron actions
(defun no-op (msg scope)
(list msg scope))
(defun remember (msg scope)
;(list msg (make-neuron-state (shape:data msg) syns))
(list msg (shape:data msg) syns))