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

The following examples show how to use org.semanticweb.owlapi.model.OWLOntologyManager#applyChanges() . 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: RenameOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given an ontology, an IOHelper, and a map of old IRIs to new IRIs, rename each old IRI with the
 * new IRI.
 *
 * @param ontology OWLOntology to rename entities in
 * @param ioHelper IOHelper to create IRIs
 * @param mappings map of old IRI to new IRI
 * @throws Exception if the old IRI in a mapping does not exist
 */
public static void renameFull(
    OWLOntology ontology, IOHelper ioHelper, Map<String, String> mappings) throws Exception {
  OWLOntologyManager manager = ontology.getOWLOntologyManager();
  OWLEntityRenamer entityRenamer = new OWLEntityRenamer(manager, Sets.newHashSet(ontology));
  for (Map.Entry<String, String> mapping : mappings.entrySet()) {
    IRI oldIRI = ioHelper.createIRI(mapping.getKey());
    IRI newIRI = ioHelper.createIRI(mapping.getValue());
    if (!ontology.containsEntityInSignature(oldIRI)) {
      throw new Exception(String.format(missingEntityError, oldIRI.toString()));
    }
    manager.applyChanges(entityRenamer.changeIRI(oldIRI, newIRI));
  }
}
 
Example 2
Source File: ProofTestUtils.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
public static <I extends Inference<?>> void proofCompletenessTest(
		final OWLProver prover, final OWLAxiom entailment,
		final Object conclusion, final Proof<? extends I> proof,
		final InferenceJustifier<? super I, ? extends Set<? extends OWLAxiom>> justifier,
		final boolean mustNotBeATautology) {

	final OWLOntology ontology = prover.getRootOntology();
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();

	// compute repairs

	final Set<Set<? extends OWLAxiom>> repairs = new HashSet<Set<? extends OWLAxiom>>();
	MinimalSubsetEnumerators.enumerateRepairs(conclusion, proof, justifier,
			InterruptMonitor.DUMMY,
			new MinimalSubsetCollector<OWLAxiom>(repairs));

	if (mustNotBeATautology) {
		assertFalse("Entailment is a tautology; there are no repairs!",
				repairs.isEmpty());
	}

	for (final Set<? extends OWLAxiom> repair : repairs) {

		final List<OWLOntologyChange> deletions = new ArrayList<OWLOntologyChange>();
		final List<OWLOntologyChange> additions = new ArrayList<OWLOntologyChange>();
		for (final OWLAxiom axiom : repair) {
			deletions.add(new RemoveAxiom(ontology, axiom));
			additions.add(new AddAxiom(ontology, axiom));
		}

		manager.applyChanges(deletions);

		final boolean conclusionDerived = prover.isEntailed(entailment);

		manager.applyChanges(additions);

		assertFalse("Not all proofs were found!\n" + "Conclusion: "
				+ conclusion + "\n" + "Repair: " + repair,
				conclusionDerived);
	}

}
 
Example 3
Source File: ProofTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
@Test
public void proofsUnderOntologyUpdate() throws Exception {
	// loading and classifying via the OWL API
	OWLOntology ontology = loadOntology(
			ProofTest.class.getClassLoader().getResourceAsStream(
					"ontologies/PropertyCompositionsWithHierarchy.owl"));
	final OWLProver prover = OWLAPITestUtils.createProver(ontology);

	prover.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	OWLClass sub = factory.getOWLClass(IRI.create("http://example.org/A"));
	OWLClass sup = factory.getOWLClass(IRI.create("http://example.org/G"));

	// printInferences(reasoner, sub, sup);
	// OWLExpression root =
	// reasoner.getDerivedExpression(factory.getOWLSubClassOfAxiom(sub,
	// sup));
	// System.err.println(OWLProofUtils.printProofTree(root));
	ProofTestUtils.provabilityTest(prover,
			factory.getOWLSubClassOfAxiom(sub, sup));

	// now convert C <= R3 some D to C < S3 some D
	OWLClass c = factory.getOWLClass(IRI.create("http://example.org/C"));
	OWLClass d = factory.getOWLClass(IRI.create("http://example.org/D"));
	OWLObjectProperty r3 = factory
			.getOWLObjectProperty(IRI.create("http://example.org/R3"));
	OWLObjectProperty s3 = factory
			.getOWLObjectProperty(IRI.create("http://example.org/S3"));
	OWLAxiom oldAx = factory.getOWLSubClassOfAxiom(c,
			factory.getOWLObjectSomeValuesFrom(r3, d));
	OWLAxiom newAx = factory.getOWLSubClassOfAxiom(c,
			factory.getOWLObjectSomeValuesFrom(s3, d));

	OWLOntologyManager manager = ontology.getOWLOntologyManager();

	manager.applyChanges(Arrays.asList(new RemoveAxiom(ontology, oldAx),
			new AddAxiom(ontology, newAx)));

	prover.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	// printInferences(reasoner, sub, sup);
	// root =
	// reasoner.getDerivedExpression(factory.getOWLSubClassOfAxiom(sub,
	// sup));
	// System.err.println(OWLProofUtils.printProofTree(root));
	ProofTestUtils.provabilityTest(prover,
			factory.getOWLSubClassOfAxiom(sub, sup));
}
 
Example 4
Source File: ProofTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
@Test
public void proofListener() throws Exception {
	OWLOntologyManager owlManager = OWLManager
			.createConcurrentOWLOntologyManager();
	OWLClass a = factory.getOWLClass(IRI.create("http://example.org/A"));
	OWLClass b = factory.getOWLClass(IRI.create("http://example.org/B"));
	OWLClass c = factory.getOWLClass(IRI.create("http://example.org/C"));
	OWLObjectProperty r = factory
			.getOWLObjectProperty(IRI.create("http://example.org/R"));
	OWLObjectProperty s = factory
			.getOWLObjectProperty(IRI.create("http://example.org/S"));
	OWLAxiom ax1 = factory.getOWLSubClassOfAxiom(a, c);
	OWLAxiom ax2 = factory.getOWLSubClassOfAxiom(c, b);
	OWLAxiom ax3 = factory.getOWLSubClassOfAxiom(a,
			factory.getOWLObjectSomeValuesFrom(r, c));
	OWLAxiom ax4 = factory.getOWLSubClassOfAxiom(
			factory.getOWLObjectSomeValuesFrom(s, c), b);
	OWLAxiom ax5 = factory.getOWLSubObjectPropertyOfAxiom(r, s);

	boolean changed = false; // means entailment has changed

	for (boolean bufferringMode : Arrays.asList(true, false)) {
		// creating an ontology
		final OWLOntology ontology = owlManager.createOntology();

		final OWLProver prover = OWLAPITestUtils.createProver(
				OWLAPITestUtils.createReasoner(ontology, bufferringMode));
		OWLSubClassOfAxiom entailment = factory.getOWLSubClassOfAxiom(a, b);
		DynamicProof<? extends Inference<OWLAxiom>> proof = prover
				.getProof(entailment);
		ProofChangeTracker tracker = new ProofChangeTracker();
		proof.addListener(tracker);

		assertFalse(Proofs.isDerivable(proof, entailment));

		// add ax1 and ax2 => ENTAIL
		owlManager.applyChanges(Arrays.asList(new AddAxiom(ontology, ax1),
				new AddAxiom(ontology, ax2)));

		// derivable only in non-buffering mode
		changed = tracker.changed();
		assertEquals(!bufferringMode, changed);
		assertEquals(!bufferringMode,
				Proofs.isDerivable(proof, entailment));
		// but always derivable after flush
		prover.flush();
		changed |= tracker.changed();
		assertTrue(changed);
		assertTrue(Proofs.isDerivable(proof, entailment));

		// remove ax1, add ax3, ax4 => NOT ENTAIL
		owlManager.applyChanges(Arrays.asList(
				new RemoveAxiom(ontology, ax1), new AddAxiom(ontology, ax3),
				new AddAxiom(ontology, ax4)));

		// still derivable only in bufferring mode
		changed = tracker.changed();
		assertEquals(!bufferringMode, changed);
		assertEquals(bufferringMode, Proofs.isDerivable(proof, entailment));
		// always non derivable after flush
		prover.flush();
		changed |= tracker.changed();
		assertTrue(changed);
		assertFalse(Proofs.isDerivable(proof, entailment));

		// add ax5 => ENTAIL
		owlManager.applyChanges(Arrays.asList(new AddAxiom(ontology, ax5)));

		// derivable only in non-buffering mode
		changed = tracker.changed();
		assertEquals(!bufferringMode, changed);
		assertEquals(!bufferringMode,
				Proofs.isDerivable(proof, entailment));
		// but always derivable after flush
		prover.flush();
		changed |= tracker.changed();
		assertTrue(changed);
		assertTrue(Proofs.isDerivable(proof, entailment));

	}

}
 
Example 5
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;
}
 
Example 6
Source File: AssertInferenceTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void  cleanupSupportOntologies(OWLGraphWrapper graph, List<OWLOntologyChange> remove)
{
	OWLOntology ontology = graph.getSourceOntology();
	OWLOntologyManager manager = ontology.getOWLOntologyManager();
	manager.applyChanges(remove);
}