view app/models/TPGraph.java @ 47:9645bfb49603

modified computeAndUpdateStatus
author one
date Thu, 04 Oct 2012 08:55:13 +0900
parents 1d5c086e069b
children d6c623e92837
line wrap: on
line source

package models;

import java.util.ArrayList;
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 Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) {
		Vertex userVertex = graph.getVertex(userName);
		if (userVertex == null) { 
			return false;
		}
		Edge edge = setLabel(claim.getVertex(), userVertex, label);
		edge.setProperty(NodeModel.STATUS, status);
		return true;
	}
	
	public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) {	
		for (String userName: users) {
			Boolean createFlag = setLabelStatusToUser(claim, userName, label, status);
			if (!createFlag) {
				return false;
			}
		}
		return true;
	}
	
	public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) {
		return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label);		
	}
	
	public Object[] searchAllUser() {
		Vertex userRootVertex = graph.getVertex(getUserRootId());
		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();		
		pipe.start(userRootVertex).out(CHILD);
		ArrayList<Object> userArray = new ArrayList<Object>();
		for (Vertex userVertex : pipe) {
			userArray.add(userVertex.getId());
		}
		if (userArray.size() == 0) {
			return null;
		}
		return userArray.toArray();
	}
	
	public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) {
		GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>();		
		pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST);
		ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>();
		for (Edge e : pipeEdge) {
			GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>();		
			pipeUserVertex.start(e).inV();
			Vertex userVertex = pipeUserVertex.next();
			if (userSet.contains(userVertex.getId())) {
				deleteEdgeArray.add(e);
			}			
		}
		for (Edge e : deleteEdgeArray) {
			graph.removeEdge(e);			
		}
		return true;
	}
	
	public Object[] checkConsensus(HashSet<Object> set) {
		Iterator<Object> iter = set.iterator();
		while (iter.hasNext()) {
			Object childId = iter.next();
			ArrayList<Object> array = getAllUpperVertex(childId);
			for (Object parentId: array.toArray()) {
				if (set.contains(parentId)) {
					if (set.contains(childId)) {
						// This behavior is anxiety.
						set.remove(childId);
						iter = set.iterator();
					}
					childId = parentId;	
				}
			}
		}
		return set.toArray();
	}
	
	
	public ArrayList<Object> getAllUpperVertex(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);
		ArrayList<Object> vertexArray = new ArrayList<Object>();
		while (pipe.hasNext()) {
			Object e = pipe.next().getId();
			vertexArray.add(e);
			pipe.start(graph.getVertex(e)).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION);
		}
		return vertexArray;
	}
	
	public Object getOneUpperVertex(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();
	}

}