/** GPUnaryNode.java * J Scott Cameron * * This GPNode holds a method that takes one parameter */ import GPNode; import GPPair; import java.util.*; import java.lang.reflect.*; public class GPUnaryNode implements GPNode { private Vector children;/* holds the child Node */ private String sign;/* the sign of the method, such as "+" */ private Object target;/* instance of the class holding the method */ private String method;/* name of the method to be invoked */ private Double value;/* value of this function tree */ private Method methodObject;/* Method object that references the method */ private Class targetClass;/* class of the target */ /* default constructor */ public GPUnaryNode() { value = new Double( 0); children = new Vector(); children.setSize(1); } /* parameterized constructor */ public GPUnaryNode(Object target,GPPair gpp) { this.target = target; this.method = gpp.name; this.sign = gpp.sign; children = new Vector(); children.setSize(1); /* try to get the method */ try{ Class [] parameters = new Class[] { (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 GPUnaryNode(GPUnaryNode gpu) { children = gpu.children; target = gpu.target; method = gpu.method; sign = gpu.sign; value = gpu.value; /* try to get the method */ try{ Class [] parameters = new Class[] { (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() { GPUnaryNode gpu = new GPUnaryNode(); gpu.target = target; gpu.method = method; gpu.sign = sign; gpu.value= value; gpu.setChild(0,getChild(0).copy()); /* try to get the method */ try{ Class [] parameters = new Class[] { (new Double(0)).getClass() }; Class c = target.getClass(); gpu.methodObject =c.getMethod(method,parameters); } catch(NoSuchMethodException e) { System.out.println(e.toString() + " "+ method + " " + toString()); } return gpu; } /* 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()}; /* call the method using the child value as the argument */ 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 + " " + toString()); throw new NoSuchMethodException(); } return value; } /* unary naode, return 1 */ public int childCount() { return 1; } /* returns the child */ public GPNode getChild(int i) { return (GPNode)children.get(i); } /* sets the child */ public void setChild(int i,GPNode gpn) { children.set(i,gpn); } /* finds the ith node in the subtree */ public GPNode find(int i) { if (i == count()) { return this;} return ((GPNode)children.get(0)).find(i); } /* sets the descendent with given nodenumber */ public void setDescendant(int number, GPNode gpn) { /* if this nodes child is the node to be replaced then replace */ if(number == ((GPNode)children.get(0)).nodeNumber()) {children.set(0,gpn);} /* else descend into the tree further */ else {((GPNode)children.get(0)).setDescendant(number,gpn);} } /* returns this nodes node number */ public int nodeNumber() { return count(); } /* returns the size of the tree */ public int count() { return ((GPNode)(children.get(0))).count() +1; } /* prints the function rooted at this node */ public String toString() { return sign+"("+((GPNode)(children.get(0))).toString()+")"; } /* return UNARY */ public int getType() { return UNARY; } }