001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 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.ArrayList;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.openrdf.model.Graph;
020: import org.openrdf.model.Literal;
021: import org.openrdf.model.Resource;
022: import org.openrdf.model.Statement;
023: import org.openrdf.model.Value;
024: import org.openrdf.model.impl.GraphImpl;
025: import org.openrdf.model.util.GraphUtil;
026: import org.openrdf.model.util.GraphUtilException;
027: import org.openrdf.model.vocabulary.RDF;
028: import org.openrdf.query.Binding;
029: import org.openrdf.query.TupleQueryResultHandler;
030: import org.openrdf.query.TupleQueryResultHandlerException;
031: import org.openrdf.query.impl.BindingImpl;
032: import org.openrdf.query.impl.MapBindingSet;
033: import org.openrdf.rio.RDFHandlerException;
034: import org.openrdf.rio.helpers.RDFHandlerBase;
035:
036: /**
037: * @author Arjohn Kampman
038: */
039: public class DAWGTestResultSetParser extends RDFHandlerBase {
040:
041: /*-----------*
042: * Constants *
043: *-----------*/
044:
045: /**
046: * RDFHandler to report the generated statements to.
047: */
048: private final TupleQueryResultHandler tqrHandler;
049:
050: /*-----------*
051: * Variables *
052: *-----------*/
053:
054: private Graph graph = new GraphImpl();
055:
056: /*--------------*
057: * Constructors *
058: *--------------*/
059:
060: public DAWGTestResultSetParser(TupleQueryResultHandler tqrHandler) {
061: this .tqrHandler = tqrHandler;
062: }
063:
064: /*---------*
065: * Methods *
066: *---------*/
067:
068: @Override
069: public void startRDF() throws RDFHandlerException {
070: graph.clear();
071: }
072:
073: @Override
074: public void handleStatement(Statement st)
075: throws RDFHandlerException {
076: graph.add(st);
077: }
078:
079: @Override
080: public void endRDF() throws RDFHandlerException {
081: try {
082: Resource resultSetNode = GraphUtil.getUniqueSubject(graph,
083: RDF.TYPE, RESULTSET);
084:
085: List<String> bindingNames = getBindingNames(resultSetNode);
086: tqrHandler.startQueryResult(bindingNames);
087:
088: Iterator<Value> solIter = GraphUtil.getObjectIterator(
089: graph, resultSetNode, SOLUTION);
090: while (solIter.hasNext()) {
091: Value solutionNode = solIter.next();
092:
093: if (solutionNode instanceof Resource) {
094: reportSolution((Resource) solutionNode,
095: bindingNames);
096: } else {
097: new RDFHandlerException("Value for " + SOLUTION
098: + " is not a resource: " + solutionNode);
099: }
100: }
101:
102: tqrHandler.endQueryResult();
103: } catch (GraphUtilException e) {
104: throw new RDFHandlerException(e.getMessage(), e);
105: } catch (TupleQueryResultHandlerException e) {
106: throw new RDFHandlerException(e.getMessage(), e);
107: }
108: }
109:
110: private List<String> getBindingNames(Resource resultSetNode)
111: throws RDFHandlerException {
112: List<String> bindingNames = new ArrayList<String>(16);
113:
114: Iterator<Value> varIter = GraphUtil.getObjectIterator(graph,
115: resultSetNode, RESULTVARIABLE);
116:
117: while (varIter.hasNext()) {
118: Value varName = varIter.next();
119:
120: if (varName instanceof Literal) {
121: bindingNames.add(((Literal) varName).getLabel());
122: } else {
123: throw new RDFHandlerException("Value for "
124: + RESULTVARIABLE + " is not a literal: "
125: + varName);
126: }
127: }
128:
129: return bindingNames;
130: }
131:
132: private void reportSolution(Resource solutionNode,
133: List<String> bindingNames) throws RDFHandlerException,
134: GraphUtilException {
135: MapBindingSet bindingSet = new MapBindingSet(bindingNames
136: .size());
137:
138: Iterator<Value> bindingIter = GraphUtil.getObjectIterator(
139: graph, solutionNode, BINDING);
140: while (bindingIter.hasNext()) {
141: Value bindingNode = bindingIter.next();
142:
143: if (bindingNode instanceof Resource) {
144: Binding binding = getBinding((Resource) bindingNode);
145: bindingSet.addBinding(binding);
146: } else {
147: throw new RDFHandlerException("Value for " + BINDING
148: + " is not a resource: " + bindingNode);
149: }
150: }
151:
152: try {
153: tqrHandler.handleSolution(bindingSet);
154: } catch (TupleQueryResultHandlerException e) {
155: throw new RDFHandlerException(e.getMessage(), e);
156: }
157: }
158:
159: private Binding getBinding(Resource bindingNode)
160: throws GraphUtilException {
161: Literal name = GraphUtil.getUniqueObjectLiteral(graph,
162: bindingNode, VARIABLE);
163: Value value = GraphUtil.getUniqueObject(graph, bindingNode,
164: VALUE);
165: return new BindingImpl(name.getLabel(), value);
166: }
167: }
|