[cells-devel] Lots of instances, callback when particular instance changes

Frank Goenninger frgo at mac.com
Sat May 5 10:02:15 UTC 2007


Hi -

Am 04.05.2007 um 22:41 schrieb Josip Gracin:

> Here's what I'm currently doing (just an outline):
>
> (defmodel switch (component)
>   ((switch-position :cell t
>                     :initarg :switch-position
>                     :initform (c-in :off)
>                     :accessor switch-position)
>    (change-handler :cell nil
>                    :initform nil
>                    :initarg :change-handler
>                    :reader switch-change-handler)))
>
>
> (def-c-output switch-position (self new-value old-value)
>   (when (switch-change-handler self)
>     (funcall (switch-change-handler self) self new-value old-value)))
>
> And then create instances:
>
> ...
> (mksys 'switch :main-battery-switch)
> (mksys 'switch :apu-start-switch                            
> 			 :change-handler 'apu-start-switch-change-handler)
> ...
>
> The reason I need the change handler is that the change handler has  
> to modify the value of the self switch.  For example, when apu- 
> start-switch changes its value to :START, change handler schedules  
> an action which will set the switch back to :ON after some time.  I  
> fail to see how I can do this using regular slot-dependency  
> mechanisms provided by Cells.
>
> Am I making any sense?

You do - yet there is another approach I adopted from Kenny:

-X-X-X-

(in-package :cells)

(defmodel component ()
	((.md-name :accessor id :initarg :id)))

(defmodel switch (component)	
   ((value :accessor value :initform (c-in nil)
	  		 :initarg :value)))

(defmodel apu-start-switch (switch)
   ((opcode :initform (c? (if (eql (^value) :START)					
		:DO-START
		:DO-NOTHING))))
	(:default-initargs
			:value (c-in :off)))

(defobserver opcode ((self apu-start-switch))
     (when (eql new-value :DO-START)
	(format t "~&*** APU STARTING ...")
   	(sleep 3) ;; Do something meaningful here ...
	(format t "~&*** ... APU STARTED - NOW SETTING APU SWITCH TO :ON")
	(with-integrity (:change self) ;; This is how you set the value
             (setf (^value) :ON))))           ;; while maintaining  
Cells rules for integrity

(defun run-test ()
	(let ((apu-start-switch
				  (make-instance 'apu-start-switch :id :apu-start-switch)))
		(format t "~&*** APU START SWITCH \"~A\" created."
						(id apu-start-switch))
		(sleep 1)
		(format t "~&*** Now starting APU !")
		(setf (value apu-start-switch) :START)
		(format t "~&*** APU START SWITCH is now in position \"~A\"."
						(value apu-start-switch))))

-X-X-X-
(you may need to reformat the code to be able to read - use your  
favorate editor)

Instead of making different instances of just the class SWITCH I  
often subclass - as shown above for the APU-START-SWITCH. I then  
model behaviour by having "remote control facility" - the OPCODE. By  
assigning values to the opcode the corresponding observer (see  
DEFOBSERVER above) is a way of modeling state-transitions  
conviniently IMO.

Feedback from  others ???

HTH,
    Frank




More information about the cells-devel mailing list