Java Code Examples for org.semanticweb.owlapi.model.OWLOntologyManager#removeAxiom()

The following examples show how to use org.semanticweb.owlapi.model.OWLOntologyManager#removeAxiom() . 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: OwlApiIncrementalReasoningTestDelegate.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
@Override
public void applyChanges(final Iterable<OWLAxiom> changes,
		final IncrementalChangeType type) {
	// the changes are applied indirectly by modifying the ontology
	final OWLOntologyManager manager = testOntology_
			.getOWLOntologyManager();

	for (OWLAxiom axiom : changes) {
		switch (type) {
		case ADD:
			manager.addAxiom(testOntology_, axiom);
			break;
		case DELETE:
			manager.removeAxiom(testOntology_, axiom);
			break;
		}
	}

	standardReasoner_.flush();
	incrementalReasoner_.flush();
}
 
Example 2
Source File: FilterOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Remove axioms from the input ontology. This version expects a set of OWLObjectProperties.
 *
 * @param ontology the ontology to filter
 * @param properties a set of OWLObjectProperties to retain
 */
public static void filter(OWLOntology ontology, Set<OWLObjectProperty> properties) {
  logger.debug("Filtering ontology for axioms with ObjectProperties " + properties);

  OWLOntologyManager manager = ontology.getOWLOntologyManager();
  Set<OWLAxiom> axioms = ontology.getAxioms();
  logger.debug("Ontology has {} axioms before filtering", axioms.size());

  // For each axiom, get all its object properties,
  // then remove the properties that we're looking for.
  // If there are no object properties left, then we keep this axiom.
  // All annotation axioms, declarations, and subClass relations remains.
  for (OWLAxiom axiom : axioms) {
    Set<OWLObjectProperty> ps = axiom.getObjectPropertiesInSignature();
    ps.removeAll(properties);
    if (ps.size() > 0) {
      manager.removeAxiom(ontology, axiom);
    }
  }

  logger.debug("Ontology has {} axioms after filtering", ontology.getAxioms().size());
}
 
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: IgnoreChangesInNonImportedOntologiesTest.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 */
@Test
public void ignoreChangesInNonImportedOntologies() throws Exception {

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

	// set up resolution of prefixes
	DefaultPrefixManager 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#");

	OWLClass extA = dataFactory.getOWLClass("A:A", pm);
	OWLClass extB = dataFactory.getOWLClass("B:B", pm);

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

	// Create an ELK reasoner.
	ElkReasoner reasoner = (ElkReasoner) new ElkReasonerFactory()
			.createReasoner(root);
	// make sure the reasoner loads the ontology
	reasoner.flush();
	reasoner.isConsistent();

	try {
		OWLOntology nonImported = loadOntology(man, "nonImported.owl");

		OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB);
		man.removeAxiom(nonImported, axiom);
		reasoner.flush();

		AbstractReasonerState state = reasoner.getInternalReasoner();

		assertTrue(state.stageManager.inputLoadingStage.isCompleted());
	} finally {
		reasoner.dispose();
	}
}
 
Example 5
Source File: RedundantAxiomTagger.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void tagRedundantAxioms(OWLReasoner reasoner) {
    OWLOntology ont = reasoner.getRootOntology();
    OWLOntologyManager mgr = ont.getOWLOntologyManager();
    OWLDataFactory df = mgr.getOWLDataFactory();
    OWLAnnotationProperty anProp = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#source"));
    for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
        if (!ax.getSuperClass().isAnonymous()) {
            OWLClass supc = (OWLClass) ax.getSuperClass();
            
            mgr.removeAxiom(ont, ax);
            reasoner.flush();
            NodeSet<OWLClass> ancs = reasoner.getSuperClasses(ax.getSubClass(), false);
            //LOG.info(ax + " ANCS="+ancs);
            if (ancs.containsEntity( supc)) {
                String direct = "indirect";
                if (reasoner.getSuperClasses(ax.getSubClass(), true).containsEntity( supc)) {
                    direct = "direct";
                }
                LOG.info("SCA = "+ax+" D="+direct);
                OWLAnnotation ann = df.getOWLAnnotation(anProp, df.getOWLLiteral(direct));
                OWLAxiom newAxiom = changeAxiomAnnotations(ax, Collections.singleton(ann), df);
                mgr.addAxiom(ont, newAxiom);
            }
            else {
                // put it back
                mgr.addAxiom(ont, ax);
            }
        }
    }
   
}
 
Example 6
Source File: AxiomAnnotationTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static OWLAxiom modifyInferredAxiom(OWLAxiom axiom, OWLOntology ontology, boolean add) {
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();
	final OWLDataFactory factory = manager.getOWLDataFactory();
	final OWLAxiom newAxiom = updateInferredAxiom(axiom, factory, add);
	
	// update ontology
	manager.removeAxiom(ontology, axiom);
	manager.addAxiom(ontology, newAxiom);
	return newAxiom;
}
 
Example 7
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();
}
 
Example 8
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 9
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>
 * trying to remove "A:A is-a B:B"
 * <p>
 * Because the removed axiom belongs to the imported ontology and
 * not main ontology, the remove does not make any effect. So, we
 * should end up with the ontology we have started with.
 * <p>
 * This test is ignored, because as of OWL API 4.1.3 the removal
 * of the axiom is broadcasted even though the axiom is not removed.
 */
@Test
public void testRemovingAB() 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 {

		// ************************************
		// ** trying to remove "A:A is-a B:B"
		// ************************************
		OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB);
		man.removeAxiom(root, axiom);
		reasoner.flush();

		// Because the removed axiom belongs to the imported ontology and
		// not main ontology, the remove does not make any effect. So, we
		// should end up with the ontology we have started with

		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 subsumptions 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 10
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>,
 * adding the import declaration for </impB> and removing
 * ":Y is-a B:B"
 */
@Test
public void testRemovingImpAAddingImpBRemovingYB() 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 {

		// ************************************
		// ** adding the import declaration for </impB> and removing
		// ":Y is-a B:B"
		// ************************************

		OWLImportsDeclaration importA = new OWLImportsDeclarationImpl(
				IRI.create("http://www.example.com#impA"));
		OWLOntologyChange change = new RemoveImport(root, importA);
		man.applyChange(change);
		OWLImportsDeclaration importB = new OWLImportsDeclarationImpl(
				IRI.create("http://www.example.com#impB"));
		change = new AddImport(root, importB);
		man.applyChange(change);
		OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainY, extB);
		man.removeAxiom(root, axiom);
		reasoner.flush();

		// Now ontology should import only ontology </impB>
		assertEquals(root.getAxiomCount(), 2);
		assertEquals(root.getImportsClosure().size(), 2);
		assertEquals(getAxioms(root).size(), 4);

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

	} finally {
		reasoner.dispose();
	}

}
 
Example 11
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 12
Source File: AxiomAnnotationTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Update the given axiom to a new set of axiom annotation.<br>
 * <b>Side effect</b>: This removes the old axiom and adds the new axiom to
 * the given ontology. The method also returns the new axiom to enable
 * chaining.
 * 
 * @param axiom
 * @param annotations
 * @param ontology
 * @return newAxiom
 */
public static OWLAxiom changeAxiomAnnotations(OWLAxiom axiom, Set<OWLAnnotation> annotations, OWLOntology ontology) {
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();
	final OWLDataFactory factory = manager.getOWLDataFactory();
	final OWLAxiom newAxiom = changeAxiomAnnotations(axiom, annotations, factory);
	manager.removeAxiom(ontology, axiom);
	manager.addAxiom(ontology, newAxiom);
	return newAxiom;
}