How to resolve the algorithm Active object step by step in the Go programming language
How to resolve the algorithm Active object step by step in the Go 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 Go programming language
Overview:
- The code implements an "active integrator" class
aio
that continuously integrates an input functionk(t)
over time and stores the result in a state variables
. - The integrator runs as a separate goroutine to handle the integration process asynchronously.
Components:
1. Type Definitions:
type tFunc func(time.Duration) float64
type aio struct {
iCh chan tFunc // channel for setting input function
oCh chan chan float64 // channel for requesting output
}
tFunc
represents the input function that takes a time duration and returns a float64 value.aio
is the active integrator object. It manages the input and output channels for controlling the integration function and obtaining the integrated value.
2. Constructor:
func newAio() *aio {
var a aio
a.iCh = make(chan tFunc)
a.oCh = make(chan chan float64)
go aif(&a)
return &a
}
newAio()
creates an instance of the active integrator and starts a goroutineaif
that runs the integration process.
3. Input Method:
func (a aio) input(f tFunc) {
a.iCh <- f
}
input
allows the user to set the input functionf
that will be integrated.
4. Output Method:
func (a aio) output() float64 {
sCh := make(chan float64)
a.oCh <- sCh
return <-sCh
}
output
returns the current integrated value ofs
by sending a channel to theaif
goroutine and receiving the value back.
5. Integration Function:
func aif(a *aio) {
var k tFunc = zeroFunc // integration function
s := 0. // "object state" initialized to 0
t0 := time.Now() // initial time
k0 := k(0) // initial sample value
t1 := t0 // t1, k1 used for trapezoid formula
k1 := k0
tk := time.Tick(10 * time.Millisecond) // 10 ms -> 100 Hz
for {
select {
case t2 := <-tk: // timer tick event
k2 := k(t2.Sub(t0)) // new sample value
s += (k1 + k2) * .5 * t2.Sub(t1).Seconds() // trapezoid formula
t1, k1 = t2, k2 // save time and value
case k = <-a.iCh: // input method event: function change
case sCh := <-a.oCh: // output method event: sample object state
sCh <- s
}
}
}
aif
is the goroutine that continuously integrates the input function.- It uses a 10 ms timer to sample the input function at 100 Hz.
- It calculates the integral using the trapezoid formula.
Main Function:
func main() {
a := newAio() // create object
a.input(func(t time.Duration) float64 { // 1. set input to sin function
return math.Sin(t.Seconds() * math.Pi)
})
time.Sleep(2 * time.Second) // 2. sleep 2 sec
a.input(zeroFunc) // 3. set input to zero function
time.Sleep(time.Second / 2) // 4. sleep .5 sec
fmt.Println(a.output()) // output should be near zero
}
- The
main
function sets up the integration by creating anaio
instance and configuring the input function to be a sine wave. - It sleeps for 2 seconds and then changes the input function to zero.
- Finally, it prints the integrated value after sleeping for another 0.5 seconds. The output should be close to zero since the input function becomes zero.
Source code in the go programming language
package main
import (
"fmt"
"math"
"time"
)
// type for input function, k.
// input is duration since an arbitrary start time t0.
type tFunc func(time.Duration) float64
// active integrator object. state variables are not here, but in
// function aif, started as a goroutine in the constructor.
type aio struct {
iCh chan tFunc // channel for setting input function
oCh chan chan float64 // channel for requesting output
}
// constructor
func newAio() *aio {
var a aio
a.iCh = make(chan tFunc)
a.oCh = make(chan chan float64)
go aif(&a)
return &a
}
// input method required by task description. in practice, this method is
// unnecessary; you would just put that single channel send statement in
// your code wherever you wanted to set the input function.
func (a aio) input(f tFunc) {
a.iCh <- f
}
// output method required by task description. in practice, this method too
// would not likely be best. instead any client interested in the value would
// likely make a return channel sCh once, and then reuse it as needed.
func (a aio) output() float64 {
sCh := make(chan float64)
a.oCh <- sCh
return <-sCh
}
// integration function that returns constant 0
func zeroFunc(time.Duration) float64 { return 0 }
// goroutine serializes access to integrated function k and state variable s
func aif(a *aio) {
var k tFunc = zeroFunc // integration function
s := 0. // "object state" initialized to 0
t0 := time.Now() // initial time
k0 := k(0) // initial sample value
t1 := t0 // t1, k1 used for trapezoid formula
k1 := k0
tk := time.Tick(10 * time.Millisecond) // 10 ms -> 100 Hz
for {
select {
case t2 := <-tk: // timer tick event
k2 := k(t2.Sub(t0)) // new sample value
s += (k1 + k2) * .5 * t2.Sub(t1).Seconds() // trapezoid formula
t1, k1 = t2, k2 // save time and value
case k = <-a.iCh: // input method event: function change
case sCh := <-a.oCh: // output method event: sample object state
sCh <- s
}
}
}
func main() {
a := newAio() // create object
a.input(func(t time.Duration) float64 { // 1. set input to sin function
return math.Sin(t.Seconds() * math.Pi)
})
time.Sleep(2 * time.Second) // 2. sleep 2 sec
a.input(zeroFunc) // 3. set input to zero function
time.Sleep(time.Second / 2) // 4. sleep .5 sec
fmt.Println(a.output()) // output should be near zero
}
You may also check:How to resolve the algorithm Integer sequence step by step in the ChucK programming language
You may also check:How to resolve the algorithm Object serialization step by step in the Go programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the bc programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Elena programming language