14
|
1 package models;
|
|
2
|
29
|
3 import java.util.ArrayList;
|
36
|
4 import java.util.HashSet;
|
29
|
5
|
16
|
6 import org.codehaus.jackson.JsonNode;
|
14
|
7 import org.codehaus.jackson.node.ObjectNode;
|
|
8
|
|
9 import play.libs.Json;
|
|
10
|
44
|
11 import com.tinkerpop.blueprints.Direction;
|
31
|
12 import com.tinkerpop.blueprints.Edge;
|
36
|
13 import com.tinkerpop.blueprints.Graph;
|
14
|
14 import com.tinkerpop.blueprints.Vertex;
|
29
|
15 import com.tinkerpop.gremlin.java.GremlinPipeline;
|
14
|
16
|
|
17 public class ClaimModel extends NodeModel {
|
|
18
|
|
19 public ClaimModel(Vertex vertex) {
|
|
20 super(vertex);
|
|
21 }
|
52
|
22
|
47
|
23 public void setClaimProperties(JsonNode toulmin, String type) {
|
|
24 String title = toulmin.findPath(TITLE).getTextValue();
|
|
25 String contents = toulmin.findPath(CONTENTS).getTextValue();
|
|
26 String q = toulmin.findPath(QUALIFIER).getTextValue(); // Qualifier
|
|
27 String d = toulmin.findPath(DATA).getTextValue(); // Data
|
|
28 String w = toulmin.findPath(WARRANT).getTextValue(); // Warrant
|
|
29 String b = toulmin.findPath(BACKING).getTextValue(); // Backing
|
|
30 String r = toulmin.findPath(REBUTTLE).getTextValue(); // Rebuttle
|
|
31 ObjectNode t = Json.newObject();
|
|
32 t.put(TITLE, title);
|
|
33 t.put(CONTENTS, contents);
|
|
34 t.put(QUALIFIER, q);
|
|
35 t.put(DATA, d);
|
|
36 t.put(WARRANT, w);
|
|
37 t.put(BACKING, b);
|
|
38 t.put(REBUTTLE, r);
|
|
39 if (type == null) {
|
|
40 setProperty(TYPE, UNANIMOUSLY);
|
|
41 } else {
|
|
42 setProperty(TYPE, type);
|
|
43 }
|
|
44 if (getProperty(STATUS) == null) {
|
|
45 setProperty(STATUS, UNKNOWN); // Default Status is unknown.
|
|
46 }
|
|
47 setProperty(TOULMIN, t);
|
|
48 }
|
52
|
49
|
31
|
50 public ObjectNode getSimpleClaimInfo() {
|
29
|
51 ObjectNode property = Json.newObject();
|
|
52 property.put(TYPE, Json.toJson(getProperty(TYPE)));
|
|
53 property.put(STATUS, Json.toJson(getProperty(STATUS)));
|
|
54 property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
|
30
|
55 property.put(L_AUTHOR, Json.toJson(getAuthorId()));
|
|
56 property.put(MENTIONS, Json.toJson(getMentionsId()));
|
31
|
57 property.put(USERS, Json.toJson(getUsersId()));
|
29
|
58 return property;
|
|
59 }
|
52
|
60
|
31
|
61 public ObjectNode getClaimInfoTraverse() {
|
|
62 ObjectNode property = Json.newObject();
|
|
63 property.put(TYPE, Json.toJson(getProperty(TYPE)));
|
|
64 property.put(STATUS, Json.toJson(getProperty(STATUS)));
|
|
65 property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
|
|
66 property.put(L_AUTHOR, Json.toJson(getAuthorId()));
|
32
|
67 property.put(MENTIONS, Json.toJson(getClaimMentionsRecursive()));
|
31
|
68 property.put(USERS, Json.toJson(getUsersIdAndStatus()));
|
|
69 return property;
|
|
70 }
|
52
|
71
|
32
|
72 public Object[] getClaimMentionsRecursive() {
|
52
|
73 GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
|
|
74 pipe.start(vertex).outE(L_QUESTION, L_REFUTATION, L_SUGGESTION);
|
|
75 ArrayList<Object> array = new ArrayList<Object>();
|
|
76 for (Edge e : pipe) {
|
31
|
77 String label = e.getLabel();
|
32
|
78 ObjectNode info = Json.newObject();
|
|
79 info.put(TYPE, Json.toJson(label));
|
52
|
80 GremlinPipeline<Edge, Vertex> pipeChildVertex = new GremlinPipeline<Edge, Vertex>();
|
32
|
81 pipeChildVertex.start(e).inV();
|
|
82 ClaimModel childClaim = new ClaimModel(pipeChildVertex.next());
|
|
83 info.put(CLAIM, childClaim.getClaimInfoTraverse());
|
38
|
84 info.put(ID, Json.toJson(childClaim.getId()));
|
52
|
85 array.add(info);
|
31
|
86 }
|
32
|
87 return array.toArray();
|
30
|
88 }
|
52
|
89
|
|
90 private Iterable<Vertex> getVertexIterable(Direction direction,
|
|
91 String... labels) {
|
|
92 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
|
|
93 if (direction.equals(Direction.IN)) {
|
44
|
94 pipe.start(vertex).in(labels);
|
|
95 } else if (direction.equals(Direction.OUT)) {
|
|
96 pipe.start(vertex).out(labels);
|
|
97 } else {
|
|
98 pipe.start(vertex).both(labels);
|
|
99 }
|
|
100 ArrayList<Vertex> array = new ArrayList<Vertex>();
|
42
|
101 for (Vertex v : pipe) {
|
44
|
102 array.add(v);
|
42
|
103 }
|
|
104 if (array.size() == 0) {
|
|
105 return null;
|
|
106 }
|
44
|
107 return array;
|
|
108 }
|
|
109
|
52
|
110 private Object[] getVertexArray(Direction direction, String... labels) {
|
|
111 Iterable<Vertex> iter = getVertexIterable(direction, labels);
|
44
|
112 if (iter == null) {
|
|
113 return null;
|
|
114 }
|
|
115 ArrayList<Object> array = new ArrayList<Object>();
|
|
116 for (Vertex v : iter) {
|
|
117 array.add(v.getId());
|
|
118 }
|
30
|
119 return array.toArray();
|
|
120 }
|
|
121
|
|
122 public Object[] getMentionsId() {
|
52
|
123 return getVertexArray(Direction.OUT, L_QUESTION, L_REFUTATION,
|
|
124 L_SUGGESTION);
|
30
|
125 }
|
52
|
126
|
31
|
127 public Object[] getUsersId() {
|
44
|
128 return getVertexArray(Direction.OUT, L_REQUEST);
|
42
|
129 }
|
52
|
130
|
|
131 private Iterable<Edge> getEdgeIterable(Direction direction,
|
|
132 String... labels) {
|
|
133 GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
|
|
134 if (direction.equals(Direction.IN)) {
|
48
|
135 pipe.start(vertex).inE(labels);
|
|
136 } else if (direction.equals(Direction.OUT)) {
|
|
137 pipe.start(vertex).outE(labels);
|
|
138 } else {
|
|
139 pipe.start(vertex).bothE(labels);
|
|
140 }
|
|
141 ArrayList<Edge> array = new ArrayList<Edge>();
|
|
142 for (Edge v : pipe) {
|
|
143 array.add(v);
|
|
144 }
|
|
145 if (array.size() == 0) {
|
|
146 return null;
|
|
147 }
|
|
148 return array;
|
|
149 }
|
52
|
150
|
|
151 public Object[] getEdgeArray(Direction direction, String... labels) {
|
|
152 Iterable<Edge> iter = getEdgeIterable(direction, labels);
|
|
153 /*
|
|
154 * GremlinPipeline<Vertex,Edge> pipe = new
|
|
155 * GremlinPipeline<Vertex,Edge>(); pipe.start(vertex).outE(labels);
|
|
156 */
|
42
|
157 ArrayList<Object> array = new ArrayList<Object>();
|
48
|
158 for (Edge e : iter) {
|
42
|
159 array.add(e.getId());
|
|
160 }
|
|
161 if (array.size() == 0) {
|
|
162 return null;
|
|
163 }
|
|
164 return array.toArray();
|
|
165 }
|
52
|
166
|
42
|
167 public Object[] getRequestEdges() {
|
52
|
168 return getEdgeArray(Direction.OUT, L_REQUEST);
|
30
|
169 }
|
52
|
170
|
39
|
171 public ObjectNode getUserRequestStatus(UserModel user) {
|
52
|
172 GremlinPipeline<Vertex, Edge> pipeEdge = new GremlinPipeline<Vertex, Edge>();
|
39
|
173 pipeEdge.start(vertex).outE(L_REQUEST);
|
|
174 ObjectNode info = Json.newObject();
|
|
175 for (Edge e : pipeEdge) {
|
52
|
176 GremlinPipeline<Edge, Vertex> pipeChildVertex = new GremlinPipeline<Edge, Vertex>();
|
39
|
177 pipeChildVertex.start(e).inV();
|
|
178 Vertex childVertex = pipeChildVertex.next();
|
|
179 if (childVertex.getId() == user.getId()) {
|
|
180 info.put(STATUS, Json.toJson(e.getProperty(STATUS)));
|
|
181 break;
|
|
182 }
|
52
|
183 }
|
39
|
184 return info;
|
|
185 }
|
52
|
186
|
|
187 public Boolean updateUserRequestStatus(ClaimModel claim, UserModel user,
|
|
188 String status) {
|
|
189 GremlinPipeline<Vertex, Edge> pipeEdge = new GremlinPipeline<Vertex, Edge>();
|
41
|
190 pipeEdge.start(vertex).outE(L_REQUEST);
|
|
191 for (Edge e : pipeEdge) {
|
52
|
192 GremlinPipeline<Edge, Vertex> pipeChildVertex = new GremlinPipeline<Edge, Vertex>();
|
41
|
193 pipeChildVertex.start(e).inV();
|
|
194 Vertex childVertex = pipeChildVertex.next();
|
|
195 if (childVertex.getId() == user.getId()) {
|
|
196 e.setProperty(STATUS, status);
|
|
197 break;
|
|
198 }
|
52
|
199 }
|
41
|
200 return true;
|
|
201 }
|
52
|
202
|
31
|
203 public Object[] getUsersIdAndStatus() {
|
52
|
204 GremlinPipeline<Vertex, Edge> pipeEdge = new GremlinPipeline<Vertex, Edge>();
|
31
|
205 pipeEdge.start(vertex).outE(L_REQUEST);
|
|
206 ArrayList<Object> array = new ArrayList<Object>();
|
|
207 for (Edge e : pipeEdge) {
|
52
|
208 GremlinPipeline<Edge, Vertex> pipeChildVertex = new GremlinPipeline<Edge, Vertex>();
|
31
|
209 ObjectNode info = Json.newObject();
|
32
|
210 pipeChildVertex.start(e).inV();
|
31
|
211 Vertex childVertex = pipeChildVertex.next();
|
|
212 info.put(ID, Json.toJson(childVertex.getId()));
|
|
213 info.put(STATUS, Json.toJson(e.getProperty(STATUS)));
|
|
214 array.add(info);
|
|
215 }
|
|
216 return array.toArray();
|
|
217 }
|
30
|
218
|
|
219 public Object[] getRequestUsersId() {
|
44
|
220 return getVertexArray(Direction.OUT, NodeModel.REQUESTS);
|
30
|
221 }
|
52
|
222
|
36
|
223 public void editRequestsEdgeUsers(Object[] updateUsers) {
|
|
224 TPGraph tpGraph = TPGraph.getInstance();
|
|
225 Object[] currentUsers = getUsersId();
|
|
226 HashSet<Object> currentUsersHashSet = new HashSet<Object>();
|
|
227 for (Object u : currentUsers) {
|
|
228 currentUsersHashSet.add(u);
|
|
229 }
|
|
230 for (Object updateUser : updateUsers) {
|
|
231 if (currentUsersHashSet.contains(updateUser)) {
|
|
232 currentUsersHashSet.remove(updateUser);
|
|
233 } else {
|
52
|
234 tpGraph.setLabelStatusToUser(this, updateUser.toString(),
|
|
235 L_REQUEST, UNKNOWN);
|
36
|
236 }
|
|
237 }
|
|
238 tpGraph.deleteRequestEdge(this, currentUsersHashSet);
|
|
239 }
|
52
|
240
|
30
|
241 public Object getAuthorId() {
|
52
|
242 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
|
29
|
243 pipe.start(vertex).out(L_AUTHOR);
|
32
|
244 if (!pipe.hasNext()) {
|
|
245 return null;
|
|
246 }
|
29
|
247 Vertex authorV = pipe.next();
|
|
248 return authorV.getId();
|
|
249 }
|
47
|
250
|
42
|
251 public long getAgreedNumber(Object[] requestEdges) {
|
|
252 return getEdgeStatusNumber(requestEdges, AGREED);
|
|
253 }
|
52
|
254
|
42
|
255 public long getDeninedNumber(Object[] requestEdges) {
|
52
|
256 return getEdgeStatusNumber(requestEdges, DENIED);
|
42
|
257 }
|
52
|
258
|
42
|
259 public long getDeniedNumber(Object[] requestEdges) {
|
|
260 return getEdgeStatusNumber(requestEdges, DENIED);
|
|
261 }
|
52
|
262
|
42
|
263 private long getEdgeStatusNumber(Object[] requestEdges, String checkStatus) {
|
|
264 TPGraph tpGraph = TPGraph.getInstance();
|
|
265 Graph graph = tpGraph.getGraph();
|
|
266 long count = 0;
|
|
267 for (Object eId : requestEdges) {
|
|
268 Edge e = graph.getEdge(eId);
|
|
269 String status = e.getProperty(STATUS).toString();
|
|
270 if (status.equals(checkStatus)) {
|
|
271 count++;
|
|
272 }
|
|
273 }
|
|
274 return count;
|
|
275 }
|
52
|
276
|
|
277 private Boolean checkUnanimously(String inverseRefutationStatus,
|
|
278 String queAndSugStatus, long requestsNumber, long agreedNumber,
|
|
279 long deniedNumber) {
|
42
|
280 String preStatus = getProperty(STATUS).toString();
|
52
|
281 if (inverseRefutationStatus == null && queAndSugStatus == null) {
|
48
|
282 if (requestsNumber == agreedNumber) {
|
|
283 setProperty(STATUS, PASS);
|
|
284 } else if (requestsNumber == deniedNumber) {
|
52
|
285 setProperty(STATUS, FAILED);
|
49
|
286 } else {
|
52
|
287 setProperty(STATUS, UNKNOWN);
|
50
|
288 }
|
48
|
289 } else if (inverseRefutationStatus == null && queAndSugStatus != null) {
|
|
290 if (requestsNumber == agreedNumber && queAndSugStatus.equals(PASS)) {
|
|
291 setProperty(STATUS, PASS);
|
52
|
292 } else if (requestsNumber == deniedNumber
|
|
293 && queAndSugStatus.equals(FAILED)) {
|
|
294 setProperty(STATUS, FAILED);
|
53
|
295 } else {
|
|
296 setProperty(STATUS, UNKNOWN);
|
52
|
297 }
|
|
298 } else if (inverseRefutationStatus != null & queAndSugStatus == null) {
|
|
299 if (requestsNumber == agreedNumber && inverseRefutationStatus.equals(PASS)) {
|
|
300 setProperty(STATUS, PASS);
|
|
301 } else if (requestsNumber == deniedNumber
|
|
302 && inverseRefutationStatus.equals(FAILED)) {
|
|
303 setProperty(STATUS, FAILED);
|
53
|
304 } else {
|
|
305 setProperty(STATUS, UNKNOWN);
|
52
|
306 }
|
|
307 }else if (inverseRefutationStatus != null & queAndSugStatus != null) {
|
|
308 String childStatus = UNKNOWN;
|
|
309 if (inverseRefutationStatus.equals(queAndSugStatus)) {
|
|
310 childStatus = inverseRefutationStatus;
|
|
311 }
|
53
|
312 if ( requestsNumber == agreedNumber &&childStatus.equals(PASS) ){
|
52
|
313 setProperty(STATUS, PASS);
|
|
314 } else if ( requestsNumber == deniedNumber && childStatus.equals(DENIED)) {
|
|
315 setProperty(STATUS, FAILED);
|
|
316 } else {
|
|
317 setProperty(STATUS, UNKNOWN);
|
|
318 }
|
42
|
319 }
|
|
320 String nowStatus = getProperty(STATUS).toString();
|
|
321 return nowStatus.equals(preStatus);
|
|
322 }
|
52
|
323
|
|
324 private Boolean checkMajority(long requestsNumber, long agreedNumber,
|
|
325 long deniedNumber) {
|
42
|
326 // TODO
|
|
327 return false;
|
|
328 }
|
52
|
329
|
42
|
330 public void computeAndUpdateStatus() {
|
47
|
331 /* Check child claim */
|
48
|
332 String inverseRefutationStatus = checkRefutationClaims();
|
47
|
333 String queAndSugStatus = checkQuestionAndSuggestionClaims();
|
|
334 /* Check user request status */
|
52
|
335 Object[] requestEdges = getRequestEdges();
|
42
|
336 String type = getProperty(TYPE).toString();
|
52
|
337 long requestsNumber = requestEdges.length;
|
42
|
338 long agreedNumber = getAgreedNumber(requestEdges);
|
|
339 long deniedNumber = getDeninedNumber(requestEdges);
|
48
|
340 Boolean notChanged = false;
|
42
|
341 if (type.equals(UNANIMOUSLY)) {
|
52
|
342 notChanged = checkUnanimously(inverseRefutationStatus,
|
|
343 queAndSugStatus, requestsNumber, agreedNumber, deniedNumber);
|
42
|
344 } else if (type.equals(MAJORITY)) {
|
55
|
345 /* !!! !!! */
|
|
346 notChanged = checkUnanimously(inverseRefutationStatus,
|
|
347 queAndSugStatus, requestsNumber, agreedNumber, deniedNumber);
|
42
|
348 } else {
|
48
|
349 notChanged = false;
|
|
350 }
|
|
351 if (notChanged) {
|
|
352 return;
|
42
|
353 }
|
48
|
354 ClaimModel parentClaim = new ClaimModel(getParentVertex());
|
52
|
355 if (parentClaim.getVertex() == null) { // If parentClaim is Root.
|
48
|
356 return;
|
42
|
357 }
|
52
|
358 parentClaim.computeAndUpdateStatus();
|
42
|
359 }
|
52
|
360
|
46
|
361 private String checkQuestionAndSuggestionClaims() {
|
52
|
362 Iterable<Vertex> iter = getVertexIterable(Direction.OUT, L_QUESTION,
|
|
363 L_SUGGESTION);
|
44
|
364 if (iter == null) {
|
48
|
365 return null;
|
44
|
366 }
|
|
367 String status = null;
|
|
368 for (Vertex v : iter) {
|
|
369 String nextChildStatus = v.getProperty(STATUS).toString();
|
|
370 if (status == null) {
|
|
371 status = nextChildStatus;
|
|
372 }
|
|
373 if (!nextChildStatus.equals(status)) {
|
|
374 return UNKNOWN;
|
|
375 }
|
|
376 }
|
|
377 return status;
|
|
378 }
|
52
|
379
|
46
|
380 private String checkRefutationClaims() {
|
48
|
381 Iterable<Vertex> iter = getVertexIterable(Direction.OUT, L_REFUTATION);
|
46
|
382 if (iter == null) {
|
48
|
383 return null;
|
44
|
384 }
|
46
|
385 String status = null;
|
|
386 for (Vertex v : iter) {
|
|
387 String childStatus = v.getProperty(STATUS).toString();
|
|
388 if (status == null) {
|
|
389 status = childStatus;
|
|
390 }
|
|
391 if (status.equals(PASS)) {
|
|
392 return FAILED;
|
|
393 } else if (!status.equals(childStatus)) {
|
|
394 status = UNKNOWN;
|
|
395 }
|
|
396 }
|
|
397 if (status.equals(FAILED)) {
|
44
|
398 return PASS;
|
|
399 }
|
52
|
400 return UNKNOWN;
|
44
|
401 }
|
52
|
402
|
44
|
403 private String checkAllChildsStatus() {
|
45
|
404 String queAndSugStatus = checkQuestionAndSuggestionClaims();
|
|
405 String refutationStatus = checkRefutationClaims();
|
|
406 if (refutationStatus.equals(FAILED)) {
|
|
407 return FAILED;
|
46
|
408 } else if (refutationStatus.equals(queAndSugStatus)) {
|
45
|
409 return queAndSugStatus;
|
44
|
410 } else {
|
|
411 return UNKNOWN;
|
|
412 }
|
|
413 }
|
52
|
414
|
42
|
415 private Vertex getParentVertex() {
|
52
|
416 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
|
|
417 pipe.start(vertex).in(L_QUESTION, L_REFUTATION, L_SUGGESTION);
|
42
|
418 if (pipe.hasNext()) {
|
|
419 return pipe.next();
|
|
420 }
|
|
421 return null;
|
|
422 }
|
14
|
423 }
|