ChucK/Patterns/ExternalDataLoader
From CSWiki
I have been experimenting with a pattern for loading external data, such that once the data is loaded into the ChucK VM, an event is triggered and a consumer of the data is informed that the data is ready for loadinginng. The code below is just scratch code that I am in the process of cleaning up. When this page is complete, I will have 4 distinct modules: a DataLoadedEvent class, a data producer, a data consumer, a class to hold globally accessible static fields like the DataLoadedEvent, and a class to hold externally loaded data (class will provide nice methods to iterate over the data loaded (without having to know the specifics of the data collection)
public class DataObj {
int mydata[];
}
public class TheEvent extends Event
{
int value;
}
//GlobalSpace - All public class that holds globally accessible
// Objects (in this case a user defined event class
//However you cannot (in v. 1.2.1.1) use a straightforward
//approach to capturing non-primative types in a static variable
//So we do a bare declare of e...
public class GlobalSpace {
static TheEvent @ e;
//static int data[200];
static DataObj @ callback;
}
// ...then use the following to do the assignment
new TheEvent @=> GlobalSpace.e;
//This seems to be a necessary hack to allow us to
//use a non-primitive in a static variable space
//
// Event class is defined in the_event.ck
// and the reference to the event class is held in the GlobalSpace
// as a static variable
int data_ready;
int count;
DataObj @ dataObj;
// handler
fun int hi( TheEvent e )
{
while( true )
{
// wait on event
e => now;
// get the data
<<<"Data Is Ready">>>;
1 => data_ready;
0 => count;
GlobalSpace.callback @=> dataObj;
}
}
// spork
<<<"Event Receiver">>>;
spork ~ hi( GlobalSpace.e );
spork ~ hi( GlobalSpace.e );
spork ~ hi( GlobalSpace.e );
spork ~ hi( GlobalSpace.e );
// infinite time loop
/**************/
while( true )
{
// advance time
1::second => now;
if (data_ready) {
if (count < dataObj.mydata.cap()) {
<<< dataObj.mydata[count]>>>;
1 +=> count;
} else {
0 => data_ready => count;
}
}
}
/* */
// the event TheEvent @ e; GlobalSpace.e @=> e; <<<"Flag data is ready">>>; DataObj dataObj; [1,2,3,5,7,11,13] @=> dataObj.mydata; dataObj @=> GlobalSpace.callback; e.signal();
Michael Nardell
