Java Code Examples for org.semanticweb.owlapi.reasoner.OWLReasoner#precomputeInferences()

The following examples show how to use org.semanticweb.owlapi.reasoner.OWLReasoner#precomputeInferences() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetrievingInstances.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 * @throws OWLOntologyCreationException
 */
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();

	// Load your ontology.
	OWLOntology ont = man
			.loadOntologyFromOntologyDocument(new File(args[0]));
	
	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);
	
	// Precompute instances for each named class in the ontology
	reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);

	// List representative instances for each class.
	for (OWLClass clazz : ont.getClassesInSignature()) {
		for (Node<OWLNamedIndividual> individual : reasoner.getInstances(
				clazz, true)) {
			System.out.println(clazz + "("
					+ individual.getRepresentativeElement() + ")");
		}
	}

	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 2
Source File: SavingInferredAxioms.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws OWLOntologyStorageException,
		OWLOntologyCreationException {
	OWLOntologyManager inputOntologyManager = OWLManager.createOWLOntologyManager();
	OWLOntologyManager outputOntologyManager = OWLManager.createOWLOntologyManager();

	// Load your ontology.
	OWLOntology ont = inputOntologyManager.loadOntologyFromOntologyDocument(new File("path-to-ontology"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Classify the ontology.
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	// To generate an inferred ontology we use implementations of
	// inferred axiom generators
	List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
	gens.add(new InferredSubClassAxiomGenerator());
	gens.add(new InferredEquivalentClassAxiomGenerator());

	// Put the inferred axioms into a fresh empty ontology.
	OWLOntology infOnt = outputOntologyManager.createOntology();
	InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,
			gens);
	iog.fillOntology(outputOntologyManager.getOWLDataFactory(), infOnt);

	// Save the inferred ontology.
	outputOntologyManager.saveOntology(infOnt,
			new FunctionalSyntaxDocumentFormat(),
			IRI.create((new File("path-to-output").toURI())));

	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 3
Source File: IncrementalClassification.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws OWLOntologyStorageException,
		OWLOntologyCreationException {
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

	// Load your ontology
	OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File("path-to-ontology"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Classify the ontology.
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	OWLDataFactory factory = manager.getOWLDataFactory();
	OWLClass subClass = factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#AbsoluteShapeState"));
	OWLAxiom removed = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#ShapeState")));
	
	OWLAxiom added = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#GeneralisedStructure")));
	// Remove an existing axiom, add a new axiom
	manager.addAxiom(ont, added);
	manager.removeAxiom(ont, removed);
	// This is a buffering reasoner, so you need to flush the changes
	reasoner.flush();
	
	// Re-classify the ontology, the changes should be accommodated
	// incrementally (i.e. without re-inferring all subclass relationships)
	// You should be able to see it from the log output
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);		
	
	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 4
Source File: EmptyImportTest.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
/**
 * Testing loading of ontologies that have no axioms (but possibly import
 * declarations).
 * 
 * @see <a
 *      href="http://code.google.com/p/elk-reasoner/issues/detail?id=7">Issue 7<a>
 * @throws OWLOntologyCreationException
 * @throws URISyntaxException
 */
@Test
public void testImport() throws OWLOntologyCreationException,
		URISyntaxException {

	OWLOntologyManager man = TestOWLManager.createOWLOntologyManager();

	// loading the root ontology
	OWLOntology root = loadOntology(man, "root.owl");

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(root);

	try {
		// statistics about the root ontology
		assertEquals(root.getAxiomCount(), 0);
		// all two ontologies should be in the closure
		assertEquals(root.getImportsClosure().size(), 2);
		// all axioms from two ontologies should be in the closure
		assertEquals(getAxioms(root).size(), 0);

		// reasoner queries -- all subclasses are there
		reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
	} finally {
		reasoner.dispose();
	}

}
 
Example 5
Source File: ReasonOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given an ontology, a reasoner, and a map of options, use the reasoner to validate the ontology,
 * compute class hierarchy, and find equivalencies.
 *
 * @param ontology OWLOntology to reason over
 * @param reasoner OWLReasoner to use
 * @param options Map of reason options
 * @throws OntologyLogicException on invalid ontology
 */
private static void reason(
    OWLOntology ontology, OWLReasoner reasoner, Map<String, String> options)
    throws OntologyLogicException {
  long startTime = System.currentTimeMillis();
  logger.info("Starting reasoning...");

  // Validate and maybe dump the unsat classes into a file
  String dumpFilePath = OptionsHelper.getOption(options, "dump-unsatisfiable", null);
  ReasonerHelper.validate(reasoner, dumpFilePath);

  logger.info("Precomputing class hierarchy...");
  reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);

  EquivalentClassReasoningMode mode =
      EquivalentClassReasoningMode.from(options.getOrDefault("equivalent-classes-allowed", ""));
  logger.info("Finding equivalencies...");

  EquivalentClassReasoning equivalentReasoning =
      new EquivalentClassReasoning(ontology, reasoner, mode);
  boolean passesEquivalenceTests = equivalentReasoning.reason();
  equivalentReasoning.logReport(logger);
  if (!passesEquivalenceTests) {
    throw new OntologyLogicException(equivalentClassAxiomError);
  }

  float elapsedTime = System.currentTimeMillis() - startTime;
  long seconds = (int) Math.ceil(elapsedTime / 1000);
  logger.info("Reasoning took {} seconds.", seconds);
}
 
Example 6
Source File: Example_Extended.java    From sparql-dl-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) 
{
	try {
		// Create our ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

		// Load a copy of the wine ontology.  We'll load the ontology from the web.
           OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

		// Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
           StructuralReasonerFactory factory = new StructuralReasonerFactory();
		OWLReasoner reasoner = factory.createReasoner(ont);
           // Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner);
		
           // Some queries which demonstrate more sophisticated language constructs of SPARQL-DL

           // The empty ASK is true by default
           processQuery(
			"ASK {}"
		);

           // The response to an empty SELECT is an empty response
           processQuery(
			"SELECT * WHERE {}"
		);

           // There can't be an instance of owl:Nothing. Therefore this query has no solutions.
           processQuery(
               "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
			"SELECT * WHERE { Type(?x,owl:Nothing) }"
		);

           // A complicated way to retrieve all individuals. Note that the WHERE keyword is optional.
           processQuery(
               "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
			"SELECT DISTINCT ?x { Type(?x,?y), ComplementOf(owl:Nothing,?y) }"
		);

           // All wines which are OffDry
           processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
               "PREFIX food: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/food#>\n" +
			"SELECT DISTINCT ?w WHERE { PropertyValue(?w, wine:hasWineDescriptor, food:OffDry) }"
		);

           // A query returning pairs of results, namely all sources and fillers of yearValue
           processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT DISTINCT ?w ?g WHERE { PropertyValue(?w, wine:yearValue, ?g)" +
			"}"
		);

           // The most specific types of wines of all wineries
           processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT DISTINCT ?x ?y WHERE {\n" +
                   "Type(?x, wine:Winery), \n" +
                   "DirectType(?z, ?y), \n" +
                   "PropertyValue(?x, wine:producesWine, ?z)" +
			"}"
		);

           // All entities which are either object properties or classes
		processQuery(
			"SELECT ?i WHERE {" +
			    "ObjectProperty(?i) " +
			"} OR WHERE {" +
                   "Class(?i)" +
               "}"
		);

           // Equivalent query to the one above
           processQuery(
			"SELECT * WHERE {" +
			    "ObjectProperty(?i) " +
			"} OR WHERE {" +
                   "Class(?j)" +
               "}"
		);

       }
       catch(UnsupportedOperationException exception) {
           System.out.println("Unsupported reasoner operation.");
       }
       catch(OWLOntologyCreationException e) {
           System.out.println("Could not load the pizza ontology: " + e.getMessage());
       }
}
 
Example 7
Source File: Example_Basic.java    From sparql-dl-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) 
{
	try {
		// Create an ontology manager
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

		// Load the wine ontology from the web.
           OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

		// Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
           StructuralReasonerFactory factory = new StructuralReasonerFactory();
		OWLReasoner reasoner = factory.createReasoner(ont);
           // Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner, true);

           // Some queries which cover important basic language constructs of SPARQL-DL

           // All white wines (all individuals of the class WhiteWine and sub classes thereof)
		processQuery(
			"SELECT * WHERE {\n" +
			    "Type(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#WhiteWine>)" +
			"}"	
		);

           // The white wines (the individuals of WhiteWine but not of it's sub classes) 
           processQuery(
			"SELECT * WHERE {\n" +
			    "DirectType(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#RedTableWine>)" +
			"}"
		);

           // Is PinotBlanc a sub class of Wine?
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"ASK {\n" +
				"SubClassOf(wine:PinotBlanc, wine:Wine)" +
			"}"
		);

           // The direct sub classes of FrenchWine
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT ?x WHERE {\n" +
				"DirectSubClassOf(?x, wine:FrenchWine)" +
			"}"	
		);

		// All individuals
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
				"Individual(?x)" +
			"}"
		);

           // All functional ObjectProperties
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
                   "ObjectProperty(?x), " +
				"Functional(?x)" +
			"}"
		);

           // The strict sub classes of DryWhiteWine (sub classes with are not equivalent to DryWhiteWine)
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT ?x WHERE {\n" +
				"StrictSubClassOf(?x, wine:DryWhiteWine)" +
			"}"
		);

           // All the grapes from which RedTableWines are made from (without duplicates)
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" + 
			"SELECT DISTINCT ?v WHERE {\n" +
			    "Type(?i, wine:RedTableWine),\n" +
			    "PropertyValue(?i, wine:madeFromGrape, ?v)" +
			"}"	
		);

       }
       catch(UnsupportedOperationException exception) {
           System.out.println("Unsupported reasoner operation.");
       }
       catch(OWLOntologyCreationException e) {
           System.out.println("Could not load the wine ontology: " + e.getMessage());
       }
}
 
Example 8
Source File: Example_XML_JSON.java    From sparql-dl-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) 
{
	try {
		// Create an ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

           // Load the wine ontology from the web.
           OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

           // Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
           StructuralReasonerFactory factory = new StructuralReasonerFactory();
		OWLReasoner reasoner = factory.createReasoner(ont);
           // Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner);
		
		processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
			    "SubClassOf(wine:PinotBlanc, ?x),\n" +
			    "SubClassOf(?x, wine:Wine)\n" +
			"}"
		);
		
		processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"ASK {\n" +
			    "SubClassOf(wine:PinotBlanc, wine:Wine)\n" +
			"}"
		);
       }
       catch(UnsupportedOperationException exception) {
           System.out.println("Unsupported reasoner operation.");
       }
       catch(OWLOntologyCreationException e) {
           System.out.println("Could not load the ontology: " + e.getMessage());
       }
}
 
Example 9
Source File: SparqlDLQueryTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testQuery() 
{
	OWLReasoner reasoner = null;
	try {
		// Create an ontology manager
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

		// Load the wine ontology from the web.
		OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

		// Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
		StructuralReasonerFactory factory = new StructuralReasonerFactory();
		reasoner = factory.createReasoner(ont);
		// Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner, true);

		// Some queries which cover important basic language constructs of SPARQL-DL

		// All white wines (all individuals of the class WhiteWine and sub classes thereof)
		processQuery(
				"SELECT * WHERE {\n" +
						"Type(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#WhiteWine>)" +
						"}"	
				);

		// The white wines (the individuals of WhiteWine but not of it's sub classes) 
		processQuery(
				"SELECT * WHERE {\n" +
						"DirectType(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#RedTableWine>)" +
						"}"
				);

		// Is PinotBlanc a sub class of Wine?
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
						"ASK {\n" +
						"SubClassOf(wine:PinotBlanc, wine:Wine)" +
						"}"
				);

		// The direct sub classes of FrenchWine
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
						"SELECT ?x WHERE {\n" +
						"DirectSubClassOf(?x, wine:FrenchWine)" +
						"}"	
				);

		// All individuals
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
						"SELECT * WHERE {\n" +
						"Individual(?x)" +
						"}"
				);

		// All functional ObjectProperties
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
						"SELECT * WHERE {\n" +
						"ObjectProperty(?x), " +
						"Functional(?x)" +
						"}"
				);

		// The strict sub classes of DryWhiteWine (sub classes with are not equivalent to DryWhiteWine)
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
						"SELECT ?x WHERE {\n" +
						"StrictSubClassOf(?x, wine:DryWhiteWine)" +
						"}"
				);

		// All the grapes from which RedTableWines are made from (without duplicates)
		processQuery(
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" + 
						"SELECT DISTINCT ?v WHERE {\n" +
						"Type(?i, wine:RedTableWine),\n" +
						"PropertyValue(?i, wine:madeFromGrape, ?v)" +
						"}"	
				);

	}
	catch(UnsupportedOperationException exception) {
		System.out.println("Unsupported reasoner operation.");
	}
	catch(OWLOntologyCreationException e) {
		System.out.println("Could not load the wine ontology: " + e.getMessage());
	}
	finally {
		if (reasoner != null) {
			reasoner.dispose();
		}
	}
}