// 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.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;


public class TestG {
	private static String vstring(Vertex v) {
		return String.format("%d (%s)", v.getProperty("xid"), v.getProperty("text"));
	}
	private static void showConnects(Vertex v) {
		System.out.printf("Node %s is connected to:\n", vstring(v));
		for(Vertex vc : v.getVertices(Direction.BOTH)) {
			System.out.printf("  Node %s\n", vstring(vc));
		}
	}
	private static void showConnects(OrientGraph db, int xid) {
		Vertex v = (Vertex)db.getVertices("xid", xid).iterator().next();
		showConnects(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 xid) {
		Vertex v = (Vertex)db.getVertices("xid", xid).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 vc : rs) {
			Vertex vc2 = (Vertex)vc.getProperty("path");
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	System.out.print(vstring(vc2));
        }
        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.addVertex("V");
    	v1.setProperty("xid", 1);
    	v1.setProperty("text", "This is V1");
    	db.commit();
    	Vertex v2 = db.addVertex("V");
    	v2.setProperty("xid", 2);
    	v2.setProperty("text", "This is V2");
    	db.commit();
    	Vertex v3 = db.addVertex("V");
    	v3.setProperty("xid", 3);
    	v3.setProperty("text", "This is V3");
    	db.commit();
    	Vertex v4 = db.addVertex("V");
    	v4.setProperty("xid", 4);
    	v4.setProperty("text", "This is V4");
    	db.commit();
    	Vertex v5 = db.addVertex("V");
    	v5.setProperty("xid", 5);
    	v5.setProperty("text", "This is V5");
    	db.commit();
    	Vertex v6 = db.addVertex("V");
    	v6.setProperty("xid", 6);
    	v6.setProperty("text", "This is V6");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e12 = db.addEdge("class:E", v1, v2, "e12");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e23 = db.addEdge("class:E", v2, v3, "e23");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e24 = db.addEdge("class:E", v2, v4, "e24");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e35 = db.addEdge("class:E", v3, v5, "e35");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e36 = db.addEdge("class:E", v3, v6, "e36");
    	db.commit();
    	@SuppressWarnings("unused")
		Edge e56 = db.addEdge("class:E", v5, v6, "e56");
    	db.commit();
    	// query connections
    	showConnects(v1);
    	showConnects(v2);
    	showConnects(v3);
    	showConnects(v4);
    	showConnects(v5);
    	showConnects(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);
	}
}
