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