view app/models/TPGraph.java @ 25:97b249d9fad1

fix infinite loop on getConsensus Action
author one
date Wed, 03 Oct 2012 03:40:51 +0900
parents 81d1d7c7bcde
children ad7cfb1f9d03
line wrap: on
line source

package models;

import java.util.HashSet;
import java.util.Iterator;

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.gremlin.java.GremlinPipeline;

public class TPGraph {

	private static TPGraph instance = new TPGraph();
	private Object claimRootId;
	private Object userRootId;
	
	/*
	 *  Edge type
	 */
	protected final String CHILD = "child";
	
	
	private TPGraph() {
		
	}
	
	public static TPGraph getInstance() {
		return instance;
	}

	private Graph graph; 
	private String path = null;
	
	public void setPath(String path) {
		this.path = path;
	}

	public Graph newGraph() {
		if (path == null) {
			graph = new TinkerGraph();
		} else {
			graph = new TinkerGraph(path);
			
		}
		return graph;
	}

	public Graph getGraph() {
		return graph;
	}
	
	public void setClaimRootId(Object id) {
		this.claimRootId = id;
	}
	
	public void setUserRootId(Object id) {
		this.userRootId = id;
	}
	
	public Object getClaimRootId() {
		return claimRootId;
	}

	public Object getUserRootId() {
		return userRootId;
	}

	public Vertex getClaimRootVertex() {
		return graph.getVertex(claimRootId);
	}
	
	public Vertex getUserRootVertex() {
		return graph.getVertex(userRootId);
	}

	private Edge setLabel(Vertex fromV, Vertex toV, String label) {
		return graph.addEdge(null, fromV, toV, label);		
	}
	
	public Edge setLabelToRootUser(UserModel user) {
		Vertex rootUser = getUserRootVertex(); 

		//  rootUser ---child---> newUser
		return setLabel(rootUser, user.getVertex(), CHILD);
	}

	public Edge setLabelToRootClaim(ClaimModel claim) {
		Vertex rootClaim = getClaimRootVertex(); 

		//  rootUser ---child---> newUser
		return setLabel(rootClaim, claim.getVertex(), CHILD);
	}
	
	public Edge setLabelToAuthor(ClaimModel claim, String author) {
		Vertex authorVertex = graph.getVertex(author);

		//  claim ---author---> authorVertex(userVertex)
		return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR);
	}
	
	public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) {
		for (String userName: users) {
			Vertex userVertex = graph.getVertex(userName);
			if (userVertex == null) return false;
			setLabel(claim.getVertex(), userVertex, label);
		}
		return true;
	}
	
	public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) {
		return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label);		
	}
	
	public Object[] checkConsensus(HashSet<Object> set) {
		Iterator<Object> iter = set.iterator();
		while(iter.hasNext()) {
			Object childId = iter.next();
			System.out.println("childId = "+childId);
			Object parentId;
			while ( (parentId = checkUpperVertex(childId)) != null){
				if (set.contains(parentId)) {
					set.remove(childId);
					iter = set.iterator();
					childId = parentId;
					continue;
				}
				childId = parentId;
			}
		}
		return set.toArray();
	}
	
	public Object checkUpperVertex(Object id) {
		Vertex startV = graph.getVertex(id);
		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
		pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION);
		if (pipe.hasNext()) { 
			Vertex v = pipe.next();
			return v.getId();
		} else {
			return null;
		}
	}
	
	
	public void shutdownGraph() {
		graph.shutdown();
	}

}