Mercurial > hg > Members > nobuyasu > Consensus
annotate app/models/TPGraph.java @ 82:cddb5ed942a6 draft
modified copyConsensusTree method
author | one |
---|---|
date | Sat, 09 Mar 2013 21:26:40 +0900 |
parents | f744331287ea |
children | c6929060c85f |
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"; |
57 | 23 |
7 | 24 private TPGraph() { |
25 | |
26 } | |
27 | |
28 public static TPGraph getInstance() { | |
29 return instance; | |
30 } | |
61 | 31 |
32 public static void resetInstance() { | |
33 instance = new TPGraph(); | |
34 } | |
7 | 35 |
52 | 36 private Graph graph = null; |
7 | 37 private String path = null; |
38 | |
39 public void setPath(String path) { | |
40 this.path = path; | |
41 } | |
42 | |
43 public Graph newGraph() { | |
44 if (path == null) { | |
45 graph = new TinkerGraph(); | |
46 } else { | |
47 graph = new TinkerGraph(path); | |
48 | |
49 } | |
50 return graph; | |
51 } | |
52 | |
53 public Graph getGraph() { | |
54 return graph; | |
55 } | |
56 | |
76 | 57 public Vertex addVertex(Object vId) { |
58 return graph.addVertex(vId); | |
59 } | |
60 | |
61 public Vertex getVertex(Object vId) { | |
62 return graph.getVertex(vId); | |
63 } | |
64 | |
65 public Edge getEdge(Object eId) { | |
66 return graph.getEdge(eId); | |
67 } | |
68 | |
8 | 69 public void setClaimRootId(Object id) { |
70 this.claimRootId = id; | |
7 | 71 } |
72 | |
8 | 73 public void setUserRootId(Object id) { |
74 this.userRootId = id; | |
7 | 75 } |
76 | |
13 | 77 public Object getClaimRootId() { |
78 return claimRootId; | |
79 } | |
80 | |
8 | 81 public Object getUserRootId() { |
82 return userRootId; | |
83 } | |
76 | 84 |
8 | 85 public Vertex getClaimRootVertex() { |
76 | 86 return getVertex(claimRootId); |
7 | 87 } |
88 | |
8 | 89 public Vertex getUserRootVertex() { |
76 | 90 return getVertex(userRootId); |
7 | 91 } |
8 | 92 |
25 | 93 private Edge setLabel(Vertex fromV, Vertex toV, String label) { |
94 return graph.addEdge(null, fromV, toV, label); | |
18 | 95 } |
96 | |
70 | 97 public Edge setLabelFromRootUser(UserModel user) { |
8 | 98 Vertex rootUser = getUserRootVertex(); |
18 | 99 // rootUser ---child---> newUser |
25 | 100 return setLabel(rootUser, user.getVertex(), CHILD); |
13 | 101 } |
8 | 102 |
70 | 103 public Edge setLabelFromRootClaim(ClaimModel claim) { |
13 | 104 Vertex rootClaim = getClaimRootVertex(); |
18 | 105 // rootUser ---child---> newUser |
25 | 106 return setLabel(rootClaim, claim.getVertex(), CHILD); |
8 | 107 } |
108 | |
25 | 109 public Edge setLabelToAuthor(ClaimModel claim, String author) { |
76 | 110 Vertex authorVertex = getVertex(author); |
18 | 111 // claim ---author---> authorVertex(userVertex) |
25 | 112 return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR); |
16 | 113 } |
114 | |
74 | 115 public Edge setLabelPrev(ClaimModel fromClaim, ClaimModel toClaim) { |
116 // fromClaim ---prev---> toClaim | |
117 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), NodeModel.L_PREV ); | |
118 } | |
119 | |
20 | 120 public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) { |
18 | 121 for (String userName: users) { |
76 | 122 Vertex userVertex = getVertex(userName); |
57 | 123 if (userVertex == null) { |
124 return false; | |
125 } | |
20 | 126 setLabel(claim.getVertex(), userVertex, label); |
18 | 127 } |
128 return true; | |
16 | 129 } |
9 | 130 |
36 | 131 public Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) { |
76 | 132 Vertex userVertex = getVertex(userName); |
36 | 133 if (userVertex == null) { |
134 return false; | |
135 } | |
136 Edge edge = setLabel(claim.getVertex(), userVertex, label); | |
137 edge.setProperty(NodeModel.STATUS, status); | |
138 return true; | |
139 } | |
140 | |
30 | 141 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { |
142 for (String userName: users) { | |
36 | 143 Boolean createFlag = setLabelStatusToUser(claim, userName, label, status); |
144 if (!createFlag) { | |
145 return false; | |
146 } | |
30 | 147 } |
148 return true; | |
149 } | |
150 | |
25 | 151 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) { |
152 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label); | |
153 } | |
154 | |
40 | 155 public Object[] searchAllUser() { |
76 | 156 Vertex userRootVertex = getVertex(getUserRootId()); |
40 | 157 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); |
158 pipe.start(userRootVertex).out(CHILD); | |
159 ArrayList<Object> userArray = new ArrayList<Object>(); | |
160 for (Vertex userVertex : pipe) { | |
161 userArray.add(userVertex.getId()); | |
162 } | |
163 if (userArray.size() == 0) { | |
164 return null; | |
165 } | |
166 return userArray.toArray(); | |
167 } | |
168 | |
36 | 169 public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) { |
170 GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>(); | |
171 pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST); | |
172 ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>(); | |
173 for (Edge e : pipeEdge) { | |
174 GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>(); | |
175 pipeUserVertex.start(e).inV(); | |
176 Vertex userVertex = pipeUserVertex.next(); | |
177 if (userSet.contains(userVertex.getId())) { | |
178 deleteEdgeArray.add(e); | |
179 } | |
180 } | |
181 for (Edge e : deleteEdgeArray) { | |
182 graph.removeEdge(e); | |
183 } | |
184 return true; | |
185 } | |
186 | |
71 | 187 /* |
188 * Return CLAIM numbers of top consensus vertex. | |
189 */ | |
23 | 190 public Object[] checkConsensus(HashSet<Object> set) { |
191 Iterator<Object> iter = set.iterator(); | |
57 | 192 iter = set.iterator(); |
27 | 193 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
|
194 Object childId = iter.next(); |
74 | 195 ArrayList<Object> array = getAllUpperVertexId(childId); |
50 | 196 for (Object parentId: array) { |
71 | 197 /* |
198 * If there is a number of the number of parent and child in the [set], | |
199 * remove [childId]. | |
200 */ | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
201 if (set.contains(parentId)) { |
27 | 202 if (set.contains(childId)) { |
71 | 203 set.remove(childId); |
27 | 204 // This behavior is anxiety. |
205 iter = set.iterator(); | |
206 } | |
207 childId = parentId; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
208 } |
23 | 209 } |
210 } | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
211 return set.toArray(); |
23 | 212 } |
71 | 213 |
214 /* | |
215 * Return CLAIM numbers of above [id] CLAIM. | |
216 */ | |
74 | 217 public ArrayList<Object> getAllUpperVertexId(Object id) { |
76 | 218 Vertex startV = getVertex(id); |
27 | 219 ArrayList<Object> vertexArray = new ArrayList<Object>(); |
57 | 220 while (true) { |
221 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
222 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
223 if (pipe.hasNext()) { | |
224 Vertex e = pipe.next(); | |
225 vertexArray.add(e.getId()); | |
226 startV = e; | |
227 } else { | |
228 break; | |
229 } | |
27 | 230 } |
231 return vertexArray; | |
232 } | |
233 | |
74 | 234 public Object getOneUpperClaimVertexId(Object id) { |
76 | 235 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
|
236 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
|
237 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
|
238 if (pipe.hasNext()) { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
239 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
|
240 return v.getId(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
241 } else { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
242 return null; |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
243 } |
23 | 244 } |
245 | |
74 | 246 public Object getTopClaimVertexId(Object id) { |
247 Object v = id; | |
248 Object upV = id; | |
249 while (upV != null) { | |
250 v = upV; | |
251 upV = getOneUpperClaimVertexId(v); | |
252 } | |
253 return v; | |
254 } | |
255 | |
82 | 256 private void recursiveCopyDownClaimsAndSetLabel(ClaimModel oldUpClaim, ClaimModel latestUpClaim, |
257 String timestamp, String... labels) { | |
75 | 258 for (String label: labels) { |
259 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); | |
260 pipe.start(oldUpClaim.getVertex()).out(label); | |
261 for (Vertex oldDownV : pipe) { | |
262 ClaimModel oldDownClaim = new ClaimModel(oldDownV); | |
82 | 263 ClaimModel latestDownClaim = oldDownClaim.cloneAndSetLabelPrev(timestamp); |
75 | 264 setLabel(latestUpClaim.getVertex(), latestDownClaim.getVertex(), label); |
82 | 265 recursiveCopyDownClaimsAndSetLabel(oldDownClaim, latestDownClaim, timestamp, labels); |
75 | 266 } |
267 } | |
268 } | |
269 | |
82 | 270 private ClaimModel copyDownClaims(ClaimModel oldTopClaim, String timestamp) { |
271 ClaimModel latestTopClaim = oldTopClaim.cloneAndSetLabelPrev(timestamp); | |
272 recursiveCopyDownClaimsAndSetLabel(oldTopClaim, latestTopClaim, timestamp, | |
75 | 273 NodeModel.L_REFUTATION, NodeModel.L_QUESTION, NodeModel.L_SUGGESTION); |
274 return latestTopClaim; | |
74 | 275 } |
276 | |
82 | 277 public ClaimModel copyConsensusTree(ClaimModel claim, String timestamp) { |
77 | 278 ClaimModel oldTopClaim = new ClaimModel(getVertex(getTopClaimVertexId(claim.getId()))); |
82 | 279 ClaimModel latestTopClaim = copyDownClaims(oldTopClaim, timestamp); |
75 | 280 return latestTopClaim; |
74 | 281 } |
282 | |
7 | 283 public void shutdownGraph() { |
57 | 284 if (path == null) { |
285 return; | |
286 } else { | |
287 graph.shutdown(); | |
288 } | |
7 | 289 } |
290 | |
291 } |