package kr.ac.kaist.lilliput.TitanUtility; import java.util.Iterator; import java.util.Scanner; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import com.thinkaurelius.titan.core.TitanFactory; import com.thinkaurelius.titan.core.TitanGraph; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; /** * Hello world! * */ public class App { public static void removeAllVertices(TitanGraph g) { int vc = 0; int ec = 0; Iterator<Vertex> iter = g.getVertices().iterator(); while( iter.hasNext() ) { Vertex tv = iter.next(); g.removeVertex(tv); vc++; } Iterator<Edge> iter2 = g.getEdges().iterator(); while( iter2.hasNext() ) { Edge te = iter2.next(); g.removeEdge(te); ec++; } System.out.println("[Titan/Cassandra Utility] Total " + vc + " vertices will be removed "); System.out.println("[Titan/Cassandra Utility] Total " + ec + " edges will be removed "); g.commit(); System.out.println("[Titan/Cassandra Utility] Changes are committed "); } public static void showAllVertices(TitanGraph g) { Iterator<Vertex> iter = g.getVertices().iterator(); while( iter.hasNext()) { Vertex tv = iter.next(); Iterator<String> iter2 = tv.getPropertyKeys().iterator(); while( iter2.hasNext() ) { String key = iter2.next(); System.out.print(key + " : " + tv.getProperty(key) + "\t"); } System.out.println(); } } public static void showAllEdges(TitanGraph g) { Iterator<Edge> iter = g.getEdges().iterator(); while( iter.hasNext()) { Edge edge = iter.next(); String outName = ""; String inName = ""; if( edge.getVertex(Direction.OUT).getProperty("name") != null ) { outName = edge.getVertex(Direction.OUT).getProperty("name"); } else if( edge.getVertex(Direction.OUT).getProperty("android_id") != null ) { outName = edge.getVertex(Direction.OUT).getProperty("android_id"); } else if( edge.getVertex(Direction.OUT).getProperty("minLon") != null) { outName = edge.getVertex(Direction.OUT).getProperty("minLon") + "," + edge.getVertex(Direction.OUT).getProperty("minLat") ; } else if( edge.getVertex(Direction.OUT).getProperty("longitude") != null) { outName = edge.getVertex(Direction.OUT).getProperty("longitude") + "," + edge.getVertex(Direction.OUT).getProperty("latitude") ; } if( edge.getVertex(Direction.IN).getProperty("name") != null ) { inName = edge.getVertex(Direction.IN).getProperty("name"); } else if( edge.getVertex(Direction.IN).getProperty("android_id") != null ) { inName = edge.getVertex(Direction.IN).getProperty("android_id"); } else if( edge.getVertex(Direction.IN).getProperty("minLon") != null) { inName = edge.getVertex(Direction.IN).getProperty("minLon") + "," + edge.getVertex(Direction.IN).getProperty("minLat") ; } else if( edge.getVertex(Direction.IN).getProperty("longitude") != null) { inName = edge.getVertex(Direction.IN).getProperty("longitude") + "," + edge.getVertex(Direction.IN).getProperty("latitude") ; } System.out.println(outName + " " + edge.getLabel() + " " + inName); } } public static void showAllObjects(TitanGraph g) { Iterator<Vertex> iter = g.getVertices().iterator(); while( iter.hasNext() ) { Vertex tv = iter.next(); String ltype = tv.getProperty("ltype"); if( ltype != null ) { if( ltype.equals("Object")) { Iterator<String> iter2 = tv.getPropertyKeys().iterator(); while( iter2.hasNext() ) { String key = iter2.next(); System.out.print(key + " : " + tv.getProperty(key) + "\t"); } System.out.println(); } } } } public static void showAllPlaces(TitanGraph g) { Iterator<Vertex> iter = g.getVertices().iterator(); while( iter.hasNext() ) { Vertex tv = iter.next(); String ltype = tv.getProperty("ltype"); if( ltype != null ) { if( ltype.contains("Place") ) { Iterator<String> iter2 = tv.getPropertyKeys().iterator(); while( iter2.hasNext() ) { String key = iter2.next(); System.out.print(key + " : " + tv.getProperty(key) + "\t"); } System.out.println(); } } } } @SuppressWarnings("resource") public static void main( String[] args ) { Scanner sc = new Scanner(System.in); Configuration conf = new BaseConfiguration(); conf.setProperty("storage.backend", "cassandra"); conf.setProperty("storage.hostname", "127.0.0.1"); TitanGraph g = TitanFactory.open(conf); while(true) { System.out.println( "Titan/Cassandra Utilities: 1. Remove All Graph, 2. Show All Vertices, 3. Show All Edges, 4. Show All Objects, 5. Show All Places, e. exit" ); String str = sc.next(); if( str.equals("1")) { removeAllVertices(g); } else if( str.equals("2")) { showAllVertices(g); } else if( str.equals("3")) { showAllEdges(g); } else if( str.equals("4")) { showAllObjects(g); } else if( str.equals("5")) { showAllPlaces(g); } else if( str.equals("e")) { g.shutdown(); System.out.println("[Titan/Cassandra Utility] Good Bye "); break; } } } }