How to resolve the algorithm Active object step by step in the Ada programming language
How to resolve the algorithm Active object step by step in the Ada programming language
Table of Contents
Problem Statement
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: 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Active object step by step in the Ada programming language
Source code in the ada programming language
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;
You may also check:How to resolve the algorithm Power set step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Java programming language
You may also check:How to resolve the algorithm Loops/While step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Quine step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Scala programming language