use of org.apache.solr.client.solrj.io.graph.Traversal in project lucene-solr by apache.
the class TestGraphMLResponseWriter method testGraphMLOutput.
@Test
public void testGraphMLOutput() throws Exception {
// Just need a request to attach the stream and traversal to.
SolrQueryRequest request = req("blah", "blah");
SolrQueryResponse response = new SolrQueryResponse();
Map context = request.getContext();
//Simulates a GatherNodesStream
TupleStream stream = new TestStream();
Traversal traversal = new Traversal();
context.put("traversal", traversal);
context.put("stream", stream);
StringWriter writer = new StringWriter();
GraphMLResponseWriter graphMLResponseWriter = new GraphMLResponseWriter();
graphMLResponseWriter.write(writer, request, response);
String graphML = writer.toString();
//Validate the nodes
String error = h.validateXPath(graphML, "//graph/node[1][@id ='bill']", "//graph/node[2][@id ='jim']", "//graph/node[3][@id ='max']");
if (error != null) {
throw new Exception(error);
}
//Validate the edges
error = h.validateXPath(graphML, "//graph/edge[1][@source ='jim']", "//graph/edge[1][@target ='bill']", "//graph/edge[2][@source ='max']", "//graph/edge[2][@target ='bill']", "//graph/edge[3][@source ='max']", "//graph/edge[3][@target ='jim']", "//graph/edge[4][@source ='jim']", "//graph/edge[4][@target ='max']");
if (error != null) {
throw new Exception(error);
}
}
use of org.apache.solr.client.solrj.io.graph.Traversal in project lucene-solr by apache.
the class GraphHandler method handleRequestBody.
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
SolrParams params = req.getParams();
params = adjustParams(params);
req.setParams(params);
TupleStream tupleStream = null;
try {
tupleStream = this.streamFactory.constructStream(params.get("expr"));
} catch (Exception e) {
//Catch exceptions that occur while the stream is being created. This will include streaming expression parse rules.
SolrException.log(logger, e);
Map requestContext = req.getContext();
requestContext.put("stream", new DummyErrorStream(e));
return;
}
StreamContext context = new StreamContext();
context.setSolrClientCache(StreamHandler.clientCache);
context.put("core", this.coreName);
Traversal traversal = new Traversal();
context.put("traversal", traversal);
tupleStream.setStreamContext(context);
Map requestContext = req.getContext();
requestContext.put("stream", new TimerStream(new ExceptionStream(tupleStream)));
requestContext.put("traversal", traversal);
}
use of org.apache.solr.client.solrj.io.graph.Traversal in project lucene-solr by apache.
the class GraphMLResponseWriter method write.
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse res) throws IOException {
Exception e1 = res.getException();
if (e1 != null) {
e1.printStackTrace(new PrintWriter(writer));
return;
}
TupleStream stream = (TupleStream) req.getContext().get("stream");
if (stream instanceof GraphHandler.DummyErrorStream) {
GraphHandler.DummyErrorStream d = (GraphHandler.DummyErrorStream) stream;
Exception e = d.getException();
e.printStackTrace(new PrintWriter(writer));
return;
}
Traversal traversal = (Traversal) req.getContext().get("traversal");
PrintWriter printWriter = new PrintWriter(writer);
try {
stream.open();
Tuple tuple = null;
int edgeCount = 0;
printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
printWriter.println("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" ");
printWriter.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
printWriter.print("xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns ");
printWriter.println("http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">");
printWriter.println("<graph id=\"G\" edgedefault=\"directed\">");
while (true) {
//Output the graph
tuple = stream.read();
if (tuple.EOF) {
break;
}
String id = tuple.getString("node");
if (traversal.isMultiCollection()) {
id = tuple.getString("collection") + "." + id;
}
writer.write("<node id=\"" + replace(id) + "\"");
List<String> outfields = new ArrayList();
Iterator<String> keys = tuple.fields.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
if (key.equals("node") || key.equals("ancestors") || key.equals("collection")) {
continue;
} else {
outfields.add(key);
}
}
if (outfields.size() > 0) {
printWriter.println(">");
for (String nodeAttribute : outfields) {
Object o = tuple.get(nodeAttribute);
if (o != null) {
printWriter.println("<data key=\"" + nodeAttribute + "\">" + o.toString() + "</data>");
}
}
printWriter.println("</node>");
} else {
printWriter.println("/>");
}
List<String> ancestors = tuple.getStrings("ancestors");
if (ancestors != null) {
for (String ancestor : ancestors) {
++edgeCount;
writer.write("<edge id=\"" + edgeCount + "\" ");
writer.write(" source=\"" + replace(ancestor) + "\" ");
printWriter.println(" target=\"" + replace(id) + "\"/>");
}
}
}
writer.write("</graph></graphml>");
} finally {
stream.close();
}
}
Aggregations