01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.dawg;
07:
08: import org.openrdf.model.Graph;
09: import org.openrdf.model.Statement;
10: import org.openrdf.model.impl.GraphImpl;
11: import org.openrdf.query.QueryEvaluationException;
12: import org.openrdf.query.TupleQueryResult;
13: import org.openrdf.query.TupleQueryResultHandlerException;
14: import org.openrdf.query.impl.TupleQueryResultBuilder;
15: import org.openrdf.rio.RDFHandlerException;
16: import org.openrdf.rio.helpers.StatementCollector;
17:
18: /**
19: * @author Arjohn Kampman
20: */
21: public class DAWGTestResultSetUtil {
22:
23: public static TupleQueryResult toTupleQueryResult(
24: Iterable<? extends Statement> dawgGraph)
25: throws DAWGTestResultSetParseException {
26: TupleQueryResultBuilder tqrBuilder = new TupleQueryResultBuilder();
27: DAWGTestResultSetParser parser = new DAWGTestResultSetParser(
28: tqrBuilder);
29:
30: try {
31: parser.startRDF();
32: for (Statement st : dawgGraph) {
33: parser.handleStatement(st);
34: }
35: parser.endRDF();
36:
37: return tqrBuilder.getQueryResult();
38: } catch (RDFHandlerException e) {
39: throw new DAWGTestResultSetParseException(e.getMessage(), e);
40: }
41: }
42:
43: public static Graph toGraph(TupleQueryResult tqr)
44: throws QueryEvaluationException {
45: Graph graph = new GraphImpl();
46: DAWGTestResultSetWriter writer = new DAWGTestResultSetWriter(
47: new StatementCollector(graph));
48:
49: try {
50: writer.startQueryResult(tqr.getBindingNames());
51: while (tqr.hasNext()) {
52: writer.handleSolution(tqr.next());
53: }
54: writer.endQueryResult();
55: } catch (TupleQueryResultHandlerException e) {
56: // No exceptions expected from DAWGTestResultSetWriter or
57: // StatementCollector, foud a bug?
58: throw new RuntimeException(e);
59: }
60:
61: return graph;
62: }
63:
64: public static boolean toBooleanQueryResult(
65: Iterable<? extends Statement> dawgGraph)
66: throws DAWGTestResultSetParseException {
67: DAWGTestBooleanParser parser = new DAWGTestBooleanParser();
68:
69: try {
70: parser.startRDF();
71: for (Statement st : dawgGraph) {
72: parser.handleStatement(st);
73: }
74: parser.endRDF();
75:
76: return parser.getValue();
77: } catch (RDFHandlerException e) {
78: throw new DAWGTestResultSetParseException(e.getMessage(), e);
79: }
80: }
81: }
|