org.semanticweb.owlapi.model.OWLImportsDeclaration Java Examples

The following examples show how to use org.semanticweb.owlapi.model.OWLImportsDeclaration. 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: OntologyMetadata.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public OntologyMetadata(OWLOntology ont) {
	super();
	OWLOntologyID id = ont.getOntologyID();
	if (id.getOntologyIRI().isPresent())
		ontologyIRI = id.getOntologyIRI().get().toString();
	if (id.getVersionIRI().isPresent())
		versionIRI = id.getVersionIRI().get().toString();
	importDirectives = new HashSet<String>();
	for (OWLImportsDeclaration oid : ont.getImportsDeclarations()) {
		importDirectives.add(oid.getIRI().toString());
	}
	classCount = ont.getClassesInSignature().size();
	namedIndividualCount = ont.getIndividualsInSignature().size();
	axiomCount = ont.getAxiomCount();
	annotations = new HashSet<OntologyAnnotation>();
	for (OWLAnnotation ann : ont.getAnnotations()) {
		annotations.add(new OntologyAnnotation(ann));
	}
}
 
Example #2
Source File: OWLGraphWrapperBasic.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void addImportsFromSupportOntologies() {
	OWLOntology sourceOntology = getSourceOntology();
	OWLDataFactory factory = getDataFactory();
	for (OWLOntology  o : getSupportOntologySet()) {
		Optional<IRI> ontologyIRI = o.getOntologyID().getOntologyIRI();
		if (ontologyIRI.isPresent()) {
			OWLImportsDeclaration importsDeclaration = factory.getOWLImportsDeclaration(ontologyIRI.get());
			AddImport ai = new AddImport(sourceOntology, importsDeclaration);
			LOG.info("Applying: "+ai);
			getManager().applyChange(ai);
		}
		else {
			String msg = "Could not add import due to missing ontology id: "+o;
			LOG.error(msg);
			throw new RuntimeException(msg);
		}
	}
	this.setSupportOntologySet(new HashSet<OWLOntology>());
}
 
Example #3
Source File: OWLGraphWrapperBasic.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void mergeImportClosure(boolean isRemovedImportsDeclarations) throws OWLOntologyCreationException {
	OWLOntologyManager manager = getManager();
	Set<OWLOntology> imports = sourceOntology.getImportsClosure();
	for (OWLOntology o : imports) {
		if (o.equals(sourceOntology))
			continue;

		String comment = "Includes "+summarizeOntology(o);
		LOG.info(comment);
		addCommentToOntology(sourceOntology, comment);

		manager.addAxioms(sourceOntology, o.getAxioms());
	}
	Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations();
	for (OWLImportsDeclaration oid : oids) {
		RemoveImport ri = new RemoveImport(sourceOntology, oid);
		getManager().applyChange(ri);
	}
}
 
Example #4
Source File: OWLGraphWrapperBasic.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Merge a specific ontology from the import closure into the main ontology.
 * Removes the import statement.
 * 
 * @param ontologyIRI id of the ontology to merge
 * @throws OWLOntologyCreationException
 */
public void mergeSpecificImport(IRI ontologyIRI) throws OWLOntologyCreationException {
	OWLOntologyManager manager = getManager();
	Set<OWLOntology> imports = sourceOntology.getImportsClosure();
	for (OWLOntology o : imports) {
		if (o.equals(sourceOntology))
			continue;
		Optional<IRI> currentIRI = o.getOntologyID().getOntologyIRI();
		if (currentIRI.isPresent() && currentIRI.get().equals(ontologyIRI)) {
			String comment = "Includes "+summarizeOntology(o);
			LOG.info(comment);
			addCommentToOntology(sourceOntology, comment);
			manager.addAxioms(sourceOntology, o.getAxioms());	
		}
	}
	Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations();
	for (OWLImportsDeclaration oid : oids) {
		if (ontologyIRI.equals(oid.getIRI())) {
			RemoveImport ri = new RemoveImport(sourceOntology, oid);
			getManager().applyChange(ri);
		}
	}
}
 
Example #5
Source File: AssertInferenceTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static List<OWLOntologyChange> handleSupportOntologies(OWLGraphWrapper graph)
{
	OWLOntology ontology = graph.getSourceOntology();
	OWLOntologyManager manager = ontology.getOWLOntologyManager();
	OWLDataFactory factory = manager.getOWLDataFactory();
	
	List<OWLOntologyChange> removeImportChanges = new ArrayList<OWLOntologyChange>();
	Set<OWLOntology> supportOntologySet = graph.getSupportOntologySet();
	for (OWLOntology support : supportOntologySet) {
		Optional<IRI> supportIRI = support.getOntologyID().getOntologyIRI();
		if(supportIRI.isPresent()) {
			IRI ontologyIRI = supportIRI.get();
			OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(ontologyIRI);
			ChangeApplied status = manager.applyChange(new AddImport(ontology, importDeclaration));
			if (ChangeApplied.SUCCESSFULLY == status) {
				// the change was successful, create remove import for later
				removeImportChanges.add(new RemoveImport(ontology, importDeclaration));
			}
		}
	}
	return removeImportChanges;
}
 
Example #6
Source File: OwlOntologyProducer.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
void addOntologyStructure(OWLOntologyManager manager, OWLOntology ontology) {
  long parent = graph.createNode(OwlApiUtils.getIri(ontology));
  graph.addLabel(parent, OwlLabels.OWL_ONTOLOGY);
  for (OWLImportsDeclaration importDeclaration : ontology.getImportsDeclarations()) {
    OWLOntology childOnt = manager.getImportedOntology(importDeclaration);
    if (null == childOnt) {
      // TODO: Why is childOnt sometimes null (when importing rdf)?
      continue;
    }
    long child = graph.createNode(OwlApiUtils.getIri(childOnt));
    graph.addLabel(parent, OwlLabels.OWL_ONTOLOGY);
    if (graph.getRelationship(child, parent, OwlRelationships.RDFS_IS_DEFINED_BY).isPresent()) {
      continue;
    }
    graph.createRelationship(child, parent, OwlRelationships.RDFS_IS_DEFINED_BY);
    addOntologyStructure(manager, childOnt);
  }
}
 
Example #7
Source File: SimpleOntology.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<IRI> getImportedOntologyIRIs() {
    return owlOntology.importsDeclarations()
            .map(OWLImportsDeclaration::getIRI)
            .map(SimpleOntologyValues::mobiIRI)
            .collect(Collectors.toSet());
}
 
Example #8
Source File: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Deprecated
public void addImport(String importedIRIString) {
	OWLImportsDeclaration iax = dataFactory.getOWLImportsDeclaration(IRI.create(importedIRIString));
	//AddImport addAx = new AddImport(ontology, iax);
	AddImport addAx = new AddImport(getOntology(), iax);
	manager.applyChange(addAx);
}
 
Example #9
Source File: OWLGraphWrapperBasic.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void mergeOntology(OWLOntology extOnt, LabelPolicy labelPolicy) throws OWLOntologyCreationException {
	OWLOntologyManager manager = getManager();
	LOG.info("Merging "+extOnt+" policy: "+labelPolicy);
	for (OWLAxiom axiom : extOnt.getAxioms()) {
		if (labelPolicy != LabelPolicy.ALLOW_DUPLICATES) {
			if (axiom instanceof OWLAnnotationAssertionAxiom) {
				OWLAnnotationAssertionAxiom aa = (OWLAnnotationAssertionAxiom)axiom;
				if (aa.getProperty().isLabel()) {
					OWLAnnotationSubject subj = aa.getSubject();
					if (subj instanceof IRI) {
						Optional<OWLLiteral> label = null;
						for (OWLAnnotationAssertionAxiom a1 : sourceOntology.getAnnotationAssertionAxioms(subj)) {
							if (a1.getProperty().isLabel()) {
								label = a1.getValue().asLiteral();
							}
						}
						if (label != null && label.isPresent()) {
							if (labelPolicy == LabelPolicy.PRESERVE_SOURCE) {
								LOG.info("Preserving existing label:" +subj+" "+label+" // ditching: "+axiom);
								continue;
							}
							if (labelPolicy == LabelPolicy.PRESERVE_EXT) {
								LOG.info("Replacing:" +subj+" "+label+" with: "+axiom);
								LOG.error("NOT IMPLEMENTED");
							}
						}
					}
				}
			}
		}
		manager.applyChange(new AddAxiom(sourceOntology, axiom));
	}
	for (OWLImportsDeclaration oid: extOnt.getImportsDeclarations()) {
		manager.applyChange(new AddImport(sourceOntology, oid));
	}
	addCommentToOntology(sourceOntology, "Includes "+summarizeOntology(extOnt));
}
 
Example #10
Source File: OWLGsonRenderer.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Object[] convertSet(Set objs) {
	Object[] arr = new Object[objs.size()];
	int i=0;
	for (Object obj : objs) {
		if (obj instanceof OWLAxiom)
			arr[i] = convert((OWLAxiom) obj);
		else if (obj instanceof OWLImportsDeclaration)
			arr[i] = convert((OWLImportsDeclaration) obj);
		else 
			arr[i] = convert((OWLObject) obj);
		i++;
	}
	return arr;
}
 
Example #11
Source File: ImportedXrefTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test() throws Exception {
	// load the base ontology
	ParserWrapper pw = new ParserWrapper();
	OWLOntology direct = pw.parseOBO(getResourceIRIString("graph/xref_test.obo"));
	
	OWLGraphWrapper directGraph = new OWLGraphWrapper(direct);
	
	// check that the test class has the expected number of xrefs
	OWLClass c = directGraph.getOWLClassByIdentifier("FOO:0001");
	
	List<String> directDefXrefs = directGraph.getDefXref(c);
	assertEquals(2, directDefXrefs.size());

	List<String> directXrefs = directGraph.getXref(c);
	assertEquals(2, directXrefs.size());
	
	// create an ontology using an import
	OWLOntologyManager manager = pw.getManager();
	OWLDataFactory factory = manager.getOWLDataFactory();
	OWLOntology importer = manager.createOntology();
	OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(direct.getOntologyID().getOntologyIRI().orNull());
	manager.applyChange(new AddImport(importer, importDeclaration));
	
	OWLGraphWrapper importerGraph = new OWLGraphWrapper(importer);
	
	// check that the wrapper uses also imports for lookups of xrefs
	List<String> importedDefXrefs = importerGraph.getDefXref(c);
	assertEquals(2, importedDefXrefs.size());

	List<String> importedXrefs = importerGraph.getXref(c);
	assertEquals(2, importedXrefs.size());
}
 
Example #12
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 #13
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 #14
Source File: OWLGraphManipulator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Merges {@code OWLAxiom}s from the import ontology closure, with the source ontology.
     * This method is similar to {@link OWLGraphWrapperBasic#mergeImportClosure(boolean)}, 
     * except that: i) an {@code OWLAxiom} will be added to the source ontology only if 
     * it is not already present in the source (without taking annotations into account); 
     * ii) a check is performed to ensure that there will not be more than 
     * one annotation axiom for a given subject, when the annotation property 
     * corresponds to a tag with max cardinality one in OBO format 
     * (see {@code org.obolibrary.oboformat.model.Frame#check()}). As for 
     * {@link OWLGraphWrapperBasic#mergeImportClosure(boolean)}, annotations on the ontology 
     * itself are not imported, and the import declarations are removed after execution. 
     */
    private void mergeImportClosure() {
        log.info("Merging axioms from import closure with source ontology...");
        OWLOntology sourceOnt = this.getOwlGraphWrapper().getSourceOntology();
        
        //the method OWLOntology.containsAxiomIgnoreAnnotations is really 
        //not well optimized, so we get all OWLAxioms without annotations 
        //from the source ontology. 
        log.debug("Retrieving OWLAxioms without annotations from source ontology...");
        Set<OWLAxiom> sourceAxiomsNoAnnots = new HashSet<OWLAxiom>();
        for (OWLAxiom ax: sourceOnt.getAxioms()) {
            sourceAxiomsNoAnnots.add(ax.getAxiomWithoutAnnotations());
        }
        if (log.isDebugEnabled()) {
            log.debug(sourceAxiomsNoAnnots.size() + " axioms without annotations retrieved " +
            		"over " + sourceOnt.getAxiomCount() + " axioms.");
        }
        
        for (OWLOntology importedOnt: sourceOnt.getImportsClosure()) {
            if (importedOnt.equals(sourceOnt)) {
                continue;
            }
            log.info("Merging " + importedOnt);
            
//            OWLDataFactory df = sourceOnt.getOWLOntologyManager().getOWLDataFactory();
//            OWLAnnotationProperty p = 
//                    df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_COMMENT.getIRI());
//            OWLLiteral v = df.getOWLLiteral("Imported " + importedOnt);
//            OWLAnnotation ann = df.getOWLAnnotation(p, v);
//            AddOntologyAnnotation addAnn = 
//                    new AddOntologyAnnotation(sourceOnt, ann);
//            sourceOnt.getOWLOntologyManager().applyChange(addAnn);
            
            //filter the axioms imported to avoid redundancy (too bad there is not 
            //a method OWLOntology.getAxiomsIgnoreAnnotations())
            int importedAxiomCount = 0;
            importAxioms: for (OWLAxiom importedAx: importedOnt.getAxioms()) {
                if (sourceAxiomsNoAnnots.contains(importedAx.getAxiomWithoutAnnotations())) {
                    continue importAxioms;
                }
                    
                //if it is an annotation axion, we need to ensure that 
                //there will not be more than one axiom for a given subject, 
                //when the annotation corresponds to a tag with max cardinality one 
                //in OBO format (see org.obolibrary.oboformat.model.Frame#check()).
                if (importedAx instanceof OWLAnnotationAssertionAxiom) {
                    OWLAnnotationAssertionAxiom castAx = 
                            (OWLAnnotationAssertionAxiom) importedAx;

                    if (maxOneCardinalityAnnots.contains(
                            castAx.getProperty().getIRI().toString()) || 
                            maxOneCardinalityAnnots.contains(
                                    this.getOwlGraphWrapper().getIdentifier(
                                            castAx.getProperty()))) {

                        //check whether we have an annotation for the same subject 
                        //in the source ontology.
                        for (OWLAnnotationAssertionAxiom sourceAnnotAx: 
                            sourceOnt.getAnnotationAssertionAxioms(
                                    castAx.getSubject())) {
                            if (sourceAnnotAx.getProperty().equals(
                                    castAx.getProperty())) {
                                //discard the axiom from import ontology, there is already 
                                //an annotation with same property on same subject
                                log.trace("Discarding axiom: " + castAx);
                                continue importAxioms;
                            }
                        }
                    }
                }
                
                //all verifications passed, include the axiom
                importedAxiomCount++;
                sourceAxiomsNoAnnots.add(importedAx.getAxiomWithoutAnnotations());
                sourceOnt.getOWLOntologyManager().addAxiom(sourceOnt, importedAx);
            }
            log.info(importedAxiomCount + " axioms imported.");
        }

        //remove the import declarations
        Set<OWLImportsDeclaration> oids = sourceOnt.getImportsDeclarations();
        for (OWLImportsDeclaration oid : oids) {
            RemoveImport ri = new RemoveImport(sourceOnt, oid);
            sourceOnt.getOWLOntologyManager().applyChange(ri);
        }
        
        log.info("Done merging axioms.");
    }
 
Example #15
Source File: OWLGsonRenderer.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Object convert(OWLImportsDeclaration obj) {
	return convert(((OWLImportsDeclaration)obj).getIRI());
}