How to resolve the algorithm Active object step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Active object step by step in the J 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 J programming language

Source code in the j programming language

coclass 'activeobject'
require'dates'

create=:setinput NB. constructor

T=:3 :0
  if. nc<'T0' do. T0=:tsrep 6!:0'' end.
  0.001*(tsrep 6!:0'')-T0
)

F=:G=:0:
Zero=:0

setinput=:3 :0
  zero=. getoutput''
  '`F ignore'=: y,_:`''
  G=: F f.d._1
  Zero=: zero-G T ''
  getoutput''
)

getoutput=:3 :0
  Zero+G T''
)


cocurrent 'testrig'

delay=: 6!:3

object=: conew 'activeobject'
setinput__object 1&o.@o.`''
smoutput (T__object,getoutput__object) ''

delay 2

smoutput (T__object,getoutput__object) ''
setinput__object 0:`''
smoutput (T__object,getoutput__object) ''

delay 0.5

smoutput (T__object,getoutput__object) ''


delay=: 6!:3

task=: {{
  obj=. '' conew 'integra'
  F__obj=: 1 o. o.
  delay 2
  F__obj=: 0:
  delay 0.5
  s=. S__obj
  destroy__obj''
  s
}}

coclass'integra'
 reqthreads=: {{ 0&T.@''^:(0>.y-1 T.'')0 }}
 time=: 6!:1
 F=: 0:
 K=: S=: SHUTDOWN=: 0
 create=: {{
  reqthreads cores=. {.8 T. ''
  integrator t. '' T=: time''
 }}
 destroy=: {{ codestroy '' [ SHUTDOWN=: 1 }}
 integrator=: {{
  while. -.SHUTDOWN do.
   t=. time''
   k=. F t
   S=: S + (k+K)*t-T
   T=: t
   K=: k
  end.
 }}


   task''
0.0194745
   task''
_4.40316e_15
   task''
0.00874017
   task''
_0.0159841


  

You may also check:How to resolve the algorithm Visualize a tree step by step in the REXX programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Delphi programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Prolog programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Glee programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Lean programming language