Mercurial > hg > Members > nobuyasu > Consensus
annotate app/models/TPGraph.java @ 71:c76b3b60eb18 draft
add comment
author | one |
---|---|
date | Thu, 21 Feb 2013 03:15:39 +0900 |
parents | 290a5883ac5e |
children | c5ddd2cc52e1 |
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 | |
20 | 103 public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) { |
18 | 104 for (String userName: users) { |
105 Vertex userVertex = graph.getVertex(userName); | |
57 | 106 if (userVertex == null) { |
107 return false; | |
108 } | |
20 | 109 setLabel(claim.getVertex(), userVertex, label); |
18 | 110 } |
111 return true; | |
16 | 112 } |
9 | 113 |
36 | 114 public Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) { |
115 Vertex userVertex = graph.getVertex(userName); | |
116 if (userVertex == null) { | |
117 return false; | |
118 } | |
119 Edge edge = setLabel(claim.getVertex(), userVertex, label); | |
120 edge.setProperty(NodeModel.STATUS, status); | |
121 return true; | |
122 } | |
123 | |
30 | 124 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { |
125 for (String userName: users) { | |
36 | 126 Boolean createFlag = setLabelStatusToUser(claim, userName, label, status); |
127 if (!createFlag) { | |
128 return false; | |
129 } | |
30 | 130 } |
131 return true; | |
132 } | |
133 | |
25 | 134 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) { |
135 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label); | |
136 } | |
137 | |
40 | 138 public Object[] searchAllUser() { |
139 Vertex userRootVertex = graph.getVertex(getUserRootId()); | |
140 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
141 pipe.start(userRootVertex).out(CHILD); | |
142 ArrayList<Object> userArray = new ArrayList<Object>(); | |
143 for (Vertex userVertex : pipe) { | |
144 userArray.add(userVertex.getId()); | |
145 } | |
146 if (userArray.size() == 0) { | |
147 return null; | |
148 } | |
149 return userArray.toArray(); | |
150 } | |
151 | |
36 | 152 public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) { |
153 GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>(); | |
154 pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST); | |
155 ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>(); | |
156 for (Edge e : pipeEdge) { | |
157 GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>(); | |
158 pipeUserVertex.start(e).inV(); | |
159 Vertex userVertex = pipeUserVertex.next(); | |
160 if (userSet.contains(userVertex.getId())) { | |
161 deleteEdgeArray.add(e); | |
162 } | |
163 } | |
164 for (Edge e : deleteEdgeArray) { | |
165 graph.removeEdge(e); | |
166 } | |
167 return true; | |
168 } | |
169 | |
71 | 170 /* |
171 * Return CLAIM numbers of top consensus vertex. | |
172 */ | |
23 | 173 public Object[] checkConsensus(HashSet<Object> set) { |
174 Iterator<Object> iter = set.iterator(); | |
57 | 175 iter = set.iterator(); |
27 | 176 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
|
177 Object childId = iter.next(); |
27 | 178 ArrayList<Object> array = getAllUpperVertex(childId); |
50 | 179 for (Object parentId: array) { |
71 | 180 /* |
181 * If there is a number of the number of parent and child in the [set], | |
182 * remove [childId]. | |
183 */ | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
184 if (set.contains(parentId)) { |
27 | 185 if (set.contains(childId)) { |
71 | 186 set.remove(childId); |
27 | 187 // This behavior is anxiety. |
188 iter = set.iterator(); | |
189 } | |
190 childId = parentId; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
191 } |
23 | 192 } |
193 } | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
194 return set.toArray(); |
23 | 195 } |
71 | 196 |
197 /* | |
198 * Return CLAIM numbers of above [id] CLAIM. | |
199 */ | |
27 | 200 public ArrayList<Object> getAllUpperVertex(Object id) { |
201 Vertex startV = graph.getVertex(id); | |
202 ArrayList<Object> vertexArray = new ArrayList<Object>(); | |
57 | 203 while (true) { |
204 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
205 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
206 if (pipe.hasNext()) { | |
207 Vertex e = pipe.next(); | |
208 vertexArray.add(e.getId()); | |
209 startV = e; | |
210 } else { | |
211 break; | |
212 } | |
27 | 213 } |
214 return vertexArray; | |
215 } | |
216 | |
217 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
|
218 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
|
219 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
|
220 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
|
221 if (pipe.hasNext()) { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
222 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
|
223 return v.getId(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
224 } else { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
225 return null; |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
226 } |
23 | 227 } |
228 | |
7 | 229 public void shutdownGraph() { |
57 | 230 if (path == null) { |
231 return; | |
232 } else { | |
233 graph.shutdown(); | |
234 } | |
7 | 235 } |
236 | |
237 } |