Mercurial > hg > Members > nobuyasu > Consensus
view app/models/TPGraph.java @ 109:2633ac31c233 draft
create updateRelationNode function
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 17 Mar 2013 22:39:58 +0900 |
parents | d45f76774fd8 |
children | 3440be06e501 |
line wrap: on
line source
package models; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import com.tinkerpop.blueprints.Direction; 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() { if (instance.getGraph() != null) { instance.shutdownGraph(); } instance = new TPGraph(); instance.newGraph(); } 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 timestamp, 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(timestamp); setLabel(latestUpClaim.getVertex(), latestDownClaim.getVertex(), label); recursiveCopyDownClaimsAndSetLabel(oldDownClaim, latestDownClaim, timestamp, labels); } } } private ClaimModel copyDownClaims(ClaimModel oldTopClaim, String timestamp) { ClaimModel latestTopClaim = oldTopClaim.cloneAndSetLabelPrev(timestamp); recursiveCopyDownClaimsAndSetLabel(oldTopClaim, latestTopClaim, timestamp, NodeModel.L_REFUTATION, NodeModel.L_QUESTION, NodeModel.L_SUGGESTION); return latestTopClaim; } public ClaimModel copyConsensusTree(ClaimModel claim, String timestamp) { ClaimModel oldTopClaim = new ClaimModel(getVertex(getTopClaimVertexId(claim.getId()))); ClaimModel latestTopClaim = copyDownClaims(oldTopClaim, timestamp); return latestTopClaim; } public Object[] checkLatestVertices(Object[] vIds) { ArrayList<Object> array = new ArrayList<Object>(); for (Object vId: vIds) { GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); pipe.start(getVertex(vId)).in(NodeModel.L_PREV); /* * get latest claims. * c1 <--prev-- c1' <--prev-- c1'' * c1'' is latest. */ if (!pipe.hasNext()) { array.add(vId); } } return array.toArray(); } public Object getLatestVertexId(Object id) { Vertex v = getVertex(id); if (v == null) { return null; } GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); pipe.start(v).in(NodeModel.L_PREV); while (pipe.hasNext()) { /* * get latest claims. * v1 <--prev-- v1' <--prev-- v1'' * c1'' is latest. */ v = pipe.next(); pipe.start(v).in(NodeModel.L_PREV); } return v.getId(); } private ArrayList<Object> getVertexIdRecursiveTraverse(NodeModel vModel, Direction direction, String... labels) { ArrayList<Object> array = new ArrayList<Object>(); ArrayList<Object> nextArray = new ArrayList<Object>(); Object[] ids = vModel.getVertexIdArrayTraverseLabel(direction, labels); if (ids == null) { return new ArrayList<Object>(); } for (Object id: ids) { array.add(id); NodeModel nextModel = new NodeModel(getVertex(id)); ArrayList<Object> tmpArray = getVertexIdRecursiveTraverse(nextModel, direction, labels); if (!tmpArray.isEmpty()) { nextArray.addAll(tmpArray); } } if (!nextArray.isEmpty()) { array.addAll(nextArray); } return array; } /* * [latestId, latestId-1, ..., id, ..., oldIds+1, oldIds] */ public Object[] getClaimRevision(String id) { NodeModel vModel = new NodeModel(getVertex(id)); if (vModel.getVertex() == null) { return null; } ArrayList<Object> array = new ArrayList<Object>(); ArrayList<Object> inPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.IN, NodeModel.L_PREV); for (int i=inPrevIds.size()-1; i>=0; i--) { array.add(inPrevIds.get(i)); } array.add(id); ArrayList<Object> outPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.OUT, NodeModel.L_PREV); array.addAll(outPrevIds); return array.toArray(); } public void shutdownGraph() { graph.shutdown(); } }