changeset 24:81d1d7c7bcde

create getConsensus action. but this action can not test because there is no createMention action.
author one
date Wed, 03 Oct 2012 01:46:43 +0900
parents cad8e054fe4b
children 97b249d9fad1
files app/controllers/User.java app/models/NodeModel.java app/models/TPGraph.java app/models/UserModel.java test/RequestTest.java
diffstat 5 files changed, 67 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/User.java	Tue Oct 02 23:46:55 2012 +0900
+++ b/app/controllers/User.java	Wed Oct 03 01:46:43 2012 +0900
@@ -2,7 +2,6 @@
 
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 
 import com.tinkerpop.blueprints.Graph;
 import com.tinkerpop.blueprints.Vertex;
@@ -34,7 +33,6 @@
 
 
 	public static Result getUser(String name) {
-		
 		TPGraph tpGraph = TPGraph.getInstance();
 		Graph graph = tpGraph.getGraph();
 		Vertex v = graph.getVertex(name);
@@ -42,7 +40,7 @@
 			return notFound();
 		} else {
 			UserModel user = new UserModel(v);
-			HashMap<Object, Object[]> hash = user.getUserInfo();
+			HashMap<Object, Object[]> hash = user.getUserInfo(tpGraph);
 			return created(Json.toJson(hash));
 		}
 	}
@@ -69,7 +67,6 @@
 			return notFound("user: "+name+" not found");
 		} else {
 			UserModel user = new UserModel(v);
-			System.out.println(name);
 			HashSet<Object> set = user.getClaimsAndRequests();
 			if (set == null) return notFound("Consensus not found");
 			Object[] consensus = tpGraph.checkConsensus(set);
@@ -90,12 +87,5 @@
 			return created(Json.toJson(claims));
 		}		
 	}
-	
-	
-	
-	
-	
-	
-	
-	
+		
 }
--- a/app/models/NodeModel.java	Tue Oct 02 23:46:55 2012 +0900
+++ b/app/models/NodeModel.java	Wed Oct 03 01:46:43 2012 +0900
@@ -21,10 +21,10 @@
 	 *  Edge Labels.
 	 */
 	public static  final String L_AUTHOR = "author";
+	public static  final String L_REQUEST = "request";
 	public static  final String L_QUESTION = "question";
 	public static  final String L_REFUTATION = "refutation";
-	public static  final String L_REQUEST = "request";
-
+	public static  final String L_SUGGESTION = "suggestion";
 
 	public static  final String MAJORITY = "majority";
 	public static  final String UNANIMOUSLY = "unanimously";
--- a/app/models/TPGraph.java	Tue Oct 02 23:46:55 2012 +0900
+++ b/app/models/TPGraph.java	Wed Oct 03 01:46:43 2012 +0900
@@ -6,6 +6,7 @@
 import com.tinkerpop.blueprints.Graph;
 import com.tinkerpop.blueprints.Vertex;
 import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
 
 public class TPGraph {
 
@@ -107,22 +108,32 @@
 	}
 	
 	public Object[] checkConsensus(HashSet<Object> set) {
-		HashSet<Object> consensus = new HashSet<Object>();
 		Iterator<Object> iter = set.iterator();
 		while(iter.hasNext()) {
-			Object o = iter.next();
-			Iterator<Object> iter2 = set.iterator();
-			while(iter2.hasNext()) {
-				Object o2 = iter2.next();
+			Object childId = iter.next();
+			Object parentId;
+			while ( (parentId = checkUpperVertex(childId)) != null){
+				if (set.contains(parentId)) {
+					set.remove(childId);
+					iter = set.iterator();
+					continue;
+				}
+				childId = parentId;
 			}
 		}
-		return consensus.toArray();
+		return set.toArray();
 	}
 	
-	private Object checkUpperVertex(Object id) {
-		Vertex v = graph.getVertex(id);
-		
-		return null;
+	public Object checkUpperVertex(Object id) {
+		Vertex startV = graph.getVertex(id);
+		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+		pipe.start(startV).in(NodeModel.L_QUESTION, NodeModel.L_REFUTATION, NodeModel.L_SUGGESTION);
+		if (pipe.hasNext()) { 
+			Vertex v = pipe.next();
+			return v.getId();
+		} else {
+			return null;
+		}
 	}
 	
 	
--- a/app/models/UserModel.java	Tue Oct 02 23:46:55 2012 +0900
+++ b/app/models/UserModel.java	Wed Oct 03 01:46:43 2012 +0900
@@ -3,7 +3,6 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 
 import com.tinkerpop.blueprints.Vertex;
 import com.tinkerpop.gremlin.java.GremlinPipeline;
@@ -35,24 +34,35 @@
 	}
 	
 	public HashSet<Object> getClaimsAndRequests() {
+		Object[] requests = getUserRequests();
 		Object[] claims = getUserClaims();
-		Object[] requests = getUserRequests();
+		return makeSet(requests, claims);
+	}
+	
+	private HashSet<Object> makeSet(Object[] obj1, Object[] obj2) {
 		HashSet<Object> set = new HashSet<Object>();
-		if (claims != null)
-			for (Object id : claims) set.add(id);
-		if (requests != null)
-			for (Object id : requests) set.add(id);
+		if (obj1 != null)
+			for (Object o : obj1) set.add(o);
+		if (obj2 != null) 
+			for (Object o : obj2) set.add(o);
 		if (set.size() == 0) return null;
 		return set;
-		
+	}
 
+	/*
+	public Object[] getUserConsensus() {
+		return null;
 	}
+	 */
 	
-	public HashMap<Object,Object[]> getUserInfo() {
+	public HashMap<Object,Object[]> getUserInfo(TPGraph tpGraph) {
+		Object[] requests = getUserRequests();
+		Object[] claims = getUserClaims();
 		HashMap<Object,Object[]> hash = new HashMap<Object, Object[]>();
-		hash.put(NodeModel.REQUESTS, getUserRequests());
-		hash.put(NodeModel.CLAIMS, getUserClaims());
-		hash.put(NodeModel.CONSENSUS, getUserConsensus());
+		hash.put(NodeModel.REQUESTS, requests);
+		hash.put(NodeModel.CLAIMS, claims);
+		HashSet<Object> set = makeSet(requests, claims);
+		hash.put(NodeModel.CONSENSUS, tpGraph.checkConsensus(set));
 		return hash;
 	}
 
--- a/test/RequestTest.java	Tue Oct 02 23:46:55 2012 +0900
+++ b/test/RequestTest.java	Wed Oct 03 01:46:43 2012 +0900
@@ -25,15 +25,34 @@
 		String[] users = {user2,user3};
 		createClaim(user1, users);
 		createClaim(user1, users);
+/*
+		HashSet<Object> set = new HashSet<Object>();
+		set.add("1");
+		set.add("2");
+		set.add("3");
+		Iterator<Object> iter = set.iterator();
+		while (iter.hasNext() ) {
+			Object o = iter.next();
+			System.out.println("o = "+o);
+		}
 
+		Iterator<Object> iter2 = set.iterator();
+		while (iter2.hasNext() ) {
+			Object o = iter2.next();
+			System.out.println("o = "+o);
+			if (set.remove("2")) {
+				iter2 = set.iterator();
+			}
+		}*/
+		
 /*
 		getUser(user1);
-		getUserInfo(user2,"requests/");
 		getUserInfo(user2,"claims/");
 */
 
+		getUserInfo(user2,"requests/");
 		getUserInfo(user3,"requests/");
-		getUserInfo(user3,"claims/");
+//		getUserInfo(user3,"claims/");
 		getUserInfo(user3,"consensus/");
 
 	}