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.dawg;
007:
008: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.BINDING;
009: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.RESULTSET;
010: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.RESULTVARIABLE;
011: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.SOLUTION;
012: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.VALUE;
013: import static org.openrdf.query.dawg.DAWGTestResultSetSchema.VARIABLE;
014:
015: import java.util.HashMap;
016: import java.util.List;
017: import java.util.Map;
018:
019: import org.openrdf.model.BNode;
020: import org.openrdf.model.Literal;
021: import org.openrdf.model.Resource;
022: import org.openrdf.model.URI;
023: import org.openrdf.model.Value;
024: import org.openrdf.model.ValueFactory;
025: import org.openrdf.model.impl.ValueFactoryImpl;
026: import org.openrdf.model.vocabulary.RDF;
027: import org.openrdf.query.Binding;
028: import org.openrdf.query.BindingSet;
029: import org.openrdf.query.TupleQueryResultHandler;
030: import org.openrdf.query.TupleQueryResultHandlerException;
031: import org.openrdf.rio.RDFHandler;
032: import org.openrdf.rio.RDFHandlerException;
033:
034: /**
035: * A TupleQueryResultWriter that converts query results to an RDF graph using
036: * the Data Access Working Group Test Result Set RDF Vocabulary
037: * (http://www.w3.org/2001/sw/DataAccess/tests/result-set#).
038: */
039: public class DAWGTestResultSetWriter implements TupleQueryResultHandler {
040:
041: /*-----------*
042: * Constants *
043: *-----------*/
044:
045: /**
046: * RDFHandler to report the generated statements to.
047: */
048: private final RDFHandler rdfHandler;
049:
050: private final ValueFactory vf;
051:
052: /*-----------*
053: * Variables *
054: *-----------*/
055:
056: private BNode resultSetNode;
057:
058: private Map<BNode, BNode> bnodeMap = new HashMap<BNode, BNode>();
059:
060: /*--------------*
061: * Constructors *
062: *--------------*/
063:
064: public DAWGTestResultSetWriter(RDFHandler rdfHandler) {
065: this (rdfHandler, new ValueFactoryImpl());
066: }
067:
068: public DAWGTestResultSetWriter(RDFHandler rdfHandler,
069: ValueFactory vf) {
070: this .rdfHandler = rdfHandler;
071: this .vf = vf;
072: }
073:
074: /*---------*
075: * Methods *
076: *---------*/
077:
078: public void startQueryResult(List<String> bindingNames)
079: throws TupleQueryResultHandlerException {
080: try {
081: rdfHandler.startRDF();
082:
083: resultSetNode = vf.createBNode();
084: bnodeMap.clear();
085:
086: reportStatement(resultSetNode, RDF.TYPE, RESULTSET);
087:
088: for (String bindingName : bindingNames) {
089: Literal bindingNameLit = vf.createLiteral(bindingName);
090: reportStatement(resultSetNode, RESULTVARIABLE,
091: bindingNameLit);
092: }
093: } catch (RDFHandlerException e) {
094: throw new TupleQueryResultHandlerException(e);
095: }
096: }
097:
098: public void endQueryResult()
099: throws TupleQueryResultHandlerException {
100: resultSetNode = null;
101:
102: try {
103: rdfHandler.endRDF();
104: } catch (RDFHandlerException e) {
105: throw new TupleQueryResultHandlerException(e);
106: }
107: }
108:
109: public void handleSolution(BindingSet bindingSet)
110: throws TupleQueryResultHandlerException {
111: try {
112: BNode solutionNode = vf.createBNode();
113:
114: reportStatement(resultSetNode, SOLUTION, solutionNode);
115:
116: for (Binding binding : bindingSet) {
117: BNode bindingNode = vf.createBNode();
118:
119: reportStatement(solutionNode, BINDING, bindingNode);
120: reportStatement(bindingNode, VARIABLE, vf
121: .createLiteral(binding.getName()));
122:
123: Value value = binding.getValue();
124:
125: // Map bnodes to new bnodes to prevent collisions with the bnodes
126: // generated for the result format
127: if (value instanceof BNode) {
128: BNode mappedBNode = bnodeMap.get(value);
129:
130: if (mappedBNode == null) {
131: mappedBNode = vf.createBNode();
132: bnodeMap.put((BNode) value, mappedBNode);
133: }
134:
135: value = mappedBNode;
136: }
137:
138: reportStatement(bindingNode, VALUE, value);
139: }
140: } catch (RDFHandlerException e) {
141: throw new TupleQueryResultHandlerException(e);
142: }
143: }
144:
145: private void reportStatement(Resource subject, URI predicate,
146: Value object) throws RDFHandlerException {
147: rdfHandler.handleStatement(vf.createStatement(subject,
148: predicate, object));
149: }
150: }
|