Java Code Examples for org.semanticweb.owlapi.reasoner.OWLReasonerFactory#createReasoner()

The following examples show how to use org.semanticweb.owlapi.reasoner.OWLReasonerFactory#createReasoner() . 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: SpeciesSubsetterUtilTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSubsetterSpecies() throws Exception {
	ParserWrapper p = new ParserWrapper();
	p.setCheckOboDoc(false);
	OWLOntology owlOntology = p.parse(getResourceIRIString("speciesMergeTest.obo"));
	OWLGraphWrapper graph = new OWLGraphWrapper(owlOntology);
	OWLReasonerFactory rf = new ElkReasonerFactory();
	OWLReasoner reasoner = rf.createReasoner(graph.getSourceOntology());
	SpeciesSubsetterUtil smu = new SpeciesSubsetterUtil(graph);
	//smu.viewProperty = graph.getOWLObjectPropertyByIdentifier("BFO:0000050");
	smu.taxClass = graph.getOWLClassByIdentifier("T:1");
	smu.reasoner = reasoner;
	smu.removeOtherSpecies();
	
	p.saveOWL(smu.ont, new OBODocumentFormat(), "target/speciesSubset.obo");
	//p.saveOWL(smu.ont,  getResourceIRIString("target/speciesSubset.owl"));
	
	assertNull(graph.getOWLClassByIdentifier("U:24"));
}
 
Example 2
Source File: SpeciesMergeUtilTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testMergeSpecies() throws Exception {
	ParserWrapper p = new ParserWrapper();
	OWLOntology owlOntology = p.parse(getResourceIRIString("speciesMergeTest.obo"));
	OWLGraphWrapper graph = new OWLGraphWrapper(owlOntology);
	OWLReasonerFactory rf = new ElkReasonerFactory();
	OWLReasoner reasoner = rf.createReasoner(graph.getSourceOntology());
	SpeciesMergeUtil smu = new SpeciesMergeUtil(graph);
	smu.viewProperty = graph.getOWLObjectPropertyByIdentifier("BFO:0000050");
	smu.taxClass = graph.getOWLClassByIdentifier("T:1");
	smu.reasoner = reasoner;
	smu.suffix = "coelocanth";
	smu.merge();
	
	p.saveOWL(smu.ont, new OBODocumentFormat(), "target/speciesMergeOut.obo");
	
}
 
Example 3
Source File: ReasonerHelperTest.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test checking for incoherent OPs.
 *
 * <p>See https://github.com/ontodev/robot/issues/104
 *
 * @throws IOException if file error
 * @throws IncoherentTBoxException if has unsatisfiable classes
 * @throws InconsistentOntologyException if has inconsistencies
 */
@Test
public void testIncoherentRBox()
    throws IOException, IncoherentTBoxException, InconsistentOntologyException {
  OWLOntology ontology = loadOntology("/incoherent-rbox.owl");
  OWLReasonerFactory reasonerFactory = new org.semanticweb.elk.owlapi.ElkReasonerFactory();
  OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
  boolean isCaughtException = false;
  try {
    ReasonerHelper.validate(reasoner);

  } catch (IncoherentRBoxException e) {
    isCaughtException = true;
  }
  assertTrue(isCaughtException);
}
 
Example 4
Source File: PropertyViewOntologyBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param reasonerFactory
 * @throws OWLOntologyCreationException 
 */
public void build(OWLReasonerFactory reasonerFactory) throws OWLOntologyCreationException {
	buildViewOntology();
	OWLReasoner reasoner = reasonerFactory.createReasoner(assertedViewOntology);
	try {
		buildInferredViewOntology(reasoner);
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 5
Source File: ReasonerHelperTest.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test for no false positives in validation.
 *
 * @throws IOException if file error
 * @throws IncoherentTBoxException if has unsatisfiable classes
 * @throws InconsistentOntologyException if has inconsistencies
 * @throws IncoherentRBoxException if has unsatisfiable properties
 */
@Test
public void testNoFalsePositives()
    throws IOException, IncoherentTBoxException, InconsistentOntologyException,
        IncoherentRBoxException {
  OWLOntology ontology = loadOntology("/simple.owl");
  OWLReasonerFactory reasonerFactory = new org.semanticweb.elk.owlapi.ElkReasonerFactory();
  OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
  ReasonerHelper.validate(reasoner);

  // trivially true, if no exceptions are caught
  assertTrue(true);
}
 
Example 6
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 7
Source File: SpeciesMergeUtilTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMergeFly() throws Exception {
	ParserWrapper p = new ParserWrapper();
	OWLOntology owlOntology = p.parse(getResourceIRIString("interneuron-fly.obo"));
	Set<OWLClass> clsBefore = owlOntology.getClassesInSignature();
	OWLGraphWrapper graph = new OWLGraphWrapper(owlOntology);
	OWLReasonerFactory rf = new ElkReasonerFactory();
	OWLReasoner reasoner = rf.createReasoner(graph.getSourceOntology());
	SpeciesMergeUtil smu = new SpeciesMergeUtil(graph);

	smu.viewProperty = graph.getOWLObjectPropertyByIdentifier("BFO:0000050");
	smu.taxClass = graph.getOWLClassByIdentifier("NCBITaxon:7227");
	smu.reasoner = reasoner;
	smu.suffix = "fly";
	smu.includedProperties = Collections.singleton(smu.viewProperty);
	//smu.includedProperties = Collections.singleton(graph.getOWLObjectPropertyByIdentifier("BFO:0000051"));
	smu.merge();
	
	Set<OWLClass> clsAfter = smu.ont.getClassesInSignature();
	
	LOG.info("Before: "+clsBefore.size());
	LOG.info("After: "+clsAfter.size());
	assertEquals(100, clsBefore.size());
	assertEquals(90, clsAfter.size());
	p.saveOWL(smu.ont, new OBODocumentFormat(), "target/flyMergeOut.obo");

}
 
Example 8
Source File: EquivalentClassReasoningTest.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test asserted only with inferred equiv axioms. Pass if false.
 *
 * @throws IOException on error
 */
@Test
public void testReasonOnlyAssertedAllowedOnInferred() throws IOException {
  OWLOntology inferred = loadOntology("/inferred-equiv.owl");
  OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
  OWLReasoner reasoner = reasonerFactory.createReasoner(inferred);
  EquivalentClassReasoning assertedOnlyReasoning =
      new EquivalentClassReasoning(
          inferred, reasoner, EquivalentClassReasoningMode.ASSERTED_ONLY);

  assertFalse(assertedOnlyReasoning.reason());
}
 
Example 9
Source File: EcoTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a new instance using the ECO graph wrapper.
 * 
 * @param eco
 */
public EcoTools(OWLGraphWrapper eco) {
	this.eco = eco;
	OWLReasonerFactory factory = new ElkReasonerFactory();
	final OWLOntology sourceOntology = eco.getSourceOntology();
	reasoner = factory.createReasoner(sourceOntology);
	disposeReasonerP = true;
}
 
Example 10
Source File: EquivalentClassReasoningTest.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test all equivalences allowed with inferred equiv axioms. Pass if true.
 *
 * @throws IOException on error
 */
@Test
public void testReasonAllEquivalencesAllowed() throws IOException {
  OWLOntology inferred = loadOntology("/inferred-equiv.owl");
  OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
  OWLReasoner reasoner = reasonerFactory.createReasoner(inferred);
  EquivalentClassReasoning allAllowedReasoning =
      new EquivalentClassReasoning(inferred, reasoner, EquivalentClassReasoningMode.ALL);

  assertTrue(allAllowedReasoning.reason());
}
 
Example 11
Source File: GoIPICatalyticActivityRestrictionsRule.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GoIPICatalyticActivityRestrictionsRule(OWLGraphWrapper graph, TraversingEcoMapper eco) {
	this.message = MESSAGE;
	this.violationType = ViolationType.Warning;
	
	evidences = eco.getAllValidEvidenceIds("IPI", true);
	
	classSubSet = new HashSet<String>();
	
	OWLClass rootClass = graph.getOWLClassByIdentifier("GO:0003824"); // catalytic activity
	OWLReasonerFactory factory = new ElkReasonerFactory();
	OWLReasoner reasoner = factory.createReasoner(graph.getSourceOntology());
	try {
		NodeSet<OWLClass> nodeSet = reasoner.getSubClasses(rootClass, false);
		for(OWLClass cls : nodeSet.getFlattened()) {
			if (cls.isBottomEntity() || cls.isTopEntity()) {
				continue;
			}
			String oboId = graph.getIdentifier(cls);
			if (oboId != null) {
				classSubSet.add(oboId);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 12
Source File: ReasonerHelperTest.java    From robot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test creating an unsatisfiable module
 *
 * @throws Exception on any problem
 */
@Test
public void testCreateUnsatisfiableModule() throws Exception {
  OWLOntology ontologyMain = loadOntology("/incoherent-tbox.owl");
  IRI iri = ontologyMain.getOntologyID().getOntologyIRI().orNull();
  if (iri == null) {
    throw new Exception("Ontology 'incoherent-tbox.owl' does not have an IRI");
  }
  OWLOntology ontology = ontologyMain.getOWLOntologyManager().createOntology();
  OWLDataFactory factory = ontology.getOWLOntologyManager().getOWLDataFactory();
  AddImport ai = new AddImport(ontology, factory.getOWLImportsDeclaration(iri));
  ontology.getOWLOntologyManager().applyChange(ai);
  OWLReasonerFactory reasonerFactory = new org.semanticweb.elk.owlapi.ElkReasonerFactory();
  OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
  boolean isCaughtException = false;
  IOHelper ioHelper = new IOHelper();
  String PATH = "target/unsat.owl";
  try {
    ReasonerHelper.validate(reasoner, PATH, ioHelper);
  } catch (IncoherentTBoxException e) {
    isCaughtException = true;
    IOHelper ioh = new IOHelper();
    OWLOntology unsatisfiableOntology = ioh.loadOntology(PATH);
    Set<OWLSubClassOfAxiom> scas = unsatisfiableOntology.getAxioms(AxiomType.SUBCLASS_OF);

    // we expect each axiom to have exactly one annotation, which is an rdfs:isDefinedBy
    // pointing at the ontology in which the axiom came from
    for (OWLSubClassOfAxiom sca : scas) {
      assertEquals(1, sca.getAnnotations().size());
      OWLAnnotation ann = sca.getAnnotations().iterator().next();
      assertEquals(factory.getRDFSIsDefinedBy(), ann.getProperty());
      OWLLiteral v = ann.getValue().asLiteral().orNull();
      if (v == null) {
        throw new Exception(String.format("OWLAnnotation '%s' has no value.", ann.toString()));
      }
      String val = v.getLiteral();
      String originalOntIdVal = ontologyMain.getOntologyID().toString();
      assertEquals(originalOntIdVal, val);
    }
    assertEquals(2, scas.size());
  }
  assertTrue(isCaughtException);
}
 
Example 13
Source File: SolrCommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Experimental method for trying out the loading of complex_annotation doc type.
 * Works with --read-ca-list <file>.
 * 
 * @param opts
 * @throws Exception
 */
@CLIMethod("--solr-load-complex-exp")
public void loadComplexAnnotationSolr(Opts opts) throws Exception {

	// Check to see if the global url has been set.
	String url = sortOutSolrURL(globalSolrURL);				

	// Only proceed if our environment was well-defined.
	if( caFiles == null || caFiles.isEmpty() ){
		LOG.warn("LEGO environment not well defined--will skip loading LEGO/CA.");
	}else{

		// NOTE: These two lines are remainders from old code, and I'm not sure of their place in this world of ours.
		// I wish there was an arcitecture diagram somehwere...
		OWLOntologyManager manager = pw.getManager();
		OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();

		// Actual loading--iterate over our list and load individually.
		for( String fname : caFiles ){
			OWLReasoner currentReasoner = null;
			OWLOntology ontology = null;

			// TODO: Temp cover for missing group labels and IDs.
			//String agID = legoFile.getCanonicalPath();
			String pretmp = StringUtils.removeEnd(fname, ".owl");
			String[] bits = StringUtils.split(pretmp, "/");
			String agID = bits[bits.length -1];
			String agLabel = new String(StringUtils.replaceOnce(agID, ":", "_"));

			try {
				ontology = pw.parseOWL(IRI.create(fname));
				currentReasoner = reasonerFactory.createReasoner(ontology);

				// Some sanity checks--some of the genereated ones are problematic.
				boolean consistent = currentReasoner.isConsistent();
				if( consistent == false ){
					LOG.info("Skip since inconsistent: " + fname);
					continue;
				}
				Set<OWLClass> unsatisfiable = currentReasoner.getUnsatisfiableClasses().getEntitiesMinusBottom();
				if (unsatisfiable.isEmpty() == false) {
					LOG.info("Skip since unsatisfiable: " + fname);
					continue;
				}

				Set<OWLNamedIndividual> individuals = ontology.getIndividualsInSignature();
				Set<OWLAnnotation> modelAnnotations = ontology.getAnnotations();
				OWLGraphWrapper currentGraph = new OWLGraphWrapper(ontology);						
				try {
					LOG.info("Trying complex annotation load of: " + fname);
					ComplexAnnotationSolrDocumentLoader loader =
							new ComplexAnnotationSolrDocumentLoader(url, currentGraph, currentReasoner, individuals, modelAnnotations, agID, agLabel, fname);
					loader.load();
				} catch (SolrServerException e) {
					LOG.info("Complex annotation load of " + fname + " at " + url + " failed!");
					e.printStackTrace();
					System.exit(1);
				}
			} finally {
				// Cleanup reasoner and ontology.
				if (currentReasoner != null) {
					currentReasoner.dispose();
				}
				if (ontology != null) {
					manager.removeOntology(ontology);
				}
			}
		}
	}
}
 
Example 14
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner when changes are made to other, imported or not, ontologies
 * 
 */
@Test
public void testChangesToOtherOntologies() throws Exception {

	OWLOntologyManager man = TestOWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// set up resolution of prefixes
	PrefixManager pm = new DefaultPrefixManager();
	pm.setDefaultPrefix("http://www.example.com/main#");
	pm.setPrefix("A:", "http://www.example.com/A#");
	pm.setPrefix("B:", "http://www.example.com/B#");

	// define query classes
	OWLClass mainY = dataFactory.getOWLClass(":Y", pm);
	OWLClass extA = dataFactory.getOWLClass("A:A", pm);
	OWLClass extB = dataFactory.getOWLClass("B:B", pm);
	OWLClass extC = dataFactory.getOWLClass("B:C", pm);

	// loading the root ontology
	OWLOntology root = loadOntology(man, "root.owl");
	// the imported ontologies must be loaded
	OWLOntology ontoA = man.getOntology(IRI.create("http://www.example.com/A"));
	OWLOntology ontoB = man.getOntology(IRI.create("http://www.example.com/B"));

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

	try {
		
		assertTrue(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));
		assertTrue(reasoner.getSuperClasses(mainY, false).containsEntity(
				extC));
		
		// ************************************
		// ** removing an axiom "A:A is-a B:B" from impA
		// ************************************
		OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB);
		man.removeAxiom(ontoA, axiom);
		reasoner.flush();
		
		assertFalse(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));
		// put it back
		man.addAxiom(ontoA, axiom);
		reasoner.flush();
		
		assertTrue(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));

		// ************************************
		// ** removing an axiom "B:B is-a B:C" from impB
		// ************************************
		axiom = dataFactory.getOWLSubClassOfAxiom(extB, extC);
		man.removeAxiom(ontoB, axiom);
		reasoner.flush();
		
		assertFalse(reasoner.getSuperClasses(mainY, false).containsEntity(
				extC));

	}
	finally {
		reasoner.dispose();
	}
}
 
Example 15
Source File: AnnotationTaxonRule.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unused")
private Pair<OWLReasoner, OWLReasonerFactory> createHermit(final OWLOntology ontology) {
	// use Hermit, as GO has inverse_of relations between part_of and has_part
	OWLReasonerFactory factory = new org.semanticweb.HermiT.ReasonerFactory();
	
	final Configuration configuration = new Configuration();
	configuration.reasonerProgressMonitor = new ReasonerProgressMonitor() {

		// generated
		private static final long serialVersionUID = 5925042026163506083L;

		@Override
		public void reasonerTaskStopped() {
			logger.info("HermiT reasoning task - Finished.");
			
		}
		
		@Override
		public void reasonerTaskStarted(String taskName) {
			logger.info("HermiT reasoning task - Start: "+taskName);
			
		}
		
		double lastProgress = 0.0d;
		
		@Override
		public void reasonerTaskProgressChanged(int value, int max) {
			double progress = value / (double) max;
			if (Math.abs(progress - lastProgress) > 0.05d) {
				NumberFormat percentFormat = NumberFormat.getPercentInstance();
				percentFormat.setMaximumFractionDigits(1);
				logger.info("HermiT reasoning task - Progress: "+percentFormat.format(progress));
				lastProgress = progress;
			}
		}
		
		@Override
		public void reasonerTaskBusy() {
			// do nothing
		}
	};
	OWLReasoner reasoner = factory.createReasoner(ontology, configuration);
	return Pair.of(reasoner, factory);
}
 
Example 16
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 * <p>
 * removing the import declaration for </impA>
 */
@Test
public void testRemovingImpA() throws Exception {

	OWLOntologyManager man = TestOWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// set up resolution of prefixes
	PrefixManager pm = new DefaultPrefixManager();
	pm.setDefaultPrefix("http://www.example.com/main#");
	pm.setPrefix("A:", "http://www.example.com/A#");
	pm.setPrefix("B:", "http://www.example.com/B#");

	// define query classes
	OWLClass mainX = dataFactory.getOWLClass(":X", pm);
	OWLClass mainY = dataFactory.getOWLClass(":Y", pm);
	OWLClass extA = dataFactory.getOWLClass("A:A", pm);
	OWLClass extB = dataFactory.getOWLClass("B:B", pm);
	OWLClass extC = dataFactory.getOWLClass("B:C", pm);

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

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

	try {

		// ************************************
		// ** removing the import declaration for </impA>
		// ************************************

		OWLImportsDeclaration importA = new OWLImportsDeclarationImpl(
				IRI.create("http://www.example.com#impA"));
		OWLOntologyChange change = new RemoveImport(root, importA);
		man.applyChange(change);
		reasoner.flush();

		// Now the root ontology should not import anything
		assertEquals(root.getAxiomCount(), 3);
		assertEquals(root.getImportsClosure().size(), 1);
		assertEquals(getAxioms(root).size(), 3);

		// reasoner queries -- only subsumptions of the root ontology are
		// there
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				mainY));
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				extA));
		assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity(
				extB));
		assertFalse(reasoner.getSuperClasses(extA, true).containsEntity(
				extB));
		assertFalse(reasoner.getSuperClasses(extB, true).containsEntity(
				extC));

	} finally {
		reasoner.dispose();
	}

}
 
Example 17
Source File: AnnotationTaxonRule.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Pair<OWLReasoner, OWLReasonerFactory> createElk(OWLOntology ontology) {
	OWLReasonerFactory factory = new ElkReasonerFactory();
	OWLReasoner reasoner = factory.createReasoner(ontology);
	return Pair.of(reasoner, factory);
}
 
Example 18
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 * 
 * removing an axiom ":X is-a :Y"
 */
@Test
public void testRemovingXY() throws Exception {

	OWLOntologyManager man = TestOWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// set up resolution of prefixes
	PrefixManager pm = new DefaultPrefixManager();
	pm.setDefaultPrefix("http://www.example.com/main#");
	pm.setPrefix("A:", "http://www.example.com/A#");
	pm.setPrefix("B:", "http://www.example.com/B#");

	// define query classes
	OWLClass mainX = dataFactory.getOWLClass(":X", pm);
	OWLClass mainY = dataFactory.getOWLClass(":Y", pm);
	OWLClass extA = dataFactory.getOWLClass("A:A", pm);
	OWLClass extB = dataFactory.getOWLClass("B:B", pm);
	OWLClass extC = dataFactory.getOWLClass("B:C", pm);

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

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

	try {

		// ************************************
		// ** removing an axiom ":X is-a :Y"
		// ************************************
		OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainX, mainY);
		man.removeAxiom(root, axiom);
		reasoner.flush();

		// the root ontology contains one fewer axioms
		assertEquals(root.getAxiomCount(), 2);
		// the number of ontologies in the import closure does not change
		assertEquals(root.getImportsClosure().size(), 3);
		// the total number of axioms reduces
		assertEquals(getAxioms(root).size(), 5);

		// reasoner queries -- first subsumption is gone
		assertFalse(reasoner.getSuperClasses(mainX, true).containsEntity(
				mainY));
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				extA));
		assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity(
				extB));
		assertTrue(reasoner.getSuperClasses(extA, true)
				.containsEntity(extB));
		assertTrue(reasoner.getSuperClasses(extB, true)
				.containsEntity(extC));

	} finally {
		reasoner.dispose();
	}

}
 
Example 19
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 * 
 */
@Test
public void testNoChanges() throws Exception {

	OWLOntologyManager man = TestOWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// set up resolution of prefixes
	PrefixManager pm = new DefaultPrefixManager();
	pm.setDefaultPrefix("http://www.example.com/main#");
	pm.setPrefix("A:", "http://www.example.com/A#");
	pm.setPrefix("B:", "http://www.example.com/B#");

	// define query classes
	OWLClass mainX = dataFactory.getOWLClass(":X", pm);
	OWLClass mainY = dataFactory.getOWLClass(":Y", pm);
	OWLClass extA = dataFactory.getOWLClass("A:A", pm);
	OWLClass extB = dataFactory.getOWLClass("B:B", pm);
	OWLClass extC = dataFactory.getOWLClass("B:C", pm);

	// 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(), 3);
		// all three ontologies should be in the closure
		assertEquals(root.getImportsClosure().size(), 3);
		// all axioms from three ontologies should be in the closure
		assertEquals(getAxioms(root).size(), 6);

		// reasoner queries -- all subclasses are there
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				mainY));
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				extA));
		assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity(
				extB));
		assertTrue(reasoner.getSuperClasses(extA, true)
				.containsEntity(extB));
		assertTrue(reasoner.getSuperClasses(extB, true)
				.containsEntity(extC));
		
	} finally {
		reasoner.dispose();
	}

}
 
Example 20
Source File: QueryingUnnamedClassExpressions.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// Load your ontology.
	OWLOntology ont = man.loadOntologyFromOntologyDocument(new File(
			"c:/ontologies/ontology.owl"));

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

	// Create your desired query class expression. In this example we
	// will query ObjectIntersectionOf(A ObjectSomeValuesFrom(R B)).
	PrefixManager pm = new DefaultPrefixManager("http://example.org/");
	OWLClass A = dataFactory.getOWLClass(":A", pm);
	OWLObjectProperty R = dataFactory.getOWLObjectProperty(":R", pm);
	OWLClass B = dataFactory.getOWLClass(":B", pm);
	OWLClassExpression query = dataFactory.getOWLObjectIntersectionOf(A,
			dataFactory.getOWLObjectSomeValuesFrom(R, B));

	// Create a fresh name for the query.
	OWLClass newName = dataFactory.getOWLClass(IRI.create("temp001"));
	// Make the query equivalent to the fresh class
	OWLAxiom definition = dataFactory.getOWLEquivalentClassesAxiom(newName,
			query);
	man.addAxiom(ont, definition);

	// Remember to either flush the reasoner after the ontology change
	// or create the reasoner in non-buffering mode. Note that querying
	// a reasoner after an ontology change triggers re-classification of
	// the whole ontology which might be costly. Therefore, if you plan
	// to query for multiple complex class expressions, it will be more
	// efficient to add the corresponding definitions to the ontology at
	// once before asking any queries to the reasoner.
	reasoner.flush();

	// You can now retrieve subclasses, superclasses, and instances of
	// the query class by using its new name instead.
	reasoner.getSubClasses(newName, true);
	reasoner.getSuperClasses(newName, true);
	reasoner.getInstances(newName, false);

	// After you are done with the query, you should remove the definition
	man.removeAxiom(ont, definition);

	// You can now add new definitions for new queries in the same way

	// After you are done with all queries, do not forget to free the
	// resources occupied by the reasoner
	reasoner.dispose();
}