Skip to content

Commit

Permalink
Ensure QueryExecutions are closed after use.
Browse files Browse the repository at this point in the history
  • Loading branch information
balhoff committed Aug 28, 2020
1 parent 713da8a commit 79e969f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.monarchinitiative.dosdp.cli
import java.io.{File, PrintWriter}

import com.github.tototoshi.csv.{CSVWriter, TSVFormat}
import org.apache.jena.query.{QueryExecutionFactory, QueryFactory, ResultSet}
import org.apache.jena.query.{QueryExecutionFactory, QueryFactory, QuerySolution}
import org.apache.jena.rdf.model.ModelFactory
import org.backuity.clist._
import org.monarchinitiative.dosdp._
Expand Down Expand Up @@ -63,12 +63,10 @@ object Query extends Command(description = "query an ontology for terms matching
writer.close()
} else {
val ont = ontologyOpt.getOrElse(throw new RuntimeException("Can't run query; no ontology provided."))
val results = performQuery(processedQuery, ont)
val columns = results.getResultVars.asScala.toList
val (columns, results) = performQuery(processedQuery, ont)
val writer = CSVWriter.open(target.outputFile, "utf-8")(sepFormat)
writer.writeRow(columns)
while (results.hasNext) {
val qs = results.next()
results.foreach { qs =>
writer.writeRow(columns.map(variable => Option(qs.get(variable)).map(_.toString).getOrElse("")))
}
writer.close()
Expand All @@ -77,7 +75,7 @@ object Query extends Command(description = "query an ontology for terms matching
reasonerOpt.foreach(_.dispose())
}

def performQuery(sparql: String, ont: OWLOntology): ResultSet = {
def performQuery(sparql: String, ont: OWLOntology): (List[String], List[QuerySolution]) = {
val model = ModelFactory.createDefaultModel()
val allAxioms = for {
completeOnt <- ont.getImportsClosure.asScala
Expand All @@ -87,7 +85,12 @@ object Query extends Command(description = "query an ontology for terms matching
val triples = SesameJena.ontologyAsTriples(manager.createOntology(allAxioms.asJava))
model.add(triples.toList.asJava)
val query = QueryFactory.create(sparql)
QueryExecutionFactory.create(query, model).execSelect()
val qe = QueryExecutionFactory.create(query, model)
val resultSet = qe.execSelect()
val columns = resultSet.getResultVars.asScala.toList
val results = resultSet.asScala.toList
qe.close()
(columns, results)
}

private final case class QueryTarget(templateFile: String, outputFile: String)
Expand Down

0 comments on commit 79e969f

Please sign in to comment.