Download this file

TarqlProcessor.java    50 lines (39 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package edu.teco.deps.agent.processors;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import org.deri.tarql.CSVQueryExecutionFactory;
import org.deri.tarql.CSVToValues;
import org.deri.tarql.TarqlParser;
import org.deri.tarql.TarqlQuery;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.algebra.table.TableData;
public class TarqlProcessor implements Processor {
TarqlQuery query;
public TarqlProcessor(Reader queryReader) throws FileNotFoundException {
TarqlParser parser = new TarqlParser(queryReader);
query = parser.getResult();
}
public void process(Reader reader, Writer writer) throws IOException {
CSVToValues transformer = new CSVToValues(reader, false);
TableData table = transformer.read();
Model resultModel = ModelFactory.createDefaultModel();
for (Query q: query.getQueries()) {
// Clone the damn query first, as CSVQueryExecutionFactory is going to alter it
q = q.cloneQuery();
Model previousResults = ModelFactory.createDefaultModel();
previousResults.add(resultModel);
CSVQueryExecutionFactory.setPreviousResults(previousResults);
QueryExecution execution = CSVQueryExecutionFactory.create(table, q);
resultModel.setNsPrefixes(resultModel);
execution.execConstruct(resultModel);
CSVQueryExecutionFactory.resetPreviousResults();
}
resultModel.write(writer, "N-TRIPLE", query.getPrologue().getBaseURI());
writer.flush();
}
}