Active object: Difference between revisions

From Rosetta Code
Content added Content deleted
(fix E example, meet intent of task, add note)
(Task changed in order to clarify the objective)
Line 5: Line 5:


===The task===
===The task===
Implement an active integrator object. The object has an input and output. The input can be set using the method ''Input''. 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 and the output is S, the object state S is changed to S + K * dT, where dT is the time increment. Initially K and S are 0.
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''(''t''<sub>1</sub>) + ''K''(''t''<sub>0</sub>)) * (''t''<sub>1</sub> - ''t''<sub>0</sub>) / 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:
In order to test the object:
# set its input to sin (2pi ''f t''), where the frequency ''f''=0.5Hz. The phase is irrelevant.
# set its input to 1
# wait 2s
# wait 2s
# set the input to 0
# set the input to constant 0
# wait 0.5s
# wait 0.5s
# set it to 0.5
# wait 1s
# set it to 0.


Verify that now the object's output is approximately 2.5s. The accuracy of the result will depend on the [[OS]] scheduler time slicing and the accuracy of the clock.
Verify that now the object's output is approximately 0s (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.

Note, float numbers were intentionally chosen in order to prevent use of built-in atomic operations supported by modern processors (like integer atomic increment, for instance). The objective of the task is to illustrate interplay of the object's the public methods and the encapsulated task, rather than some hardware tricks.


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
<ada>
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar; use Ada.Calendar;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;

with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Integrator is
procedure Test_Integrator is
type Func is access function (T : Time) return Float;

function Zero (T : Time) return Float is
begin
return 0.0;
end Zero;

Epoch : constant Time := Clock;

function Sine (T : Time) return Float is
begin
return Sin (Pi * Float (T - Epoch));
end Sine;

task type Integrator is
task type Integrator is
entry Input (Value : Float);
entry Input (Value : Func);
entry Output (Value : out Float);
entry Output (Value : out Float);
entry Shut_Down;
entry Shut_Down;
end Integrator;
end Integrator;

task body Integrator is
task body Integrator is
K, S : Float := 0.0;
K : Func := Zero'Access;
T0 : Time := Clock;
S : Float := 0.0;
F0 : Float := 0.0;
F1 : Float;
T0 : Time := Clock;
T1 : Time;
T1 : Time;
begin
begin
loop
loop
select
select
accept Input (Value : Float) do
accept Input (Value : Func) do
K := Value;
K := Value;
end Input;
end Input;
Line 49: Line 63:
else
else
T1 := Clock;
T1 := Clock;
S := S + K * Float (T1 - T0);
F1 := K (T1);
S := S + 0.5 * (F1 + F0) * Float (T1 - T0);
T0 := T1;
T0 := T1;
F0 := F1;
end select;
end select;
end loop;
end loop;
end Integrator;
end Integrator;

I : Integrator;
I : Integrator;
S : Float;
S : Float;
begin
begin
I.Input (1.0);
I.Input (Sine'Access);
delay 2.0;
delay 2.0;
I.Input (0.0);
I.Input (Zero'Access);
delay 0.5;
delay 0.5;
I.Input (0.5);
delay 1.0;
I.Input (0.0);
I.Output (S);
I.Output (S);
Put_Line ("Integrated" & Float'Image (S) & "s");
Put_Line ("Integrated" & Float'Image (S) & "s");
Line 72: Line 85:
Sample output:
Sample output:
<pre>
<pre>
Integrated 2.60153E+00s
Integrated-5.34100E-05s
</pre>
</pre>



Revision as of 09:54, 5 November 2008

Task
Active object
You are encouraged to solve this task according to the task description, using any language you may know.

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:

  1. set its input to sin (2pi f t), where the frequency f=0.5Hz. The phase is irrelevant.
  2. wait 2s
  3. set the input to constant 0
  4. wait 0.5s

Verify that now the object's output is approximately 0s (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.

Ada

<ada> with Ada.Calendar; use Ada.Calendar; with Ada.Numerics; use Ada.Numerics; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Integrator is

  type Func is access function (T : Time) return Float;
  function Zero (T : Time) return Float is
  begin
     return 0.0;
  end Zero;
  Epoch : constant Time := Clock;
  function Sine (T : Time) return Float is
  begin
     return Sin (Pi * Float (T - Epoch));
  end Sine;
  task type Integrator is
     entry Input  (Value : Func);
     entry Output (Value : out Float);
     entry Shut_Down;
  end Integrator;

  task body Integrator is
     K  : Func  := Zero'Access;
     S  : Float := 0.0;
     F0 : Float := 0.0;
     F1 : Float;
     T0 : Time  := Clock;
     T1 : Time;
  begin
     loop
        select
           accept Input (Value : Func) do
              K := Value;
           end Input;
        or accept Output (Value : out Float) do
              Value := S;
           end Output;
        or accept Shut_Down;
           exit;
        else
           T1 := Clock;
           F1 := K (T1);
           S  := S + 0.5 * (F1 + F0) * Float (T1 - T0);
           T0 := T1;
           F0 := F1;
        end select;
     end loop;
  end Integrator;

  I : Integrator;
  S : Float;

begin

  I.Input (Sine'Access);
  delay 2.0;
  I.Input (Zero'Access);
  delay 0.5;
  I.Output (S);
  Put_Line ("Integrated" & Float'Image (S) & "s");
  I.Shut_Down;

end Test_Integrator; </ada> Sample output:

Integrated-5.34100E-05s

E

def makeIntegrator() {
    var r := 0.0
    var dr := 0.0
    var t := timer.now()
    
    def update() {
        def t2 := timer.now()
        r += dr * (t2 - t) / 1000
        t := t2
    }
    
    var task() {
      update()
      task <- ()
    }
    task()
    
    def integrator {
        to input(new :float64) :void {
            dr := new
        }
        to output() :float64 {
            return r
        }
        to shutdown() {
            task := fn {}
        }
    }
    return integrator
}
def test() {
    def result
    def base := timer.now()
    def i := makeIntegrator()
    i.input(1)
    timer.whenPast(base + 2000, fn {
        i.input(0)
    })
    timer.whenPast(base + 2500, fn {
        i.input(0.5)
    })
    timer.whenPast(base + 3500, fn {
        i.input(0)
        bind result := i.output()
        i.shutdown()
    })
    return result
}

This integrator need not actually use any continuous background processing; it can compute only when interacted with. To do this, insert update() at the beginning of the input and output methods, and delete the definitions of task and shutdown.