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

The following examples show how to use org.semanticweb.owlapi.model.OWLDataFactory#getOWLEquivalentClassesAxiom() . 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: DLQueryTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Execute the DL query on the given ontology graph. Uses the factory to create 
 * the {@link OWLReasoner} for an internal query ontology.
 * 
 * @param dlQuery
 * @param graph
 * @param reasonerFactory
 * @return set of {@link OWLClass} which 
 * @throws OWLParserException
 * @throws OWLOntologyCreationException
 */
public static Set<OWLClass> executeDLQuery(String dlQuery, OWLGraphWrapper graph, 
		OWLReasonerFactory reasonerFactory) throws OWLParserException, OWLOntologyCreationException 
{
	// create parser and parse DL query string
	ManchesterSyntaxTool parser = null;
	
	OWLClassExpression ce;
	try {
		parser = new ManchesterSyntaxTool(graph.getSourceOntology(), graph.getSupportOntologySet());
		ce = parser.parseManchesterExpression(dlQuery);
	} finally {
		// always dispose parser to avoid a memory leak
		if (parser != null) {
			parser.dispose();
		}
	}
	
	// create query ontology
	OWLOntologyManager m = OWLManager.createOWLOntologyManager();
	OWLOntology queryOntology = m.createOntology(IRI.generateDocumentIRI(), graph.getAllOntologies());
	OWLDataFactory f = m.getOWLDataFactory();
	OWLClass qc = f.getOWLClass(IRI.create("http://owltools.org/Q"));
	OWLEquivalentClassesAxiom ax = f.getOWLEquivalentClassesAxiom(ce, qc);
	m.addAxiom(queryOntology, ax);
	
	Set<OWLClass> subset = executeQuery(ce, queryOntology, reasonerFactory);
	if(subset.isEmpty()) {
		LOG.warn("No classes found for query subclass of:"+dlQuery);
	}
	return subset;
}
 
Example 2
Source File: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * For every pair X DisjointWith Y, generate an axiom
 * A and Y = Nothing
 * 
 * (may become deprecated after Elk supports disjoints)
 * 
 * @param ont
 * @param manager
 * @param dataFactory
 */
public static void translateDisjointsToEquivalents(OWLOntology ont, OWLOntologyManager manager, OWLDataFactory dataFactory) {
	for (OWLDisjointClassesAxiom dca : ont.getAxioms(AxiomType.DISJOINT_CLASSES, Imports.INCLUDED)) {
		for (OWLClassExpression ce1 : dca.getClassExpressions()) {
			for (OWLClassExpression ce2 : dca.getClassExpressions()) {
				if (ce1.compareTo(ce2) <= 0)
					continue;
				OWLEquivalentClassesAxiom eca = dataFactory.getOWLEquivalentClassesAxiom(dataFactory.getOWLNothing(),
						dataFactory.getOWLObjectIntersectionOf(ce1, ce2));
				manager.addAxiom(ont, eca);
				// TODO - remove if requested
			}
		}
	}
}
 
Example 3
Source File: QueryingUnnamedClassExpressions.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// Load your ontology.
	OWLOntology ont = man.loadOntologyFromOntologyDocument(new File(
			"c:/ontologies/ontology.owl"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Create your desired query class expression. In this example we
	// will query ObjectIntersectionOf(A ObjectSomeValuesFrom(R B)).
	PrefixManager pm = new DefaultPrefixManager("http://example.org/");
	OWLClass A = dataFactory.getOWLClass(":A", pm);
	OWLObjectProperty R = dataFactory.getOWLObjectProperty(":R", pm);
	OWLClass B = dataFactory.getOWLClass(":B", pm);
	OWLClassExpression query = dataFactory.getOWLObjectIntersectionOf(A,
			dataFactory.getOWLObjectSomeValuesFrom(R, B));

	// Create a fresh name for the query.
	OWLClass newName = dataFactory.getOWLClass(IRI.create("temp001"));
	// Make the query equivalent to the fresh class
	OWLAxiom definition = dataFactory.getOWLEquivalentClassesAxiom(newName,
			query);
	man.addAxiom(ont, definition);

	// Remember to either flush the reasoner after the ontology change
	// or create the reasoner in non-buffering mode. Note that querying
	// a reasoner after an ontology change triggers re-classification of
	// the whole ontology which might be costly. Therefore, if you plan
	// to query for multiple complex class expressions, it will be more
	// efficient to add the corresponding definitions to the ontology at
	// once before asking any queries to the reasoner.
	reasoner.flush();

	// You can now retrieve subclasses, superclasses, and instances of
	// the query class by using its new name instead.
	reasoner.getSubClasses(newName, true);
	reasoner.getSuperClasses(newName, true);
	reasoner.getInstances(newName, false);

	// After you are done with the query, you should remove the definition
	man.removeAxiom(ont, definition);

	// You can now add new definitions for new queries in the same way

	// After you are done with all queries, do not forget to free the
	// resources occupied by the reasoner
	reasoner.dispose();
}
 
Example 4
Source File: BioChebiGenerator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Create the GCIs for BioChEBI. Add the axioms into the given ontology.
 * 
 * @param ontology
 * @param ignoredClasses
 */
public void expand(OWLOntology ontology, Set<OWLClass> ignoredClasses) {
	final OWLOntologyManager manager = ontology.getOWLOntologyManager();
	final OWLDataFactory factory = manager.getOWLDataFactory();
	
	// scan axioms
	Set<OWLSubClassOfAxiom> axioms = ontology.getAxioms(AxiomType.SUBCLASS_OF, Imports.INCLUDED);
	for (OWLSubClassOfAxiom axiom : axioms) {
		OWLClassExpression superCE = axiom.getSuperClass();
		OWLClassExpression subCE = axiom.getSubClass();
		if (subCE.isAnonymous()) {
			// sub class needs to be an named OWLClass
			continue;
		}

		if (superCE instanceof OWLObjectSomeValuesFrom == false) {
			continue;
		}
		OWLObjectSomeValuesFrom some = (OWLObjectSomeValuesFrom) superCE;

		OWLObjectPropertyExpression expression = some.getProperty();
		if (expression.isAnonymous()) {
			// object property expression needs to be a named OWLObjectProperty 
			continue;
		}

		OWLObjectProperty p = (OWLObjectProperty) expression;
		
		Set<OWLObjectProperty> expansions = expansionMap.get(p);
		if (expansions == null) {
			continue;
		}

		// get content for GCI
		OWLClassExpression y = some.getFiller();
		OWLClass x = subCE.asOWLClass();
		if (ignoredClasses.contains(x)) {
			continue;
		}
		for (OWLObjectProperty createProperty : expansions) {
			OWLClassExpression ce1 = factory.getOWLObjectSomeValuesFrom(createProperty, x);
			OWLClassExpression ce2 = factory.getOWLObjectSomeValuesFrom(createProperty, y);
			OWLEquivalentClassesAxiom eq = factory.getOWLEquivalentClassesAxiom(ce1, ce2);
			manager.addAxiom(ontology, eq);
		}
	}
	
	Set<OWLOntology> imports = ontology.getImports();
	StringBuilder sb = new StringBuilder();
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	sb.append("Generated on ").append(dateFormat.format(new Date())).append(" using the following import chain:");
	for (OWLOntology owlOntology : imports) {
		OWLOntologyID ontologyID = owlOntology.getOntologyID();
		sb.append(" ");
		appendOntologyId(ontologyID, sb);
	}
	addComment(sb.toString(), ontology);
}
 
Example 5
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;
}