Search in sources :

Example 1 with Traversal

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);
    }
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) TupleStream(org.apache.solr.client.solrj.io.stream.TupleStream) StringWriter(java.io.StringWriter) Traversal(org.apache.solr.client.solrj.io.graph.Traversal) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Traversal

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);
}
Also used : SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Traversal(org.apache.solr.client.solrj.io.graph.Traversal) HashMap(java.util.HashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException)

Example 3 with 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();
    }
}
Also used : GraphHandler(org.apache.solr.handler.GraphHandler) ArrayList(java.util.ArrayList) Traversal(org.apache.solr.client.solrj.io.graph.Traversal) IOException(java.io.IOException) TupleStream(org.apache.solr.client.solrj.io.stream.TupleStream) Tuple(org.apache.solr.client.solrj.io.Tuple) PrintWriter(java.io.PrintWriter)

Aggregations

Traversal (org.apache.solr.client.solrj.io.graph.Traversal)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TupleStream (org.apache.solr.client.solrj.io.stream.TupleStream)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Tuple (org.apache.solr.client.solrj.io.Tuple)1 SolrException (org.apache.solr.common.SolrException)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 SolrParams (org.apache.solr.common.params.SolrParams)1 GraphHandler (org.apache.solr.handler.GraphHandler)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 Test (org.junit.Test)1