use of org.apache.jena.rdf.model.Model in project jena by apache.
the class T_TransSystem method describeWithAbort.
public static int describeWithAbort(String queryStr, DatasetGraph dsg, long abortTime) {
int counter = 0;
Query query = QueryFactory.create(queryStr, Syntax.syntaxARQ);
try (QueryExecution qExec = QueryExecutionFactory.create(query, DatasetFactory.wrap(dsg))) {
qExec.setTimeout(abortTime);
Model model = qExec.execDescribe();
//ResultSet rs = qExec.execSelect() ;
for (Iterator<Statement> stmIterator = model.listStatements(); stmIterator.hasNext(); ) {
stmIterator.next();
counter++;
}
return counter;
}
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ResultSetFactory method result.
/**
* Read in any kind of result kind (result set, boolean, graph)
*/
public static SPARQLResult result(String filenameOrURI, ResultsFormat format) {
if (format == null)
format = ResultsFormat.guessSyntax(filenameOrURI);
if (format == null) {
Log.warn(ResultSet.class, "Null format - defaulting to XML");
format = ResultsFormat.FMT_RS_XML;
}
if (format.equals(ResultsFormat.FMT_TEXT)) {
Log.error(ResultSet.class, "Can't read a text result set");
throw new ResultSetException("Can't read a text result set");
}
if (format.equals(ResultsFormat.FMT_RS_XML) || format.equals(ResultsFormat.FMT_RS_JSON) || format.equals(ResultsFormat.FMT_RS_TSV) || format.equals(ResultsFormat.FMT_RS_CSV)) {
InputStream in = null;
try {
in = FileManager.get().open(filenameOrURI);
if (in == null)
throw new NotFoundException(filenameOrURI);
} catch (NotFoundException ex) {
throw new NotFoundException("File not found: " + filenameOrURI);
}
SPARQLResult x = null;
if (format.equals(ResultsFormat.FMT_RS_JSON))
return JSONInput.make(in, GraphFactory.makeDefaultModel());
else if (format.equals(ResultsFormat.FMT_RS_XML))
return XMLInput.make(in, GraphFactory.makeDefaultModel());
else if (format.equals(ResultsFormat.FMT_RS_TSV)) {
ResultSet rs = TSVInput.fromTSV(in);
return new SPARQLResult(rs);
} else if (format.equals(ResultsFormat.FMT_RS_CSV)) {
ResultSet rs = CSVInput.fromCSV(in);
return new SPARQLResult(rs);
} else if (format.equals(ResultsFormat.FMT_RS_BIO)) {
ResultSet rs = BIOInput.fromBIO(in);
return new SPARQLResult(rs);
}
}
if (ResultsFormat.isRDFGraphSyntax(format)) {
Model model = FileManager.get().loadModel(filenameOrURI);
return new SPARQLResult(model);
}
Log.error(ResultSet.class, "Unknown result set syntax: " + format);
return null;
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class AssemblerUtils method readAssemblerFile.
public static Model readAssemblerFile(String assemblerFile) {
Model spec = null;
try {
spec = RDFDataMgr.loadModel(assemblerFile);
} catch (Exception ex) {
throw new ARQException("Failed reading assembler description: " + ex.getMessage());
}
spec.add(modelExtras);
return spec;
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class DescribeBNodeClosure method describe.
// Check all named graphs
@Override
public void describe(Resource r) {
// Default model.
Closure.closure(otherModel(r, dataset.getDefaultModel()), false, acc);
// Find all the named graphs in which this resource
// occurs as a subject. Faster than iterating in the
// names of graphs in the case of very large numbers
// of graphs, few of which contain the resource, in
// some kind of persistent storage.
QuerySolutionMap qsm = new QuerySolutionMap();
qsm.add("s", r);
QueryExecution qExec = QueryExecutionFactory.create(query, dataset, qsm);
ResultSet rs = qExec.execSelect();
for (; rs.hasNext(); ) {
QuerySolution qs = rs.next();
String gName = qs.getResource("g").getURI();
Model model = dataset.getNamedModel(gName);
Resource r2 = otherModel(r, model);
Closure.closure(r2, false, acc);
}
// // Named graphs
// for ( Iterator<String> iter = dataset.listNames() ; iter.hasNext() ; )
// {
// String name = iter.next();
// Model model = dataset.getNamedModel(name) ;
// Resource r2 = otherModel(r, model) ;
// Closure.closure(r2, false, acc) ;
// }
Closure.closure(r, false, acc);
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class QueryExecUtils method outputResultSet.
@SuppressWarnings("deprecation")
public static void outputResultSet(ResultSet results, Prologue prologue, ResultsFormat outputFormat) {
// Proper ResultSet formats.
Lang lang = ResultsFormat.convert(outputFormat);
if (lang != null) {
ResultSetMgr.write(System.out, results, lang);
System.out.flush();
return;
}
// Old way.
boolean done = false;
if (prologue == null)
prologue = new Prologue(globalPrefixMap);
if (outputFormat.equals(ResultsFormat.FMT_UNKNOWN))
outputFormat = ResultsFormat.FMT_TEXT;
if (outputFormat.equals(ResultsFormat.FMT_NONE) || outputFormat.equals(ResultsFormat.FMT_COUNT)) {
int count = ResultSetFormatter.consume(results);
if (outputFormat.equals(ResultsFormat.FMT_COUNT)) {
System.out.println("Count = " + count);
}
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RDF_XML) || outputFormat.equals(ResultsFormat.FMT_RDF_N3) || outputFormat.equals(ResultsFormat.FMT_RDF_TTL)) {
Model m = RDFOutput.encodeAsModel(results);
m.setNsPrefixes(prologue.getPrefixMapping());
m.setNsPrefix("rs", ResultSetGraphVocab.getURI());
RDFDataMgr.write(System.out, m, Lang.TURTLE);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
ResultSetFormatter.outputAsXML(System.out, results);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_JSON)) {
ResultSetFormatter.outputAsJSON(System.out, results);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_SSE)) {
ResultSetFormatter.outputAsSSE(System.out, results, prologue);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
ResultSetFormatter.out(System.out, results, prologue);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_TUPLES)) {
PlainFormat pFmt = new PlainFormat(System.out, prologue);
ResultSetApply a = new ResultSetApply(results, pFmt);
a.apply();
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_CSV)) {
ResultSetFormatter.outputAsCSV(System.out, results);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_TSV)) {
ResultSetFormatter.outputAsTSV(System.out, results);
done = true;
}
if (outputFormat.equals(ResultsFormat.FMT_RS_BIO)) {
ResultSetFormatter.outputAsBIO(System.out, results);
done = true;
}
if (!done)
System.err.println("Unknown format request: " + outputFormat);
results = null;
System.out.flush();
}
Aggregations