In object-oriented programming an object is active when its state depends on clock. Usually an active object encapsulates a task that updates the object's state. To the outer world the object looks like a normal object with methods that can be called from outside. Implementation of such methods must have a certain synchronization mechanism with the encapsulated task in order to prevent object's state corruption.

A typical instance of an active object is an animation widget. The widget state changes with the time, while as an object it has all properties of a normal widget.

The task

Implement an active integrator object. The object has an input and output. The input can be set using the method Input. The input is a function of time. The output can be queried using the method Output. The object integrates its input over the time and the result becomes the object's output. So if the input is K(t) and the output is S, the object state S is changed to S + (K(t1) + K(t0)) * (t1 - t0) / 2, i.e. it integrates K using the trapeze method. Initially K is constant 0 and S is 0.

In order to test the object:

 set its input to sin (2π f t), where the frequency f=0.5Hz. The phase is irrelevant.
 wait 2s
 set the input to constant 0
 wait 0.5s
Verify that now the object's output is approximately 0 (the sine has the period of 2s). The accuracy of the result will depend on the OS scheduler time slicing and the accuracy of the clock.

Prototype.d ValueFunction(f.d, t.d)
 
Class IntegralClass
  Time0.i
  Mutex.i
  S.d
  Freq.d
  Thread.i
  Quit.i
  *func.ValueFunction
 
  Protect Method Sampler()
    Repeat
      Delay(1)
      If This\func And This\Mutex
        LockMutex(This\Mutex)
        This\S + This\func(This\Freq, ElapsedMilliseconds()-This\Time0)
        UnlockMutex(This\Mutex)
      EndIf
    Until This\Quit 
  EndMethod
 
  BeginPublic
    Method Input(*func.ValueFunction)
      LockMutex(This\Mutex)
      This\func = *func
      UnlockMutex(This\Mutex)
    EndMethod
 
    Method.d Output()
      Protected Result.d
      LockMutex(This\Mutex)
      Result = This\S
      UnlockMutex(This\Mutex)
      MethodReturn Result
    EndMethod
 
    Method Init(F.d, *f)
      This\Freq   = F
      This\func   = *f
      This\Mutex  = CreateMutex()
      This\Time0  = ElapsedMilliseconds()
      This\Thread = CreateThread(This\Sampler, This)
      ThreadPriority(This\Thread, 10)
    EndMethod
 
    Method Release()
      This\Quit = #True
      WaitThread(This\Thread)
    EndMethod
  EndPublic
 
EndClass
 
;- Procedures for generating values
Procedure.d n(f.d, t.d)
  ; Returns nothing
EndProcedure
 
Procedure.d f(f.d, t.d)
  ; Returns the function of this task
  ProcedureReturn Sin(2*#PI*f*t)
EndProcedure
 
;- Test Code
*a.IntegralClass = NewObject.IntegralClass(0.5, @n()) ; Create the AO
*a\Input(@f()) ; Start sampling function f()
Delay(2000)    ; Delay 2 sec
*a\Input(@n()) ; Change to sampling 'nothing'
Delay( 500)    ; Wait 1/2 sec
MessageRequester("Info", StrD(*a\Output()))           ; Present the result
*a= FreeObject