view src/pagerank/TPReadWikiLink.java @ 15:e1d758d08e9c draft

modify id in WikiPage.java
author one
date Sat, 08 Sep 2012 06:01:00 +0900
parents 86567db31710
children 4ed8a8a5ef92
line wrap: on
line source

package pagerank;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;

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;

import pagerank.WikiPage;

public class TPReadWikiLink {

	public static void main(String[] args) {

//		final String fileDB = "./resources/tinkerpopDB";

		final long PAGENUM = 22; 
		final String fileDB = "./resources/tinkerGraph"+Long.toString(PAGENUM);	
		final String pageRankLog = "./resources/wikiPageRank"+Long.toString(PAGENUM)+".log";	
		
		try {
			Graph graph = new TinkerGraph();
			FileInputStream in = new FileInputStream(new File(fileDB));
			GraphMLReader.inputGraph(graph, in);
			in.close();
			LinkToVertex ltv = new LinkToVertex(graph);

			ltv.initPageRankAllVertex();

			final long AllVertexNumber = ltv.searchAllVertices();
			HashMap<String, WikiPage> wikiHash = ltv.getWikiPageHash();
			System.out.println("AllVertexNumber = "+AllVertexNumber);


			Object vertexIds[] = {80, 79, 20, 28};
			

			long start = java.lang.System.currentTimeMillis();
			writeComputeTransition(ltv, vertexIds, 11, PAGENUM);
			long end = java.lang.System.currentTimeMillis();
			long time = end - start;
			System.out.println(time);

			/*
			for (int i=0; i<10; i++ ) {
			long start = java.lang.System.currentTimeMillis();
			for (int j=0; j<10; j++){
				for (Vertex v : graph.getVertices()) {
					ltv.computePageRankUsingPipes(v.getId());
				}
			}
			long end = java.lang.System.currentTimeMillis();
			long time = end - start;
			System.out.println(time);
			}
*/			
			

			FileOutputStream fos = new FileOutputStream(new File(pageRankLog));
			descendingOrder(graph, ltv, fos);			
/*			
			FileOutputStream out = new FileOutputStream(new File(fileDB));
			GraphMLWriter.outputGraph(graph, out);
			out.close();
*/			
//			loop(ltv);
			
		} catch (NumberFormatException e){
			System.out.println("Program exit");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

	public static void loop(LinkToVertex ltv) throws IOException {
		BufferedReader r = new BufferedReader( new InputStreamReader(System.in), 1);
		System.out.print("\nPlease enter Node Id...>");
		System.out.flush();
		String s;
		while ((s = r.readLine()) != null ) {
			int nodeId = Integer.parseInt(s);
			ltv.printVertexInfo(nodeId);
			System.out.print("\nPlease enter Node Id...>");
			System.out.flush();
		}
	}

	public static void writeComputeTransition(LinkToVertex ltv,final Object[] vertexIds, int count, long pagenum) throws IOException {
		LinkedList<FileOutputStream> fosList = new LinkedList<FileOutputStream>();
		for (Object id: vertexIds) {
			String filename = "./resources/VertexId_"+id+"_num"+Long.toString(pagenum)+".dat";
			FileOutputStream fos = null;
			fos = new FileOutputStream(filename);

			Vertex v = ltv.getVertexById(id);
			fos.write( ("# Vertex ID "+id+" "+ ltv.getPageTitle(v)+"\n").getBytes());
			fosList.add(fos);
		}
		
		for (int i=0; i<count; i++) {
			for (Vertex v : ltv.getAllVertices() ) {
//				ltv.computePageRank(v);
				ltv.computePageRankUsingPipes(v.getId());
			}

			for (int index=0; index< vertexIds.length; index++){
				FileOutputStream fos = fosList.get(index);
				printPageRankLog(fos, ltv, vertexIds[index], i);
			}
		}
		for (FileOutputStream fos: fosList) {
			fos.close();
		}
		
	}
	
	// Write PageRank in descending order to fos.
	public static void descendingOrder(HashMap<String, WikiPage> wikiHash , FileOutputStream fos) throws IOException {
		ArrayList<WikiPage> list = new ArrayList<WikiPage>();
		for (String title : wikiHash.keySet()) {
			WikiPage w = wikiHash.get(title);
			list.add(w);
		}
		Collections.sort(list, new Comparator<WikiPage>(){
				public int compare(WikiPage w1, WikiPage w2) {
				return (int)(w2.getRank()*Math.pow(10, 5)) - (int)(w1.getRank()*Math.pow(10, 5));
				}
		});
		
		long count = 1;
		for (WikiPage w : list) {
			fos.write(("No."+count+"\n").getBytes());
			w.printInfo(fos);
			count++;
		}
		fos.close();
		
	}

	public static void descendingOrder(Graph graph, LinkToVertex ltv, FileOutputStream fos) throws IOException {
		ArrayList<WikiPage> list = new ArrayList<WikiPage>();
		for (Vertex v : graph.getVertices()) {
			WikiPage w = new WikiPage(v);
			w.setInHasLink(ltv.computeInHasLink(v));
			w.setOutHasLink(ltv.computeOutHasLink(v));
			list.add(w);
		}
		Collections.sort(list, new Comparator<WikiPage>(){
				public int compare(WikiPage w1, WikiPage w2) {
				return (int)(w2.getRank()*Math.pow(10, 5)) - (int)(w1.getRank()*Math.pow(10, 5));
				}
		});
		
		long count = 1;
		for (WikiPage w : list) {
			fos.write(("No."+count+"\n").getBytes());
			w.printInfo(fos);
			count++;
		}
		fos.close();
		
	}
	
	public static void printPageRankLog(FileOutputStream fos, int x, double rank) throws IOException {
		fos.write( (x+" "+ rank+"\n").getBytes() );		
		fos.flush();
	}
	
	public static void printPageRankLog(FileOutputStream fos, LinkToVertex ltv, Object id, int x) throws IOException {
		double rank = ltv.getPageRank(ltv.getVertexById(id));
		fos.write( (x+" "+ rank+"\n").getBytes() );		
		fos.flush();
	}

}