Mercurial > hg > Members > nobuyasu > Consensus
annotate 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 |
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 | |
84 | 7 import com.tinkerpop.blueprints.Direction; |
25 | 8 import com.tinkerpop.blueprints.Edge; |
7 | 9 import com.tinkerpop.blueprints.Graph; |
10 import com.tinkerpop.blueprints.Vertex; | |
11 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
|
12 import com.tinkerpop.gremlin.java.GremlinPipeline; |
7 | 13 |
14 public class TPGraph { | |
15 | |
16 private static TPGraph instance = new TPGraph(); | |
17 private Object claimRootId; | |
18 private Object userRootId; | |
19 | |
13 | 20 /* |
21 * Edge type | |
22 */ | |
8 | 23 protected final String CHILD = "child"; |
57 | 24 |
7 | 25 private TPGraph() { |
26 | |
27 } | |
28 | |
29 public static TPGraph getInstance() { | |
30 return instance; | |
31 } | |
61 | 32 |
33 public static void resetInstance() { | |
85 | 34 if (instance.getGraph() != null) { |
35 instance.shutdownGraph(); | |
36 } | |
61 | 37 instance = new TPGraph(); |
85 | 38 instance.newGraph(); |
61 | 39 } |
7 | 40 |
52 | 41 private Graph graph = null; |
7 | 42 private String path = null; |
43 | |
44 public void setPath(String path) { | |
45 this.path = path; | |
46 } | |
47 | |
48 public Graph newGraph() { | |
49 if (path == null) { | |
50 graph = new TinkerGraph(); | |
51 } else { | |
52 graph = new TinkerGraph(path); | |
53 | |
54 } | |
55 return graph; | |
56 } | |
57 | |
58 public Graph getGraph() { | |
59 return graph; | |
60 } | |
61 | |
76 | 62 public Vertex addVertex(Object vId) { |
63 return graph.addVertex(vId); | |
64 } | |
65 | |
66 public Vertex getVertex(Object vId) { | |
67 return graph.getVertex(vId); | |
68 } | |
69 | |
70 public Edge getEdge(Object eId) { | |
71 return graph.getEdge(eId); | |
72 } | |
73 | |
8 | 74 public void setClaimRootId(Object id) { |
75 this.claimRootId = id; | |
7 | 76 } |
77 | |
8 | 78 public void setUserRootId(Object id) { |
79 this.userRootId = id; | |
7 | 80 } |
81 | |
13 | 82 public Object getClaimRootId() { |
83 return claimRootId; | |
84 } | |
85 | |
8 | 86 public Object getUserRootId() { |
87 return userRootId; | |
88 } | |
76 | 89 |
8 | 90 public Vertex getClaimRootVertex() { |
76 | 91 return getVertex(claimRootId); |
7 | 92 } |
93 | |
8 | 94 public Vertex getUserRootVertex() { |
76 | 95 return getVertex(userRootId); |
7 | 96 } |
8 | 97 |
25 | 98 private Edge setLabel(Vertex fromV, Vertex toV, String label) { |
99 return graph.addEdge(null, fromV, toV, label); | |
18 | 100 } |
101 | |
70 | 102 public Edge setLabelFromRootUser(UserModel user) { |
8 | 103 Vertex rootUser = getUserRootVertex(); |
18 | 104 // rootUser ---child---> newUser |
25 | 105 return setLabel(rootUser, user.getVertex(), CHILD); |
13 | 106 } |
8 | 107 |
70 | 108 public Edge setLabelFromRootClaim(ClaimModel claim) { |
13 | 109 Vertex rootClaim = getClaimRootVertex(); |
18 | 110 // rootUser ---child---> newUser |
25 | 111 return setLabel(rootClaim, claim.getVertex(), CHILD); |
8 | 112 } |
113 | |
25 | 114 public Edge setLabelToAuthor(ClaimModel claim, String author) { |
76 | 115 Vertex authorVertex = getVertex(author); |
18 | 116 // claim ---author---> authorVertex(userVertex) |
25 | 117 return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR); |
16 | 118 } |
119 | |
74 | 120 public Edge setLabelPrev(ClaimModel fromClaim, ClaimModel toClaim) { |
121 // fromClaim ---prev---> toClaim | |
122 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), NodeModel.L_PREV ); | |
123 } | |
124 | |
20 | 125 public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) { |
18 | 126 for (String userName: users) { |
76 | 127 Vertex userVertex = getVertex(userName); |
57 | 128 if (userVertex == null) { |
129 return false; | |
130 } | |
20 | 131 setLabel(claim.getVertex(), userVertex, label); |
18 | 132 } |
133 return true; | |
16 | 134 } |
9 | 135 |
36 | 136 public Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) { |
76 | 137 Vertex userVertex = getVertex(userName); |
36 | 138 if (userVertex == null) { |
139 return false; | |
140 } | |
141 Edge edge = setLabel(claim.getVertex(), userVertex, label); | |
142 edge.setProperty(NodeModel.STATUS, status); | |
143 return true; | |
144 } | |
145 | |
30 | 146 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { |
147 for (String userName: users) { | |
36 | 148 Boolean createFlag = setLabelStatusToUser(claim, userName, label, status); |
149 if (!createFlag) { | |
150 return false; | |
151 } | |
30 | 152 } |
153 return true; | |
154 } | |
155 | |
25 | 156 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) { |
157 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label); | |
158 } | |
159 | |
40 | 160 public Object[] searchAllUser() { |
76 | 161 Vertex userRootVertex = getVertex(getUserRootId()); |
40 | 162 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); |
163 pipe.start(userRootVertex).out(CHILD); | |
164 ArrayList<Object> userArray = new ArrayList<Object>(); | |
165 for (Vertex userVertex : pipe) { | |
166 userArray.add(userVertex.getId()); | |
167 } | |
168 if (userArray.size() == 0) { | |
169 return null; | |
170 } | |
171 return userArray.toArray(); | |
172 } | |
173 | |
36 | 174 public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) { |
175 GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>(); | |
176 pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST); | |
177 ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>(); | |
178 for (Edge e : pipeEdge) { | |
179 GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>(); | |
180 pipeUserVertex.start(e).inV(); | |
181 Vertex userVertex = pipeUserVertex.next(); | |
182 if (userSet.contains(userVertex.getId())) { | |
183 deleteEdgeArray.add(e); | |
184 } | |
185 } | |
186 for (Edge e : deleteEdgeArray) { | |
187 graph.removeEdge(e); | |
188 } | |
189 return true; | |
190 } | |
191 | |
71 | 192 /* |
193 * Return CLAIM numbers of top consensus vertex. | |
194 */ | |
23 | 195 public Object[] checkConsensus(HashSet<Object> set) { |
196 Iterator<Object> iter = set.iterator(); | |
57 | 197 iter = set.iterator(); |
27 | 198 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
|
199 Object childId = iter.next(); |
74 | 200 ArrayList<Object> array = getAllUpperVertexId(childId); |
50 | 201 for (Object parentId: array) { |
71 | 202 /* |
203 * If there is a number of the number of parent and child in the [set], | |
204 * remove [childId]. | |
205 */ | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
206 if (set.contains(parentId)) { |
27 | 207 if (set.contains(childId)) { |
71 | 208 set.remove(childId); |
27 | 209 // This behavior is anxiety. |
210 iter = set.iterator(); | |
211 } | |
212 childId = parentId; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
213 } |
23 | 214 } |
215 } | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
216 return set.toArray(); |
23 | 217 } |
71 | 218 |
219 /* | |
220 * Return CLAIM numbers of above [id] CLAIM. | |
221 */ | |
74 | 222 public ArrayList<Object> getAllUpperVertexId(Object id) { |
76 | 223 Vertex startV = getVertex(id); |
27 | 224 ArrayList<Object> vertexArray = new ArrayList<Object>(); |
57 | 225 while (true) { |
226 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
227 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
228 if (pipe.hasNext()) { | |
229 Vertex e = pipe.next(); | |
230 vertexArray.add(e.getId()); | |
231 startV = e; | |
232 } else { | |
233 break; | |
234 } | |
27 | 235 } |
236 return vertexArray; | |
237 } | |
238 | |
74 | 239 public Object getOneUpperClaimVertexId(Object id) { |
76 | 240 Vertex startV = getVertex(id); |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
241 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
|
242 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
|
243 if (pipe.hasNext()) { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
244 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
|
245 return v.getId(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
246 } else { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
247 return null; |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
248 } |
23 | 249 } |
250 | |
74 | 251 public Object getTopClaimVertexId(Object id) { |
252 Object v = id; | |
253 Object upV = id; | |
254 while (upV != null) { | |
255 v = upV; | |
256 upV = getOneUpperClaimVertexId(v); | |
257 } | |
258 return v; | |
259 } | |
83 | 260 |
82 | 261 private void recursiveCopyDownClaimsAndSetLabel(ClaimModel oldUpClaim, ClaimModel latestUpClaim, |
262 String timestamp, String... labels) { | |
75 | 263 for (String label: labels) { |
264 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); | |
265 pipe.start(oldUpClaim.getVertex()).out(label); | |
266 for (Vertex oldDownV : pipe) { | |
267 ClaimModel oldDownClaim = new ClaimModel(oldDownV); | |
82 | 268 ClaimModel latestDownClaim = oldDownClaim.cloneAndSetLabelPrev(timestamp); |
75 | 269 setLabel(latestUpClaim.getVertex(), latestDownClaim.getVertex(), label); |
82 | 270 recursiveCopyDownClaimsAndSetLabel(oldDownClaim, latestDownClaim, timestamp, labels); |
75 | 271 } |
272 } | |
273 } | |
274 | |
82 | 275 private ClaimModel copyDownClaims(ClaimModel oldTopClaim, String timestamp) { |
276 ClaimModel latestTopClaim = oldTopClaim.cloneAndSetLabelPrev(timestamp); | |
277 recursiveCopyDownClaimsAndSetLabel(oldTopClaim, latestTopClaim, timestamp, | |
75 | 278 NodeModel.L_REFUTATION, NodeModel.L_QUESTION, NodeModel.L_SUGGESTION); |
279 return latestTopClaim; | |
74 | 280 } |
281 | |
82 | 282 public ClaimModel copyConsensusTree(ClaimModel claim, String timestamp) { |
77 | 283 ClaimModel oldTopClaim = new ClaimModel(getVertex(getTopClaimVertexId(claim.getId()))); |
82 | 284 ClaimModel latestTopClaim = copyDownClaims(oldTopClaim, timestamp); |
75 | 285 return latestTopClaim; |
74 | 286 } |
287 | |
84 | 288 public Object[] checkLatestVertices(Object[] vIds) { |
289 ArrayList<Object> array = new ArrayList<Object>(); | |
290 for (Object vId: vIds) { | |
291 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
292 pipe.start(getVertex(vId)).in(NodeModel.L_PREV); | |
293 /* | |
294 * get latest claims. | |
295 * c1 <--prev-- c1' <--prev-- c1'' | |
296 * c1'' is latest. | |
297 */ | |
298 if (!pipe.hasNext()) { | |
299 array.add(vId); | |
300 } | |
301 } | |
302 return array.toArray(); | |
303 } | |
304 | |
305 public Object getLatestVertexId(Object id) { | |
306 Vertex v = getVertex(id); | |
307 if (v == null) { | |
308 return null; | |
309 } | |
310 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
311 pipe.start(v).in(NodeModel.L_PREV); | |
312 while (pipe.hasNext()) { | |
313 /* | |
314 * get latest claims. | |
315 * v1 <--prev-- v1' <--prev-- v1'' | |
316 * c1'' is latest. | |
317 */ | |
85 | 318 v = pipe.next(); |
84 | 319 pipe.start(v).in(NodeModel.L_PREV); |
320 } | |
321 return v.getId(); | |
322 } | |
323 | |
324 private ArrayList<Object> getVertexIdRecursiveTraverse(NodeModel vModel, Direction direction, String... labels) { | |
325 ArrayList<Object> array = new ArrayList<Object>(); | |
326 ArrayList<Object> nextArray = new ArrayList<Object>(); | |
327 Object[] ids = vModel.getVertexIdArrayTraverseLabel(direction, labels); | |
328 if (ids == null) { | |
329 return new ArrayList<Object>(); | |
330 } | |
331 for (Object id: ids) { | |
332 array.add(id); | |
333 NodeModel nextModel = new NodeModel(getVertex(id)); | |
334 ArrayList<Object> tmpArray = getVertexIdRecursiveTraverse(nextModel, direction, labels); | |
335 if (!tmpArray.isEmpty()) { | |
336 nextArray.addAll(tmpArray); | |
337 } | |
338 } | |
339 if (!nextArray.isEmpty()) { | |
340 array.addAll(nextArray); | |
341 } | |
342 return array; | |
343 } | |
344 | |
345 /* | |
346 * [latestId, latestId-1, ..., id, ..., oldIds+1, oldIds] | |
347 */ | |
348 public Object[] getClaimRevision(String id) { | |
349 NodeModel vModel = new NodeModel(getVertex(id)); | |
350 if (vModel.getVertex() == null) { | |
351 return null; | |
352 } | |
353 ArrayList<Object> array = new ArrayList<Object>(); | |
354 ArrayList<Object> inPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.IN, NodeModel.L_PREV); | |
355 for (int i=inPrevIds.size()-1; i>=0; i--) { | |
356 array.add(inPrevIds.get(i)); | |
357 } | |
358 array.add(id); | |
359 ArrayList<Object> outPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.OUT, NodeModel.L_PREV); | |
360 array.addAll(outPrevIds); | |
361 return array.toArray(); | |
362 } | |
363 | |
7 | 364 public void shutdownGraph() { |
85 | 365 graph.shutdown(); |
7 | 366 } |
367 } |