Mercurial > hg > Members > nobuyasu > Consensus
view app/models/TPGraph.java @ 35:5d422941b702
fix
author | one |
---|---|
date | Wed, 03 Oct 2012 18:43:49 +0900 |
parents | 80b5628f17d8 |
children | 5f7fcdf98380 |
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 setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { for (String userName: users) { 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 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(); 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(); } }