Difference between revisions of "ChucK/Patterns/ExternalDataLoader"

From CSWiki
Jump to: navigation, search
Line 1: Line 1:
 
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
 
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
 
+
<pre>
 +
public class DataObj {
 +
int mydata[];
 +
}
 +
</pre>
 +
<pre>
 +
public class TheEvent extends Event
 +
{
 +
    int value;
 +
}
 +
</pre>
 
<pre>
 
<pre>
 
//GlobalSpace - All public class that holds globally accessible  
 
//GlobalSpace - All public class that holds globally accessible  
Line 21: Line 31:
 
//
 
//
 
</pre>
 
</pre>
 +
<pre>
 +
// 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;
 +
 +
}
 +
    }
 +
 +
}
 +
/*  */
 +
</pre>
 +
<pre>
 +
// 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();
 +
</pre>
  
 
Michael Nardell
 
Michael Nardell

Revision as of 23:26, 2 December 2007

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

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