/*
 *  Copyright (C) 2011-2012 The Animo Project
 *  http://animotron.org
 *  	
 *  This file is part of Animotron.
 *  
 *  Animotron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *  
 *  Animotron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of 
 *  the GNU Affero General Public License along with Animotron.  
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package org.animotron.graph.traverser;

import org.animotron.graph.handler.AnimoGraphHandler;
import org.animotron.graph.handler.GraphHandler;
import org.animotron.io.PipeIterator;
import org.animotron.manipulator.Evaluator;
import org.animotron.manipulator.QCAVector;
import org.animotron.statement.Statement;
import org.animotron.statement.Statements;
import org.animotron.statement.operator.*;
import org.neo4j.graphdb.Relationship;

import java.io.IOException;
import java.util.Iterator;

/**
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 * @author <a href="mailto:gazdovskyd@gmail.com">Evgeny Gazdovsky</a>
 * 
 */
public class AnimoResultOneStepTraverser extends ResultTraverser {
	
    public AnimoResultOneStepTraverser() {}

    @Override
    protected void process(GraphHandler handler, Statement s, Statement parent, QCAVector rr, int level, boolean isOne, int pos, boolean isLast, boolean evaluable, Relationship def) throws IOException {
    	Statement qS = Statements.relationshipType(rr.getQuestion());
    	if (qS instanceof Definition && rr.hasAnswer()) {
        	Relationship r = rr.getClosest();
			handler.start(qS, null, rr.getQuestion(), level++, isOne, pos, isLast);
            iterate(handler, rr, s, r, level, evaluable, def);
            handler.end(qS, null, rr.getQuestion(), --level, isOne, pos, isLast);
        } else if (s != null) {
			//avoid cycling
			if (rr.hasAnswer() && rr.getAnswer().equals(rr.getQuestion()))
				evaluable = false;
			if (evaluable && s instanceof Evaluable  && !handler.isStepMade() ) {
                result(handler, rr, level, isOne, pos, isLast, def);
            } else {
				Relationship r = rr.getClosest();
				handler.start(s, parent, r, level++, isOne, pos, isLast);
                if (!(s instanceof REF && !(qS instanceof Reference || qS instanceof Definition))) {
                    iterate(s, handler, parent, rr, r, level, pos, evaluable,def);
                }
                handler.end(s, parent, r, --level, isOne, pos, isLast);
            }
        }
    }

    protected void result(GraphHandler handler, QCAVector rr, int level, boolean isOne, int pos, boolean isLast, Relationship def) throws IOException {
    	Relationship r = rr.getClosest();
    	
        Iterator<QCAVector> in = 
        		new PipeIterator( 
        				Evaluator._.execute(handler.getController(), rr.question(r), null, false) );
        
        iterate(handler, null, rr, in, level, isOne, pos, isLast, true, def);
    }
}
@@ -1495,39 +1495,8 @@
 nds 
-ResultTraverser %7B%0A%09%0A    public 
 Anim
@@ -1494,39 +1494,32 @@
 ends AnimoResult
-OneStep
 Traverser() %7B%7D%0A%0A
@@ -1515,1322 +1515,55 @@
 rser
-() %7B%7D%0A%0A    @Override%0A    protected void process(GraphHandler handler, Statement s, Statement parent, QCAVector rr, int level, boolean isOne, int pos, boolean isLast, boolean evaluable, Relationship def) throws IOException %7B%0A    %09Statement qS = Statements.relationshipType(rr.getQuestion());%0A    %09if (qS instanceof Definition && rr.hasAnswer()) %7B%0A        %09Relationship r = rr.getClosest();%0A%09%09%09handler.start(qS, null, rr.getQuestion(), level++, isOne, pos, isLast);%0A            iterate(handler, rr, s, r, level, evaluable, def);%0A            handler.end(qS, null, rr.getQuestion(), --level, isOne, pos, isLast);%0A        %7D else if (s != null) %7B%0A%09%09%09//avoid cycling%0A%09%09%09if (rr.hasAnswer() && rr.getAnswer().equals(rr.getQuestion()))%0A%09%09%09%09evaluable = false;%0A%09%09%09if (evaluable && s instanceof Evaluable  && !handler.isStepMade() ) %7B%0A                result(handler, rr, level, isOne, pos, isLast, def);%0A            %7D else %7B%0A%09%09%09%09Relationship r = rr.getClosest();%0A%09%09%09%09handler.start(s, parent, r, level++, isOne, pos, isLast);%0A                if (!(s instanceof REF && !(qS instanceof Reference %7C%7C qS instanceof Definition))) %7B%0A                    iterate(s, handler, parent, rr, r, level, pos, evaluable,def);%0A                %7D%0A                handler.end(s, parent, r, --level, isOne, pos, isLast);%0A            %7D%0A        %7D%0A    
+ %7B%0A%09%0A    public AnimoResultOneStepTraverser() %7B
 %7D%0A%0A 

Revert "bugfix?"