Java Code Examples for org.semanticweb.owlapi.model.OWLDataFactory#getOWLSubClassOfAxiom()

The following examples show how to use org.semanticweb.owlapi.model.OWLDataFactory#getOWLSubClassOfAxiom() . 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: RoleAxiomOWLAPILowLevelIncrementalClassTest.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
@Override
void prepare() {
	final OWLDataFactory factory = new OWLDataFactoryImpl();
	A = factory.getOWLClass(IRI.create("A"));
	B = factory.getOWLClass(IRI.create("B"));
	OWLObjectProperty R = factory.getOWLObjectProperty(IRI.create("R"));
	OWLObjectProperty S = factory.getOWLObjectProperty(IRI.create("S"));
	OWLAxiom axExSsomeAsubB = factory.getOWLSubClassOfAxiom(
			factory.getOWLObjectSomeValuesFrom(S, A), B);
	OWLAxiom axReflR = factory.getOWLReflexiveObjectPropertyAxiom(R);
	axRsubS = factory.getOWLSubObjectPropertyOfAxiom(R, S);
	// from these three axioms it should follow A subclass B
	ontologyManager = TestOWLManager.createOWLOntologyManager();
	try {
		ont = ontologyManager.createOntology(new HashSet<OWLAxiom>(Arrays
				.asList(axExSsomeAsubB, axReflR, axRsubS)));
	} catch (OWLOntologyCreationException e) {
		fail(e.toString());
	}
}
 
Example 2
Source File: RetrievingProofsForEntailment.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
private static OWLAxiom getEntailment() {
	// Let's pick some class subsumption we want to explain
	OWLDataFactory factory = OWLManager.getOWLDataFactory();
	
	OWLClass subsumee = factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#LiquidFood"));
	OWLClass subsumer = factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#Food"));
	
	return factory.getOWLSubClassOfAxiom(subsumee, subsumer);
}
 
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: ConceptAxiomOWLAPILowLevelIncrementalClassTest.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
@Override
void prepare() {
	final OWLDataFactory factory = new OWLDataFactoryImpl();
	A = factory.getOWLClass(IRI.create("A"));
	B = factory.getOWLClass(IRI.create("B"));
	axAsubB = factory.getOWLSubClassOfAxiom(A, B);
	axBsubA = factory.getOWLSubClassOfAxiom(B, A);
	ontologyManager = TestOWLManager.createOWLOntologyManager();
	try {
		ont = ontologyManager.createOntology(new HashSet<OWLAxiom>(Arrays
				.asList(axAsubB, axBsubA)));
	} catch (OWLOntologyCreationException e) {
		fail(e.toString());
	}
}
 
Example 5
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 6
Source File: OWLGraphWrapperEdgeTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEdgeCache() throws Exception {
	OWLGraphWrapper g = getGraph("graph/cache-test.obo");
	
	OWLOntology o = g.getSourceOntology();
	OWLOntologyManager m = o.getOWLOntologyManager();
	OWLDataFactory f = m.getOWLDataFactory();
	
	OWLClass orphan = g.getOWLClassByIdentifier("FOO:0004");
	OWLClass root = g.getOWLClassByIdentifier("FOO:0001");
	
	g.getEdgesBetween(orphan, root); //just to trigger the cache
	
	OWLSubClassOfAxiom ax = f.getOWLSubClassOfAxiom(orphan, root);
	AddAxiom addAx = new AddAxiom(o, ax);
	m.applyChange(addAx);
	
	Set<OWLGraphEdge> edges = g.getEdgesBetween(orphan, root);
	assertNotNull(edges);
	
	assertEquals(0, edges.size());
	
	g.clearCachedEdges(); // test clear cache method
	
	edges = g.getEdgesBetween(orphan, root);
	assertNotNull(edges);
	
	assertEquals(1, edges.size());
}
 
Example 7
Source File: OWLConverter.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convenience method for asserting a subClass relation between
 * a parent and child class in an ontology.
 *
 * @param ontology the current ontology
 * @param child the child class
 * @param parent the parent class
 * @return the axiom
 */
protected static OWLSubClassOfAxiom assertSubClass(
		OWLOntology ontology, OWLClass child, OWLClass parent) {
	OWLOntologyManager manager = ontology.getOWLOntologyManager();
	OWLDataFactory dataFactory = manager.getOWLDataFactory();
	OWLSubClassOfAxiom axiom = 
		dataFactory.getOWLSubClassOfAxiom(child, parent);
	ontology.getOWLOntologyManager().addAxiom(ontology, axiom);
	return axiom;
}
 
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: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * given an ontology *ont* and a set of object properties *filterProps*, remove all axioms from ontology that
 * have an object property P in their signature where P is not in *filterProps*
 * 
 * use basic reasoning to map up more specific relationships
 * 
 * @param ont
 * @param filterProps
 * @param reasoner
 */
public static void retainAxiomsInPropertySubset(OWLOntology ont, Set<OWLObjectProperty> filterProps, OWLReasoner reasoner) {
	LOG.info("Removing axioms that use properties not in set: "+filterProps);
	Set<OWLAxiom> rmAxioms = new HashSet<OWLAxiom>();
	Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
	OWLDataFactory df = ont.getOWLOntologyManager().getOWLDataFactory();
	for (OWLAxiom ax : ont.getAxioms()) {
		Set<OWLObjectProperty> ps = ax.getObjectPropertiesInSignature();
		ps.removeAll(filterProps);
		if (ps.size() > 0) {
			rmAxioms.add(ax);

			// if p not-in SubSet, and
			//  A = X SubClassOf p Some Y,
			// then rewrite as
			//  A = X SubClassOf p' some Y, where p' SubPropertyOf p
			// rewrite as weaker axioms.
			// TOOD - Elk does not support superobjectprops - do in wrapper for now?
			if (ax instanceof OWLSubClassOfAxiom) {
			    OWLSubClassOfAxiom sca = (OWLSubClassOfAxiom)ax;
			    if (!sca.getSubClass().isAnonymous()) {
			        if (sca.getSuperClass() instanceof OWLObjectSomeValuesFrom) {
			            OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom) sca.getSuperClass();
			            OWLObjectPropertyExpression p = svf.getProperty();
			            Set<OWLObjectPropertyExpression> sps = 
			                    getSuperObjectProperties(p, reasoner, ont);
			            sps.retainAll(filterProps);
			            for (OWLObjectPropertyExpression sp : sps) {
			                OWLObjectSomeValuesFrom newSvf = df.getOWLObjectSomeValuesFrom(sp, svf.getFiller());
			                OWLSubClassOfAxiom newSca = df.getOWLSubClassOfAxiom(sca.getSubClass(), newSvf);
			                LOG.info("REWRITE: "+sca+" --> "+newSca);
			                newAxioms.add(newSca);
			            }
			        }
			    }
			}
			//else if (ax instanceof OWLEquivalentClassesAxiom) {
			//	((OWLEquivalentClassesAxiom)ax).getClassExpressions();
			//}
			
		}
	}
	LOG.info("Removing "+rmAxioms.size()+" axioms");
	ont.getOWLOntologyManager().removeAxioms(ont, rmAxioms);
	LOG.info("Adding "+newAxioms.size()+" axioms");
	ont.getOWLOntologyManager().addAxioms(ont, newAxioms);
}
 
Example 13
Source File: OWLGraphManipulator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Relaxes all {@code OWLObjectIntersectionOf}s. This method will relax 
 * {@code OWLSubClassOfAxiom}s, whose superclass is an {@code OWLObjectIntersectionOf}, 
 * into multiple {@code OWLSubClassOfAxiom}s, using a <a 
 * href='http://owlapi.sourceforge.net/javadoc/org/semanticweb/owlapi/SplitSubClassAxioms.html'>
 * SplitSubClassAxioms</a>. It will also relax {@code OWLSubClassOfAxiom}s, whose 
 * superclass is an {@code OWLObjectSomeValuesFrom} with a filler being an 
 * {@code OWLObjectIntersectionOf}, into multiple {@code OWLSubClassOfAxiom}s with 
 * an {@code OWLObjectSomeValuesFrom} as superclass, with the same 
 * {@code OWLPropertyExpression}, and individual operands as filler. 
 * <p>
 * Note that it is likely that the {@code OWLObjectIntersectionOf}s where used in
 * {@code OWLEquivalentClassesAxiom}s, rather than in {@code OWLSubClassOfAxiom}s. 
 * But the method {@link #convertEquivalentClassesToSuperClasses()} would have transformed 
 * them into {@code OWLSubClassOfAxiom}s. It must be called before this method.
 * 
 * @see #performDefaultModifications()
 * @see #convertEquivalentClassesToSuperClasses()
 */
private void splitSubClassAxioms() {
    log.info("Relaxing OWLSubClassOfAxioms whose superclass is an OWLObjectIntersectionOf");
    
    //first, split subClassOf axioms whose superclass is an OWLObjectIntersectionOf
    SplitSubClassAxioms split = new SplitSubClassAxioms(
            this.getOwlGraphWrapper().getAllOntologies(), 
            this.getOwlGraphWrapper().getDataFactory());
    this.getOwlGraphWrapper().getManager().applyChanges(split.getChanges());
    this.triggerWrapperUpdate();
    
    //some ontologies use an OWLObjectIntersectionOf as the filler of 
    //an OWLObjectSomeValuesFrom class expression. We go only one level down 
    //(i.e., we would not translate another OWLObjectSomeValuesFrom part of the 
    //OWLObjectIntersectionOf)
    OWLDataFactory dataFactory = this.getOwlGraphWrapper().getDataFactory();
    List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
    for (OWLOntology ont : this.getOwlGraphWrapper().getAllOntologies()) {
        for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
            OWLClassExpression superClsExpr = ax.getSuperClass();
            if (superClsExpr instanceof OWLObjectSomeValuesFrom) {
                OWLObjectSomeValuesFrom someValuesFrom = 
                        (OWLObjectSomeValuesFrom) superClsExpr;
                if (someValuesFrom.getFiller() instanceof OWLObjectIntersectionOf) {
                    //remove original axiom
                    changes.add(new RemoveAxiom(ont, ax));
                    
                    OWLObjectIntersectionOf filler = 
                            (OWLObjectIntersectionOf) someValuesFrom.getFiller();
                    for (OWLClassExpression op : filler.getOperands()) {
                        //we accept only OWLClasses, otherwise we would need to compose 
                        //OWLObjectPropertyExpressions
                        if (op instanceof OWLClass) {
                            OWLAxiom replAx = dataFactory.
                                    getOWLSubClassOfAxiom(ax.getSubClass(), 
                                    dataFactory.getOWLObjectSomeValuesFrom(
                                                someValuesFrom.getProperty(), op));
                            changes.add(new AddAxiom(ont, replAx));
                        }
                    }
                }
                
            }
        }
    }
    this.getOwlGraphWrapper().getManager().applyChanges(changes);
    this.triggerWrapperUpdate();
    
    log.info("OWLObjectIntersectionOf relaxation done.");
}
 
Example 14
Source File: OWLGraphManipulatorTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test the functionalities of 
 * {@link OWLGraphManipulator#removeClassAndPropagateEdges(String)}.
 */
@Test
public void shouldRemoveClassAndPropagateEdges()
{
	//remove FOO:0004, which has 2 outgoing edges: 
	//FOO:0004 part_of FOO:0002 and FOO:0004 overlaps FOO:0001
	//and two incoming edges: 
	//FOO:0015 is_a FOO:0004 and FOO:0003 in_deep_part_of FOO:0004
	//Of note, FOO:0003 has also a relation part_of to FOO:0001.
	int relsPropagated = this.graphManipulator.removeClassAndPropagateEdges("FOO:0004");
	//2 edges should have been propagated
	assertEquals("Incorrect number of edges propagated", 3, relsPropagated);
	//check that the class was indeed removed
	assertNull("Class FOO:0004 was not removed", 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0004"));
	
	//check the actual relations
	OWLOntology ont = this.graphManipulator.getOwlGraphWrapper().getSourceOntology();
	OWLDataFactory factory = this.graphManipulator.getOwlGraphWrapper().
			getManager().getOWLDataFactory();
	OWLObjectProperty partOf = this.graphManipulator.getOwlGraphWrapper().
			getOWLObjectPropertyByIdentifier("BFO:0000050");
	OWLObjectProperty overlaps = this.graphManipulator.getOwlGraphWrapper().
			getOWLObjectPropertyByIdentifier("RO:0002131");
	
	//all outgoing edges of FOO:0004 should have been propagated to FOO:0015 
	//(because FOO:0015 is_a FOO:0004), but a more precise relation 
	//already exists. The resulting new edge is: FOO:0015 part_of FOO:0002.
	//FOO:0015 overlaps FOO:0001 is also added, (it already exists a relation 
	//FOO:0015 part_of FOO:0001, but not with same GCI filler)
	OWLClass source = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0015");
	OWLClass target = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0002");
	
	OWLGraphEdge checkEdge = new OWLGraphEdge(source, target, partOf, 
			Quantifier.SOME, ont, null, this.graphManipulator.getOwlGraphWrapper().
			getOWLClassByIdentifier("NCBITaxon:9606"), partOf);
	OWLAxiom axiom = factory.getOWLSubClassOfAxiom(
	        (OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
               edgeToSourceExpression(checkEdge), 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge));
	assertTrue("Relation FOO:0015 part_of FOO:0002 was not correctly created", 
			ont.containsAxiom(axiom));
	
	//check that the relation overlaps FOO:0001 was correctly created
	target = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0001");
	
	checkEdge = new OWLGraphEdge(source, target, overlaps, 
			Quantifier.SOME, ont, null, this.graphManipulator.getOwlGraphWrapper().
               getOWLClassByIdentifier("NCBITaxon:9606"), partOf);
	axiom = factory.getOWLSubClassOfAxiom(
	        (OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
               edgeToSourceExpression(checkEdge), 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge));
	assertTrue("Relation FOO:0015 overlaps FOO:0001 was incorrectly created", 
			ont.containsAxiom(axiom));
	
	//Propagation from FOO:0003: FOO:0003 in_deep_part_of FOO:0004
	//only one resulting new edge, FOO:0003 part_of FOO:0002 (as FOO:0004 part_of FOO:0002).
	//the other outgoing edge (FOO:0004 overlaps FOO:0001) cannot be combined 
	//because overlaps is not transitive
	source = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0003");
	target = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0002");
	
	checkEdge = new OWLGraphEdge(source, target, partOf, 
			Quantifier.SOME, ont, null, this.graphManipulator.getOwlGraphWrapper().
               getOWLClassByIdentifier("NCBITaxon:9606"), partOf);
	axiom = factory.getOWLSubClassOfAxiom(
	        (OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
               edgeToSourceExpression(checkEdge), 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge));
	assertTrue("Relation FOO:0003 part_of FOO:0002 was not correctly created", 
			ont.containsAxiom(axiom));
	
	//check that FOO:0003 overlaps FOO:0001 was not incorrectly added
	source = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0003");
	target = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0001");
	
	checkEdge = new OWLGraphEdge(source, target, overlaps, 
			Quantifier.SOME, ont, null, this.graphManipulator.getOwlGraphWrapper().
               getOWLClassByIdentifier("NCBITaxon:9606"), partOf);
	axiom = factory.getOWLSubClassOfAxiom(
	        (OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
               edgeToSourceExpression(checkEdge), 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge));
	assertFalse("Relation FOO:0003 overlaps FOO:0001 was incorrectly added", 
			ont.containsAxiom(axiom));
}
 
Example 15
Source File: OWLGraphManipulatorTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test that two {@code OWLClass}es that are equal have a same hashcode, 
 * because the OWLGraphEdge bug get me paranoid. 
 */
@Test
public void testOWLOntologyChangeHashCode()
{
   	
	OWLOntology ont = this.graphManipulator.getOwlGraphWrapper().getSourceOntology();
	OWLDataFactory factory = this.graphManipulator.getOwlGraphWrapper().
			getManager().getOWLDataFactory();
	OWLClass source = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0005");
	OWLClass target = 
			this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0001");
	
	OWLGraphEdge checkEdge = new OWLGraphEdge(source, target, ont);
	OWLAxiom axiom = factory.getOWLSubClassOfAxiom(source, 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge));
	OWLAxiomChange rm1 = new RemoveAxiom(ont, axiom);
	
	OWLGraphEdge checkEdge2 = new OWLGraphEdge(source, target, ont);
	OWLAxiom axiom2 = factory.getOWLSubClassOfAxiom(source, 
			(OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			edgeToTargetExpression(checkEdge2));
	OWLAxiomChange rm2 = new RemoveAxiom(ont, axiom2);
	
	assertTrue("The two OWLAxiomChange objects are equal", 
			 rm1.equals(rm2));
	 //then of course the hashcodes will be the same...
	 assertTrue("Two OWLAxiomChange are equal but have different hashcode", 
			 rm1.equals(rm2) && rm1.hashCode() == rm2.hashCode());
	 
	 
	 
	 source = 
			 this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0014");
	 target = 
			 this.graphManipulator.getOwlGraphWrapper().getOWLClassByIdentifier("FOO:0001");

	 checkEdge = new OWLGraphEdge(source, target, ont);
	 axiom = factory.getOWLSubClassOfAxiom(source, 
			 (OWLClassExpression) this.graphManipulator.getOwlGraphWrapper().
			 edgeToTargetExpression(checkEdge));
	 OWLAxiomChange rm3 = new RemoveAxiom(ont, axiom);

	 assertFalse("Different OWLAxiomChange objects are equal", 
			 rm1.equals(rm3));
	 //then of course the hashcodes will be the same...
	 assertFalse("Different OWLAxiomChanges have same hashcode", 
			 rm1.hashCode() == rm3.hashCode());
}