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