Mercurial > hg > Members > nobuyasu > Consensus
changeset 18:c8ad59a52c7e
fix
author | one |
---|---|
date | Tue, 02 Oct 2012 21:50:31 +0900 |
parents | a134edaebf6f |
children | 20b2daf76635 |
files | app/controllers/Application.java app/controllers/Claim.java app/controllers/User.java app/models/ClaimModel.java app/models/NodeModel.java app/models/TPGraph.java app/models/UserModel.java test/RequestTest.java |
diffstat | 8 files changed, 107 insertions(+), 133 deletions(-) [+] |
line wrap: on
line diff
--- a/app/controllers/Application.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/controllers/Application.java Tue Oct 02 21:50:31 2012 +0900 @@ -15,7 +15,8 @@ public class Application extends Controller { public static Result index() { - return ok(index.render("Your new application is ready.")); +// return ok(index.render("Your new application is ready.")); + return ok(); } public static Result test(Long[] id) {
--- a/app/controllers/Claim.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/controllers/Claim.java Tue Oct 02 21:50:31 2012 +0900 @@ -25,18 +25,16 @@ @BodyParser.Of(BodyParser.Json.class) public static Result crateClaim() { JsonNode json = request().body().asJson(); - String author = json.findPath(NodeModel.AUTHOR).getTextValue(); // Author + String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); - if ( graph.getVertex(author) == null) return badRequest("Author "+ author + "is not exist."); + if ( graph.getVertex(author) == null) + return badRequest("Author "+ author + "is not exist."); JsonNode toulmin = json.findPath(NodeModel.TOULMIN); - ObjectNode result = Json.newObject(); - if (toulmin.findPath(NodeModel.TITLE) == null) { - result.put("message", "Please set title"); - return badRequest(result); - } + if (toulmin.findPath(NodeModel.TITLE) == null) + return badRequest("Please set title"); JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) @@ -44,15 +42,23 @@ Vertex claimVertex = null; claimVertex = graph.addVertex(null); ClaimModel newClaim = new ClaimModel(claimVertex); - newClaim.setClaimProperties(toulmin, author, usersJson.toString(), type); - tpGraph.updateUserVertex(claimVertex, usersJson); + tpGraph.setLabelToAuthor(author, newClaim); + newClaim.setClaimProperties(toulmin, type); + String[] users = toStringArray(usersJson); + tpGraph.setLabelToUsers(claimVertex, users, NodeModel.L_REQUEST); tpGraph.setLabelToRootClaim(newClaim); tpGraph.setLabelToAuthor(author, newClaim); - - return created(); - - + } + + private static String[] toStringArray(JsonNode jsonNode) { + int length = jsonNode.size(); + if (length == 0) return null; + String[] userArray = new String[length]; + for (int i=0; i<length; i++ ) { + userArray[i] = jsonNode.get(i).getTextValue(); + } + return userArray; }
--- a/app/controllers/User.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/controllers/User.java Tue Oct 02 21:50:31 2012 +0900 @@ -26,7 +26,6 @@ return status(CONFLICT, name+" is already exists"); } newUser = new UserModel(v); - newUser.setUserInfo(); tpGraph.setLabelToRootUser(newUser); // newUser.setName(name); // user node hasn't name property(only TinkerGraph). return created(); @@ -43,6 +42,9 @@ } else { UserModel user = new UserModel(v); HashMap<Object,Object> hash = user.getAllProperty(); + +// HashMap<Object,Object> hash = user.getAllInfo(); + return created(Json.toJson(hash)); } } @@ -55,9 +57,9 @@ return notFound("user: "+name+" not found"); } else { UserModel user = new UserModel(v); - HashMap<Object,Object> hash = user.getUserRequestsHash(); - if (hash == null) return notFound("requests not found"); - return created(Json.toJson(hash)); + Object[] requests = user.getUserRequests(); + if (requests == null) return notFound("Requests not found"); + return created(Json.toJson(requests)); } } @@ -69,12 +71,12 @@ return notFound("user: "+name+" not found"); } else { UserModel user = new UserModel(v); - HashMap<Object,Object> hash = user.getUserConsensusHash(); - if (hash == null) return notFound("requests not found"); - return created(Json.toJson(hash)); + Object[] consensus = user.getUserConsensus(); + if (consensus == null) return notFound("Consensus not found"); + return created(Json.toJson(consensus)); } } - + public static Result getUserClaims(String name) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); @@ -83,13 +85,15 @@ return notFound("user: "+name+" not found"); } else { UserModel user = new UserModel(v); - HashMap<Object,Object> hash = user.getUserClaimsHash(); - if (hash == null) return notFound("requests not found"); - return created(Json.toJson(hash)); + Object[] claims = user.getUserClaims(); + if (claims == null) return notFound("Claims not found"); + return created(Json.toJson(claims)); } } + + }
--- a/app/models/ClaimModel.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/models/ClaimModel.java Tue Oct 02 21:50:31 2012 +0900 @@ -17,7 +17,7 @@ } - public void setClaimProperties(JsonNode toulmin, String author, String users, String type) { + public void setClaimProperties(JsonNode toulmin, String type) { String title = toulmin.findPath(NodeModel.TITLE).getTextValue(); String contents = toulmin.findPath(NodeModel.CONTENTS).getTextValue(); String q = toulmin.findPath(NodeModel.QUALIFIER).getTextValue(); // Qualifier @@ -35,16 +35,13 @@ t.put(BACKING, b); t.put(REBUTTLE, r); - setProperty(AUTHOR,author); - setProperty(USERS, users); setProperty(TYPE, type); setProperty(MENTIONS, null); - setProperty(STATUS, FAIL); + setProperty(STATUS, FAIL); // Default Status is fail. setProperty(TOULMIN, t); + } - - - + }
--- a/app/models/NodeModel.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/models/NodeModel.java Tue Oct 02 21:50:31 2012 +0900 @@ -14,23 +14,23 @@ public static final String ID = "id"; - public static final String AUTHOR = "author"; public static final String TYPE = "type"; public static final String MENTIONS = "mentions"; + /* + * Edge Labels. + */ + public static final String L_AUTHOR = "author"; + public static final String L_QUESTION = "question"; + public static final String L_REFUTATION = "refutation"; + public static final String L_CLAIMS = "claims"; + public static final String L_REQUEST = "request"; - public static final String QUESTION = "question"; - public static final String REFUTATION = "refutation"; public static final String MAJORITY = "majority"; public static final String UNANIMOUSLY = "unanimously"; - /* - * User property - */ public static final String CONSENSUS = "consensus"; - public static final String CLAIMS = "claims"; - public static final String REQUESTS = "requests"; /* * Claim property @@ -45,6 +45,7 @@ public static final String REBUTTLE = "r"; public static final String USERS = "users"; public static final String STATUS = "status"; + public static final String REQUESTS = "status"; // Status statement public static final String PASS = "pass"; public static final String FAIL = "fail";
--- a/app/models/TPGraph.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/models/TPGraph.java Tue Oct 02 21:50:31 2012 +0900 @@ -75,56 +75,43 @@ return graph.getVertex(userRootId); } + private void setLabel(Vertex fromV, Vertex toV, String label) { + graph.addEdge(null, fromV, toV, label); + } + public void setLabelToRootUser(UserModel user) { Vertex rootUser = getUserRootVertex(); - /* - * rootUser ---child---> newUser - */ - graph.addEdge(null, rootUser, user.getVertex(), CHILD); + + // rootUser ---child---> newUser + setLabel(rootUser, user.getVertex(), CHILD); } public void setLabelToRootClaim(ClaimModel claim) { Vertex rootClaim = getClaimRootVertex(); - /* - * rootUser ---child---> newUser - */ - graph.addEdge(null, rootClaim, claim.getVertex(), CHILD); + + // rootUser ---child---> newUser + setLabel(rootClaim, claim.getVertex(), CHILD); } public void setLabelToAuthor(String author, ClaimModel claim) { Vertex authorVertex = graph.getVertex(author); - /* - * claim ---author---> authorVertex(userVertex) - */ - graph.addEdge(null, claim.getVertex(), authorVertex, NodeModel.AUTHOR); - + // claim ---author---> authorVertex(userVertex) + setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR); } - public void updateUserVertex(Vertex claimVertex, JsonNode usersJson) { - - for (Iterator<JsonNode> iter = usersJson.getElements(); iter.hasNext();) { - JsonNode j = iter.next(); - String userName = j.toString(); - - Vertex userVertex = graph.getVertex(userName); - if (userVertex == null) continue; - UserModel user = new UserModel(userVertex); - user.appendRequests(claimVertex); - - graph.addEdge(null, claimVertex, userVertex, NodeModel.REQUESTS); - - } + public Boolean setLabelToUsers(Vertex claimVertex, String[] users, String label) { + for (String userName: users) { + Vertex userVertex = graph.getVertex(userName); + if (userVertex == null) return false; + setLabel(claimVertex, userVertex, label); + } + return true; } public void shutdownGraph() { graph.shutdown(); } - - - - - }
--- a/app/models/UserModel.java Tue Oct 02 14:51:24 2012 +0900 +++ b/app/models/UserModel.java Tue Oct 02 21:50:31 2012 +0900 @@ -11,7 +11,10 @@ import play.libs.Json; +import com.tinkerpop.blueprints.Direction; +import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.gremlin.java.GremlinPipeline; public class UserModel extends NodeModel { @@ -20,67 +23,41 @@ super(vertex); } - - public void setUserInfo() { - setProperty(CONSENSUS, null); - setProperty(CLAIMS, null); - setProperty(REQUESTS, null); - } - - public HashMap<Object, Object> getUserRequestsHash() { - HashMap<Object, Object> hash = getOnePropertyHash(REQUESTS); - return hash; - } - - public HashMap<Object, Object> getUserConsensusHash() { - HashMap<Object, Object> hash = getOnePropertyHash(CONSENSUS); - return hash; + private Object[] getEdgeInUser(String labels) { + GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>(); + pipe.start(this.vertex).in(labels); + long length = pipe.count(); + if (length == 0) return null; + Object[] requests = new Object[(int) length]; + int i = 0; + for (Vertex v :pipe) { + requests[i] = v.getId(); + i++; + } + return requests; } - public HashMap<Object, Object> getUserClaimsHash() { - HashMap<Object, Object> hash = getOnePropertyHash(CLAIMS); - return hash; + public Object[] getUserRequests() { + return getEdgeInUser(L_REQUEST); + } + + public Object[] getUserClaims() { + return getEdgeInUser(L_CLAIMS); } - - - private HashMap<Object,Object> getOnePropertyHash(String key) { - HashMap<Object, Object> hash = new HashMap<Object,Object>(1); - Object obj = vertex.getProperty(key); - if (obj == null) return null; - hash.put(key,obj); - return hash; + public Object[] getUserConsensus() { + return null; } - public Object getRequestsObject() { - return vertex.getProperty(REQUESTS); - } - public void appendRequests(Vertex claimVertex) { - - Object[] requestsObj = (Object[])getRequestsObject(); + Object[] requestsObj = getUserRequests(); int length = requestsObj == null ? 0 : requestsObj.length; Object[] newRequestsObj = new Object[length+1]; - for (int i=0; i<length; i++ ) { newRequestsObj[i] = requestsObj[i]; } newRequestsObj[length] = claimVertex.getId(); - vertex.setProperty(REQUESTS, newRequestsObj); - - /* - Object requestsObj = getRequestsObject(); - JsonNode jNodes = Json.toJson(requestsObj); - ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode(); - - for (Iterator<JsonNode> iter = jNodes.getElements(); iter.hasNext();) { - JsonNode element = iter.next(); - arrayNode.add(element); - } - - vertex.setProperty(REQUESTS, arrayNode); -*/ - +// vertex.setProperty(REQUESTS, newRequestsObj); } /*
--- a/test/RequestTest.java Tue Oct 02 14:51:24 2012 +0900 +++ b/test/RequestTest.java Tue Oct 02 21:50:31 2012 +0900 @@ -15,20 +15,22 @@ public static void main(String[] args) throws JSONException { - createUser("taro"); - /* - getUser("taro"); - getUserInfo("taro","requests/"); - getUserInfo("taro","claims/"); - getUserInfo("taro","consensus/"); + createUser("akifumi"); + createUser("takaaki"); + createUser("yosiaki"); + +/* + getUser("akifumi"); + getUserInfo("akifumi","requests/"); + getUserInfo("akifumi","claims/"); + getUserInfo("akifumi","consensus/"); */ - createClaim("taro"); - - - + String[] users = {"takaaki", "yosiaki"}; + createClaim("akifumi", users); + getUserInfo("akifumi","requests/"); } - public static void createClaim(String author) throws JSONException { + public static void createClaim(String author, String[] users) throws JSONException { JSONObject toulmin = new JSONObject(); toulmin.put(NodeModel.TITLE, "アプリでGraphDBを利用する。"); toulmin.put(NodeModel.CONTENTS, "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。"); @@ -40,8 +42,7 @@ JSONObject jobj = new JSONObject(); jobj.put(NodeModel.TOULMIN, toulmin); - jobj.put(NodeModel.AUTHOR, author); - String[] users = {"akifumi","yosiaki"}; + jobj.put(NodeModel.L_AUTHOR, author); jobj.put(NodeModel.USERS,users); jobj.put(NodeModel.TYPE, "unanimously");