Sychronizing list inputs?

Oct 6, 2007 at 5:33pm

Sychronizing list inputs?

I need to sychronize the input of two lists. Right now I have a hunk of code that looks like this:

public void list(Atom[] list){
Atom[] A=null,B=null;
int z = getInlet();

if(z==0){
A =list;
}
if(z==1){
B=list;
}
}

But, I get null pointers. I’m a n00b with mxj coding so any help would be appreciated.

One solution I have devised is to merge the two lists into one list and then parse the list itno two seperate lists once it enters the object; but that seems obnoxious. Do I have to pause the thread? ewww….

Best!

#34008
Oct 6, 2007 at 6:11pm

> I need to sychronize the input of two lists.

I’m not sure what this means: can you explain?

> public void list(Atom[] list){
> Atom[] A=null,B=null;
> int z = getInlet();
>
> if(z==0){
> A =list;
> }
> if(z==1){
> B=list;
> }
> }

This looks OK, as far as it goes. What are you trying to achieve from
here?

– N.

Nick Rothwell / Cassiel.com Limited
http://www.cassiel.com
http://www.myspace.com/cassieldotcom
http://www.loadbang.net

#114248
Oct 6, 2007 at 6:23pm

Do you receive two lists at approximately the same time, and want to
wait for whichever arrives 2nd before starting a computation on both?

If that’s the case I’ve found the following works well:

1. In your constructor, set up a MaxClock to trigger a process function.

c = new MaxClock(this, “process”);

2. In your list handler (your code below), move the data storage to a
class array (i.e. persistant longer than that function). Finish the
function by starting the clock

c.delay(thresh);// wait for more input and if none, then process

3. Create a function to process your multiple arguments that is
triggered by the clock.

When messages come in, they will be cached by your list handler.
After a short delay (2ms works well on my machine), the process
function is called. If both inlets haven’t been supplied by then
you’ll have to handle an empty list gracefully, but if both lists
arrive at more or less the same time, A & B will be valid by the time
the clock fires.

Hope that helps,

r.

On 07-10-06, at 1033, Timothy Dalbey wrote:
>
> I need to sychronize the input of two lists. Right now I have a
> hunk of code that looks like this:
>
> public void list(Atom[] list){
> Atom[] A=null,B=null;
> int z = getInlet();
>
> if(z==0){
> A =list;
> }
> if(z==1){
> B=list;
> }
> }
>
> But, I get null pointers. I’m a n00b with mxj coding so any help
> would be appreciated.
>
> One solution I have devised is to merge the two lists into one list
> and then parse the list itno two seperate lists once it enters the
> object; but that seems obnoxious. Do I have to pause the thread?
> ewww….
>
> Best!

#114249
Oct 6, 2007 at 7:13pm

Something like this?

import com.cycling74.max.*;
import java.util.*;

/*
*
Written by Timothy Dalbey
Universiy of Virginia 07′
*
*/

public class mutation extends MaxObject{
double match,delete,error,insert,merge;//presets for errorchecking
ArrayList> bio = new ArrayList>();
Random random = new Random();
MaxClock c = new MaxClock(this, “list”);

public mutation(Atom[] args)
{
declareInlets(new int[]{
DataTypes.ALL,DataTypes.ALL,DataTypes.FLOAT, DataTypes.FLOAT,DataTypes.FLOAT,DataTypes.FLOAT, DataTypes.FLOAT});
declareOutlets(new int[]{
DataTypes.ALL, DataTypes.ALL});
setInletAssist(0,”Uncorrupt Pattern”);
setInletAssist(1,”Corrupt Pattern”);
setInletAssist(2,”Probability Match”);
setInletAssist(3,”Probabilty Delete”);
setInletAssist(4,”Probability Error”);
setInletAssist(5,”Probability Insert”);
setInletAssist(6,”Probability Merge”);
setOutletAssist(0, “Corrupt Pattern”);
createInfoOutlet(false);

}

public void inlet(int i)
{
}

public void inlet(float f){
int z = getInlet();
if (f>1|f<0){
post(“can not have probability greater than 1.”);
}else{
outlet(1,z);
switch(z){
case 2:
match = (double)f;
case 3:
delete = (double)f;
case 4:
error = (double)f;
case 5:
insert=(double)f;
case 6:
merge=(double)f;
}
}
}

public void bang()
{
}

public void list(Atom[] list){
Atom[] A=null,B=null;
int z = getInlet();

if(z==0){
outlet(1,z);
A =list;
}
if(z==1){
B=list;
}
c.delay(2.);
evolve(A,B);
}

public void evolve(Atom[] A, Atom[] B){
int c=A.length;
float x=0;
Atom a,b,d;
post(match +” “+delete+” “+error+” “+insert+” “+merge);
for(int j = 0; j
x = random.nextFloat();
a=A[j];
d=B[j];

if (j+1
b=A[j+1];

}else{
b=A[random.nextInt(A.length)];
}

if (x>=0 && x
matchmech(d);
continue;
}
if(x>=match && x
deletemech(a);
continue;
}
if(x>=delete && x
errormech(a);
continue;
}
if(x>=error && x
insertmech(d);
continue;
}
if(x>=insert){
j=j+mergemech(a,b);
continue;
}
}

Atom[] L = new Atom[bio.size()];
for(int i = 0; i
L[i]=(Atom) bio.get(i);
}
outlet(0,L);
bio.clear();
}
public void matchmech(Atom a){
bio.add(a);
}
public void deletemech(Atom a){
}
public void errormech(Atom a){
int b=a.toInt();
if (b==0){
bio.add(Atom.newAtom(1));
}else{
bio.add(Atom.newAtom(0));
}
}
public void insertmech(Atom a){
boolean z=random.nextBoolean();
bio.add(a);
if (z){
bio.add(Atom.newAtom(1));
}else{
bio.add(Atom.newAtom(0));
}
}
public int mergemech(Atom a, Atom b){
if(a==b){
bio.add(a);
return 1;
}else{
return 0;
}
}
}

#114250
Oct 6, 2007 at 7:24pm

Not quite. I’m just editing this in my mail client and it’s been
quite a while since I looked at mxj, so it might not compile.
Hopefully it will point you in the correct direction.

import com.cycling74.max.*;
import java.util.*;

/*
*
Written by Timothy Dalbey
Universiy of Virginia 07′
*
*/

public class mutation extends MaxObject{
double match,delete,error,insert,merge;//presets for errorchecking
ArrayList> bio = new
ArrayList
>();
Random random = new Random();

private MaxClock c;
private Atom[] A;
private Atom[] B;

public mutation(Atom[] args)
{
declareInlets(new int[]{
DataTypes.ALL,DataTypes.ALL,DataTypes.FLOAT,
DataTypes.FLOAT,DataTypes.FLOAT,DataTypes.FLOAT, DataTypes.FLOAT});
declareOutlets(new int[]{
DataTypes.ALL, DataTypes.ALL});
setInletAssist(0,”Uncorrupt Pattern”);
setInletAssist(1,”Corrupt Pattern”);
setInletAssist(2,”Probability Match”);
setInletAssist(3,”Probabilty Delete”);
setInletAssist(4,”Probability Error”);
setInletAssist(5,”Probability Insert”);
setInletAssist(6,”Probability Merge”);
setOutletAssist(0, “Corrupt Pattern”);
createInfoOutlet(false);

c = new MaxClock(this, evolve);
}

public void inlet(int i)
{
}

public void inlet(float f){
int z = getInlet();
if (f>1|f<0){
post(“can not have probability greater than 1.”);
}else{
outlet(1,z);
switch(z){
case 2:
match = (double)f;
case 3:
delete = (double)f;
case 4:
error = (double)f;
case 5:
insert=(double)f;
case 6:
merge=(double)f;
}
}
}

public void bang()
{
}

public void list(Atom[] list){
int z = getInlet();

if(z==0){
//outlet(1,z);
A=list;
}
if(z==1){
B=list;
}
c.delay(2);
}

public void evolve(){
if (A == null || B == null) {
// bail gracefully
}

int c=A.length;
float x=0;
Atom a,b,d;
post(match +” “+delete+” “+error+” “+insert+” “+merge);
for(int j = 0; j
x = random.nextFloat();
a=A[j];
d=B[j];

if (j+1
b=A[j+1];

}else{
b=A[random.nextInt(A.length)];
}

if (x>=0 && x
matchmech(d);
continue;
}
if(x>=match && x
deletemech(a);
continue;
}
if(x>=delete && x
errormech(a);
continue;
}
if(x>=error && x
insertmech(d);
continue;
}
if(x>=insert){
j=j+mergemech(a,b);
continue;
}
}

Atom[] L = new Atom[bio.size()];
for(int i = 0; i
L[i]=(Atom) bio.get(i);
}

// maybe set A + B to null when you’re done with them

outlet(0,L);
bio.clear();
}
public void matchmech(Atom a){
bio.add(a);
}
public void deletemech(Atom a){
}
public void errormech(Atom a){
int b=a.toInt();
if (b==0){
bio.add(Atom.newAtom(1));
}else{
bio.add(Atom.newAtom(0));
}
}
public void insertmech(Atom a){
boolean z=random.nextBoolean();
bio.add(a);
if (z){
bio.add(Atom.newAtom(1));
}else{
bio.add(Atom.newAtom(0));
}
}
public int mergemech(Atom a, Atom b){
if(a==b){
bio.add(a);
return 1;
}else{
return 0;
}
}
}

#114251
Oct 7, 2007 at 2:45pm

Yeah… that definitely doesnt compile but you pointed me in the right direction. For the record, maxClock saved the day. I was able to pause the thread for 2ms to allow both lists to be filled in an otherwise “simultaneous” manner. Thanks for the direction.

Best.

#114252

You must be logged in to reply to this topic.