September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
70
Task/Active-object/Java/active-object.java
Normal file
70
Task/Active-object/Java/active-object.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/**
|
||||
* Integrates input function K over time
|
||||
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
|
||||
*/
|
||||
public class Integrator {
|
||||
|
||||
public interface Function {
|
||||
double apply(double timeSinceStartInSeconds);
|
||||
}
|
||||
|
||||
private final long start;
|
||||
private volatile boolean running;
|
||||
|
||||
private Function func;
|
||||
private double t0;
|
||||
private double v0;
|
||||
private double sum;
|
||||
|
||||
public Integrator(Function func) {
|
||||
this.start = System.nanoTime();
|
||||
setFunc(func);
|
||||
new Thread(this::integrate).start();
|
||||
}
|
||||
|
||||
public void setFunc(Function func) {
|
||||
this.func = func;
|
||||
v0 = func.apply(0.0);
|
||||
t0 = 0;
|
||||
}
|
||||
|
||||
public double getOutput() {
|
||||
return sum;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
private void integrate() {
|
||||
running = true;
|
||||
while (running) {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
update();
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void update() {
|
||||
double t1 = (System.nanoTime() - start) / 1.0e9;
|
||||
double v1 = func.apply(t1);
|
||||
double rect = (t1 - t0) * (v0 + v1) / 2;
|
||||
this.sum += rect;
|
||||
t0 = t1;
|
||||
v0 = v1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Integrator integrator = new Integrator(t -> Math.sin(Math.PI * t));
|
||||
Thread.sleep(2000);
|
||||
|
||||
integrator.setFunc(t -> 0.0);
|
||||
Thread.sleep(500);
|
||||
|
||||
integrator.stop();
|
||||
System.out.println(integrator.getOutput());
|
||||
}
|
||||
}
|
||||
74
Task/Active-object/OoRexx/active-object.rexx
Normal file
74
Task/Active-object/OoRexx/active-object.rexx
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
integrater = .integrater~new(.routines~sine) -- start the integrater function
|
||||
call syssleep 2
|
||||
integrater~input = .routines~zero -- update the integrater function
|
||||
call syssleep .5
|
||||
|
||||
say integrater~output
|
||||
integrater~stop -- terminate the updater thread
|
||||
|
||||
::class integrater
|
||||
::method init
|
||||
expose stopped start v last_v last_t k
|
||||
use strict arg k
|
||||
stopped = .false
|
||||
start = .datetime~new -- initial time stamp
|
||||
v = 0
|
||||
last_v = 0
|
||||
last_t = 0
|
||||
self~input = k
|
||||
self~start
|
||||
|
||||
-- spin off a new thread and start updating. Note, this method is unguarded
|
||||
-- to allow other threads to make calls
|
||||
::method start unguarded
|
||||
expose stopped
|
||||
|
||||
reply -- this spins this method invocation off onto a new thread
|
||||
|
||||
do while \stopped
|
||||
call sysSleep .1
|
||||
self~update -- perform the update operation
|
||||
end
|
||||
|
||||
-- turn off the thread. Since this is unguarded,
|
||||
-- it can be called any time, any where
|
||||
::method stop unguarded
|
||||
expose stopped
|
||||
stopped = .true
|
||||
|
||||
-- perform the update. Since this is a guarded method, the object
|
||||
-- start is protected.
|
||||
::method update
|
||||
expose start v last_v t last_t k
|
||||
|
||||
numeric digits 20 -- give a lot of precision
|
||||
|
||||
current = .datetime~new
|
||||
t = (current - start)~microseconds
|
||||
new_v = k~call(t) -- call the input function
|
||||
v += (last_v + new_v) * (t - last_t) / 2
|
||||
last_t = t
|
||||
last_v = new_v
|
||||
say new value is v
|
||||
|
||||
-- a write-only attribute setter (this is GUARDED)
|
||||
::attribute input SET
|
||||
expose k last_t last_v
|
||||
self~update -- update current values
|
||||
use strict arg k -- update the call function to the provided value
|
||||
last_t = 0
|
||||
last_v = k~call(0) -- and update to the zero value
|
||||
|
||||
-- the output function...returns current calculated value
|
||||
::attribute output GET
|
||||
expose v
|
||||
return v
|
||||
|
||||
::routine zero
|
||||
return 0
|
||||
|
||||
::routine sine
|
||||
use arg t
|
||||
return rxcalcsin(rxcalcpi() * t)
|
||||
|
||||
::requires rxmath library
|
||||
19
Task/Active-object/Zkl/active-object-1.zkl
Normal file
19
Task/Active-object/Zkl/active-object-1.zkl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class Integrator{
|
||||
// continuously integrate a function `K`, at each `interval` seconds'
|
||||
fcn init(f,interval=1e-4){
|
||||
var _interval=interval, K=Ref(f), S=Ref(0.0), run=True;
|
||||
self.launch(); // start me as a thread
|
||||
}
|
||||
fcn liftoff{ // entry point for the thread
|
||||
start:=Time.Clock.timef; // floating point seconds since Epoch
|
||||
t0,k0,s:=0,K.value(0),S.value;
|
||||
while(run){
|
||||
Atomic.sleep(_interval);
|
||||
t1,k1:=Time.Clock.timef - start, K.value(t1);
|
||||
s+=(k1 + k0)*(t1 - t0)/2.0; S.set(s);
|
||||
t0,k0=t1,k1;
|
||||
}
|
||||
}
|
||||
fcn sample { S.value }
|
||||
fcn setF(f) { K.set(f) }
|
||||
}
|
||||
7
Task/Active-object/Zkl/active-object-2.zkl
Normal file
7
Task/Active-object/Zkl/active-object-2.zkl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
ai:=Integrator(fcn(t){ ((0.0).pi*t).sin() });
|
||||
Atomic.sleep(2);
|
||||
ai.sample().println();
|
||||
|
||||
ai.setF(fcn{ 0 });
|
||||
Atomic.sleep(0.5);
|
||||
ai.sample().println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue