/** * Obtain a new Node from a type that matches the given string. It will try to correctly detect * RealVar and constant nodes. The Node is cloned and initialized, so it can be used separatedly * * @param name The type of the requested Node. It must coincide with one of the class names of * available Nodes, unless it represents a variable or constant terminal * @param currentGlobalDepth The depth of the requested Node in the Tree */ public Node newNode(String name, int currentGlobalDepth) { System.out.println(name); Node outNode = null; if (name.startsWith("X")) { outNode = new RealVar(Integer.parseInt(name.substring("X".length(), name.length()))); } else if (name.startsWith("angle")) { outNode = new Angle(Integer.parseInt(name.substring("angle".length(), name.length()))); } else if (name.substring(0, 1).matches("\\d")) { outNode = new RealConstant(Double.parseDouble(name)); } else if (name.equals("true")) { outNode = new LogicConstant(1); } else if (name.equals("false")) { outNode = new LogicConstant(0); } else { List<Node> all = new ArrayList<Node>(); for (int i = 0; i < nodeSets.length; i++) { all.addAll(nodeSets[i].getAll()); } for (Node n : all) { if (name.equals(n.name())) { try { outNode = (Node) n.clone(); } catch (CloneNotSupportedException e) { Logger.log(e); } break; } } } outNode.setCurrentDepth(currentGlobalDepth); return outNode; }
/** * Obtain a new Node randomly chosen from the given list. The Node is cloned and initialized, so * it can be used separatedly * * @param l The list of Nodes from which to choose * @param currentGlobalDepth The depth of the requested Node in the Tree */ public Node newRandomNode(List<Node> l, int currentGlobalDepth) { int which = Common.globalRandom.nextInt(l.size()); Node outNode = null; try { outNode = (Node) l.get(which).clone(); } /* This should never happen, as nodes do support cloning, so it's ok * to catch this exception here */ catch (CloneNotSupportedException e) { Logger.log(e); } outNode.init(config, problemData); outNode.setCurrentDepth(currentGlobalDepth); return outNode; }