Mercurial > hg > Members > nobuyasu > Consensus
view app/models/TPGraph.java @ 76:41b6f3788cc3 draft
TPGraph wrap graph
author | one |
---|---|
date | Fri, 08 Mar 2013 16:18:53 +0900 |
parents | 9448734399db |
children | 4c5b3e990712 |
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; } public static void resetInstance() { instance = new TPGraph(); } private Graph graph = null; 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 Vertex addVertex(Object vId) { return graph.addVertex(vId); } public Vertex getVertex(Object vId) { return graph.getVertex(vId); } public Edge getEdge(Object eId) { return graph.getEdge(eId); } 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 getVertex(claimRootId); } public Vertex getUserRootVertex() { return getVertex(userRootId); } private Edge setLabel(Vertex fromV, Vertex toV, String label) { return graph.addEdge(null, fromV, toV, label); } public Edge setLabelFromRootUser(UserModel user) { Vertex rootUser = getUserRootVertex(); // rootUser ---child---> newUser return setLabel(rootUser, user.getVertex(), CHILD); } public Edge setLabelFromRootClaim(ClaimModel claim) { Vertex rootClaim = getClaimRootVertex(); // rootUser ---child---> newUser return setLabel(rootClaim, claim.getVertex(), CHILD); } public Edge setLabelToAuthor(ClaimModel claim, String author) { Vertex authorVertex = getVertex(author); // claim ---author---> authorVertex(userVertex) return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR); } public Edge setLabelPrev(ClaimModel fromClaim, ClaimModel toClaim) { // fromClaim ---prev---> toClaim return setLabel(fromClaim.getVertex(), toClaim.getVertex(), NodeModel.L_PREV ); } public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) { for (String userName: users) { Vertex userVertex = 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 = 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 = 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; } /* * Return CLAIM numbers of top consensus vertex. */ public Object[] checkConsensus(HashSet<Object> set) { Iterator<Object> iter = set.iterator(); iter = set.iterator(); while (iter.hasNext()) { Object childId = iter.next(); ArrayList<Object> array = getAllUpperVertexId(childId); for (Object parentId: array) { /* * If there is a number of the number of parent and child in the [set], * remove [childId]. */ if (set.contains(parentId)) { if (set.contains(childId)) { set.remove(childId); // This behavior is anxiety. iter = set.iterator(); } childId = parentId; } } } return set.toArray(); } /* * Return CLAIM numbers of above [id] CLAIM. */ public ArrayList<Object> getAllUpperVertexId(Object id) { Vertex startV = getVertex(id); ArrayList<Object> vertexArray = new ArrayList<Object>(); while (true) { 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 e = pipe.next(); vertexArray.add(e.getId()); startV = e; } else { break; } } return vertexArray; } public Object getOneUpperClaimVertexId(Object id) { Vertex startV = 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 Object getTopClaimVertexId(Object id) { Object v = id; Object upV = id; while (upV != null) { v = upV; upV = getOneUpperClaimVertexId(v); } return v; } private void recursiveCopyDownClaimsAndSetLabel(ClaimModel oldUpClaim, ClaimModel latestUpClaim, String... labels) { for (String label: labels) { GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); pipe.start(oldUpClaim.getVertex()).out(label); for (Vertex oldDownV : pipe) { ClaimModel oldDownClaim = new ClaimModel(oldDownV); ClaimModel latestDownClaim = oldDownClaim.cloneAndSetLabelPrev(); setLabel(latestUpClaim.getVertex(), latestDownClaim.getVertex(), label); recursiveCopyDownClaimsAndSetLabel(oldDownClaim, latestDownClaim, labels); } } } private ClaimModel copyDownClaims(ClaimModel oldTopClaim) { ClaimModel latestTopClaim = oldTopClaim.cloneAndSetLabelPrev(); recursiveCopyDownClaimsAndSetLabel(oldTopClaim, latestTopClaim, NodeModel.L_REFUTATION, NodeModel.L_QUESTION, NodeModel.L_SUGGESTION); return latestTopClaim; } /* * TODO: update TimeStamp */ public ClaimModel copyConsensusTree(ClaimModel claim) { ClaimModel oldTopClaim = new ClaimModel(graph.getVertex(getTopClaimVertexId(claim.getId()))); ClaimModel latestTopClaim = copyDownClaims(oldTopClaim); return latestTopClaim; } public void shutdownGraph() { if (path == null) { return; } else { graph.shutdown(); } } }