/** GPBinaryNode.java * J Scott Cameron * * GPNode that hold a methad that takes 2 parameters */ import GPNode; import java.util.*; import java.lang.reflect.*; import GPPair; public class GPBinaryNode implements GPNode { private Vector children;/*holds the two children nodes*/ private String sign;/* holds the sign of the method*/ private Object target;/* instance object that has the method in it*/ private String method;/* name of the method */ private Double value;/* the value of the node */ private Method methodObject;/*Method Object that references the method*/ /* default constructor */ public GPBinaryNode() { value = new Double( 0); children = new Vector(); children.setSize(2); } /* parameterized constructor */ public GPBinaryNode(Object target,GPPair gpp) { this.target = target; this.method = gpp.name; this.sign = gpp.sign; children = new Vector(); children.setSize(2); /* try to get the method */ try{ Class [] parameters = new Class[] { (new Double(0)).getClass() , (new Double(0)).getClass() }; Class c = target.getClass(); methodObject =c.getMethod(method,parameters); } catch(NoSuchMethodException e) { System.out.println(e.toString() + " "+ method + " " + toString()); } value = new Double( 0); } /* copy constructor */ public GPBinaryNode(GPBinaryNode gpb) { children = gpb.children; target = gpb.target; method = gpb.method; sign = gpb.sign; value = gpb.value; /* try to get the method */ try{ Class [] parameters = new Class[] { (new Double(0)).getClass(), (new Double(0)).getClass() }; Class c = target.getClass(); methodObject =c.getMethod(method,parameters); } catch(NoSuchMethodException e) { System.out.println(e.toString() + " "+ method + " " + toString()); } } /* returns a new copy of the node*/ public GPNode copy() { GPBinaryNode gpb = new GPBinaryNode(); gpb.target =target; gpb.method = method; gpb.sign = sign; gpb.value = value; gpb.children.set(0,getChild(0).copy()); gpb.children.set(1,getChild(1).copy()); /* try to get the method */ try{ Class [] parameters = new Class[] { (new Double(0)).getClass(), (new Double(0)).getClass() }; Class c = target.getClass(); gpb.methodObject =c.getMethod(method,parameters); } catch(NoSuchMethodException e) { System.out.println(e.toString() + " "+ method + " " + toString()); } return gpb; } /* sets the function */ public void setFunction(GPPair gpp) { method = gpp.name; sign = gpp.sign; } /* returns the value of the tree */ public Double value() throws NoSuchMethodException { try{ Object[] args = {((GPNode)(children.get(0))).value(), ((GPNode)(children.get(1))).value()}; /*invoke the method using the values of the two subtrees * as arguments */ value = ((Double)methodObject.invoke(target,args)) ; } catch(InvocationTargetException e) { System.out.println(e); } catch(IllegalAccessException e) { System.out.println(e); } catch(NoSuchMethodException e) { System.out.println(e.toString() + " "+ method); throw new NoSuchMethodException(); } return value; } /* binary node, return 2*/ public int childCount() { return 2; } /* returns child node i*/ public GPNode getChild(int i) { return (GPNode)children.get(i); } /* sets child i to given node*/ public void setChild(int i,GPNode gpn) { children.set(i,gpn); } /* finds child with nodeNumber i */ public GPNode find(int i) { int split = nodeNumber(); if(i < split) { return ((GPNode)(children.get(0))).find(i); } if(i > split) { return ((GPNode)(children.get(0))).find(i-split); } return this; } /* sets descendant i */ public void setDescendant(int number, GPNode gpn) { int split = nodeNumber(); if(number < split) { /*if this child is the one to be set, do it*/ if (number == ((GPNode)(children.get(0))).nodeNumber()) children.set(0,gpn); /* else search deeper into subtree */ else ((GPNode)(children.get(0))).setDescendant(number,gpn); } else if(number > split) { int i = number-split; /*if this child is the one to be set, do it*/ if (i == ((GPNode)(children.get(1))).nodeNumber()) children.set(1,gpn); /* else search deeper into subtree */ else ((GPNode)(children.get(1))).setDescendant(number,gpn); } } /* returns BINARY */ public int getType() { return BINARY; } /* returns the nodenumber of this node within the local * tree */ public int nodeNumber() { return ((GPNode)(children.get(0))).count()+1; } /* returns the size of the local tree */ public int count() { return ((GPNode)(children.get(0))).count() + ((GPNode)(children.get(1))).count() +1; } /* prints the string representing the local tree */ public String toString() { return sign+"("+((GPNode)(children.get(0))).toString()+","+ ((GPNode)(children.get(1))).toString()+")"; } }