// 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.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.id.ORID;

public class TestGX {
	private static String vstring(OVertex v) {
		return String.format("%d (%s)", v.getProperty("id"), v.getProperty("text"));
	}
	private static void showConnects(ODatabaseSession db, OVertex v) {
		System.out.printf("Node %s is connected to:\n", vstring(v));
		OResultSet rs = db.query("SELECT EXPAND(BOTH('E')) FROM ?", v.getIdentity());
		while(rs.hasNext()) {
			OVertex vc = rs.next().getVertex().get();
			System.out.printf("  Node %s\n", vstring(vc));
		}
	}
	private static void showConnects(ODatabaseSession db, int id) {
		OVertex v = (OVertex)db.query("SELECT FROM V WHERE id = ?", id).next().getRecord().get();
		showConnects(db, v);
	}
	private static void walk(ODatabaseSession db, OVertex v) {
        OResultSet rs = db.query("TRAVERSE out() FROM " + v.getIdentity());
        System.out.print("Walk: ");
        boolean first = true;
        while(rs.hasNext()) {
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	OVertex vc = (OVertex)rs.next().getRecord().get();
        	System.out.print(vstring(vc));
        }
        System.out.println();
    	rs.close();
	}
	private static void walk(ODatabaseSession db, int id) {
		OVertex v = (OVertex)db.query("SELECT FROM V WHERE id = ?", id).next().getRecord().get();
		walk(db, v);
	}
	private static void findWay(ODatabaseSession db, OVertex vfrom, OVertex vto) {
        OResultSet rs = db.query("SELECT SHORTESTPATH(?,?) AS path UNWIND path", vfrom.getIdentity(), vto.getIdentity());
        System.out.print("Find: ");
        boolean first = true;
        while(rs.hasNext()) {
        	OResult r = rs.next();
        	ORID z = r.getProperty("path");
        	OVertex vc = db.load(z);
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	System.out.print(vstring(vc));
        }
    	rs.close();
        System.out.println();
	}
	public static void main(String[] args) {
        // open
    	OrientDB client = new OrientDB("remote:localhost", null);
    	ODatabaseSession db = client.open("testg", "admin", "hemmeligt");
db.command("DELETE EDGE E");
db.command("DELETE VERTEX V");
    	// put
		OVertex v1 = db.command("CREATE VERTEX V SET id = ?, text = ?", 1, "This is V1").next().getVertex().get();
		OVertex v2 = db.command("CREATE VERTEX V SET id = ?, text = ?", 2, "This is V2").next().getVertex().get();
		OVertex v3 = db.command("CREATE VERTEX V SET id = ?, text = ?", 3, "This is V3").next().getVertex().get();
		OVertex v4 = db.command("CREATE VERTEX V SET id = ?, text = ?", 4, "This is V4").next().getVertex().get();
		OVertex v5 = db.command("CREATE VERTEX V SET id = ?, text = ?", 5, "This is V5").next().getVertex().get();
		OVertex v6 = db.command("CREATE VERTEX V SET id = ?, text = ?", 6, "This is V6").next().getVertex().get();
    	@SuppressWarnings("unused")
		OEdge e12 = db.command("CREATE EDGE E FROM ? TO ?", v1.getIdentity(), v2.getIdentity()).next().getEdge().get();
    	@SuppressWarnings("unused")
    	OEdge e23 = db.command("CREATE EDGE E FROM ? TO ?", v2.getIdentity(), v3.getIdentity()).next().getEdge().get();
    	@SuppressWarnings("unused")
    	OEdge e24 = db.command("CREATE EDGE E FROM ? TO ?", v2.getIdentity(), v4.getIdentity()).next().getEdge().get();
    	@SuppressWarnings("unused")
    	OEdge e35 = db.command("CREATE EDGE E FROM ? TO ?", v3.getIdentity(), v5.getIdentity()).next().getEdge().get();
    	@SuppressWarnings("unused")
    	OEdge e36 = db.command("CREATE EDGE E FROM ? TO ?", v3.getIdentity(), v6.getIdentity()).next().getEdge().get();
    	@SuppressWarnings("unused")
    	OEdge e56 = db.command("CREATE EDGE E FROM ? TO ?", v5.getIdentity(), v6.getIdentity()).next().getEdge().get();
    	// 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);
        // close
        db.close();
        client.close();
	}

}
