/*
 * Encog(tm) Core v3.2 - Java Version
 * http://www.heatonresearch.com/encog/
 * https://github.com/encog/encog-java-core
 
 * Copyright 2008-2013 Heaton Research, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *   
 * For more information on Heaton Research copyrights, licenses 
 * and trademarks visit:
 * http://www.heatonresearch.com/copyright
 */
package org.encog.ml.tree.traverse.tasks;

import org.encog.ml.prg.ProgramNode;
import org.encog.ml.tree.TreeNode;
import org.encog.ml.tree.traverse.DepthFirstTraversal;
import org.encog.ml.tree.traverse.TreeTraversalTask;

/**
 * Task to replace a node.
 */
public class TaskReplaceNode implements TreeTraversalTask {
	
	/**
	 * Node to replace.
	 */
	private final ProgramNode replaceThisNode; 
	
	/**
	 * Node to replace with.
	 */
	private final ProgramNode replaceWith;
	
	/**
	 * Are we done?
	 */
	private boolean done;
	
	public TaskReplaceNode(ProgramNode theReplaceThisNode, ProgramNode theReplaceWith) {
		this.replaceThisNode = theReplaceThisNode;
		this.replaceWith = theReplaceWith;
		this.done = false;
	}
		
	/**
	 * @return The node to replace.
	 */
	public ProgramNode getReplaceThisNode() {
		return replaceThisNode;
	}

	/**
	 * @return What to replace with.
	 */
	public ProgramNode getReplaceWith() {
		return replaceWith;
	}


	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean task(TreeNode node) {
		if( done ) {
			return false;
		}
		
		for(int i=0;i<node.getChildNodes().size();i++) {
			TreeNode childNode = node.getChildNodes().get(i);
			if( childNode==replaceThisNode) {
				node.getChildNodes().set(i, this.replaceWith);
				done = true;
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Replace one node with another.
	 * @param rootNode The root node.
	 * @param replaceThisNode The node to replace.
	 * @param replaceWith What to replace with.
	 */
	public static void process(ProgramNode rootNode, ProgramNode replaceThisNode, ProgramNode replaceWith) {
		TaskReplaceNode task = new TaskReplaceNode(replaceThisNode, replaceWith);
		DepthFirstTraversal trav = new DepthFirstTraversal();
		trav.traverse(rootNode, task);
	}

}

@@ -938,46 +938,8 @@
 %0D%0A%0D%0A
-import org.encog.ml.prg.ProgramNode;%0D%0A
 impo
@@ -970,16 +970,16 @@
 eNode;%0D%0A
+
 import o
@@ -1225,39 +1225,36 @@
 %0A%09private final 
-Program
+Tree
 Node replaceThis
@@ -1318,23 +1318,20 @@
 e final 
-Program
+Tree
 Node rep
@@ -1425,23 +1425,20 @@
 aceNode(
-Program
+Tree
 Node the
@@ -1450,31 +1450,28 @@
 ceThisNode, 
-Program
+Tree
 Node theRepl
@@ -1642,39 +1642,36 @@
 %0D%0A%09 */%0D%0A%09public 
-Program
+Tree
 Node getReplaceT
@@ -1771,23 +1771,20 @@
 %09public 
-Program
+Tree
 Node get
@@ -2430,23 +2430,20 @@
 process(
-Program
+Tree
 Node roo
@@ -2445,31 +2445,28 @@
 e rootNode, 
-Program
+Tree
 Node replace
@@ -2479,15 +2479,12 @@
 de, 
-Program
+Tree
 Node

Use tree node