// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package nosql.orientdb.graph;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;

public class TestGX {
	private static String vstring(Vertex v) {
		return String.format("%d (%s)", v.getProperty("id"), v.getProperty("text"));
	}
	private static void showConnects(OrientGraph db, Vertex v) {
		System.out.printf("Node %s is connected to:\n", vstring(v));
		Iterable<Vertex> rs = db.command(new OCommandSQL(String.format("SELECT EXPAND(BOTH('E')) FROM %s", v.getId()))).execute();
		for(Vertex vc : rs) {
			System.out.printf("  Node %s\n", vstring(vc));
		}
	}
	private static void showConnects(OrientGraph db, int id) {
		Iterable<Vertex> rs = db.command(new OCommandSQL(String.format("SELECT FROM V WHERE id = %d", id))).execute();
		Vertex v = rs.iterator().next();
		showConnects(db, v);
	}
	private static void walk(OrientGraph db, Vertex v) {
		Iterable<Vertex> rs = db.command(new OCommandSQL(String.format("TRAVERSE out() FROM %s", v.getId()))).execute();
        System.out.print("Walk: ");
        boolean first = true;
        for(Vertex vc : rs) {
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	System.out.print(vstring(vc));
        }
        System.out.println();
	}
	private static void walk(OrientGraph db, int id) {
		Iterable<Vertex> rs = db.command(new OCommandSQL(String.format("SELECT FROM V WHERE id = %d", id))).execute();
		Vertex v = rs.iterator().next();
		walk(db, v);
	}
	private static void findWay(OrientGraph db, Vertex vfrom, Vertex vto) {
        Iterable<Vertex> rs = db.command(new OCommandSQL(String.format("SELECT SHORTESTPATH(%s,%s) AS path UNWIND path", vfrom.getId(), vto.getId()))).execute();
        System.out.print("Find: ");
        boolean first = true;
        for(Vertex v : rs) {
        	Vertex vc = v.getProperty("path");
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	System.out.print(vstring(vc));
        }
        System.out.println();
	}
	public static void main(String[] args) {
        // open
    	OrientGraph db = new OrientGraph("remote:192.168.60.129/testg", "admin", "hemmeligt");
db.command(new OCommandSQL("DELETE EDGE E")).execute();
db.command(new OCommandSQL("DELETE VERTEX V")).execute();
    	// put
		Vertex v1 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 1, "This is V1"))).execute();
		Vertex v2 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 2, "This is V2"))).execute();
		Vertex v3 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 3, "This is V3"))).execute();
		Vertex v4 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 4, "This is V4"))).execute();
		Vertex v5 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 5, "This is V5"))).execute();
		Vertex v6 = db.command(new OCommandSQL(String.format("CREATE VERTEX V SET id = %d, text = '%s'", 6, "This is V6"))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v1.getId(), v2.getId()))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v2.getId(), v3.getId()))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v2.getId(), v4.getId()))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v3.getId(), v5.getId()))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v3.getId(), v6.getId()))).execute();
    	db.command(new OCommandSQL(String.format("CREATE EDGE E FROM %s TO %s", v5.getId(), v6.getId()))).execute();
    	// query connections
    	showConnects(db, v1);
    	showConnects(db, v2);
    	showConnects(db, v3);
    	showConnects(db, v4);
    	showConnects(db, v5);
    	showConnects(db, v6);
    	showConnects(db, 1);
    	showConnects(db, 2);
    	showConnects(db, 3);
    	showConnects(db, 4);
    	showConnects(db, 5);
    	showConnects(db, 6);
    	// traverse
    	walk(db, v1);
    	walk(db, v2);
    	walk(db, v3);
    	walk(db, v4);
    	walk(db, v5);
    	walk(db, v6);
    	walk(db, 1);
    	walk(db, 2);
    	walk(db, 3);
    	walk(db, 4);
    	walk(db, 5);
    	walk(db, 6);
    	// shortest path
    	findWay(db, v1, v2);
    	findWay(db, v1, v3);
    	findWay(db, v1, v4);
    	findWay(db, v1, v5);
    	findWay(db, v1, v6);
	}
}
