Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sparql timeout #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/main/java/edu/isi/kcap/ontapi/jena/KBAPIJena.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,13 @@ public ArrayList<ArrayList<SparqlQuerySolution>> sparqlQueryRemote(String queryS
return list;
}

public ArrayList<ArrayList<SparqlQuerySolution>> sparqlQuery(String queryString) {
public ArrayList<ArrayList<SparqlQuerySolution>> sparqlQuery(String queryString) {
Integer timeOut = 1000 * 60 * 5; //default timeout is 5 minutes
ArrayList<ArrayList<SparqlQuerySolution>> list = new ArrayList<ArrayList<SparqlQuerySolution>>();
Query query = QueryFactory.create(queryString);
ArrayList<String> vars = new ArrayList<String>(query.getResultVars());
QueryExecution qexec = QueryExecutionFactory.create(query, ontmodel);
qexec.setTimeout(timeOut);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
Expand All @@ -679,13 +681,48 @@ public ArrayList<ArrayList<SparqlQuerySolution>> sparqlQuery(String queryString)
}
list.add(inner);
}

}
catch(QueryCancelledException e){
System.out.println("Query cancelled");
throw e;
} finally {
qexec.close();
}
return list;
}

public ArrayList<ArrayList<SparqlQuerySolution>> sparqlQuery(String queryString, int timeOut) {
ArrayList<ArrayList<SparqlQuerySolution>> list = new ArrayList<ArrayList<SparqlQuerySolution>>();
Query query = QueryFactory.create(queryString);
ArrayList<String> vars = new ArrayList<String>(query.getResultVars());
QueryExecution qexec = QueryExecutionFactory.create(query, ontmodel);
qexec.setTimeout(timeOut);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
QuerySolution soln = results.nextSolution();
ArrayList<SparqlQuerySolution> inner = new ArrayList<SparqlQuerySolution>();
for (String variableName : vars) {
RDFNode x = soln.get(variableName);
// System.out.println(soln.toString());
KBObject item = null;
if (x != null)
item = new KBObjectJena(x);

SparqlQuerySolution sqs = new SparqlQuerySolution(variableName, item);
inner.add(sqs);
}
list.add(inner);
}
}
catch(QueryCancelledException e){
System.out.println("Query cancelled");
throw e;
} finally {
qexec.close();
}
return list;
}
public ArrayList<KBObject> getSubClasses(KBObject cls, boolean direct_only) {
ArrayList<KBObject> list = new ArrayList<KBObject>();
OntClass cl = (OntClass) cls.getInternalNode();
Expand Down