changeset 84:bb547f2a3c88 draft

create getClaimRevision methos
author one
date Sat, 09 Mar 2013 23:58:01 +0900
parents c6929060c85f
children d45f76774fd8
files app/controllers/Claim.java app/models/ClaimModel.java app/models/NodeModel.java app/models/TPGraph.java app/models/UserModel.java conf/routes
diffstat 6 files changed, 173 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/Claim.java	Sat Mar 09 21:32:00 2013 +0900
+++ b/app/controllers/Claim.java	Sat Mar 09 23:58:01 2013 +0900
@@ -178,6 +178,12 @@
 		ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();
 		return ok(resultEntity);
 	}
+	
+	public static Result getClaimRevision(String id) {
+		TPGraph tpGraph = TPGraph.getInstance();
+		Object[] revision = tpGraph.getClaimRevision(id);
+		return created(Json.toJson(revision));
+	}
 
 	public static Result copyClaims(String id) {
 		TPGraph tpGraph = TPGraph.getInstance();
--- a/app/models/ClaimModel.java	Sat Mar 09 21:32:00 2013 +0900
+++ b/app/models/ClaimModel.java	Sat Mar 09 23:58:01 2013 +0900
@@ -85,85 +85,13 @@
 		return array.toArray();
 	}
 
-	private Iterable<Vertex> getVertexIterable(Direction direction,
-			String... labels) {
-		GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
-		if (direction.equals(Direction.IN)) {
-			pipe.start(vertex).in(labels);
-		} else if (direction.equals(Direction.OUT)) {
-			pipe.start(vertex).out(labels);
-		} else {
-			pipe.start(vertex).both(labels);
-		}
-		ArrayList<Vertex> array = new ArrayList<Vertex>();
-		for (Vertex v : pipe) {
-			array.add(v);
-		}
-		if (array.size() == 0) {
-			return null;
-		}
-		return array;
-	}
-
-	private Object[] getVertexArray(Direction direction, String... labels) {
-		Iterable<Vertex> iter = getVertexIterable(direction, labels);
-		if (iter == null) {
-			return null;
-		}
-		ArrayList<Object> array = new ArrayList<Object>();
-		for (Vertex v : iter) {
-			array.add(v.getId());
-		}
-		return array.toArray();
-	}
-
 	public Object[] getMentionsId() {
-		return getVertexArray(Direction.OUT, L_QUESTION, L_REFUTATION,
+		return getVertexIdArrayTraverseLabel(Direction.OUT, L_QUESTION, L_REFUTATION,
 				L_SUGGESTION);
 	}
 
 	public Object[] getUsersId() {
-		return getVertexArray(Direction.OUT, L_REQUEST);
-	}
-
-	private Iterable<Edge> getEdgeIterable(Direction direction,
-			String... labels) {
-		GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
-		if (direction.equals(Direction.IN)) {
-			pipe.start(vertex).inE(labels);
-		} else if (direction.equals(Direction.OUT)) {
-			pipe.start(vertex).outE(labels);
-		} else {
-			pipe.start(vertex).bothE(labels);
-		}
-		ArrayList<Edge> array = new ArrayList<Edge>();
-		for (Edge v : pipe) {
-			array.add(v);
-		}
-		if (array.size() == 0) {
-			return null;
-		}
-		return array;
-	}
-
-	public Object[] getEdgeArray(Direction direction, String... labels) {
-		Iterable<Edge> iter = getEdgeIterable(direction, labels);
-		/*
-		 * GremlinPipeline<Vertex,Edge> pipe = new
-		 * GremlinPipeline<Vertex,Edge>(); pipe.start(vertex).outE(labels);
-		 */
-		ArrayList<Object> array = new ArrayList<Object>();
-		for (Edge e : iter) {
-			array.add(e.getId());
-		}
-		if (array.size() == 0) {
-			return null;
-		}
-		return array.toArray();
-	}
-
-	public Object[] getRequestEdges() {
-		return getEdgeArray(Direction.OUT, L_REQUEST);
+		return getVertexIdArrayTraverseLabel(Direction.OUT, L_REQUEST);
 	}
 
 	public ObjectNode getUserRequestStatus(UserModel user) {
@@ -215,7 +143,7 @@
 	}
 
 	public Object[] getRequestUsersId() {
-		return getVertexArray(Direction.OUT, NodeModel.REQUESTS);
+		return getVertexIdArrayTraverseLabel(Direction.OUT, NodeModel.REQUESTS);
 	}
 
 	public void editRequestsEdgeUsers(Object[] updateUsers) {
@@ -404,7 +332,7 @@
 		return UNKNOWN;
 	}
 
-	private String checkAllChildsStatus() {
+	public String checkAllChildsStatus() {
 		String queAndSugStatus = checkQuestionAndSuggestionClaims();
 		String refutationStatus = checkRefutationClaims();
 		if (refutationStatus.equals(FAILED)) {
--- a/app/models/NodeModel.java	Sat Mar 09 21:32:00 2013 +0900
+++ b/app/models/NodeModel.java	Sat Mar 09 23:58:01 2013 +0900
@@ -1,8 +1,12 @@
 package models;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 
+import com.tinkerpop.blueprints.Direction;
+import com.tinkerpop.blueprints.Edge;
 import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
 
 public class NodeModel {
 
@@ -92,4 +96,77 @@
 		return properties;
 	}
 
+	protected Iterable<Vertex> getVertexIterable(Direction direction,
+			String... labels) {
+		GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
+		if (direction.equals(Direction.IN)) {
+			pipe.start(vertex).in(labels);
+		} else if (direction.equals(Direction.OUT)) {
+			pipe.start(vertex).out(labels);
+		} else {
+			pipe.start(vertex).both(labels);
+		}
+		ArrayList<Vertex> array = new ArrayList<Vertex>();
+		for (Vertex v : pipe) {
+			array.add(v);
+		}
+		if (array.size() == 0) {
+			return null;
+		}
+		return array;
+	}
+
+	public Object[] getVertexIdArrayTraverseLabel(Direction direction, String... labels) {
+		Iterable<Vertex> iter = getVertexIterable(direction, labels);
+		if (iter == null) {
+			return null;
+		}
+		ArrayList<Object> array = new ArrayList<Object>();
+		for (Vertex v : iter) {
+			array.add(v.getId());
+		}
+		return array.toArray();
+	}
+	
+	protected Iterable<Edge> getEdgeIterable(Direction direction,
+			String... labels) {
+		GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
+		if (direction.equals(Direction.IN)) {
+			pipe.start(vertex).inE(labels);
+		} else if (direction.equals(Direction.OUT)) {
+			pipe.start(vertex).outE(labels);
+		} else {
+			pipe.start(vertex).bothE(labels);
+		}
+		ArrayList<Edge> array = new ArrayList<Edge>();
+		for (Edge v : pipe) {
+			array.add(v);
+		}
+		if (array.size() == 0) {
+			return null;
+		}
+		return array;
+	}
+
+	public Object[] getEdgeArray(Direction direction, String... labels) {
+		Iterable<Edge> iter = getEdgeIterable(direction, labels);
+		/*
+		 * GremlinPipeline<Vertex,Edge> pipe = new
+		 * GremlinPipeline<Vertex,Edge>(); pipe.start(vertex).outE(labels);
+		 */
+		ArrayList<Object> array = new ArrayList<Object>();
+		for (Edge e : iter) {
+			array.add(e.getId());
+		}
+		if (array.size() == 0) {
+			return null;
+		}
+		return array.toArray();
+	}
+
+	public Object[] getRequestEdges() {
+		return getEdgeArray(Direction.OUT, L_REQUEST);
+	}
+
+
 }
--- a/app/models/TPGraph.java	Sat Mar 09 21:32:00 2013 +0900
+++ b/app/models/TPGraph.java	Sat Mar 09 23:58:01 2013 +0900
@@ -4,6 +4,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 
+import com.tinkerpop.blueprints.Direction;
 import com.tinkerpop.blueprints.Edge;
 import com.tinkerpop.blueprints.Graph;
 import com.tinkerpop.blueprints.Vertex;
@@ -253,7 +254,6 @@
 		return v;
 	}
 
-	
 	private void recursiveCopyDownClaimsAndSetLabel(ClaimModel oldUpClaim, ClaimModel latestUpClaim,
 			String timestamp, String... labels) {
 		for (String label: labels) {
@@ -281,6 +281,82 @@
 		return latestTopClaim;
 	}
 	
+	public Object[] checkLatestVertices(Object[] vIds) {
+		ArrayList<Object> array = new ArrayList<Object>();
+		for (Object vId: vIds) {
+			GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+			pipe.start(getVertex(vId)).in(NodeModel.L_PREV);
+			/*
+			 * get latest claims.
+			 * c1 <--prev-- c1' <--prev-- c1'' 
+			 * c1'' is latest.
+			 */
+			if (!pipe.hasNext()) {
+				array.add(vId);
+			}			
+		}
+		return array.toArray();
+	}
+	
+	public Object getLatestVertexId(Object id) {
+		Vertex v = getVertex(id);
+		if (v == null) {
+			return null;
+		}
+		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+		pipe.start(v).in(NodeModel.L_PREV);
+		while (pipe.hasNext()) {
+			/*
+			 * get latest claims.
+			 * v1 <--prev-- v1' <--prev-- v1'' 	
+			 * c1'' is latest.
+			 */
+			pipe.start(v).in(NodeModel.L_PREV);
+			v = pipe.next();
+		}
+		return v.getId();
+	}
+	
+	private ArrayList<Object> getVertexIdRecursiveTraverse(NodeModel vModel, Direction direction, String... labels) {
+		ArrayList<Object> array = new ArrayList<Object>();
+		ArrayList<Object> nextArray =  new ArrayList<Object>();
+		Object[] ids = vModel.getVertexIdArrayTraverseLabel(direction, labels);
+		if (ids == null) {
+			return new ArrayList<Object>();
+		}
+		for (Object id: ids) {
+			array.add(id);
+			NodeModel nextModel = new NodeModel(getVertex(id));
+			ArrayList<Object> tmpArray = getVertexIdRecursiveTraverse(nextModel, direction, labels); 
+			if (!tmpArray.isEmpty()) {
+				nextArray.addAll(tmpArray);
+			}
+		}
+		if (!nextArray.isEmpty()) {
+			array.addAll(nextArray);
+		}
+		return array;
+	}
+
+	/* 
+	 * [latestId, latestId-1, ..., id, ..., oldIds+1, oldIds]
+	 */
+	public Object[] getClaimRevision(String id) {
+		NodeModel vModel = new NodeModel(getVertex(id));
+		if (vModel.getVertex() == null) {
+			return null;
+		}
+		ArrayList<Object> array = new ArrayList<Object>();
+		ArrayList<Object> inPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.IN, NodeModel.L_PREV);
+		for (int i=inPrevIds.size()-1; i>=0; i--) {
+			array.add(inPrevIds.get(i));
+		}
+		array.add(id);
+		ArrayList<Object> outPrevIds = getVertexIdRecursiveTraverse(vModel, Direction.OUT, NodeModel.L_PREV);
+		array.addAll(outPrevIds);
+		return array.toArray();
+	}
+
 	public void shutdownGraph() {
 		if (path == null) {
 			return;
@@ -288,5 +364,4 @@
 			graph.shutdown();			
 		}
 	}
-
 }
--- a/app/models/UserModel.java	Sat Mar 09 21:32:00 2013 +0900
+++ b/app/models/UserModel.java	Sat Mar 09 23:58:01 2013 +0900
@@ -36,20 +36,8 @@
 	private Object[] getLatestVertexRelatedUser(String labels) {
 		TPGraph tpGraph = TPGraph.getInstance();
 		Object[] vIds = getVertexRelatedUser(labels);
-		ArrayList<Object> array = new ArrayList<Object>();		
-		for (Object vId : vIds) {
-			GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
-			pipe.start(tpGraph.getVertex(vId)).in(L_PREV);
-			/*
-			 * get latest claims.
-			 * c1 <--prev-- c1' <--prev-- c1'' 
-			 * c1'' is latest.
-			 */
-			if (!pipe.hasNext()) {
-				array.add(vId);
-			}
-		}
-		return array.toArray();
+		Object[] vArray = tpGraph.checkLatestVertices(vIds);
+		return vArray;
 	}
 	
 	public Object[] getLatestUserRequests() {
--- a/conf/routes	Sat Mar 09 21:32:00 2013 +0900
+++ b/conf/routes	Sat Mar 09 23:58:01 2013 +0900
@@ -14,7 +14,6 @@
 GET		/claims/consensus/:id		controllers.Claim.getClaimTree(id: String)
 GET		/consensus/browse/:id		controllers.Claim.getClaimTree(id: String)
 GET		/claims/answer/:id/:name	controllers.Claim.getUserConsensusStatus(id: String, name: String)
-GET		/copytree/:id				controllers.Claim.copyClaims(id: String)
 POST	/claims/answer/:id/:name/:status	controllers.Claim.updateUserConsensusStatus(id: String, name: String, status: String)
 POST	/claims/create				controllers.Claim.createClaim()
 POST	/claims/:mentionType/:id/create			controllers.Claim.createMention(mentionType: String ,id: String)
@@ -24,6 +23,13 @@
 GET		/users/latest/consensus/:name	controllers.User.getUserLatestConsensus(name: String)
 GET		/users/latest/claims/:name	controllers.User.getUserLatestClaims(name: String)
 
+# revision
+GET		/claims/get/revision/:id	controllers.Claim.getClaimRevision(id: String)
+
+# test action
+GET		/copytree/:id				controllers.Claim.copyClaims(id: String)
+
+# reset is sample action for demo.
 GET		/reset					controllers.Claim.reset()
 
 # test action