I currently have a clunky method which will take a string array containing method names and parameters like this
String []wf = {"sobel(10,1)","sobel2(2,2)","sobelMax(sobel,100,10)"};
this tells it what method to call along with the parameters to pass. However as you can see not all the methods use the same number of parameters and not all the parameter types are the same, some floats some ints and some PImage types.
currently this is passed into this function, which trims the strings and extracts the useful info. Then passes the parameter length through a switch case to best handle the next part.
void workflow(String[] a){
String[] s = a;
for(int i=0;i<s.length;i++){
String s1 = s[i];
int [] pIndex = strIndex1(s1,"(",")");
String function = s1.substring(0,pIndex[0]);
String[]parameters = splitTokens(s[i].substring(pIndex[0]+1,pIndex[1]),",");
print("p",function ,"(");
for(int j=0;j<parameters.length;j++){
print(parameters[j]);
if(j<parameters.length-1)print(",");
}
println(")");
switch(parameters.length){
case 1: func1(function,int(parameters[0]));
println("c1");
break;
case 2: if(float(parameters[0])>-1000000&&float(parameters[0])<100000000){
func2(function,float(parameters[0]),int(parameters[1]));
println("c2");
}else {
Field field = null;
try{
field = this.getClass().getField(parameters[0]);
func2(function,field,int(parameters[1]));
}catch (NullPointerException e){
println("np c2");
}catch (NoSuchFieldException e) {
println("nsf c3");
}
}
break;
case 3:
if(float(parameters[0])>-1000000&&float(parameters[0])<100000000){
func3(function,float(parameters[0]),float(parameters[1]),int(parameters[2]));
println("c2");
}else {
Field field = null;
try{
field = this.getClass().getField(parameters[0]);
//Object.value = field.
PImage p = (PImage)field.get(this);
func3(function,p,float(parameters[1]),int(parameters[2]));
}catch (NullPointerException e){
println("np c3");
}catch (NoSuchFieldException e) {
println("nsf c3");
}catch (IllegalAccessException e) {
println("ia c3");
}
}
println("c3");
break;
//func3(function,parameters);
//case 3: func4(function,parameters);
}
}
};
finally the switch statement routes the methods and parameters accordingly to a function which will be able to match the correct method in the class.
void func1(String function,int p){
println(function,p);
Method method = null;
try {
method = this.getClass().getMethod(function,int .class);
println(method);
method.invoke(this, p);
//println("result",result);
} catch (SecurityException e) {
println(function , "se f1");
}catch (NoSuchMethodException e) {
println(function , "nsm f1");
}
catch (IllegalAccessException e) {
println(function , "ia f1");
}
catch (InvocationTargetException e) {
println(function , "it f1");
}
};
is there a way I could condense this code and maybe do away with the switch case statement as it all looks pretty unwieldy and only increases in size the more parameter.
Many Thanks
Aucun commentaire:
Enregistrer un commentaire