I am querying my ontology using SPARQL, but I observe a decreasing performance the more queries I request, the worse it gets. At the beginning the PC needs about 150ms to process the query, the 30. query needs 670ms and the 100. query needs over 7 seconds! It is depending on the query and/or ontology, but until now, I could find it in every ontology.
The line "ResultSetRewindable r = ResultSetFactory.copyResults(results);" is responsible for the time leak, but also work arounds by avoiding the line lead to similar behaviour.
I used the pizza ontology (https://ontohub.org/pizza/pizza.owl) and the following code. Additionally, I used the Jena version 2.13.
Has anybody an idea how to fix it?
public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException{
OntModel model = ModelFactory.createOntologyModel();
String OWLPath = "pizza.owl";
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX pz: <http://www.co-ode.org/ontologies/pizza/pizza.owl#> SELECT DISTINCT ?x WHERE { ?b owl:someValuesFrom pz:MozzarellaTopping. ?x rdfs:subClassOf ?b. ?x rdfs:subClassOf* pz:Pizza.}";
for(int j=0; j<100;j++){
double starttime = System.currentTimeMillis();
InputStream in = FileManager.get().open(OWLPath);
if (in == null) {
throw new IllegalArgumentException("File: " + OWLPath + " not found");
}
model.read(in, "");
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
double time1 = System.currentTimeMillis();
ResultSetRewindable r = ResultSetFactory.copyResults(results);
double time2 = System.currentTimeMillis();
ResultSetFormatter.out(ps, r, query);
String queryOutput = new String(baos.toByteArray(), "UTF-8");
String[] resultText = queryOutput.split("\n");
for(int i=0; i<resultText.length;i++){
System.out.println(resultText[i]);
}
double endtime = System.currentTimeMillis();
System.out.println("Time: "+ (endtime-starttime) +" Time for ResultSetFactory.copyResults(results): "+ (time2-time1));
}
}