1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar kernel math math.constants
4 math.functions prettyprint system threads timers ;
5 IN: rosetta-code.active-object
7 ! http://rosettacode.org/wiki/Active_object
9 ! In object-oriented programming an object is active when its
10 ! state depends on clock. Usually an active object encapsulates a
11 ! task that updates the object's state. To the outer world the
12 ! object looks like a normal object with methods that can be
13 ! called from outside. Implementation of such methods must have a
14 ! certain synchronization mechanism with the encapsulated task in
15 ! order to prevent object's state corruption.
17 ! A typical instance of an active object is an animation widget.
18 ! The widget state changes with the time, while as an object it
19 ! has all properties of a normal widget.
23 ! Implement an active integrator object. The object has an input
24 ! and output. The input can be set using the method Input. The
25 ! input is a function of time. The output can be queried using the
26 ! method Output. The object integrates its input over the time and
27 ! the result becomes the object's output. So if the input is K(t)
28 ! and the output is S, the object state S is changed to S + (K(t1)
29 ! + K(t0)) * (t1 - t0) / 2, i.e. it integrates K using the trapeze
30 ! method. Initially K is constant 0 and S is 0.
32 ! In order to test the object:
33 ! * set its input to sin (2π f t), where the frequency f=0.5Hz.
34 ! The phase is irrelevant.
36 ! * set the input to constant 0
39 ! Verify that now the object's output is approximately 0 (the
40 ! sine has the period of 2s). The accuracy of the result will
41 ! depend on the OS scheduler time slicing and the accuracy of the
44 TUPLE: active-object timer function state previous-time ;
46 : apply-stack-effect ( quot -- quot' )
47 [ call( x -- x ) ] curry ; inline
49 : nano-to-seconds ( -- seconds ) nano-count 9 10^ / ;
51 : object-times ( active-object -- t1 t2 )
53 [ nano-to-seconds [ >>previous-time drop ] keep ] bi ;
55 :: adding-function ( t1 t2 active-object -- function )
56 t2 t1 active-object function>> apply-stack-effect bi@ +
57 t2 t1 - * 2 / [ + ] curry ;
59 : integrate ( active-object -- )
62 [ swap apply-stack-effect change-state drop ] tri ;
64 : <active-object> ( -- object )
67 nano-to-seconds >>previous-time
69 dup [ integrate ] curry 1 nanoseconds every >>timer ;
71 : destroy ( active-object -- ) timer>> stop-timer ;
73 : input ( object quot -- object ) >>function ;
75 : output ( object -- val ) state>> ;
79 [ 2 pi 0.5 * * * sin ] input
83 [ output . ] [ destroy ] bi ;