Mercurial > hg > Members > nobuyasu > Consensus
annotate app/models/TPGraph.java @ 30:80b5628f17d8
modified ClaimModel/getInfo action
author | one |
---|---|
date | Wed, 03 Oct 2012 14:27:44 +0900 |
parents | e246e0dac9c8 |
children | 5d422941b702 |
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"; |
23 | |
13 | 24 |
7 | 25 private TPGraph() { |
26 | |
27 } | |
28 | |
29 public static TPGraph getInstance() { | |
30 return instance; | |
31 } | |
32 | |
33 private Graph graph; | |
34 private String path = null; | |
35 | |
36 public void setPath(String path) { | |
37 this.path = path; | |
38 } | |
39 | |
40 public Graph newGraph() { | |
41 if (path == null) { | |
42 graph = new TinkerGraph(); | |
43 } else { | |
44 graph = new TinkerGraph(path); | |
45 | |
46 } | |
47 return graph; | |
48 } | |
49 | |
50 public Graph getGraph() { | |
51 return graph; | |
52 } | |
53 | |
8 | 54 public void setClaimRootId(Object id) { |
55 this.claimRootId = id; | |
7 | 56 } |
57 | |
8 | 58 public void setUserRootId(Object id) { |
59 this.userRootId = id; | |
7 | 60 } |
61 | |
13 | 62 public Object getClaimRootId() { |
63 return claimRootId; | |
64 } | |
65 | |
8 | 66 public Object getUserRootId() { |
67 return userRootId; | |
68 } | |
69 | |
70 public Vertex getClaimRootVertex() { | |
13 | 71 return graph.getVertex(claimRootId); |
7 | 72 } |
73 | |
8 | 74 public Vertex getUserRootVertex() { |
75 return graph.getVertex(userRootId); | |
7 | 76 } |
8 | 77 |
25 | 78 private Edge setLabel(Vertex fromV, Vertex toV, String label) { |
79 return graph.addEdge(null, fromV, toV, label); | |
18 | 80 } |
81 | |
25 | 82 public Edge setLabelToRootUser(UserModel user) { |
8 | 83 Vertex rootUser = getUserRootVertex(); |
18 | 84 |
85 // rootUser ---child---> newUser | |
25 | 86 return setLabel(rootUser, user.getVertex(), CHILD); |
13 | 87 } |
8 | 88 |
25 | 89 public Edge setLabelToRootClaim(ClaimModel claim) { |
13 | 90 Vertex rootClaim = getClaimRootVertex(); |
18 | 91 |
92 // rootUser ---child---> newUser | |
25 | 93 return setLabel(rootClaim, claim.getVertex(), CHILD); |
8 | 94 } |
95 | |
25 | 96 public Edge setLabelToAuthor(ClaimModel claim, String author) { |
16 | 97 Vertex authorVertex = graph.getVertex(author); |
98 | |
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); | |
106 if (userVertex == null) return false; | |
20 | 107 setLabel(claim.getVertex(), userVertex, label); |
18 | 108 } |
109 return true; | |
16 | 110 } |
9 | 111 |
30 | 112 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) { |
113 for (String userName: users) { | |
114 Vertex userVertex = graph.getVertex(userName); | |
115 if (userVertex == null) return false; | |
116 Edge edge = setLabel(claim.getVertex(), userVertex, label); | |
117 edge.setProperty(NodeModel.STATUS, status); | |
118 } | |
119 return true; | |
120 } | |
121 | |
25 | 122 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) { |
123 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label); | |
124 } | |
125 | |
23 | 126 public Object[] checkConsensus(HashSet<Object> set) { |
127 Iterator<Object> iter = set.iterator(); | |
27 | 128 |
129 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
|
130 Object childId = iter.next(); |
27 | 131 ArrayList<Object> array = getAllUpperVertex(childId); |
132 for (Object parentId: array.toArray()) { | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
133 if (set.contains(parentId)) { |
27 | 134 if (set.contains(childId)) { |
135 | |
136 // This behavior is anxiety. | |
137 set.remove(childId); | |
138 iter = set.iterator(); | |
139 } | |
140 childId = parentId; | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
141 } |
23 | 142 } |
143 } | |
24
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
144 return set.toArray(); |
23 | 145 } |
146 | |
27 | 147 |
148 public ArrayList<Object> getAllUpperVertex(Object id) { | |
149 Vertex startV = graph.getVertex(id); | |
150 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); | |
151 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
152 ArrayList<Object> vertexArray = new ArrayList<Object>(); | |
153 | |
154 while (pipe.hasNext()) { | |
155 Object e = pipe.next().getId(); | |
156 vertexArray.add(e); | |
157 pipe.start(graph.getVertex(e)).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION); | |
158 } | |
159 | |
160 return vertexArray; | |
161 } | |
162 | |
163 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
|
164 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
|
165 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
|
166 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
|
167 if (pipe.hasNext()) { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
168 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
|
169 return v.getId(); |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
170 } else { |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
171 return null; |
81d1d7c7bcde
create getConsensus action. but this action can not test because there is no createMention action.
one
parents:
23
diff
changeset
|
172 } |
23 | 173 } |
174 | |
7 | 175 |
176 public void shutdownGraph() { | |
177 graph.shutdown(); | |
178 } | |
179 | |
180 } |