001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query;
007:
008: import java.util.Collection;
009: import java.util.HashSet;
010: import java.util.Map;
011: import java.util.Set;
012:
013: import info.aduna.iteration.Iterations;
014:
015: import org.openrdf.model.Statement;
016: import org.openrdf.model.Value;
017: import org.openrdf.model.util.ModelUtil;
018: import org.openrdf.query.dawg.DAWGTestResultSetUtil;
019: import org.openrdf.rio.RDFHandler;
020: import org.openrdf.rio.RDFHandlerException;
021:
022: /**
023: * Class offering utility methods related to query results.
024: *
025: * @author Arjohn Kampman
026: */
027: public class QueryResultUtil {
028:
029: /**
030: * Reports a tuple query result to a {@link TupleQueryResultHandler}.
031: *
032: * @param tqr
033: * The query result to report.
034: * @param handler
035: * The handler to report the query result to.
036: * @throws TupleQueryResultHandlerException
037: * If such an exception is thrown by the used query result writer.
038: */
039: public static void report(TupleQueryResult tqr,
040: TupleQueryResultHandler handler)
041: throws TupleQueryResultHandlerException,
042: QueryEvaluationException {
043: handler.startQueryResult(tqr.getBindingNames());
044: try {
045: while (tqr.hasNext()) {
046: BindingSet bindingSet = tqr.next();
047: handler.handleSolution(bindingSet);
048: }
049: } finally {
050: tqr.close();
051: }
052: handler.endQueryResult();
053: }
054:
055: /**
056: * Reports a graph query result to an {@link RDFHandler}.
057: *
058: * @param gqr
059: * The query result to report.
060: * @param rdfHandler
061: * The handler to report the query result to.
062: * @throws RDFHandlerException
063: * If such an exception is thrown by the used RDF writer.
064: * @throws QueryEvaluationException
065: */
066: public static void report(GraphQueryResult gqr,
067: RDFHandler rdfHandler) throws RDFHandlerException,
068: QueryEvaluationException {
069: try {
070: rdfHandler.startRDF();
071:
072: for (Map.Entry<String, String> entry : gqr.getNamespaces()
073: .entrySet()) {
074: String prefix = entry.getKey();
075: String namespace = entry.getValue();
076: rdfHandler.handleNamespace(prefix, namespace);
077: }
078:
079: while (gqr.hasNext()) {
080: Statement st = gqr.next();
081: rdfHandler.handleStatement(st);
082: }
083:
084: rdfHandler.endRDF();
085: } finally {
086: gqr.close();
087: }
088: }
089:
090: /**
091: * Compares the two query results by converting them to graphs and returns
092: * true if they are equal. QueryResults are equal if they contain the same
093: * set of BindingSet and have the headers. Blank nodes identifiers are not
094: * relevant for equality, they are mapped from one model to the other by
095: * using the attached properties. Note that the method consumes both query
096: * results fully.
097: *
098: * @throws QueryEvaluationException
099: */
100: public static boolean equals(TupleQueryResult tqr1,
101: TupleQueryResult tqr2) throws QueryEvaluationException {
102: Collection<? extends Statement> graph1 = DAWGTestResultSetUtil
103: .toGraph(tqr1);
104: Collection<? extends Statement> graph2 = DAWGTestResultSetUtil
105: .toGraph(tqr2);
106:
107: return ModelUtil.equals(graph1, graph2);
108: }
109:
110: public static boolean equals(GraphQueryResult result1,
111: GraphQueryResult result2) throws QueryEvaluationException {
112: Set<? extends Statement> graph1 = Iterations.asSet(result1);
113: Set<? extends Statement> graph2 = Iterations.asSet(result1);
114:
115: return ModelUtil.equals(graph1, graph2);
116: }
117:
118: /**
119: * Check whether two {@link BindingSet}s are compatible. Two binding sets
120: * are compatible if they have equal values for each binding name that occurs
121: * in both binding sets.
122: */
123: public static boolean bindingSetsCompatible(BindingSet bs1,
124: BindingSet bs2) {
125: Set<String> sharedBindings = new HashSet<String>(bs1
126: .getBindingNames());
127: sharedBindings.retainAll(bs2.getBindingNames());
128:
129: for (String bindingName : sharedBindings) {
130: Value value1 = bs1.getValue(bindingName);
131: Value value2 = bs2.getValue(bindingName);
132:
133: if (!value1.equals(value2)) {
134: return false;
135: }
136: }
137:
138: return true;
139: }
140: }
|