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

The following examples show how to use org.semanticweb.owlapi.model.OWLOntologyManager#applyChange() . 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: OboOntologyReleaseRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String handleVersion(String ontologyId) {
	// TODO add an option to set/overwrite the version manually via command-line
	// TODO re-use/create a method in obo2owl for creating an version IRI
	String version;
	OWLOntology ontology = mooncat.getOntology();
	OWLOntologyID owlOntologyId = ontology.getOntologyID();
	Optional<IRI> versionIRI = owlOntologyId.getVersionIRI();
	if (versionIRI.isPresent() == false) {
		// set a new version IRI using the current date
		version = OntologyVersionTools.format(new Date());
		versionIRI = Optional.of(IRI.create(Obo2OWLConstants.DEFAULT_IRI_PREFIX+ontologyId+"/"+oortConfig.getVersionSubdirectory()+"/"+version+"/"+ontologyId+".owl"));
		
		OWLOntologyManager m = mooncat.getManager();
		m.applyChange(new SetOntologyID(ontology, new OWLOntologyID(owlOntologyId.getOntologyIRI(), versionIRI)));
	}
	else {
		String versionIRIString = versionIRI.get().toString();
		version = OntologyVersionTools.parseVersion(versionIRIString);
		if (version == null) {
			// use the whole IRI? escape?
			logError("Could not parse a version from ontolgy version IRI: "+versionIRIString);
			version = versionIRIString;
		}
	}
	return version;
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: OboOntologyReleaseRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAxiom(String info, OWLAxiom ax, OWLOntology ont, OWLOntologyManager manager, OWLDataFactory factory, List<String> reasonerReportLines) {
	if (oortConfig.isUseIsInferred()) {
		ax = AxiomAnnotationTools.markAsInferredAxiom(ax, factory);
	}
	manager.applyChange(new AddAxiom(ont, ax));
	String ppax = owlpp.render(ax);
	String rptLine = info+"\t"+ppax;
	reasonerReportLines.add(rptLine);
}
 
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 adding an annotation assertion to the
 * ontology itself, taking a CURIE for the property and an Boolean literal.
 *
 * @param ontology the current ontology
 * @param propertyCURIE will be expanded to the full annotation
 *	property IRI
 * @param value the literal value of the annotation
 * @return the annotation axiom
 */
protected static OWLAnnotation annotate(OWLOntology ontology,
		String propertyCURIE, IRI value) {
	OWLOntologyManager manager = ontology.getOWLOntologyManager();
	OWLDataFactory dataFactory = manager.getOWLDataFactory();
	IRI iri = format.getIRI(propertyCURIE);
	OWLAnnotationProperty property =
		dataFactory.getOWLAnnotationProperty(iri);
	OWLAnnotation annotation = dataFactory.getOWLAnnotation(
			property, value);
	manager.applyChange(
		new AddOntologyAnnotation(ontology, annotation));
	return annotation;
}
 
Example 8
Source File: OWLConverter.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convenience method for adding an annotation assertion to the
 * ontology itself, taking a CURIE for the property and an Boolean literal.
 *
 * @param ontology the current ontology
 * @param propertyCURIE will be expanded to the full annotation
 *	property IRI
 * @param value the literal value of the annotation
 * @return the annotation axiom
 */
protected static OWLAnnotation annotate(OWLOntology ontology,
		String propertyCURIE, String value) {
	OWLOntologyManager manager = ontology.getOWLOntologyManager();
	OWLDataFactory dataFactory = manager.getOWLDataFactory();
	IRI iri = format.getIRI(propertyCURIE);
	OWLAnnotationProperty property =
		dataFactory.getOWLAnnotationProperty(iri);
	OWLLiteral literal = dataFactory.getOWLLiteral(value);
	OWLAnnotation annotation = dataFactory.getOWLAnnotation(
			property, literal);
	manager.applyChange(
		new AddOntologyAnnotation(ontology, annotation));
	return annotation;
}
 
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>
 * 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 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: BioChebiGenerator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addComment(String comment, OWLOntology ontology) {
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();
	final OWLDataFactory factory = manager.getOWLDataFactory();
	OWLAnnotation ontAnn = factory.getOWLAnnotation(factory.getRDFSComment(), factory.getOWLLiteral(comment));
	manager.applyChange(new AddOntologyAnnotation(ontology, ontAnn));
}
 
Example 12
Source File: DescriptionTreeSimilarity.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * makes a reduced union expression.
 * 
 * Uses the following two reduction rules:
 * 
 * (r1 some X) U (r2 some Y) ==> lcs(r1,r2) some MakeUnionOf(X,Y)
 * (r1 some X) U X ==> reflexive-version-of-r1 some X
 * 
 * TODO: test for (r some r some X) u (r some X) cases. needs to be done over final expression.
 *  
 * if a reduced form cannot be made, returns null
 * 
 * @param xa
 * @param xb
 * @return class expression
 */
private OWLClassExpression makeUnionUsingReflexiveProperty(OWLClassExpression xa, OWLClassExpression xb) {
	LOG.info("testing if there is a more compact union expression for "+xa+" ++ "+xb);
	OWLDataFactory df = graph.getDataFactory();
	if (xa instanceof OWLQuantifiedRestriction) {
		// TODO - check before casting
		OWLObjectProperty prop = (OWLObjectProperty) ((OWLQuantifiedRestriction) xa).getProperty();
		OWLClassExpression xaRest = (OWLClassExpression) ((OWLQuantifiedRestriction)xa).getFiller();
		if (xb instanceof OWLQuantifiedRestriction) {
			OWLObjectPropertyExpression p2 =
				propertySubsumer(prop, 
					((OWLQuantifiedObjectRestriction) xb).getProperty());
			
			if (p2 != null) {
				OWLClassExpression xbRest = (OWLClassExpression) ((OWLQuantifiedRestriction)xb).getFiller();
				OWLClassExpression x = makeUnionWithReduction(xaRest,xbRest);
				// todo - mixing some and all
				if (xa instanceof OWLObjectSomeValuesFrom)
					return df.getOWLObjectSomeValuesFrom(p2,x);
				else if (xa instanceof OWLObjectAllValuesFrom)
					return df.getOWLObjectAllValuesFrom(p2, x);
			}
		}
		LOG.info("  test: "+xaRest+" == "+xb);
		

		if (xaRest.equals(xb)) {
			LOG.info("     TRUE: "+xaRest+" == "+xb);

			OWLObjectProperty rprop = null;
			if (graph.getIsReflexive(prop)) {
				rprop = prop;
			}
			if (forceReflexivePropertyCreation) {
				OWLOntologyManager manager = graph.getManager();
				OWLOntology ont = graph.getSourceOntology();
				rprop = 
					df.getOWLObjectProperty(IRI.create(prop.getIRI().toString()+"_reflexive"));
				manager.applyChange(new AddAxiom(ont, df.getOWLSubObjectPropertyOfAxiom(prop, rprop)));
				manager.applyChange(new AddAxiom(ont, df.getOWLTransitiveObjectPropertyAxiom(rprop)));
				LOG.info("  reflexive prop:"+rprop);

			}
			if (rprop != null) {
				if (xa instanceof OWLObjectSomeValuesFrom)
					return df.getOWLObjectSomeValuesFrom(rprop,xb);
				else if (xa instanceof OWLObjectAllValuesFrom)
					return df.getOWLObjectAllValuesFrom(rprop, xb);
			}

		}
	}
	return null;
}