Java Code Examples for org.semanticweb.owlapi.model.OWLClassExpression#asOWLClass()

The following examples show how to use org.semanticweb.owlapi.model.OWLClassExpression#asOWLClass() . 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: ChEBIParser.java    From act with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Print the class hierarchy from this class down, assuming this class is at
 * the given level. Makes no attempt to deal sensibly with multiple
 * inheritance.
 */
private void readSubtree(OWLClass clazz, HashMap<OWLClass, OWLClass> parents, int level, boolean doPrint) throws OWLException {
  if (doPrint) {
    for (int i = 0; i < level * INDENT; i++) {
      err.print(" ");
    }
    err.println(labelFor(clazz, ontology));
  }
  /* Find the children and recurse */
  for (OWLClassExpression c : clazz.getSubClasses(ontology)) {
    OWLClass child = c.asOWLClass();
    if (!child.equals(clazz)) {
      parents.put(child, clazz); // install parents in map
      readSubtree(child, parents, level + 1, doPrint); // recurse
    }
  }
}
 
Example 2
Source File: ChEBIParser.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private OWLClass recurseToSubtreeRoot(OWLClass curr_root, String subtree) throws OWLException {
  String label = labelFor(curr_root, ontology);
  if (subtree.equals(label))
    return curr_root;

  /* Else find the children and recurse */
  OWLClass descendant = null;
  for (OWLClassExpression c : curr_root.getSubClasses(ontology)) {
    OWLClass child = c.asOWLClass();
    if (!child.equals(curr_root)) {
      descendant = recurseToSubtreeRoot(child, subtree);
      if (descendant != null)
        break;
    }
  }

  return descendant;
}
 
Example 3
Source File: DanglingReferenceCheck.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void handleIntersection(List<CheckWarning> warnings, Set<OWLOntology> allOntologies,
		OWLEquivalentClassesAxiom axiom, OWLObjectIntersectionOf intersection, OWLPrettyPrinter pp) 
{
	for(OWLClassExpression operand : intersection.getOperandsAsList()) {
		OWLClass operandCls = null;
		if (!operand.isAnonymous()) {
			operandCls = operand.asOWLClass();
		}
		else if (operand instanceof OWLObjectSomeValuesFrom) {
			OWLObjectSomeValuesFrom ristriction = (OWLObjectSomeValuesFrom) operand;
			OWLClassExpression filler = ristriction.getFiller();
			if (!filler.isAnonymous()) {
				operandCls = filler.asOWLClass();
			}
		}
		else {
			// not translatable to OBO
			handleGeneric(warnings, allOntologies, axiom, operand, pp);
		}
		if (operandCls != null && isDangling(operandCls, allOntologies)) {
			final IRI iri = operandCls.getIRI();
			String message = "Dangling reference "+iri+" in INTERSECTION_OF axiom: "+pp.render(axiom);
			warnings.add(new CheckWarning(getID(), message , isFatal(), iri, OboFormatTag.TAG_INTERSECTION_OF.getTag()));
		}
	}
}
 
Example 4
Source File: DanglingReferenceCheck.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void handleUnionOf(List<CheckWarning> warnings, Set<OWLOntology> allOntologies, 
		OWLEquivalentClassesAxiom axiom, OWLObjectUnionOf union, OWLPrettyPrinter pp) 
{
	List<OWLClassExpression> operands = union.getOperandsAsList();
	for(OWLClassExpression operand : operands) {
		if (!operand.isAnonymous()) {
			OWLClass operandCls = operand.asOWLClass();
			if (isDangling(operandCls, allOntologies)) {
				final IRI iri = operandCls.getIRI();
				String message = "Dangling reference "+iri+" in UNION_OF axiom: "+pp.render(axiom);
				warnings.add(new CheckWarning(getID(), message , isFatal(), iri, OboFormatTag.TAG_UNION_OF.getTag()));
			}
		}
		else {
			// not translatable to OBO
			handleGeneric(warnings, allOntologies, axiom, operand, pp);
		}
	}
}
 
Example 5
Source File: ModelAnnotationSolrDocumentLoader.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLClass findFirstType(OWLNamedIndividual relevant) {
	Set<OWLClassAssertionAxiom> axioms = model.getClassAssertionAxioms(relevant);
	for (OWLClassAssertionAxiom axiom : axioms) {
		OWLClassExpression ce = axiom.getClassExpression();
		if (ce.isAnonymous() == false) {
			return ce.asOWLClass();
		}
	}
	return null;
}
 
Example 6
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Update the set of redundant axioms for the given {@link OWLClass} cls.
 * 
 * @param cls
 * @param ontology
 * @param redundantAxioms
 * @param reasoner
 * @param dataFactory
 */
protected static void updateRedundant(OWLClass cls, OWLOntology ontology, Set<OWLAxiom> redundantAxioms, 
		OWLReasoner reasoner, OWLDataFactory dataFactory)
{
	final OWLClass owlThing = dataFactory.getOWLThing();
	
	// get all direct super classes
	final Set<OWLClass> direct = reasoner.getSuperClasses(cls, true).getFlattened();
	direct.remove(owlThing);

	// get all super classes (includes direct ones)
	final Set<OWLClass> indirect = reasoner.getSuperClasses(cls, false).getFlattened();
	indirect.remove(owlThing);

	// remove direct super classes from all -> redundant super classes
	indirect.removeAll(direct);
	// rename
	final Set<OWLClass> redundant = indirect;

	// filter
	// subclass of axioms, which have a super class in the redundant set
	Set<OWLSubClassOfAxiom> axioms = ontology.getSubClassAxiomsForSubClass(cls);
	for (OWLSubClassOfAxiom subClassOfAxiom : axioms) {
		OWLClassExpression ce = subClassOfAxiom.getSuperClass();
		if (!ce.isAnonymous()) {
			OWLClass superClass = ce.asOWLClass();
			if (redundant.contains(superClass)) {
				redundantAxioms.add(subClassOfAxiom);
			}
		}
	}
}
 
Example 7
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check all classes for potential redundant subClass axioms of type:
 * <pre>
 *   A SubClassOf R some B
 *    and
 *   A SubClassOf B
 * </pre>
 * 
 * @return list of axiom pairs
 */
public List<PotentialRedundant> checkPotentialRedundantSubClassAxioms() {
	List<PotentialRedundant> result = new ArrayList<PotentialRedundant>();
	for(OWLClass cls : graph.getAllOWLClasses()) {
		Set<OWLSubClassOfAxiom> axioms = graph.getAllOWLSubClassOfAxiomsForSubClass(cls);
		if (axioms.size() > 1) {
			// only check sets with more than one axiom
			for (OWLSubClassOfAxiom main : axioms) {
				OWLClassExpression mainSuperClassCE = main.getSuperClass();
				if (mainSuperClassCE.isAnonymous()) {
					continue;
				}
				OWLClass mainSuperClass = mainSuperClassCE.asOWLClass();
				for (OWLSubClassOfAxiom current : axioms) {
					if (main == current) {
						continue;
					}
					OWLClassExpression currentSuperClass = current.getSuperClass();
					if (currentSuperClass.isAnonymous() && currentSuperClass instanceof OWLObjectSomeValuesFrom) {
						OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) currentSuperClass;
						final OWLClassExpression filler = someValuesFrom.getFiller();
						if (mainSuperClass.equals(someValuesFrom.getFiller())) {
							final OWLObjectPropertyExpression property = someValuesFrom.getProperty();
							final OWLClassExpression subClass = current.getSubClass();
							final PotentialRedundant redundant = new PotentialRedundant(main, current, subClass.asOWLClass(), property.asOWLObjectProperty(), filler.asOWLClass());
							result.add(redundant);
						}
					}
				}
			}
		}
	}
	
	if (!result.isEmpty()) {
		return result;
	}
	return null;
}
 
Example 8
Source File: EliminateEJVar.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * return named classes nc such that c \subseteq nc
 * @param c
 * @return
 */
protected Set<URI> getNamedSuperclassOrSelf(OWLClassExpression c) {
	Set<OWLClassExpression> subs = taxo.getAllSubsumers(c);
	Set<URI> ret = new HashSet<URI>();
	for (OWLClassExpression ex: subs) {
		if (!ex.isAnonymous()) {
			OWLClass cl = ex.asOWLClass();
			ret.add(cl.getIRI().toURI());
		}
	}
	return ret;
}
 
Example 9
Source File: OntologyLoader.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Set<OWLClass> getChildClass(OWLClass cls) {
  Set<OWLClass> listOfClasses = new HashSet<>();
  for (OWLSubClassOfAxiom axiom : ontology.getSubClassAxiomsForSuperClass(cls)) {
    OWLClassExpression expression = axiom.getSubClass();
    if (!expression.isAnonymous()) {
      OWLClass asOWLClass = expression.asOWLClass();
      listOfClasses.add(asOWLClass);
    }
  }
  return listOfClasses;
}
 
Example 10
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 11
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Check the list of axioms for potential redundant subClass axioms of type:
 * <pre>
 *   A SubClassOf R some B
 *    and
 *   A SubClassOf B
 * </pre>
 * 
 * @param inferredAxioms
 * @return list of axiom pairs
 */
public List<PotentialRedundant> checkPotentialRedundantSubClassAxioms(Collection<? extends OWLAxiom> inferredAxioms) {
	List<PotentialRedundant> result = new ArrayList<PotentialRedundant>();
	for(OWLAxiom axiom : inferredAxioms) {
		if (axiom instanceof OWLSubClassOfAxiom) {
			OWLSubClassOfAxiom main = (OWLSubClassOfAxiom) axiom;
			OWLClassExpression mainSuperClassCE = main.getSuperClass();
			if (mainSuperClassCE.isAnonymous()) {
				continue;
			}
			OWLClassExpression mainSubClassCE = main.getSubClass();
			if (mainSubClassCE.isAnonymous()) {
				continue;
			}
			OWLClass mainSuperClass = mainSuperClassCE.asOWLClass();
			OWLClass mainSubClass = mainSubClassCE.asOWLClass();
			Set<OWLSubClassOfAxiom> subClassAxioms = graph.getAllOWLSubClassOfAxiomsForSubClass(mainSubClass);
			if (subClassAxioms != null && subClassAxioms.size() > 1) {
				for (OWLSubClassOfAxiom current : subClassAxioms) {
					if (main == current) {
						continue;
					}
					OWLClassExpression currentSuperClass = current.getSuperClass();
					if (currentSuperClass.isAnonymous() && currentSuperClass instanceof OWLObjectSomeValuesFrom) {
						OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) currentSuperClass;
						final OWLClassExpression filler = someValuesFrom.getFiller();
						if (mainSuperClass.equals(filler)) {
							final OWLObjectPropertyExpression property = someValuesFrom.getProperty();
							final OWLClassExpression subClass = current.getSubClass();
							result.add(new PotentialRedundant(main, current, subClass.asOWLClass(), property.asOWLObjectProperty(), filler.asOWLClass()));
						}
					}
				}
			}
		}
	}
	if (!result.isEmpty()) {
		return result;
	}
	return null;
}
 
Example 12
Source File: LinkMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Search for the first class with a matching equivalent class definition. 
 * 
 * @param c
 * @param genus
 * @param relation
 * @return match or null
 */
private OWLClass hasMatchingIntersection(OWLClass c, OWLClass genus, OWLObjectProperty relation) {
	for(OWLOntology o : allOntologies) {
		Set<OWLEquivalentClassesAxiom> eqAxioms = o.getEquivalentClassesAxioms(c);
		for (OWLEquivalentClassesAxiom eqAxiom : eqAxioms) {
			Set<OWLClassExpression> expressions = eqAxiom.getClassExpressionsMinus(c);
			for (OWLClassExpression expression : expressions) {
				if (expression instanceof OWLObjectIntersectionOf) {
					OWLObjectIntersectionOf intersection = (OWLObjectIntersectionOf) expression;
					OWLClass differentiaCls = null;
					boolean matchesGenus = false;
					boolean matchesRelation = false;
					Set<OWLClassExpression> operands = intersection.getOperands();
					if (operands.size() == 2) {
						for (OWLClassExpression operand : operands) {
							if (operand.isAnonymous() == false) {
								OWLClass currentGenus = operand.asOWLClass();
								if (genus.equals(currentGenus)) {
									matchesGenus = true;
								}
							}
							else if (operand instanceof OWLObjectSomeValuesFrom) {
								OWLObjectSomeValuesFrom differentia = (OWLObjectSomeValuesFrom) operand;
								if (relation.equals(differentia.getProperty())) {
									matchesRelation = true;
									OWLClassExpression filler = differentia.getFiller();
									if (!filler.isAnonymous() && !filler.isOWLNothing() && !filler.isOWLThing()) {
										differentiaCls = filler.asOWLClass();
									}
								}
							}
						}
						if (matchesGenus && matchesRelation ) {
							 return differentiaCls;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 13
Source File: OWLGraphWrapperEdgesAdvanced.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
	 * Add a set of edges, as ancestors to x in OWLShuntGraph g.
	 * This is reflexive.
	 *
	 * @param x
	 * @param g
	 * @param rel_ids
	 * @return the modified OWLShuntGraph
	 */
	public OWLShuntGraph addStepwiseAncestorsToShuntGraph(OWLObject x, OWLShuntGraph g, List<String> rel_ids) {

		// Add this node, our seed.
		String topicID = getIdentifier(x);
		String topicLabel = getLabel(x);
		OWLShuntNode tn = new OWLShuntNode(topicID, topicLabel);
		g.addNode(tn);

		// NEW VERSION
		Set<OWLObjectProperty> props = relationshipIDsToPropertySet(rel_ids);
		for (OWLGraphEdge e : getOutgoingEdges(x, props)) {
			OWLObject target = e.getTarget();
			String rel = classifyRelationship(e, target, props);

			if( rel != null ){

				// Placeholders.
				String objectID = null;
				String objectLabel = null;
				String elabel = null;
				
				if( rel == "simplesubclass" ){
					objectID = getIdentifier(target);
					objectLabel = getLabelOrDisplayId(target);
					elabel = getEdgeLabel(e);
				}else if( rel == "typesubclass" ){
					OWLObjectSomeValuesFrom some = (OWLObjectSomeValuesFrom)target;
					OWLClassExpression clsexp = some.getFiller();
					OWLClass cls = clsexp.asOWLClass();
					objectID = getIdentifier(cls);
					objectLabel = getLabelOrDisplayId(cls);
					elabel = getEdgeLabel(e);
				}
				
				// Only add when subject, object, and relation are properly defined.
				if(	elabel != null &&
					topicID != null && ! topicID.equals("") &&
					objectID != null &&	! objectID.equals("") ){
	
					// Add node.
					OWLShuntNode sn = new OWLShuntNode(objectID, objectLabel);
					boolean wuzAdded = g.addNode(sn);

					// Recur on node if it already wasn't there.
					if( wuzAdded ){
						addStepwiseAncestorsToShuntGraph(target, g, rel_ids);
					}
				
					//Add edge 
					OWLShuntEdge se = new OWLShuntEdge(topicID, objectID, elabel);
					g.addEdge(se);
				}
			}
		}

// ORIGINAL VERSION
//		// Next, get all of the named ancestors and add them to our shunt graph.
//		// We need some traversal code going up!
//		for (OWLGraphEdge e : getOutgoingEdges(x)) {
//			OWLObject t = e.getTarget();
//			if (t instanceof OWLNamedObject){				
//
//				// Figure out object.
//				String objectID = getIdentifier(t);
//				String objectLabel = getLabel(t);
//
//				// Edge.
//				String elabel = getEdgeLabel(e);
//				
//				// Only add when subject, object, and relation are properly defined.
//				if( elabel != null &&
//					topicID != null && ! topicID.equals("") &&
//					objectID != null &&	! objectID.equals("") ){
//				
//					// Add node.
//					OWLShuntNode sn = new OWLShuntNode(objectID, objectLabel);
//					boolean wuzAdded = g.addNode(sn);
//
//					// Recur on node if it already wasn't there.
//					if( wuzAdded ){
//						addStepwiseAncestorsToShuntGraph(t, g);
//					}
//				
//					//Add edge 
//					OWLShuntEdge se = new OWLShuntEdge(topicID, objectID, elabel);
//					g.addEdge(se);
//				}
//			}
//		}
		
		return g;
	}