Skip to content

Commit

Permalink
Relationship conflation works now. #12 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
tla committed Oct 23, 2015
1 parent f18bd60 commit 9aeaaeb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ public PathExpander reverse() {
.uniqueness(Uniqueness.NODE_GLOBAL).traverse(startNode)
.nodes().forEach(x -> {
if (!conflatedReadings.containsKey(x)) {
conflatedReadings.put(x, x);
// Traverse the readings for the given relationships.
db.traversalDescription().depthFirst()
ResourceIterable<Node> equivalent = db.traversalDescription().depthFirst()
.expand(relationConflater)
.evaluator(Evaluators.all())
.uniqueness(Uniqueness.NODE_GLOBAL).traverse(x)
.nodes().forEach(y -> conflatedReadings.put(x, y));
.nodes();
equivalent.forEach(y -> conflatedReadings.put(y, x));
}
});
}
Expand Down
29 changes: 27 additions & 2 deletions stemmarest/src/main/java/net/stemmaweb/rest/Tradition.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,37 @@ public Response getJson() {
}


// TODO rethink this API.
@POST
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public Response getJson(ArrayList<String> conflatedRelationships) {
return new Neo4JToTabularParser(db).exportAsJSON(traditionId, conflatedRelationships);
}

@GET
@Path("/csv")
@Produces(MediaType.TEXT_XML)
public Response get() {
@Produces(MediaType.TEXT_PLAIN)
public Response getCsv() {
return new Neo4JToTabularParser(db).exportAsCSV(traditionId, ',');
}

@GET
@Path("/tsv")
@Produces(MediaType.TEXT_PLAIN)
public Response getTsv() {
return new Neo4JToTabularParser(db).exportAsCSV(traditionId, '\t');
}

@POST
@Path("/tsv")
@Produces(MediaType.TEXT_PLAIN)
public Response getTsv(ArrayList<String> conflatedRelationships) {
return new Neo4JToTabularParser(db).exportAsCSV(traditionId, '\t', conflatedRelationships);
}



/**
* Recalculate ranks starting from 'startNode'
* Someone would typically use it after inserting a RELATION or a new Node into the graph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@ public Neo4JToTabularParser(GraphDatabaseService db){
this.db = db;
}

public Response exportAsJSON(String tradId) {
public Response exportAsJSON(String tradId, ArrayList<String> conflate) {
Node traditionNode = DatabaseService.getTraditionNode(tradId, db);
if(traditionNode==null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
// Get the model.
AlignmentModel asJson = new AlignmentModel(traditionNode);
AlignmentModel asJson = new AlignmentModel(traditionNode, conflate);
return Response.ok(asJson, MediaType.APPLICATION_JSON_TYPE).build();
}

public Response exportAsCSV(String tradId, char separator) {
public Response exportAsJSON(String tradId) {
return exportAsJSON(tradId, new ArrayList<>());
}

public Response exportAsCSV(String tradId, char separator, ArrayList<String> conflate) {
Node traditionNode = DatabaseService.getTraditionNode(tradId, db);
if(traditionNode==null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
// Get the model.
AlignmentModel asJson = new AlignmentModel(traditionNode);
AlignmentModel asJson = new AlignmentModel(traditionNode, conflate);

// Write the CSV to a string that we can return.
StringWriter sw = new StringWriter();
Expand Down Expand Up @@ -66,4 +70,8 @@ public Response exportAsCSV(String tradId, char separator) {
return Response.ok(sw.toString(), MediaType.TEXT_PLAIN_TYPE).build();
}

public Response exportAsCSV(String tradId, char separator) {
return exportAsCSV(tradId, separator, new ArrayList<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -183,6 +183,54 @@ public void testJSONExport() throws Exception {
assertEquals(3, i);
}

public void testConflatedJSONExport () throws Exception {
GraphMLToNeo4JParser graphImporter = new GraphMLToNeo4JParser();
String traditionId = null;
try
{
Response response = graphImporter.parseGraphML("src/TestFiles/globalrel_test.xml", "1", "Tradition");
traditionId = Util.getValueFromJson(response, "tradId");
}
catch(FileNotFoundException f)
{
// this error should not occur
assertTrue(false);
}
assertNotNull(traditionId);
// Get it back out
ArrayList<String> toConflate = new ArrayList<>();
toConflate.add("collated");
ClientResponse result = jerseyTest
.resource()
.path("/tradition/" + traditionId + "/json")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, toConflate);
assertEquals(Response.Status.OK.getStatusCode(), result.getStatus());
// Get the JSON out
JSONObject table = result.getEntity(JSONObject.class);
assertTrue(table.has("alignment"));
assertTrue(table.has("length"));
assertEquals(10, table.getInt("length"));
// Every reading at rank 6 should be identical
JSONArray witnesses = table.getJSONArray("alignment");
HashSet<String> readingsAt6 = new HashSet<>();
for (int i = 0; i < witnesses.length(); i++) {
Object r6 = witnesses.getJSONObject(i).getJSONArray("tokens").get(5);
if (!r6.toString().equals("null"))
readingsAt6.add(((JSONObject) r6).getString("t"));
}
assertEquals(1, readingsAt6.size());

// Every reading at rank 9 should be identical
HashSet<String> readingsAt9 = new HashSet<>();
for (int i = 0; i < witnesses.length(); i++) {
Object r9 = witnesses.getJSONObject(i).getJSONArray("tokens").get(8);
if (!r9.toString().equals("null"))
readingsAt9.add(((JSONObject) r9).getString("t"));
}
assertEquals(1, readingsAt9.size());
}

// testOutputCSV

// testOutputExcel
Expand Down

0 comments on commit 9aeaaeb

Please sign in to comment.