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

The following examples show how to use org.semanticweb.owlapi.model.OWLOntologyManager#addAxioms() . 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: ProvenanceReasonerWrapper.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public boolean isEdgeEntailed(OWLEdge e, OWLOntology currentOntology, OWLReasoner reasoner) {
	OWLOntologyManager m = getManager();

	Set<OWLSubClassOfAxiom> scas = currentOntology.getSubClassAxiomsForSubClass(e.c);
	Set<OWLSubClassOfAxiom> rmAxioms = new HashSet<OWLSubClassOfAxiom>();
	for (OWLSubClassOfAxiom sca : scas) {
		if (sca.getSuperClass().equals(e.p)) {
			LOG.info("REMOVING: "+sca);
			rmAxioms.add(sca);
		}
	}
	boolean isEdgeAsserted = rmAxioms.size() > 0;
	if (isEdgeAsserted) {
		m.removeAxioms(currentOntology, rmAxioms);
		reasoner.flush();
	}
	boolean isEntailed;
	isEntailed = reasoner.getSuperClasses(e.c, false).containsEntity(e.p);
	if (isEdgeAsserted) {
		m.addAxioms(currentOntology, rmAxioms);
		reasoner.flush();
	}

	return isEntailed;
}
 
Example 2
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 3
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 4
Source File: OboOntologyReleaseRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void createModule(String ontologyId, String moduleName, Set<OWLEntity> signature)
		throws OWLOntologyCreationException, IOException, OWLOntologyStorageException 
{
	// create a new manager, avoid unnecessary change events
	final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
	
	// extract module
	SyntacticLocalityModuleExtractor sme = new SyntacticLocalityModuleExtractor(m, mooncat.getOntology(), ModuleType.BOT);
	Set<OWLAxiom> moduleAxioms = sme.extract(signature);
	
	OWLOntology module = m.createOntology(IRI.generateDocumentIRI());
	m.addAxioms(module, moduleAxioms);
	
	// save module
	OutputStream moduleOutputStream = null;
	try {
		moduleOutputStream = getOutputSteam(getModuleFileName(ontologyId, moduleName));
		m.saveOntology(module, moduleOutputStream);
	}
	finally {
		IOUtils.closeQuietly(moduleOutputStream);
	}
}
 
Example 5
Source File: ProvenanceReasonerWrapper.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void reasonLeavingOneOut(OWLOntology leaveOutOntology) throws OWLOntologyCreationException {
	OWLOntologyManager m = getManager();
	OWLOntology ont2 = m.createOntology();
	
	LOG.info("LEAVE ONE OUT: "+leaveOutOntology);
	for (OWLOntology io : ontology.getImportsClosure()) {
		if (io.equals(leaveOutOntology)) {
			LOG.info("SKIPPING:"+io);
			continue;
		}
		m.addAxioms(ont2, io.getAxioms());
		
	}
	
	OWLReasoner reasoner = rf.createReasoner(ont2);
	for (OWLEdge e : edges) {
		if (!e.isJustified) {
			// there is no point checking unjustified edges;
			// these are edges that when removed cannot be re-inferred using the entire ontology set.
			// as reasoning is monotonic, removing imports cannot bring it back.
			continue;
		}
		//LOG.info("Testing "+e); 
		if (!isEdgeEntailed(e, ont2, reasoner)) {
			IRI req = leaveOutOntology.getOntologyID().getOntologyIRI().orNull();
			LOG.info(e + " requires "+req);
			e.requires.add(req);
			
		}
	}
	reasoner.dispose();
}
 
Example 6
Source File: AxiomAnnotationTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Mark the given axioms as inferred.<br>
 * <b>Side effect</b>: Removes the axiom in the given ontology and creates a
 * new axiom with the changed annotations. Returns the new changed axiom.
 * 
 * @param axioms
 * @param ontology
 * @return changed axiom
 */
public static Set<OWLAxiom> markAsInferredAxiom(Set<OWLAxiom> axioms, OWLOntology ontology) {
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();
	final OWLDataFactory factory = manager.getOWLDataFactory();
	
	// update axioms
	Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
	for (OWLAxiom axiom : axioms) {
		newAxioms.add(updateInferredAxiom(axiom, factory, true));
	}
	// change ontology
	manager.removeAxioms(ontology, axioms);
	manager.addAxioms(ontology, newAxioms);
	return newAxioms;
}
 
Example 7
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Generate a OboGraphs JSON ontology blob for the local axioms for an object.
 * 
 * This will include
 * 
 *  - all logical axioms about the object
 *  - all annotations for all entities in the signature
 *  
 * In other words, direct parents plus labels and other metadata on all entities
 * 
 * @param obj
 * @return JSON string
 * @throws JsonProcessingException
 * @throws OWLOntologyCreationException
 */
public String getOboGraphJSONString(OWLObject obj) throws JsonProcessingException, OWLOntologyCreationException {
	FromOwl fromOwl = new FromOwl();
	OWLOntologyManager m = sourceOntology.getOWLOntologyManager();
	if (obj instanceof OWLNamedObject) {
		OWLNamedObject nobj = (OWLNamedObject)obj;
		OWLOntology ont = m.createOntology(nobj.getIRI());
		Set<OWLAxiom> axioms = new HashSet<>();
		if (nobj instanceof OWLClass) {
			axioms.addAll(sourceOntology.getAxioms((OWLClass)nobj, Imports.INCLUDED));
		}
		else if (nobj instanceof OWLObjectProperty) {
			axioms.addAll(sourceOntology.getAxioms((OWLObjectProperty)nobj, Imports.INCLUDED));
		}
		m.addAxioms(ont, axioms);
		axioms = new HashSet<>();
		for (OWLEntity e : ont.getSignature()) {
			axioms.addAll(sourceOntology.getAnnotationAssertionAxioms(e.getIRI()));
		}
		axioms.addAll(sourceOntology.getAnnotationAssertionAxioms(nobj.getIRI()));
		m.addAxioms(ont, axioms);

		GraphDocument gd = fromOwl.generateGraphDocument(ont);
		return OgJsonGenerator.render(gd);
	}
	else {
		return "{}";
	}

}
 
Example 8
Source File: GafCommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected int preCheckOntology(String inConsistentMsg, String unsatisfiableMsg, String unsatisfiableModule) throws OWLException, IOException {
	// pre-check: only try to load ontology iff:
	// * ontology is consistent 
	// * no unsatisfiable classes
	OWLReasoner currentReasoner = reasoner;
	boolean disposeReasoner = false;
	try {
		if (currentReasoner == null) {
			disposeReasoner = true;
			currentReasoner = new ElkReasonerFactory().createReasoner(g.getSourceOntology());
		}
		boolean consistent = currentReasoner.isConsistent();
		if (consistent == false) {
			LOG.error(inConsistentMsg);
			return -1;
		}
		Set<OWLClass> unsatisfiable = currentReasoner.getUnsatisfiableClasses().getEntitiesMinusBottom();
		if (unsatisfiable.isEmpty() == false) {
			LOG.error(unsatisfiableMsg);
			OWLPrettyPrinter prettyPrinter = getPrettyPrinter();
			for (OWLClass owlClass : unsatisfiable) {
				LOG.error("Unsatisfiable: "+prettyPrinter.render(owlClass));
			}
			LOG.info("Creating module for unsatisfiable classes in file: "+unsatisfiableModule);
			ModuleType mtype = ModuleType.BOT;
			OWLOntologyManager m = g.getManager();
			SyntacticLocalityModuleExtractor sme = new SyntacticLocalityModuleExtractor(m, g.getSourceOntology(), mtype );
			Set<OWLEntity> seeds = new HashSet<OWLEntity>(unsatisfiable);
			Set<OWLAxiom> axioms = sme.extract(seeds);
			OWLOntology module = m.createOntology();
			m.addAxioms(module, axioms);
			File moduleFile = new File(unsatisfiableModule).getCanonicalFile();
			m.saveOntology(module, IRI.create(moduleFile));
			return -1;
		}
	}
	finally {
		if (disposeReasoner && currentReasoner != null) {
			currentReasoner.dispose();
		}
	}
	return 0;
}
 
Example 9
Source File: QuerySubsetGenerator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void createSubSet(OWLGraphWrapper targetGraph, 
		Set<OWLClass> subset, Set<OWLOntology> toMerge, boolean isExcludeClosure,
		boolean isRemoveDangling) throws OWLOntologyCreationException  {

	OWLOntology targetOntology = targetGraph.getSourceOntology();

	// import axioms set
	Set<OWLAxiom> importAxioms = new HashSet<OWLAxiom>();
	for (OWLOntology mergeOntology : toMerge) {
		for (OWLClass cls : subset) {
			importAxioms.addAll(mergeOntology.getAxioms(cls, Imports.EXCLUDED));
		}
	}

	// remove merge imports
	OWLOntologyManager targetManager = targetOntology.getOWLOntologyManager();
	List<OWLOntologyChange> removeImports = new ArrayList<OWLOntologyChange>();
	for(OWLOntology m : toMerge) {
		removeImports.add(new RemoveImport(targetOntology, new OWLImportsDeclarationImpl(m.getOntologyID().getOntologyIRI().get())));
	}
	targetManager.applyChanges(removeImports);

	// add axiom set to target ontology.
	targetManager.addAxioms(targetOntology, importAxioms);

	LOG.info("Start Mooncat for subset.");
	Mooncat mooncat = new Mooncat(targetGraph);
	for (OWLOntology ont : toMerge) {
		mooncat.addReferencedOntology(ont);
	}

	if (!isExcludeClosure) {
		// create Closure
		Set<OWLAxiom> axioms = mooncat.getClosureAxiomsOfExternalReferencedEntities();
		mooncat.addSubAnnotationProperties(axioms);

		// add missing axioms
		targetManager.addAxioms(targetOntology, axioms);
		LOG.info("Added "+axioms.size()+" axioms to the query ontology");
	}
	
	if (isRemoveDangling) {
		mooncat.removeDanglingAxioms();
	}

	return;
}