/*
 *  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.statement.operator;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;

import static org.neo4j.graphdb.Direction.OUTGOING;

/**
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 * @author <a href="mailto:gazdovsky@gmail.com">Evgeny Gazdovsky</a>
 */
public class ASHIFT extends REF {

	public static final ASHIFT _ = new ASHIFT();

	private ASHIFT() { super("ASHIFT"); }

    public Relationship build(Node def, Node rev) {
        return def.createRelationshipTo(rev, this);
    }

    public Relationship set(Node def, Node rev) {
        def.getSingleRelationship(this, OUTGOING).delete();
        return def.createRelationshipTo(rev, this);
    }

    public Node actualNode(Node node) {
        return node.getSingleRelationship(this, OUTGOING).getEndNode();
    }

    public Node actualNode(Relationship relationship) {
        return actualNode(relationship.getEndNode());
    }

    public Node actualEndNode(Relationship r) {
    	return r.getEndNode();
//    	Node n = r.getEndNode();
//
//		if (r.isType(REF._) || r.isType(DEF._))
//			return actualNode(n);
//
//		return n;
    }

    public Relationship actualRelationship(Relationship r) {
        return r.getEndNode().getSingleRelationship(this, OUTGOING);
    }

    @Override
    public Object reference(Relationship r) {
        return DEF._.reference(r.getStartNode());
    }



}

@@ -910,16 +910,59 @@
 ionship;
+%0Aimport org.neo4j.graphdb.RelationshipType;
 %0A%0Aimport
@@ -1178,19 +1178,35 @@
 IFT 
-extends REF
+implements RelationshipType
  %7B%0A%0A
@@ -1256,268 +1256,48 @@
 );%0A%0A
-%09private ASHIFT() %7B super(%22ASHIFT%22); %7D%0A%0A    public Relationship build(Node def, Node rev) %7B%0A        return def.createRelationshipTo(rev, this);%0A    %7D%0A%0A    public Relationship set(Node def, Node rev) %7B%0A        def.getSingleRelationship(this, OUTGOING).delete();
+    @Override%0A    public String name() %7B
 %0A   
@@ -1312,43 +1312,16 @@
 urn 
-def.createRelationshipTo(rev, this)
+%22ASHIFT%22
 ;%0A  
@@ -1565,468 +1565,6 @@
  %7D%0A%0A
-    public Node actualEndNode(Relationship r) %7B%0A    %09return r.getEndNode();%0A//    %09Node n = r.getEndNode();%0A//%0A//%09%09if (r.isType(REF._) %7C%7C r.isType(DEF._))%0A//%09%09%09return actualNode(n);%0A//%0A//%09%09return n;%0A    %7D%0A%0A    public Relationship actualRelationship(Relationship r) %7B%0A        return r.getEndNode().getSingleRelationship(this, OUTGOING);%0A    %7D%0A%0A    @Override%0A    public Object reference(Relationship r) %7B%0A        return DEF._.reference(r.getStartNode());%0A    %7D%0A%0A%0A%0A
 %7D%0A

refactor ASHIFT