view src/sample/CreateTinkerGraph.java @ 4:dcd59917a2dd draft

fix LinkToVertex
author one
date Wed, 05 Sep 2012 12:44:08 +0900
parents 1744340f8be6
children 7e38484474f4
line wrap: on
line source

package sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import pagerank.LinkToVertex;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader;
import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;

public class CreateTinkerGraph {

	public static final String filename = "./resources/testTinkerpopDB";
	
	public static void main(String[] args) {

		try {

//			outputGraph();
//			readGraph();
			readPageRankDB();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void outputGraph() throws IOException {
		Graph graph = new TinkerGraph();
		FileOutputStream out = new FileOutputStream(new File(filename));

		Vertex a = graph.addVertex(null);
		Vertex b = graph.addVertex(null);
		Vertex c = graph.addVertex(null);
		Vertex d = graph.addVertex(null);
		a.setProperty("name", "maro");
		b.setProperty("name", "Peter");
		c.setProperty("name", "smith");
		d.setProperty("name", "black");
		Edge e = graph.addEdge(null, a, b, "knows");
		Edge e2 = graph.addEdge(null, c, a, "knows");
		Edge e3 = graph.addEdge(null, d, b, "knows");
		System.out.println(e.getVertex(Direction.OUT).getProperty("name")
				+ "--" + e.getLabel() + "-->"
				+ e.getVertex(Direction.IN).getProperty("name"));
		for (Edge edge : b.getEdges(Direction.IN, "knows")) {
			Vertex v =edge.getVertex(Direction.OUT);
			System.out.println(v.getProperty("name"));
		}
		GraphMLWriter.outputGraph(graph, out);
	}

	public static void readGraph() throws IOException {
		Graph graph = new TinkerGraph();
		FileInputStream in = new FileInputStream(new File(filename));
	
		GraphMLReader.inputGraph(graph, in);
		Vertex aa = graph.getVertex("1");
		System.out.println("vertex " + aa.getId() + " has name " + aa.getProperty("name"));
		if (aa.getProperty("aaa") != null)
			System.out.println(aa.getProperty("aaa"));
		for(Edge ee : aa.getEdges(Direction.OUT)) {
		  System.out.println(ee);
		}
		
	}
	
	public static void readPageRankDB() throws IOException {
		final String pageRankDB = "./resources/tinkerpopDB";
		Graph graph = new TinkerGraph();
		FileInputStream in = new FileInputStream(new File(pageRankDB));
		GraphMLReader.inputGraph(graph, in);
		
		LinkToVertex ltn = new LinkToVertex(graph);
		
		System.out.println("print All Vertex ");
		for (Vertex v : graph.getVertices() ) {
			if ( (ltn.getPageTitle(v) == null ) ||
				(ltn.getPageRank(v) == null)) continue;
			String id = (String) v.getId();
			int nodeId = Integer.parseInt(id);
			ltn.printVertexInfo(nodeId);
		}
	}
	
}