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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
1 package models;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
2
27
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
3 import java.util.ArrayList;
23
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
4 import java.util.HashSet;
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
5 import java.util.Iterator;
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
6
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
7 import com.tinkerpop.blueprints.Edge;
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
8 import com.tinkerpop.blueprints.Graph;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
9 import com.tinkerpop.blueprints.Vertex;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
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
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
12
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
13 public class TPGraph {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
14
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
15 private static TPGraph instance = new TPGraph();
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
16 private Object claimRootId;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
17 private Object userRootId;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
18
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
19 /*
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
20 * Edge type
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
21 */
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
22 protected final String CHILD = "child";
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
23
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
24
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
25 private TPGraph() {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
26
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
27 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
28
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
29 public static TPGraph getInstance() {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
30 return instance;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
31 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
32
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
33 private Graph graph;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
34 private String path = null;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
35
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
36 public void setPath(String path) {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
37 this.path = path;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
38 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
39
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
40 public Graph newGraph() {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
41 if (path == null) {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
42 graph = new TinkerGraph();
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
43 } else {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
44 graph = new TinkerGraph(path);
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
45
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
46 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
47 return graph;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
48 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
49
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
50 public Graph getGraph() {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
51 return graph;
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
52 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
53
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
54 public void setClaimRootId(Object id) {
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
55 this.claimRootId = id;
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
56 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
57
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
58 public void setUserRootId(Object id) {
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
59 this.userRootId = id;
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
60 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
61
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
62 public Object getClaimRootId() {
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
63 return claimRootId;
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
64 }
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
65
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
66 public Object getUserRootId() {
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
67 return userRootId;
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
68 }
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
69
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
70 public Vertex getClaimRootVertex() {
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
71 return graph.getVertex(claimRootId);
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
72 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
73
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
74 public Vertex getUserRootVertex() {
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
75 return graph.getVertex(userRootId);
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
76 }
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
77
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
78 private Edge setLabel(Vertex fromV, Vertex toV, String label) {
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
79 return graph.addEdge(null, fromV, toV, label);
18
one
parents: 17
diff changeset
80 }
one
parents: 17
diff changeset
81
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
82 public Edge setLabelToRootUser(UserModel user) {
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
83 Vertex rootUser = getUserRootVertex();
18
one
parents: 17
diff changeset
84
one
parents: 17
diff changeset
85 // rootUser ---child---> newUser
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
86 return setLabel(rootUser, user.getVertex(), CHILD);
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
87 }
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
88
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
89 public Edge setLabelToRootClaim(ClaimModel claim) {
13
9b677755cb93 create action Claim/createClaim
one
parents: 9
diff changeset
90 Vertex rootClaim = getClaimRootVertex();
18
one
parents: 17
diff changeset
91
one
parents: 17
diff changeset
92 // rootUser ---child---> newUser
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
93 return setLabel(rootClaim, claim.getVertex(), CHILD);
8
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
94 }
7b314898fddd create action User.getUser()
one
parents: 7
diff changeset
95
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
96 public Edge setLabelToAuthor(ClaimModel claim, String author) {
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
97 Vertex authorVertex = graph.getVertex(author);
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
98
18
one
parents: 17
diff changeset
99 // claim ---author---> authorVertex(userVertex)
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
100 return setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR);
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
101 }
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
102
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
103 public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) {
18
one
parents: 17
diff changeset
104 for (String userName: users) {
one
parents: 17
diff changeset
105 Vertex userVertex = graph.getVertex(userName);
one
parents: 17
diff changeset
106 if (userVertex == null) return false;
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
107 setLabel(claim.getVertex(), userVertex, label);
18
one
parents: 17
diff changeset
108 }
one
parents: 17
diff changeset
109 return true;
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
110 }
9
d050b7fb4cda create getUserRequests action
one
parents: 8
diff changeset
111
30
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
112 public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) {
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
113 for (String userName: users) {
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
114 Vertex userVertex = graph.getVertex(userName);
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
115 if (userVertex == null) return false;
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
116 Edge edge = setLabel(claim.getVertex(), userVertex, label);
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
117 edge.setProperty(NodeModel.STATUS, status);
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
118 }
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
119 return true;
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
120 }
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 27
diff changeset
121
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
122 public Edge setLabelMention(ClaimModel fromClaim, ClaimModel toClaim, String label) {
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
123 return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
124 }
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 24
diff changeset
125
23
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
126 public Object[] checkConsensus(HashSet<Object> set) {
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
127 Iterator<Object> iter = set.iterator();
27
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
128
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
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
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
131 ArrayList<Object> array = getAllUpperVertex(childId);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
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
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
134 if (set.contains(childId)) {
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
135
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
136 // This behavior is anxiety.
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
137 set.remove(childId);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
138 iter = set.iterator();
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
139 }
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
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
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
142 }
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
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
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
145 }
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
146
27
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
147
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
148 public ArrayList<Object> getAllUpperVertex(Object id) {
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
149 Vertex startV = graph.getVertex(id);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
150 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
151 pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
152 ArrayList<Object> vertexArray = new ArrayList<Object>();
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
153
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
154 while (pipe.hasNext()) {
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
155 Object e = pipe.next().getId();
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
156 vertexArray.add(e);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
157 pipe.start(graph.getVertex(e)).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION);
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
158 }
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
159
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
160 return vertexArray;
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
161 }
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
162
e246e0dac9c8 modified checkConsensus
one
parents: 26
diff changeset
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
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
173 }
cad8e054fe4b modifying getConsensus action
one
parents: 20
diff changeset
174
7
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
175
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
176 public void shutdownGraph() {
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
177 graph.shutdown();
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
178 }
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
179
2122c50278bd add Global.java, ande models/*
one
parents:
diff changeset
180 }