/* This file is part of the OWL API. * The contents of this file are subject to the LGPL License, Version 3.0. * Copyright 2014, The University of Manchester * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. * * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package org.semanticweb.owlapi.examples; import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.asList; import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.asUnorderedSet; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Set; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.expression.OWLEntityChecker; import org.semanticweb.owlapi.expression.ShortFormEntityChecker; import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; import org.semanticweb.owlapi.io.StringDocumentSource; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLNamedIndividual; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.reasoner.Node; import org.semanticweb.owlapi.reasoner.NodeSet; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory; import org.semanticweb.owlapi.util.BidirectionalShortFormProvider; import org.semanticweb.owlapi.util.BidirectionalShortFormProviderAdapter; import org.semanticweb.owlapi.util.ShortFormProvider; import org.semanticweb.owlapi.util.SimpleShortFormProvider; import org.semanticweb.owlapi.util.mansyntax.ManchesterOWLSyntaxParser; /** * An example that shows how to do a Protege like DLQuery. The example contains several helper * classes:<br> * DLQueryEngine - This takes a string representing a class expression built from the terms in the * signature of some ontology. DLQueryPrinter - This takes a string class expression and prints out * the sub/super/equivalent classes and the instances of the specified class expression. * DLQueryParser - this parses the specified class expression string * * @author Matthew Horridge, The University of Manchester, Bio-Health Informatics Group * @since 3.1.0 */ @SuppressWarnings({"javadoc"}) public class DLQueryExample { private static final String KOALA = "<?xml version=\"1.0\"?>\n" + "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#\" xml:base=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl\">\n" + " <owl:Ontology rdf:about=\"\"/>\n" + " <owl:Class rdf:ID=\"Female\"><owl:equivalentClass><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"female\"/></owl:hasValue></owl:Restriction></owl:equivalentClass></owl:Class>\n" + " <owl:Class rdf:ID=\"Marsupials\"><owl:disjointWith><owl:Class rdf:about=\"#Person\"/></owl:disjointWith><rdfs:subClassOf><owl:Class rdf:about=\"#Animal\"/></rdfs:subClassOf></owl:Class>\n" + " <owl:Class rdf:ID=\"Student\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Person\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue></owl:Restriction><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#University\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n" + " <owl:Class rdf:ID=\"KoalaWithPhD\"><owl:versionInfo>1.2</owl:versionInfo><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Restriction><owl:hasValue><Degree rdf:ID=\"PhD\"/></owl:hasValue><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty></owl:Restriction><owl:Class rdf:about=\"#Koala\"/></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n" + " <owl:Class rdf:ID=\"University\"><rdfs:subClassOf><owl:Class rdf:ID=\"Habitat\"/></rdfs:subClassOf></owl:Class>\n" + " <owl:Class rdf:ID=\"Koala\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">false</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#DryEucalyptForest\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"Animal\"><rdfs:seeAlso>Male</rdfs:seeAlso><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:cardinality><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><owl:versionInfo>1.1</owl:versionInfo></owl:Class>\n" + " <owl:Class rdf:ID=\"Forest\"><rdfs:subClassOf rdf:resource=\"#Habitat\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"Rainforest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"GraduateStudent\"><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty><owl:someValuesFrom><owl:Class><owl:oneOf rdf:parseType=\"Collection\"><Degree rdf:ID=\"BA\"/><Degree rdf:ID=\"BS\"/></owl:oneOf></owl:Class></owl:someValuesFrom></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Student\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"Parent\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Animal\"/><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass><rdfs:subClassOf rdf:resource=\"#Animal\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"DryEucalyptForest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"Quokka\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"TasmanianDevil\"><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n" + " <owl:Class rdf:ID=\"MaleStudentWith3Daughters\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Student\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"male\"/></owl:hasValue></owl:Restriction><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">3</owl:cardinality></owl:Restriction><owl:Restriction><owl:allValuesFrom rdf:resource=\"#Female\"/><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n" + " <owl:Class rdf:ID=\"Degree\"/>\n <owl:Class rdf:ID=\"Gender\"/>\n" + " <owl:Class rdf:ID=\"Male\"><owl:equivalentClass><owl:Restriction><owl:hasValue rdf:resource=\"#male\"/><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></owl:equivalentClass></owl:Class>\n" + " <owl:Class rdf:ID=\"Person\"><rdfs:subClassOf rdf:resource=\"#Animal\"/><owl:disjointWith rdf:resource=\"#Marsupials\"/></owl:Class>\n" + " <owl:ObjectProperty rdf:ID=\"hasHabitat\"><rdfs:range rdf:resource=\"#Habitat\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n" + " <owl:ObjectProperty rdf:ID=\"hasDegree\"><rdfs:domain rdf:resource=\"#Person\"/><rdfs:range rdf:resource=\"#Degree\"/></owl:ObjectProperty>\n" + " <owl:ObjectProperty rdf:ID=\"hasChildren\"><rdfs:range rdf:resource=\"#Animal\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n" + " <owl:FunctionalProperty rdf:ID=\"hasGender\"><rdfs:range rdf:resource=\"#Gender\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#ObjectProperty\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:FunctionalProperty>\n" + " <owl:FunctionalProperty rdf:ID=\"isHardWorking\"><rdfs:range rdf:resource=\"http://www.w3.org/2001/XMLSchema#boolean\"/><rdfs:domain rdf:resource=\"#Person\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#DatatypeProperty\"/></owl:FunctionalProperty>\n" + " <Degree rdf:ID=\"MA\"/>\n</rdf:RDF>"; private DLQueryExample() {} public static void main(String[] args) { try { // Load an example ontology. In this case, we'll just load the pizza // ontology. OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument( new StringDocumentSource(KOALA, new RDFXMLDocumentFormat())); System.out.println("Loaded ontology: " + ontology.getOntologyID()); // We need a reasoner to do our query answering OWLReasoner reasoner = createReasoner(ontology); // Entities are named using IRIs. These are usually too long for use // in user interfaces. To solve this // problem, and so a query can be written using short class, // property, individual names we use a short form // provider. In this case, we'll just use a simple short form // provider that generates short froms from IRI // fragments. ShortFormProvider shortFormProvider = new SimpleShortFormProvider(); // Create the DLQueryPrinter helper class. This will manage the // parsing of input and printing of results DLQueryPrinter dlQueryPrinter = new DLQueryPrinter( new DLQueryEngine(reasoner, shortFormProvider), shortFormProvider); // Enter the query loop. A user is expected to enter class // expression on the command line. doQueryLoop(dlQueryPrinter); } catch (OWLOntologyCreationException e) { System.out.println("Could not load ontology: " + e.getMessage()); } catch (IOException ioEx) { System.out.println(ioEx.getMessage()); } } private static void doQueryLoop(DLQueryPrinter dlQueryPrinter) throws IOException { while (true) { // Prompt the user to enter a class expression System.out.println( "Please type a class expression in Manchester Syntax and press Enter (or press x to exit):"); System.out.println(""); String classExpression = readInput(); // Check for exit condition if (classExpression.equalsIgnoreCase("x")) { break; } dlQueryPrinter.askQuery(classExpression.trim()); System.out.println(); System.out.println(); } } private static String readInput() throws IOException { InputStream is = System.in; InputStreamReader reader; reader = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(reader); return br.readLine(); } private static OWLReasoner createReasoner(OWLOntology rootOntology) { // We need to create an instance of OWLReasoner. An OWLReasoner provides // the basic query functionality that we need, for example the ability // obtain the subclasses of a class etc. To do this we use a reasoner // factory. // Create a reasoner factory. OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory(); return reasonerFactory.createReasoner(rootOntology); } } /** * This example shows how to perform a "dlquery". The DLQuery view/tab in Protege 4 works like this. */ class DLQueryEngine { private final OWLReasoner reasoner; private final DLQueryParser parser; /** * Constructs a DLQueryEngine. This will answer "DL queries" using the specified reasoner. A * short form provider specifies how entities are rendered. * * @param reasoner The reasoner to be used for answering the queries. * @param shortFormProvider A short form provider. */ DLQueryEngine(OWLReasoner reasoner, ShortFormProvider shortFormProvider) { this.reasoner = reasoner; OWLOntology rootOntology = reasoner.getRootOntology(); parser = new DLQueryParser(rootOntology, shortFormProvider); } /** * Gets the superclasses of a class expression parsed from a string. * * @param classExpressionString The string from which the class expression will be parsed. * @param direct Specifies whether direct superclasses should be returned or not. * @return The superclasses of the specified class expression If there was a problem parsing the * class expression. */ public Set<OWLClass> getSuperClasses(String classExpressionString, boolean direct) { if (classExpressionString.trim().isEmpty()) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); NodeSet<OWLClass> superClasses = reasoner.getSuperClasses(classExpression, direct); return asUnorderedSet(superClasses.entities()); } /** * Gets the equivalent classes of a class expression parsed from a string. * * @param classExpressionString The string from which the class expression will be parsed. * @return The equivalent classes of the specified class expression If there was a problem * parsing the class expression. */ public Set<OWLClass> getEquivalentClasses(String classExpressionString) { if (classExpressionString.trim().isEmpty()) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); Node<OWLClass> equivalentClasses = reasoner.getEquivalentClasses(classExpression); return asUnorderedSet(equivalentClasses.entities().filter(c -> !c.equals(classExpression))); } /** * Gets the subclasses of a class expression parsed from a string. * * @param classExpressionString The string from which the class expression will be parsed. * @param direct Specifies whether direct subclasses should be returned or not. * @return The subclasses of the specified class expression If there was a problem parsing the * class expression. */ public Set<OWLClass> getSubClasses(String classExpressionString, boolean direct) { if (classExpressionString.trim().isEmpty()) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); NodeSet<OWLClass> subClasses = reasoner.getSubClasses(classExpression, direct); return asUnorderedSet(subClasses.entities()); } /** * Gets the instances of a class expression parsed from a string. * * @param classExpressionString The string from which the class expression will be parsed. * @param direct Specifies whether direct instances should be returned or not. * @return The instances of the specified class expression If there was a problem parsing the * class expression. */ public Set<OWLNamedIndividual> getInstances(String classExpressionString, boolean direct) { if (classExpressionString.trim().isEmpty()) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(classExpression, direct); return asUnorderedSet(individuals.entities()); } } class DLQueryParser { private final OWLOntology rootOntology; private final BidirectionalShortFormProvider bidiShortFormProvider; /** * Constructs a DLQueryParser using the specified ontology and short form provider to map entity * IRIs to short names. * * @param rootOntology The root ontology. This essentially provides the domain vocabulary for * the query. * @param shortFormProvider A short form provider to be used for mapping back and forth between * entities and their short names (renderings). */ DLQueryParser(OWLOntology rootOntology, ShortFormProvider shortFormProvider) { this.rootOntology = rootOntology; OWLOntologyManager manager = rootOntology.getOWLOntologyManager(); List<OWLOntology> importsClosure = asList(rootOntology.importsClosure()); // Create a bidirectional short form provider to do the actual mapping. // It will generate names using the input // short form provider. bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(manager, importsClosure, shortFormProvider); } /** * Parses a class expression string to obtain a class expression. * * @param classExpressionString The class expression string * @return The corresponding class expression if the class expression string is malformed or * contains unknown entity names. */ public OWLClassExpression parseClassExpression(String classExpressionString) { // Set up the real parser ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser(); parser.setStringToParse(classExpressionString); parser.setDefaultOntology(rootOntology); // Specify an entity checker that wil be used to check a class // expression contains the correct names. OWLEntityChecker entityChecker = new ShortFormEntityChecker(bidiShortFormProvider); parser.setOWLEntityChecker(entityChecker); // Do the actual parsing return parser.parseClassExpression(); } } class DLQueryPrinter { private final DLQueryEngine dlQueryEngine; private final ShortFormProvider shortFormProvider; /** * @param engine the engine * @param shortFormProvider the short form provider */ DLQueryPrinter(DLQueryEngine engine, ShortFormProvider shortFormProvider) { this.shortFormProvider = shortFormProvider; dlQueryEngine = engine; } /** * @param classExpression the class expression to use for interrogation */ public void askQuery(String classExpression) { if (classExpression.isEmpty()) { System.out.println("No class expression specified"); } else { StringBuilder sb = new StringBuilder(); sb.append( "\n--------------------------------------------------------------------------------\n"); sb.append("QUERY: "); sb.append(classExpression); sb.append('\n'); sb.append( "--------------------------------------------------------------------------------\n\n"); // Ask for the subclasses, superclasses etc. of the specified // class expression. Print out the results. Set<OWLClass> superClasses = dlQueryEngine.getSuperClasses(classExpression, true); printEntities("SuperClasses", superClasses, sb); Set<OWLClass> equivalentClasses = dlQueryEngine.getEquivalentClasses(classExpression); printEntities("EquivalentClasses", equivalentClasses, sb); Set<OWLClass> subClasses = dlQueryEngine.getSubClasses(classExpression, true); printEntities("SubClasses", subClasses, sb); Set<OWLNamedIndividual> individuals = dlQueryEngine.getInstances(classExpression, true); printEntities("Instances", individuals, sb); System.out.println(sb); } } private void printEntities(String name, Set<? extends OWLEntity> entities, StringBuilder sb) { sb.append(name); int length = 50 - name.length(); for (int i = 0; i < length; i++) { sb.append('.'); } sb.append("\n\n"); if (!entities.isEmpty()) { for (OWLEntity entity : entities) { sb.append('\t'); sb.append(shortFormProvider.getShortForm(entity)); sb.append('\n'); } } else { sb.append("\t[NONE]\n"); } sb.append('\n'); } }