Mercurial > hg > Members > nobuyasu > Consensus
annotate app/models/TPGraph.java @ 47:9645bfb49603
modified computeAndUpdateStatus
author | one |
---|---|
date | Thu, 04 Oct 2012 08:55:13 +0900 |
parents | 1d5c086e069b |
children | d6c623e92837 |
rev | line source |
---|---|
7 | 1 package models; |
2 | |
27 | 3 import java.util.ArrayList; |
23 | 4 import java.util.HashSet; |
5 import java.util.Iterator; | |
6 | |
25 | 7 import com.tinkerpop.blueprints.Edge; |
7 | 8 import com.tinkerpop.blueprints.Graph; |
9 import com.tinkerpop.blueprints.Vertex; | |
10 import com.tinkerpop.blueprints.impls.tg.TinkerGraph; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
11 import com.tinkerpop.gremlin.java.GremlinPipeline; |
7 | 12 |
13 public class TPGraph { | |
14 | |
15 private static TPGraph instance = new TPGraph(); | |
16 private Object claimRootId; | |
17 private Object userRootId; | |
18 | |
13 | 19 /* |
20 * Edge type | |
21 */ | |
8 | 22 protected final String CHILD = "child"; |
23 | |
13 | 24 |
7 | 25 private TPGraph() { |
26 | |
27 } | |
28 | |
29 public static TPGraph getInstance() { | |
30 return instance; | |
31 } | |
32 | |
33 private Graph graph; | |
34 private String path = null; | |
35 | |
36 public void setPath(String path) { | |
37 this.path = path; | |
38 } | |
39 | |
40 public Graph newGraph() { | |
41 if (path == null) { | |
42 graph = new TinkerGraph(); | |
43 } else { | |
44 graph = new TinkerGraph(path); | |
45 | |
46 } | |
47 return graph; | |
48 } | |
49 | |
50 public Graph getGraph() { | |
51 return graph; | |
52 } | |
53 | |
8 | 54 public void setClaimRootId(Object id) { |
55 this.claimRootId = id; | |
7 | 56 } |
57 | |
8 | 58 public void setUserRootId(Object id) { |
59 this.userRootId = id; | |
7 | 60 } |
61 | |
13 | 62 public Object getClaimRootId() { |
63 return claimRootId; | |
64 } | |
65 | |
8 | 66 public Object getUserRootId() { |
67 return userRootId; | |
68 } | |
69 | |
70 public Vertex getClaimRootVertex() { | |
13 | 71 return graph.getVertex(claimRootId); |
7 | 72 } |
73 | |
8 | 74 public Vertex getUserRootVertex() { |
75 return graph.getVertex(userRootId); | |
7 | 76 } |
8 | 77 |
25 | 78 private Edge setLabel(Vertex fromV, Vertex toV, String label) { |
79 return graph.addEdge(null, fromV, toV, label); | |
18 | 80 } |
81 | |
25 | 82 public Edge setLabelToRootUser(UserModel user) { |
8 | 83 Vertex rootUser = getUserRootVertex(); |
18 | 84 // rootUser ---child---> newUser |
25 | 85 return setLabel(rootUser, user.getVertex(), CHILD); |
13 | 86 } |
8 | 87 |
25 | 88 public Edge setLabelToRootClaim(ClaimModel claim) { |
13 | 89 Vertex rootClaim = getClaimRootVertex(); |
18 | 90 // rootUser ---child---> newUser |
25 | 91 return setLabel(rootClaim, claim.getVertex(), CHILD); |
8 | 92 } |
93 | |
25 | 94 public Edge setLabelToAuthor(ClaimModel claim, String author) { |
16 | 95 Vertex authorVertex = graph.getVertex(author); |
18 | 96 // claim ---author---> authorVertex(userVertex) |
25 | 97 return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR); |
16 | 98 } |
99 | |
20 | 100 public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) { |
18 | 101 for (String userName: users) { |
102 Vertex userVertex = graph.getVertex(userName); | |
103 if (userVertex == null) return false; | |
20 | 104 setLabel(claim.getVertex(), userVertex, label); |
18 | 105 } |
106 return true; | |
16 | 107 } |
9 | 108 |
36 | 109 public Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) { |
110 Vertex userVertex = graph.getVertex(userName); | |
111 if (userVertex == null) { | |
112 return false; | |
113 } | |
114 Edge edge = setLabel(claim.getVertex(), userVertex, label); | |
115 edge.setProperty(NodeModel.STATUS, status); | |
116 return true; | |
117 } | |
118 | |
30 | 119 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { |
120 for (String userName: users) { | |
36 | 121 Boolean createFlag = setLabelStatusToUser(claim, userName, label, status); |
122 if (!createFlag) { | |
123 return false; | |
124 } | |
30 | 125 } |
126 return true; | |
127 } | |
128 | |
25 | 129 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) { |
130 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label); | |
131 } | |
132 | |
40 | 133 public Object[] searchAllUser() { |
134 Vertex userRootVertex = graph.getVertex(getUserRootId()); | |
135 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
136 pipe.start(userRootVertex).out(CHILD); | |
137 ArrayList<Object> userArray = new ArrayList<Object>(); | |
138 for (Vertex userVertex : pipe) { | |
139 userArray.add(userVertex.getId()); | |
140 } | |
141 if (userArray.size() == 0) { | |
142 return null; | |
143 } | |
144 return userArray.toArray(); | |
145 } | |
146 | |
36 | 147 public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) { |
148 GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>(); | |
149 pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST); | |
150 ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>(); | |
151 for (Edge e : pipeEdge) { | |
152 GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>(); | |
153 pipeUserVertex.start(e).inV(); | |
154 Vertex userVertex = pipeUserVertex.next(); | |
155 if (userSet.contains(userVertex.getId())) { | |
156 deleteEdgeArray.add(e); | |
157 } | |
158 } | |
159 for (Edge e : deleteEdgeArray) { | |
160 graph.removeEdge(e); | |
161 } | |
162 return true; | |
163 } | |
164 | |
23 | 165 public Object[] checkConsensus(HashSet<Object> set) { |
166 Iterator<Object> iter = set.iterator(); | |
27 | 167 while (iter.hasNext()) { |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
168 Object childId = iter.next(); |
27 | 169 ArrayList<Object> array = getAllUpperVertex(childId); |
170 for (Object parentId: array.toArray()) { | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
171 if (set.contains(parentId)) { |
27 | 172 if (set.contains(childId)) { |
173 // This behavior is anxiety. | |
174 set.remove(childId); | |
175 iter = set.iterator(); | |
176 } | |
177 childId = parentId; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
178 } |
23 | 179 } |
180 } | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
181 return set.toArray(); |
23 | 182 } |
183 | |
27 | 184 |
185 public ArrayList<Object> getAllUpperVertex(Object id) { | |
186 Vertex startV = graph.getVertex(id); | |
187 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
188 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
189 ArrayList<Object> vertexArray = new ArrayList<Object>(); | |
190 while (pipe.hasNext()) { | |
191 Object e = pipe.next().getId(); | |
192 vertexArray.add(e); | |
193 pipe.start(graph.getVertex(e)).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
194 } | |
195 return vertexArray; | |
196 } | |
197 | |
198 public Object getOneUpperVertex(Object id) { | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
199 Vertex startV = graph.getVertex(id); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
200 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
201 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
202 if (pipe.hasNext()) { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
203 Vertex v = pipe.next(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
204 return v.getId(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
205 } else { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
206 return null; |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
207 } |
23 | 208 } |
209 | |
7 | 210 |
211 public void shutdownGraph() { | |
212 graph.shutdown(); | |
213 } | |
214 | |
215 } |