From 904df79a4d5824c9b9a7e38b464208c5673b8f36 Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Sun, 6 Aug 2023 17:33:02 +0300 Subject: [PATCH 1/8] First draft owlet query expansion --- .../org/obolibrary/robot/QueryCommand.java | 27 ++++++++++++++++--- robot-core/pom.xml | 20 ++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java index a1a74442c..c9ec78ebc 100644 --- a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java +++ b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java @@ -15,7 +15,10 @@ import org.apache.jena.query.Dataset; import org.apache.jena.rdf.model.Model; import org.apache.jena.tdb.TDBFactory; +import org.phenoscape.owlet.Owlet; import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.reasoner.OWLReasoner; +import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -239,14 +242,27 @@ private static void executeInMemory( CommandLine line, OWLOntology inputOntology, List> queries) throws Exception { boolean useGraphs = CommandLineHelper.getBooleanValue(line, "use-graphs", false); Dataset dataset = QueryOperation.loadOntologyAsDataset(inputOntology, useGraphs); + boolean useOwlet = CommandLineHelper.getBooleanValue(line, "owlet", false); + + Owlet owlet = getOwlet(line, inputOntology, useOwlet); try { - runQueries(line, dataset, queries); + runQueries(line, dataset, queries, owlet); } finally { dataset.close(); // TDBFactory.release(dataset); } } + private static Owlet getOwlet(CommandLine line, OWLOntology inputOntology, boolean useOwlet) { + Owlet owlet = null; + if(useOwlet) { + OWLReasonerFactory rf = CommandLineHelper.getReasonerFactory(line); + OWLReasoner r = rf.createReasoner(inputOntology); + owlet = new Owlet(r); + } + return owlet; + } + /** * Given a command line and a list of queries, execute 'query' using TDB and writing mappings to * disk. @@ -260,8 +276,9 @@ private static void executeOnDisk(CommandLine line, List> queries) Dataset dataset = createTDBDataset(line); boolean keepMappings = CommandLineHelper.getBooleanValue(line, "keep-tdb-mappings", false); String tdbDir = CommandLineHelper.getDefaultValue(line, "tdb-directory", ".tdb"); + try { - runQueries(line, dataset, queries); + runQueries(line, dataset, queries, null); } finally { dataset.close(); TDBFactory.release(dataset); @@ -396,7 +413,7 @@ private static List> getQueries(CommandLine line) { * @param queries List of queries * @throws IOException on issue reading or writing files */ - private static void runQueries(CommandLine line, Dataset dataset, List> queries) + private static void runQueries(CommandLine line, Dataset dataset, List> queries, Owlet owlet) throws IOException { String format = CommandLineHelper.getOptionalValue(line, "format"); String outputDir = CommandLineHelper.getDefaultValue(line, "output-dir", ""); @@ -406,6 +423,10 @@ private static void runQueries(CommandLine line, Dataset dataset, List org.apache.jena jena-arq - 3.17.0 + 4.9.0 com.github.jsonld-java @@ -134,7 +134,7 @@ org.apache.jena jena-tdb - 3.17.0 + 4.9.0 org.apache.poi @@ -288,6 +288,22 @@ + + org.apache.jena + apache-jena-libs + 4.9.0 + + + org.phenoscape + owlet_2.13 + 1.9 + + + net.sourceforge.owlapi + owlapi-distribution + + + com.google.code.gson gson From 632406490f31c73a34e2b02eb568c00d89dba18d Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Tue, 8 Aug 2023 12:02:11 +0300 Subject: [PATCH 2/8] Update robot-core/pom.xml Co-authored-by: Jim Balhoff --- robot-core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robot-core/pom.xml b/robot-core/pom.xml index f164c3b84..6831ab461 100644 --- a/robot-core/pom.xml +++ b/robot-core/pom.xml @@ -295,7 +295,7 @@ org.phenoscape - owlet_2.13 + owlet_${scala.version} 1.9 From fe001929b1f87ca2d003554e89381c79ccfe5e7a Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Tue, 8 Aug 2023 12:02:26 +0300 Subject: [PATCH 3/8] Update robot-core/pom.xml Co-authored-by: Jim Balhoff --- robot-core/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/robot-core/pom.xml b/robot-core/pom.xml index 6831ab461..6414e4fc3 100644 --- a/robot-core/pom.xml +++ b/robot-core/pom.xml @@ -291,6 +291,7 @@ org.apache.jena apache-jena-libs + pom 4.9.0 From b543ba76242580c5f5cda9cfd8d85082340fabd4 Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Tue, 8 Aug 2023 12:10:48 +0300 Subject: [PATCH 4/8] Update pom.xml --- robot-core/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/robot-core/pom.xml b/robot-core/pom.xml index 6414e4fc3..cf254b172 100644 --- a/robot-core/pom.xml +++ b/robot-core/pom.xml @@ -119,7 +119,7 @@ org.apache.jena jena-arq - 4.9.0 + 4.1.0 com.github.jsonld-java @@ -134,7 +134,7 @@ org.apache.jena jena-tdb - 4.9.0 + 4.1.0 org.apache.poi @@ -292,7 +292,7 @@ org.apache.jena apache-jena-libs pom - 4.9.0 + 4.1.0 org.phenoscape From b8734f4a7ab565e4e9b71eb44f14168bf249b970 Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Tue, 8 Aug 2023 13:42:26 +0300 Subject: [PATCH 5/8] add dl query command --- docs/dl-query.md | 29 +++ .../org/obolibrary/robot/DLQueryCommand.java | 184 ++++++++++++++++++ .../obolibrary/robot/DLQueryOperation.java | 91 +++++++++ 3 files changed, 304 insertions(+) create mode 100644 docs/dl-query.md create mode 100644 robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java create mode 100644 robot-core/src/main/java/org/obolibrary/robot/DLQueryOperation.java diff --git a/docs/dl-query.md b/docs/dl-query.md new file mode 100644 index 000000000..23f93c121 --- /dev/null +++ b/docs/dl-query.md @@ -0,0 +1,29 @@ +# DL-Query + +## Contents + +1. [Overview](#overview) +2. [Query types](#query-types) + +## Overview + +ROBOT can execute DL queries against an ontology. The functionality closely mimics the functionality of the DL Query Tab in Protege. + +The `dl-query` command can be used to query for ancestors, descendants, instances and other relatives of an OWL Class Expression that is provided in Manchester syntax. + +The output is always a list of Entity IRIs. Multiple queries and output files can be supplied. For example: + + robot query --input uberon_module.owl \ + --query "'part_of' some 'subdivision of trunk'" part_of_subdiv_trunk.txt \ + --query "'part_of' some 'nervous system'" part_of_nervous_system.txt + +## Query Types + +The following query types are currently supported: + +- equivalents: Classes that are exactly equivalent to the supplied class expression +- parents: Direct parents (superclasses) of the class expression provided +- children: Direct children (subclasses) of the class expression provided +- descendants (default): All subclasses of the class expression provided +- ancestors: All superclasses of the class expression provided +- instances: All named individuals that are instances of the class expression provided diff --git a/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java new file mode 100644 index 000000000..14a6047ab --- /dev/null +++ b/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java @@ -0,0 +1,184 @@ +package org.obolibrary.robot; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.*; +import java.util.stream.Collectors; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.io.FileUtils; +import org.obolibrary.robot.exceptions.InconsistentOntologyException; +import org.obolibrary.robot.providers.CURIEShortFormProvider; +import org.semanticweb.owl.explanation.api.Explanation; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxInlineAxiomParser; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.reasoner.OWLReasoner; +import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; +import org.semanticweb.owlapi.util.SimpleShortFormProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Handles inputs and outputs for the {@link DLQueryOperation}. + * + * @author Nicolas Matentzoglu + */ +public class DLQueryCommand implements Command { + /** Logger. */ + private static final Logger logger = LoggerFactory.getLogger(DLQueryCommand.class); + + private static final String NS = "dl-query#"; + + private static final List LEGAL_RELATIONS = + Arrays.asList("equivalents", "ancestors", "descendants", "instances", "parents", "children"); + OWLDataFactory df = OWLManager.getOWLDataFactory(); + + private static final String maxTypeError = NS + "MAX TYPE ERROR --max ('%s') must be an integer"; + private static final String illegalRelationError = + NS + + "ILLEGAL RELATION ERROR: %s. Must be one of " + + String.join(" ", LEGAL_RELATIONS) + + "."; + private static final String missingQueryArgumentError = + NS + "MISSING QUERY ARGUMENT ERROR: must have a valid --query."; + + /** Error message when --query does not have two arguments. */ + private static final String missingOutputError = + NS + "MISSING OUTPUT ERROR --%s requires two arguments: query and output"; + + + /** Error message when a query is not provided */ + private static final String missingQueryError = + NS + "MISSING QUERY ERROR at least one query must be provided"; + + /** Store the command-line options for the command. */ + private Options options; + + public DLQueryCommand() { + Options o = CommandLineHelper.getCommonOptions(); + o.addOption("i", "input", true, "load ontology from a file"); + o.addOption("I", "input-iri", true, "load ontology from an IRI"); + o.addOption("r", "reasoner", true, "reasoner to use: ELK, HermiT, JFact"); + + Option opt = new Option("q", "query", true, "the DL query to run"); + opt.setArgs(2); + o.addOption(opt); + + o.addOption("s", "select", true, "select what relations to query: equivalents, parents, children, ancestors, descendants, instances"); + o.addOption("o", "output", true, "save ontology containing only explanation axioms to a file"); + options = o; + } + + @Override + public String getName() { + return "dl-query"; + } + + @Override + public String getDescription() { + return "query the ontology with the given class expression"; + } + + @Override + public String getUsage() { + return "robot dl-query --input --query --output "; + } + + @Override + public Options getOptions() { + return options; + } + + /** + * Handle the command-line and file operations for the DLQueryOperation. + * + * @param args strings to use as arguments + */ + @Override + public void main(String[] args) { + try { + execute(null, args); + } catch (Exception e) { + CommandLineHelper.handleException(e); + } + } + + @Override + public CommandState execute(CommandState state, String[] args) throws Exception { + CommandLine line = CommandLineHelper.getCommandLine(getUsage(), getOptions(), args); + if (line == null) { + return null; + } + if (state == null) { + state = new CommandState(); + } + IOHelper ioHelper = CommandLineHelper.getIOHelper(line); + state = CommandLineHelper.updateInputOntology(ioHelper, state, line); + OWLOntology ontology = state.getOntology(); + + OWLReasonerFactory reasonerFactory = CommandLineHelper.getReasonerFactory(line, true); + List selects = CommandLineHelper.getOptionalValues(line, "select"); + + List> queries = getQueries(line); + for (List q : queries) { + queryOntology(q, ontology, reasonerFactory, selects); + } + + state.setOntology(ontology); + CommandLineHelper.maybeSaveOutput(line, ontology); + return state; + } + + private void queryOntology(List q, OWLOntology ontology, OWLReasonerFactory reasonerFactory, + List selects) throws InconsistentOntologyException, IOException { + OWLReasoner r = reasonerFactory.createReasoner(ontology); + String query = q.get(0); + File output = new File(q.get(1)); + OWLClassExpression classExpression = DLQueryOperation.parseOWLClassExpression(query,ontology); + if (r.isConsistent()) { + List entities = DLQueryOperation.query(classExpression, r, selects); + writeQueryResultsToFile(output, entities); + } else { + throw new InconsistentOntologyException(); + } + } + + /** + * Given a command line, get a list of queries. + * + * @param line CommandLine with options + * @return List of queries + */ + private static List> getQueries(CommandLine line) { + // Collect all queries as (queryPath, outputPath) pairs. + List> queries = new ArrayList<>(); + List qs = CommandLineHelper.getOptionalValues(line, "query"); + for (int i = 0; i < qs.size(); i += 2) { + try { + queries.add(qs.subList(i, i + 2)); + } catch (IndexOutOfBoundsException e) { + throw new IllegalArgumentException(String.format(missingOutputError, "query")); + } + } + if (queries.isEmpty()) { + throw new IllegalArgumentException(missingQueryError); + } + return queries; + } + + private void writeQueryResultsToFile( + File output, + List results) + throws IOException { + Collections.sort(results); + FileUtils.writeLines(output, results); + } + + +} diff --git a/robot-core/src/main/java/org/obolibrary/robot/DLQueryOperation.java b/robot-core/src/main/java/org/obolibrary/robot/DLQueryOperation.java new file mode 100644 index 000000000..c2f8648a8 --- /dev/null +++ b/robot-core/src/main/java/org/obolibrary/robot/DLQueryOperation.java @@ -0,0 +1,91 @@ +package org.obolibrary.robot; + +import java.util.*; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.expression.ShortFormEntityChecker; +import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxClassExpressionParser; +import org.semanticweb.owlapi.manchestersyntax.renderer.ParserException; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.reasoner.OWLReasoner; +import org.semanticweb.owlapi.util.BidirectionalShortFormProviderAdapter; +import org.semanticweb.owlapi.util.SimpleShortFormProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Query the ontology with a class expression. This simulates the DL Query Tab in Protege. + * + * @author Nicolas Matentzoglu + */ +public class DLQueryOperation { + + /** Logger */ + private static final Logger logger = LoggerFactory.getLogger(DLQueryOperation.class); + + private static final OWLDataFactory df = OWLManager.getOWLDataFactory(); + + /** + * Query the ontology with the provided class expression. + * + * @param expression the expression to be queried + * @param reasoner the initialised reasoner to query + * @param queryTypes the query results to be included (parents, children, equivants) + * @return explanations + */ + public static List query( + OWLClassExpression expression, + OWLReasoner reasoner, + List queryTypes) { + logger.debug("Querying: " + expression); + List results = new ArrayList<>(); + if(queryTypes.isEmpty()) { + logger.info("No query type supplied, using 'descendants'"); + queryTypes.add("descendants"); + } + + for (String type : queryTypes) { + switch(type) { + case "equivalents": + reasoner.getEquivalentClasses(expression).getEntities().forEach(results::add); + break; + case "parents": + reasoner.getSuperClasses(expression,true).getFlattened().forEach(results::add); + results.remove(df.getOWLThing()); + break; + case "children": + reasoner.getSubClasses(expression,true).getFlattened().forEach(results::add); + results.remove(df.getOWLNothing()); + break; + case "ancestors": + reasoner.getSuperClasses(expression,false).getFlattened().forEach(results::add); + results.remove(df.getOWLThing()); + break; + case "descendants": + reasoner.getSubClasses(expression,false).getFlattened().forEach(results::add); + results.remove(df.getOWLNothing()); + break; + case "instances": + reasoner.getInstances(expression,false).getFlattened().forEach(results::add); + break; + default: + throw new IllegalArgumentException(type + " is not a legal query relation for dl-query"); + } + } + return results; + } + +public static OWLClassExpression parseOWLClassExpression(String expression, OWLOntology ontology) throws ParserException { + OWLClassExpression classExpression = null; + BidirectionalShortFormProviderAdapter shortFormProvider = + new BidirectionalShortFormProviderAdapter(OWLManager.createOWLOntologyManager(), + Collections.singleton(ontology), new SimpleShortFormProvider()); + + ManchesterOWLSyntaxClassExpressionParser parser = + new ManchesterOWLSyntaxClassExpressionParser(ontology.getOWLOntologyManager().getOWLDataFactory(), + new ShortFormEntityChecker(shortFormProvider)); + + classExpression = parser.parse(expression); + return classExpression; +} + +} From 5a42a6f9613b2628e055f04392367fa6ad04b408 Mon Sep 17 00:00:00 2001 From: Nico Matentzoglu Date: Tue, 8 Aug 2023 13:42:42 +0300 Subject: [PATCH 6/8] add comment to owlet expand query string --- .../src/main/java/org/obolibrary/robot/QueryCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java index c9ec78ebc..669d71662 100644 --- a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java +++ b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java @@ -425,7 +425,8 @@ private static void runQueries(CommandLine line, Dataset dataset, List Date: Sat, 9 Sep 2023 11:53:46 -0400 Subject: [PATCH 7/8] Update to Owlet 2.0.0. Upgrade Jena, add jackson-databind dependency to resolve version conflicts. Migrate to newer Jena API. --- robot-core/pom.xml | 29 +++++++++---------- .../org/obolibrary/robot/QueryOperation.java | 4 +-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/robot-core/pom.xml b/robot-core/pom.xml index cf254b172..46c61c985 100644 --- a/robot-core/pom.xml +++ b/robot-core/pom.xml @@ -119,7 +119,7 @@ org.apache.jena jena-arq - 4.1.0 + 4.9.0 com.github.jsonld-java @@ -134,7 +134,7 @@ org.apache.jena jena-tdb - 4.1.0 + 4.9.0 org.apache.poi @@ -232,6 +232,11 @@ jackson-annotations 2.12.2 + + com.fasterxml.jackson.core + jackson-databind + 2.12.2 + net.sourceforge.owlapi owlexplanation @@ -288,22 +293,16 @@ - - org.apache.jena - apache-jena-libs - pom - 4.1.0 - org.phenoscape owlet_${scala.version} - 1.9 - - - net.sourceforge.owlapi - owlapi-distribution - - + 2.0.0 + + + net.sourceforge.owlapi + owlapi-distribution + + com.google.code.gson diff --git a/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java b/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java index 9362530cc..9446d3420 100644 --- a/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java +++ b/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java @@ -486,7 +486,7 @@ public static Lang getFormatLang(String formatName) { formatName = formatName.toLowerCase(); switch (formatName) { case "tsv": - format = ResultSetLang.SPARQLResultSetTSV; + format = ResultSetLang.RS_TSV; break; case "ttl": format = Lang.TTL; @@ -507,7 +507,7 @@ public static Lang getFormatLang(String formatName) { format = Lang.RDFXML; break; case "sxml": - format = ResultSetLang.SPARQLResultSetXML; + format = ResultSetLang.RS_XML; break; default: format = null; From d269e86e13332b6232ca86f4be0b55a70e83cc3b Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Sat, 9 Sep 2023 11:54:17 -0400 Subject: [PATCH 8/8] Reformat. --- .../org/obolibrary/robot/DLQueryCommand.java | 46 ++++++++----------- .../org/obolibrary/robot/QueryCommand.java | 16 ++++--- .../obolibrary/robot/DLQueryOperation.java | 45 +++++++++--------- 3 files changed, 50 insertions(+), 57 deletions(-) diff --git a/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java index 14a6047ab..9f3d561b9 100644 --- a/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java +++ b/robot-command/src/main/java/org/obolibrary/robot/DLQueryCommand.java @@ -2,25 +2,16 @@ import java.io.File; import java.io.IOException; -import java.io.Writer; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.util.*; -import java.util.stream.Collectors; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.io.FileUtils; import org.obolibrary.robot.exceptions.InconsistentOntologyException; -import org.obolibrary.robot.providers.CURIEShortFormProvider; -import org.semanticweb.owl.explanation.api.Explanation; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxInlineAxiomParser; import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; -import org.semanticweb.owlapi.util.SimpleShortFormProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,18 +32,14 @@ public class DLQueryCommand implements Command { private static final String maxTypeError = NS + "MAX TYPE ERROR --max ('%s') must be an integer"; private static final String illegalRelationError = - NS - + "ILLEGAL RELATION ERROR: %s. Must be one of " - + String.join(" ", LEGAL_RELATIONS) - + "."; + NS + "ILLEGAL RELATION ERROR: %s. Must be one of " + String.join(" ", LEGAL_RELATIONS) + "."; private static final String missingQueryArgumentError = NS + "MISSING QUERY ARGUMENT ERROR: must have a valid --query."; - + /** Error message when --query does not have two arguments. */ private static final String missingOutputError = - NS + "MISSING OUTPUT ERROR --%s requires two arguments: query and output"; + NS + "MISSING OUTPUT ERROR --%s requires two arguments: query and output"; - /** Error message when a query is not provided */ private static final String missingQueryError = NS + "MISSING QUERY ERROR at least one query must be provided"; @@ -65,12 +52,16 @@ public DLQueryCommand() { o.addOption("i", "input", true, "load ontology from a file"); o.addOption("I", "input-iri", true, "load ontology from an IRI"); o.addOption("r", "reasoner", true, "reasoner to use: ELK, HermiT, JFact"); - + Option opt = new Option("q", "query", true, "the DL query to run"); opt.setArgs(2); o.addOption(opt); - o.addOption("s", "select", true, "select what relations to query: equivalents, parents, children, ancestors, descendants, instances"); + o.addOption( + "s", + "select", + true, + "select what relations to query: equivalents, parents, children, ancestors, descendants, instances"); o.addOption("o", "output", true, "save ontology containing only explanation axioms to a file"); options = o; } @@ -135,17 +126,21 @@ public CommandState execute(CommandState state, String[] args) throws Exception return state; } - private void queryOntology(List q, OWLOntology ontology, OWLReasonerFactory reasonerFactory, - List selects) throws InconsistentOntologyException, IOException { + private void queryOntology( + List q, + OWLOntology ontology, + OWLReasonerFactory reasonerFactory, + List selects) + throws InconsistentOntologyException, IOException { OWLReasoner r = reasonerFactory.createReasoner(ontology); String query = q.get(0); File output = new File(q.get(1)); - OWLClassExpression classExpression = DLQueryOperation.parseOWLClassExpression(query,ontology); + OWLClassExpression classExpression = DLQueryOperation.parseOWLClassExpression(query, ontology); if (r.isConsistent()) { List entities = DLQueryOperation.query(classExpression, r, selects); writeQueryResultsToFile(output, entities); } else { - throw new InconsistentOntologyException(); + throw new InconsistentOntologyException(); } } @@ -172,13 +167,8 @@ private static List> getQueries(CommandLine line) { return queries; } - private void writeQueryResultsToFile( - File output, - List results) - throws IOException { + private void writeQueryResultsToFile(File output, List results) throws IOException { Collections.sort(results); FileUtils.writeLines(output, results); } - - } diff --git a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java index 669d71662..eb2ab99e2 100644 --- a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java +++ b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java @@ -255,7 +255,7 @@ private static void executeInMemory( private static Owlet getOwlet(CommandLine line, OWLOntology inputOntology, boolean useOwlet) { Owlet owlet = null; - if(useOwlet) { + if (useOwlet) { OWLReasonerFactory rf = CommandLineHelper.getReasonerFactory(line); OWLReasoner r = rf.createReasoner(inputOntology); owlet = new Owlet(r); @@ -276,7 +276,7 @@ private static void executeOnDisk(CommandLine line, List> queries) Dataset dataset = createTDBDataset(line); boolean keepMappings = CommandLineHelper.getBooleanValue(line, "keep-tdb-mappings", false); String tdbDir = CommandLineHelper.getDefaultValue(line, "tdb-directory", ".tdb"); - + try { runQueries(line, dataset, queries, null); } finally { @@ -413,7 +413,8 @@ private static List> getQueries(CommandLine line) { * @param queries List of queries * @throws IOException on issue reading or writing files */ - private static void runQueries(CommandLine line, Dataset dataset, List> queries, Owlet owlet) + private static void runQueries( + CommandLine line, Dataset dataset, List> queries, Owlet owlet) throws IOException { String format = CommandLineHelper.getOptionalValue(line, "format"); String outputDir = CommandLineHelper.getDefaultValue(line, "output-dir", ""); @@ -423,10 +424,11 @@ private static void runQueries(CommandLine line, Dataset dataset, List query( - OWLClassExpression expression, - OWLReasoner reasoner, - List queryTypes) { + OWLClassExpression expression, OWLReasoner reasoner, List queryTypes) { logger.debug("Querying: " + expression); List results = new ArrayList<>(); - if(queryTypes.isEmpty()) { + if (queryTypes.isEmpty()) { logger.info("No query type supplied, using 'descendants'"); queryTypes.add("descendants"); } for (String type : queryTypes) { - switch(type) { + switch (type) { case "equivalents": reasoner.getEquivalentClasses(expression).getEntities().forEach(results::add); break; case "parents": - reasoner.getSuperClasses(expression,true).getFlattened().forEach(results::add); + reasoner.getSuperClasses(expression, true).getFlattened().forEach(results::add); results.remove(df.getOWLThing()); break; case "children": - reasoner.getSubClasses(expression,true).getFlattened().forEach(results::add); + reasoner.getSubClasses(expression, true).getFlattened().forEach(results::add); results.remove(df.getOWLNothing()); break; case "ancestors": - reasoner.getSuperClasses(expression,false).getFlattened().forEach(results::add); + reasoner.getSuperClasses(expression, false).getFlattened().forEach(results::add); results.remove(df.getOWLThing()); break; case "descendants": - reasoner.getSubClasses(expression,false).getFlattened().forEach(results::add); + reasoner.getSubClasses(expression, false).getFlattened().forEach(results::add); results.remove(df.getOWLNothing()); break; case "instances": - reasoner.getInstances(expression,false).getFlattened().forEach(results::add); + reasoner.getInstances(expression, false).getFlattened().forEach(results::add); break; default: throw new IllegalArgumentException(type + " is not a legal query relation for dl-query"); - } + } } return results; } -public static OWLClassExpression parseOWLClassExpression(String expression, OWLOntology ontology) throws ParserException { + public static OWLClassExpression parseOWLClassExpression(String expression, OWLOntology ontology) + throws ParserException { OWLClassExpression classExpression = null; - BidirectionalShortFormProviderAdapter shortFormProvider = - new BidirectionalShortFormProviderAdapter(OWLManager.createOWLOntologyManager(), - Collections.singleton(ontology), new SimpleShortFormProvider()); - - ManchesterOWLSyntaxClassExpressionParser parser = - new ManchesterOWLSyntaxClassExpressionParser(ontology.getOWLOntologyManager().getOWLDataFactory(), - new ShortFormEntityChecker(shortFormProvider)); - + BidirectionalShortFormProviderAdapter shortFormProvider = + new BidirectionalShortFormProviderAdapter( + OWLManager.createOWLOntologyManager(), + Collections.singleton(ontology), + new SimpleShortFormProvider()); + + ManchesterOWLSyntaxClassExpressionParser parser = + new ManchesterOWLSyntaxClassExpressionParser( + ontology.getOWLOntologyManager().getOWLDataFactory(), + new ShortFormEntityChecker(shortFormProvider)); + classExpression = parser.parse(expression); return classExpression; -} - + } }