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

The following examples show how to use org.semanticweb.owlapi.model.OWLDataFactory#getOWLClass() . 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: OWLGraphManipulatorTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 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 testOWLClassHashCode()
{
	 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	 OWLDataFactory factory = manager.getOWLDataFactory(); 
	 IRI iri = IRI.create("http://www.foo.org/#A");
	 OWLClass class1 = factory.getOWLClass(iri);
	 //get the class by another way, even if if I suspect the two references 
	 //will point to the same object
	 PrefixManager pm = new DefaultPrefixManager("http://www.foo.org/#"); 
	 OWLClass class2 = factory.getOWLClass(":A", pm);
	 
	 assertTrue("The two references point to different OWLClass objects", 
			 class1 == class2);
	 //then of course the hashcodes will be the same...
	 assertTrue("Two OWLClasses are equal but have different hashcode", 
			 class1.equals(class2) && class1.hashCode() == class2.hashCode());
}
 
Example 3
Source File: OwlSimUtil.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void addElementToAttributeAssociationsFromFile(OWLOntology ont, File file) throws IOException {
	OWLOntologyManager m = OWLManager.createOWLOntologyManager();
	OWLDataFactory df = m.getOWLDataFactory();
	List<String> lines = FileUtils.readLines(file);
	for (String line : lines) {
		if (line.startsWith("#"))
			continue;
		String[] colVals = line.split("\t");
		if (colVals.length != 2) {
			throw new IOException("Incorrect number of value: "+line);
		}
		OWLNamedIndividual i = df.getOWLNamedIndividual(getIRIById(colVals[0]));
		OWLClass c = df.getOWLClass(getIRIById(colVals[1]));
		m.addAxiom(ont, df.getOWLClassAssertionAxiom(c, i));
	}
}
 
Example 4
Source File: OWLSimReferenceBasedStatistics.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected DescriptiveStatistics computeDescriptiveStatistics(Set<OWLClass> attributes) throws UnknownOWLClassException  {
	DescriptiveStatistics statsPerAttSet = new DescriptiveStatistics();
	OWLDataFactory g = sourceOntology.getOWLOntologyManager().getOWLDataFactory();

	for (OWLClass c : attributes) {
		Double ic;
		try {
			ic = owlsim.getInformationContentForAttribute(c);
			if (ic == null) { 
				if (g.getOWLClass(c.getIRI()) != null) {
					ic = owlsim.getSummaryStatistics().max.getMax();
				} else {
					throw new UnknownOWLClassException(c); }
			}
			if (ic.isInfinite() || ic.isNaN()) {
				ic = owlsim.getSummaryStatistics().max.getMax();
			}
			statsPerAttSet.addValue(ic);	

		} catch (UnknownOWLClassException e) {
			LOG.info("Unknown class "+c.toStringID()+" submitted for summary stats. Removed from calculation.");
			continue;
		}
	}
	return statsPerAttSet;
}
 
Example 5
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 6
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testActinobacteria(OWLOntology ontology) {
	String curie = "ncbi:201174";
	IRI iri = OWLConverter.format.getIRI(curie);
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLClass taxon = df.getOWLClass(iri);
	assertTrue("Actinobacteria class in signature",
		ontology.containsClassInSignature(iri));

	// Check axioms
	Set<OWLClassAxiom> axioms = ontology.getAxioms(taxon, Imports.EXCLUDED);
	assertEquals("Count class axioms for Actinobacteria", 1, axioms.size());
	assertEquals("SubClassOf(<http://purl.obolibrary.org/obo/NCBITaxon_201174> <http://purl.obolibrary.org/obo/NCBITaxon_2>)", axioms.toArray()[0].toString());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add(expandAnnotation(curie, "ncbitaxon:has_rank", OWLConverter.format.getIRI("ncbi:phylum")));
	values.add(expandAnnotation(curie, "oio:hasOBONamespace", "ncbi_taxonomy"));
	values.add(expandAnnotation(curie, "oio:hasDbXref", "GC_ID:11"));
	values.add(expandLabel(curie, "rdfs:label", "Actinobacteria [NCBITaxon:201174]"));
	values.add(expandSynonym(curie, "ncbitaxon:scientific_name", "oio:hasExactSynonym", "Actinobacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "'Actinobacteria'"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "not Actinobacteria Cavalier-Smith 2002"));
	values.add(expandSynonym(curie, "ncbitaxon:blast_name", "oio:hasRelatedSynonym", "actinobacteria"));

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Actinobacteria",
			values.size(), annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 7
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 8
Source File: AbstractOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public SummaryStatistics computeAttributeSetSimilarityStats(Set<OWLClass> atts)  {

		SummaryStatistics statsPerAttSet = new SummaryStatistics();
		//		Set<OWLClass> allClasses = getSourceOntology().getClassesInSignature(true);
		OWLDataFactory g = getSourceOntology().getOWLOntologyManager().getOWLDataFactory();

		for (OWLClass c : atts) {
			Double ic;
			try {
				ic = this.getInformationContentForAttribute(c);
				if (ic == null) { 
					//If a class hasn't been annotated in the loaded corpus, we will
					//assume that it is very rare, and assign MaxIC
					if (g.getOWLClass(c.getIRI()) != null) {
						ic = this.getSummaryStatistics().max.getMax();
					} else {
						throw new UnknownOWLClassException(c); }
				}
				if (ic.isInfinite() || ic.isNaN()) {
					//If a class hasn't been annotated in the loaded corpus, we will
					//assume that it is very rare, and assign MaxIC
					//a different option would be to skip adding this value, 
					//but i'm not sure that's wise
					ic = this.getSummaryStatistics().max.getMax();
				}
				//LOG.info("IC for "+c.toString()+"is: "+ic);
				statsPerAttSet.addValue(ic);	

			} catch (UnknownOWLClassException e) {
				//This is an extra catch here, but really it should be caught upstream.
				LOG.info("Unknown class "+c.toStringID()+" submitted for summary stats. Removed from calculation.");
				continue;
			}
		}
		return statsPerAttSet;
	}
 
Example 9
Source File: AbstractOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void setInformationContentFromOntology(OWLOntology o) {
	OWLOntologyManager mgr = getSourceOntology().getOWLOntologyManager();
	OWLDataFactory df = mgr.getOWLDataFactory();
	clearInformationContentCache();
	for (OWLAnnotationAssertionAxiom ax : o.getAxioms(AxiomType.ANNOTATION_ASSERTION)) {
		if (ax.getProperty().getIRI().toString().equals(icIRIString)) {
			OWLLiteral lit = (OWLLiteral) ax.getValue();
			OWLClass c = df.getOWLClass((IRI) ax.getSubject());
			Double v = lit.parseDouble();
			setInformtionContectForAttribute(c, v);
		}
	}
	assignDefaultInformationContentForAllClasses();
}
 
Example 10
Source File: TransformationUtils.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Names all inner ObjectSomeValuesFrom expressions
 * 
 * @param srcOntology
 * @param tgtOntology
 * @param qmap
 * @param isAddLabels
 * @return
 */
public static Map<OWLClass, OWLClassExpression> nameObjectSomeValuesFrom(OWLOntology srcOntology,
		OWLOntology tgtOntology,
		Map<OWLClass,OWLClassExpression> qmap,
		boolean isAddLabels) {

	if (qmap == null) 
		qmap = new HashMap<OWLClass, OWLClassExpression>();
	OWLOntologyManager mgr = srcOntology.getOWLOntologyManager();
	OWLDataFactory df = mgr.getOWLDataFactory();
	for (OWLOntology ont : srcOntology.getImportsClosure()) {
		for (OWLAxiom ax : srcOntology.getAxioms()) {
			for (OWLClassExpression x : ax.getNestedClassExpressions()) {
				if (x instanceof OWLObjectSomeValuesFrom) {
					OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom)x;
					OWLClass filler = (OWLClass) svf.getFiller();
					OWLObjectProperty p = (OWLObjectProperty) svf.getProperty();
					IRI iri = getSkolemIRI(filler, p);
					OWLClass c = df.getOWLClass(iri);
					mgr.addAxiom(tgtOntology, df.getOWLEquivalentClassesAxiom(c, svf));
					qmap.put(c, svf);
					if (isAddLabels) {
						Set<OWLAnnotation> anns = OwlHelper.getAnnotations(filler, df.getRDFSLabel(), ont);
						for (OWLAnnotation ann : anns) {
							mgr.addAxiom(tgtOntology, df.getOWLAnnotationAssertionAxiom(c.getIRI(), ann));
						}
					}
				}
			}
		}
	}
	return qmap;

}
 
Example 11
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testBacteria(OWLOntology ontology) {
	String curie = "ncbi:2";
	IRI iri = OWLConverter.format.getIRI(curie);
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLClass taxon = df.getOWLClass(iri);
	assertTrue("Bacteria class in signature",
		ontology.containsClassInSignature(iri));

	// Check axioms
	Set<OWLClassAxiom> axioms = ontology.getAxioms(taxon, Imports.EXCLUDED);
	assertEquals("Count class axioms for Bacteria", 1, axioms.size());
	assertEquals("SubClassOf(<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://purl.obolibrary.org/obo/NCBITaxon_131567>)", axioms.toArray()[0].toString());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add(expandAnnotation(curie, "ncbitaxon:has_rank", OWLConverter.format.getIRI("ncbi:superkingdom")));
	values.add(expandAnnotation(curie, "oio:hasOBONamespace", "ncbi_taxonomy"));
	values.add(expandAnnotation(curie, "oio:hasDbXref", "GC_ID:11"));
	values.add(expandLabel(curie, "rdfs:label", "Bacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:genbank_common_name", "oio:hasExactSynonym", "eubacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "not Bacteria Haeckel 1894"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Prokaryota"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Monera"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Procaryotae"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Prokaryotae"));
	values.add(expandSynonym(curie, "ncbitaxon:blast_name", "oio:hasRelatedSynonym", "eubacteria"));

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Bacteria", values.size(), annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 12
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 13
Source File: ConjunctiveSetInformationContentRatioSimilarity.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void translateResultsToOWLAxioms(String id, OWLNamedIndividual result, Set<OWLAxiom> axioms) {
	OWLDataFactory df = simEngine.getGraph().getDataFactory();
	
	// declare a named class for the LCS and make this equivalent to the anonymous expression
	OWLClass namedLCS = df.getOWLClass(IRI.create(id+"_LCS"));
	axioms.add(df.getOWLAnnotationAssertionAxiom(df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI()),
			namedLCS.getIRI(), 
			df.getOWLLiteral("LCS of "+simEngine.label(a)+" and "+simEngine.label(b))));
	axioms.add(df.getOWLEquivalentClassesAxiom(namedLCS, getLCS()));

	// link the similarity object to the named LCS
	OWLAnnotationProperty lcsp = df.getOWLAnnotationProperty(annotationIRI("has_least_common_subsumer"));
	axioms.add(df.getOWLAnnotationAssertionAxiom(lcsp, result.getIRI(), namedLCS.getIRI()));
}
 
Example 14
Source File: AbstractOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public SummaryStatistics computeAttributeSetSimilarityStatsForSubgraph(Set<OWLClass> atts, OWLClass sub)  {
	SummaryStatistics statsPerAttSet = new SummaryStatistics();
	//		Set<OWLClass> allClasses = getSourceOntology().getClassesInSignature(true);
	OWLDataFactory g = getSourceOntology().getOWLOntologyManager().getOWLDataFactory();

	for (OWLClass c : atts) {
		Double ic;
		try {
			//check if sub is an inferred superclass of the current annotated class
			//TODO check if i need all of these; this might be expensive and unnecessary
			if (getReasoner().getSuperClasses(c, false).containsEntity(sub) ||
					getReasoner().getEquivalentClasses(c).contains(sub))  { 
				ic = this.getInformationContentForAttribute(c);

				if (ic == null) { 
					//If a class hasn't been annotated in the loaded corpus, we will
					//assume that it is very rare, and assign MaxIC
					if (g.getOWLClass(c.getIRI()) != null) {
						ic = this.getSummaryStatistics().max.getMax();
					} else {
						throw new UnknownOWLClassException(c); }
				}

				if (ic.isInfinite() || ic.isNaN()) {
					//If a class hasn't been annotated in the loaded corpus, we will
					//assume that it is very rare, and assign MaxIC
					ic = this.getSummaryStatistics().max.getMax();
				}
				//LOG.info("IC for "+c.toString()+"is: "+ic);
				statsPerAttSet.addValue(ic);	
			} else {
				//LOG.info("tossing "+c.toString()+"; not a subclass of "+sub.toString());
			}
		} catch (UnknownOWLClassException e) {
			//This is an extra catch here, but really it should be caught upstream.
			LOG.info("Unknown class "+c.toStringID()+" submitted for summary stats. Removed from calculation.");
			continue;
		}
	}
	return statsPerAttSet;
}
 
Example 15
Source File: FoldBasedPredictor.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Map<Bioentity, Set<OWLClass>> generateAxioms(OWLOntology generatedContainer, Map<Bioentity, ? extends Collection<GeneAnnotation>> annMap, Map<Bioentity, Set<OWLClass>> allExistingAnnotations, Map<OWLClass, PredicationDataContainer> sourceData) {
	final OWLGraphWrapper g = getGraph();
	final OWLDataFactory f = g.getDataFactory();
	final OWLOntologyManager m = g.getManager();
	
	Map<Bioentity, Set<OWLClass>> allGeneratedClasses = new HashMap<Bioentity, Set<OWLClass>>();
	for(Entry<Bioentity, ? extends Collection<GeneAnnotation>> entry : annMap.entrySet()) {
		Set<OWLClass> generatedClasses = new HashSet<OWLClass>();
		Set<OWLClass> existingAnnotations = new HashSet<OWLClass>();
		Bioentity e = entry.getKey();
		for (GeneAnnotation ann : entry.getValue()) {
			// skip ND evidence annotations
			String evidenceString = ann.getShortEvidence();
			if ("ND".equals(evidenceString)) {
				continue;
			}
			// parse annotation cls
			String annotatedToClassString = ann.getCls();
			OWLClass annotatedToClass = g.getOWLClassByIdentifierNoAltIds(annotatedToClassString);
			if (annotatedToClass == null) {
				LOG.warn("Skipping annotation for prediction. Could not find cls for id: "+annotatedToClassString);
				continue;
			}
			// add annotation class (and its super classes as known annotation) 
			existingAnnotations.add(annotatedToClass);
			existingAnnotations.addAll(reasoner.getSuperClasses(annotatedToClass, false).getFlattened());
			
			// parse c16 expressions
			List<List<ExtensionExpression>> extensionExpressionGroups = ann.getExtensionExpressions();
			if (extensionExpressionGroups != null && !extensionExpressionGroups.isEmpty()) {
				for (List<ExtensionExpression> group : extensionExpressionGroups) {
					Set<OWLClassExpression> units = new HashSet<OWLClassExpression>();
					for (ExtensionExpression ext : group) {
						String extClsString = ext.getCls();
						String extRelString = ext.getRelation();
						OWLClass extCls = f.getOWLClass(g.getIRIByIdentifier(extClsString));
						OWLObjectProperty extRel = g.getOWLObjectPropertyByIdentifier(extRelString);
						if (extRel == null) {
							continue;
						}
						units.add(f.getOWLObjectSomeValuesFrom(extRel, extCls));
					}
					if (units.isEmpty()) {
						continue;
					}
					units.add(annotatedToClass);
					final OWLClassExpression groupExpression = f.getOWLObjectIntersectionOf(units);
					OWLClass generatedClass = f.getOWLClass(IRI.generateDocumentIRI());
					OWLAxiom axiom = f.getOWLEquivalentClassesAxiom(generatedClass, groupExpression);
					m.addAxiom(generatedContainer, axiom);
					generatedClasses.add(generatedClass);
					sourceData.put(generatedClass, new PredicationDataContainer(ann, annotatedToClass, evidenceString, groupExpression, group));
				}
			}
		}
		if (generatedClasses.isEmpty() == false) {
			allGeneratedClasses.put(e, generatedClasses);
			allExistingAnnotations.put(e, existingAnnotations);
		}
	}
	return allGeneratedClasses;
}
 
Example 16
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 17
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 18
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 19
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 20
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
 * 
 */
@Test
public void testNoChanges() 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 {

		// statistics about the root ontology
		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 subclasses 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();
	}

}