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

The following examples show how to use org.semanticweb.owlapi.model.OWLClassExpression#isAnonymous() . 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: CycleCheck.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<OWLClass> getAdjacent(OWLClass cls) {
	Set<OWLClass> results = new HashSet<OWLClass>();
	Set<OWLOntology> allOntologies = graph.getAllOntologies();
	for (OWLOntology owlOntology : allOntologies) {
		Set<OWLSubClassOfAxiom> axioms = owlOntology.getSubClassAxiomsForSubClass(cls);
		if (axioms != null && !axioms.isEmpty()) {
			for (OWLSubClassOfAxiom axiom : axioms) {
				OWLClassExpression expression = axiom.getSuperClass();
				if (!expression.isAnonymous()) {
					results.add(expression.asOWLClass());
				}
			}
		}
	}
	if (results.isEmpty()) {
		return Collections.emptyList();
	}
	return new ArrayList<OWLClass>(results);
}
 
Example 2
Source File: AbstractOWLOntologyLoader.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
private void indexEquivalentRelations(OWLClass owlClass) throws OWLOntologyCreationException {
    OWLReasoner reasoner = getOWLReasoner(ontology);

    // get direct children
    addEquivalentTerms(owlClass.getIRI(),
            reasoner.getEquivalentClasses(owlClass).getEntities().stream()
                    .map(OWLNamedObject::getIRI)
                    .collect(Collectors.toSet()));

    Set<String> relatedDescriptions = new HashSet<>();

    for (OWLClassExpression expression : owlClass.getEquivalentClasses(getManager().getOntologies())) {
        if (expression.isAnonymous()) {
            relatedDescriptions.add(manSyntaxRenderer.render(expression));
        }
    }

    if (!relatedDescriptions.isEmpty()) {
        addEquivalentClassDescriptions(owlClass.getIRI(), relatedDescriptions);
    }

}
 
Example 3
Source File: NormalizedOWLQLTbox.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public Set<OWLClass> getUnsatisfiableNamedClasses() {
	if (unsatisfiableClasses == null) {
		unsatisfiableClasses = new HashSet<OWLClass>();
		// unsatisfiabe named classes C are in the negative closure in the form
		// subclass( C, not(C)) 
		for (OWLSubClassOfAxiom ax: getNegativeInclInNegClosure()) {
			OWLClassExpression sub = ax.getSubClass();
			OWLClassExpression sup = ax.getSuperClass();
			if (!sub.isAnonymous()
			&& sup.getClassExpressionType().equals(ClassExpressionType.OBJECT_COMPLEMENT_OF)
			&& ((OWLObjectComplementOf) sup).getOperand().equals(sub)) {
				unsatisfiableClasses.add(sub.asOWLClass());
			}
		}
	}
	return Collections.unmodifiableSet(unsatisfiableClasses);
	//return new HashSet<OWLClass>();
}
 
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: FrameMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Expression makeExpression(OWLClassExpression x) {
	if (x.isAnonymous()) {
		if (x instanceof OWLObjectIntersectionOf) {
			return makeIntersectionOf((OWLObjectIntersectionOf)x);
		}
		else if (x instanceof OWLObjectSomeValuesFrom) {
			return makeSomeValuesFrom((OWLObjectSomeValuesFrom)x);
		}
		else {
			return null;
		}
	}
	else {
		return makeClassStub(x);
	}
}
 
Example 6
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 7
Source File: OWLGraphWrapperEdgesAdvanced.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Classify the an edge and target as a human readable string for further processing.
 * 
 * @param owlGraphEdge edge under consideration
 * @param edgeDirector 
 * @param props properties set
 * @return null, "simplesubclass", "typesubclass", or "identity".
 * @see #addDirectDescendentsToShuntGraph
 * @see #addStepwiseAncestorsToShuntGraph
 */
public String classifyRelationship(OWLGraphEdge owlGraphEdge, OWLObject edgeDirector, Set<? extends OWLPropertyExpression> props){		
	String retval = null;
	
	OWLQuantifiedProperty qp = owlGraphEdge.getSingleQuantifiedProperty();
	if( qp.isSubClassOf() || props.contains(qp.getProperty()) ){
		//OWLObject target = owlGraphEdge.getTarget();
		if( edgeDirector instanceof OWLClass ){
			retval = "simplesubclass";
		}else if( edgeDirector instanceof OWLObjectSomeValuesFrom ){
			OWLObjectSomeValuesFrom some = (OWLObjectSomeValuesFrom)edgeDirector;
			if( props.contains(some.getProperty()) ){
				OWLClassExpression clsexp = some.getFiller();
				if( ! clsexp.isAnonymous()){
					retval = "typesubclass";
				}
			}
		}
	}else if( qp.isIdentity() ){
		retval = "identity";
	}else{
		if (LOG.isDebugEnabled()) {
			LOG.debug("Skipping complex edge: "+owlGraphEdge);
		}
	}
	
	return retval;
}
 
Example 8
Source File: LazyExpressionMaterializingReasoner.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Node<OWLClass> getEquivalentClasses(OWLClassExpression ce)
throws InconsistentOntologyException,
ClassExpressionNotInProfileException, FreshEntitiesException,
ReasonerInterruptedException, TimeOutException {
	if (ce.isAnonymous()) {
		OWLClass c = materializeExpression(ce);
		return getEquivalentClasses(c);
	}
	return getWrappedReasoner().getEquivalentClasses(ce);
}
 
Example 9
Source File: LazyExpressionMaterializingReasoner.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public NodeSet<OWLClass> getSubClasses(OWLClassExpression ce, boolean direct)
throws ReasonerInterruptedException, TimeOutException,
FreshEntitiesException, InconsistentOntologyException,
ClassExpressionNotInProfileException {
	if (ce.isAnonymous()) {
		OWLClass c = materializeExpression(ce);
		return getSubClasses(c, direct);
	}
	return getWrappedReasoner().getSubClasses(ce, direct);
}
 
Example 10
Source File: OntologyHelper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a map of related classes for a particular class.
 * 
 * @param owlClass
 * @return a map of relation type to a list of IRIs for nodes with that
 * relationship.
 */
public Map<String, List<String>> getRestrictions(OWLClass owlClass) {
	RestrictionVisitor visitor = new RestrictionVisitor(Collections.singleton(ontology));
	for (OWLSubClassOfAxiom ax : ontology.getSubClassAxiomsForSubClass(owlClass)) {
		OWLClassExpression superCls = ax.getSuperClass();
		// Ask our superclass to accept a visit from the RestrictionVisitor
		// - if it is an existential restriction then our restriction visitor
		// will answer it - if not our visitor will ignore it
		superCls.accept(visitor);
	}

	Map<String, List<String>> restrictions = new HashMap<>();
	for (OWLObjectSomeValuesFrom val : visitor.getSomeValues()) {
		OWLClassExpression exp = val.getFiller();

		// Get the shortname of the property expression
		String shortForm = null;
		Set<OWLObjectProperty> signatureProps = val.getProperty().getObjectPropertiesInSignature();
		for (OWLObjectProperty sigProp : signatureProps) {
			Collection<String> labels = findLabels(sigProp.getIRI());
			if (labels.size() > 0) {
				shortForm = new ArrayList<String>(labels).get(0);
			}
		}

		if (shortForm != null && !exp.isAnonymous()) {
			IRI iri = exp.asOWLClass().getIRI();

			if (!restrictions.containsKey(shortForm)) {
				restrictions.put(shortForm, new ArrayList<String>());
			}
			restrictions.get(shortForm).add(iri.toString());
		}
	}

	return restrictions;
}
 
Example 11
Source File: OwlApiUtils.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
public static String getIri(OWLClassExpression expression) {
  if (expression.isAnonymous()) {
    return "_:" + hash(expression.toString());
  } else {
    return expression.asOWLClass().getIRI().toString();
  }
}
 
Example 12
Source File: TransformationUtils.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void addLabel(OWLNamedIndividual i, OWLGraphWrapper g, OWLReasoner reasoner) {
	OWLOntology ontology = g.getSourceOntology();
	Set<OWLClass> types = new HashSet<>();
	if (reasoner == null) {
		for (OWLClassExpression x : OwlHelper.getTypes(i, ontology)) {
			if (!x.isAnonymous()) {
				types.add((OWLClass) x);
			}
		}
	}
	else {
		 types = reasoner.getTypes(i, true).getFlattened();
	}
	StringBuffer iLabel = null;
	for (OWLClass type : types) {
		String label = g.getLabel(type);
		if (iLabel == null)
			iLabel = new StringBuffer("a");
		else
			iLabel.append(" & ");
		iLabel.append(" "+label);
	}
	OWLDataFactory df = g.getDataFactory();
	OWLAxiom ax =
			df.getOWLAnnotationAssertionAxiom(df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI()),
			i.getIRI(), 
			df.getOWLLiteral(iLabel.toString()));
	g.getManager().addAxiom(ontology,
			ax);
}
 
Example 13
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 14
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 15
Source File: LegoShuntGraphTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void renderLegoNode(LegoNode node, OWLPrettyPrinter owlpp, OWLGraphWrapper graph, OWLShuntGraph shuntGraph) {
	
	OWLShuntNode shuntNode = new OWLShuntNode(nodeId(node.getId()));
	String label;
	// render node
	if (node.getType() == null) {
		label="?";
	}
	else {
		label = getLabel(node.getType(), owlpp, graph);
	}
	shuntNode.setLabel(label);
	Map<String, Object> metaData = new HashMap<String, Object>();
	
	StringBuilder line = new StringBuilder();
	
	line.append("<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">");
	
	OWLClass activeEntity = node.getActiveEntity();
	if (activeEntity != null) {
		// render activeEntity as box on top of the activity
		String activeEntityLabel = graph.getLabelOrDisplayId(activeEntity);
		line.append("<TR><TD>").append(activeEntityLabel).append("</TD></TR>");
		metaData.put("activeEntity", graph.getIdentifier(activeEntity));
		metaData.put("activeEntityLabel", activeEntityLabel);
	}
	if (node.isCmf()) {
		// composed molecular function
		line.append("<TR><TD BGCOLOR=\""+CMF_COLOR+"\" COLSPAN=\"2\">").append(label).append("</TD>");
		metaData.put("isCmf", Boolean.toString(true));
	}
	else if (node.isMf()) {
		// molecular function
		line.append("<TR><TD BGCOLOR=\""+MF_COLOR+"\" COLSPAN=\"2\">").append(label).append("</TD>");
		metaData.put("isMf", Boolean.toString(true));
	}
	else {
		line.append("<TR><TD COLSPAN=\"2\">").append(label).append("</TD>");
	}
	
	Collection<OWLClassExpression> cellularLocations = node.getCellularLocation();
	if (cellularLocations != null) {
		List<String> locations = new ArrayList<String>();
		for(OWLClassExpression cellularLocation : cellularLocations) {
			String location;
			if (!cellularLocation.isAnonymous()) {
				location = graph.getLabelOrDisplayId(cellularLocation.asOWLClass());
			}
			else {
				location = owlpp.render(cellularLocation);
			}
			line.append("<TD BGCOLOR=\""+CC_COLOR+"\">").append(location).append("</TD>");
			locations.add(location);
		}
		if (locations.isEmpty() == false) {
			metaData.put("cellularLocations", locations);
		}
	}
	line.append("</TR>");
	Collection<OWLClassExpression> unknowns = node.getUnknowns();
	if (unknowns != null && !unknowns.isEmpty()) {
		line.append("<TR><TD COLSPAN=\"2\">");
		line.append("<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">");
		for (OWLClassExpression expression : unknowns) {
			renderAdditionalNodeExpression(line, expression, owlpp, graph);
		}
		line.append("</TABLE>");
		line.append("</TD></TR>");
	}
	line.append("</TABLE>");
	
	metaData.put("html", line.toString());
	
	if (node.isMf() || node.isCmf()) {
		metaData.put("isAnnoton", Boolean.TRUE.toString());
	}
	else {
		metaData.put("isContext", Boolean.TRUE.toString());
	}
	
	
	shuntNode.setMetadata(metaData);
	shuntGraph.addNode(shuntNode);
	
}
 
Example 16
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 17
Source File: LegoDotWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void renderLegoNode(LegoNode node, OWLPrettyPrinter owlpp) throws IOException {
	
	CharSequence label;
	// render node
	if (node.getType() == null) {
		label="?";
	}
	else {
		label = getLabel(node.getType(), owlpp);
	}
	
	StringBuilder line = new StringBuilder(nodeId(node.getId()));
	line.append(" [shape=plaintext,label=");
	line.append('<'); // start HTML markup
	
	line.append("<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">");
	
	OWLClass activeEntity = node.getActiveEntity();
	if (activeEntity != null) {
		// render activeEntity as box on top of the activity 
		line.append("<TR><TD>").append(graph.getLabelOrDisplayId(activeEntity)).append("</TD></TR>");
	}
	if (node.isCmf()) {
		line.append("<TR><TD BGCOLOR=\""+CMF_COLOR+"\" COLSPAN=\"2\">").append(label).append("</TD>");
	}
	else if (node.isMf()) {
		line.append("<TR><TD BGCOLOR=\""+MF_COLOR+"\" COLSPAN=\"2\">").append(label).append("</TD>");
	}
	else {
		line.append("<TR><TD COLSPAN=\"2\">").append(label).append("</TD>");
	}
	
	Collection<OWLClassExpression> cellularLocations = node.getCellularLocation();
	if (cellularLocations != null) {
		for(OWLClassExpression cellularLocation : cellularLocations) {
			String location;
			if (!cellularLocation.isAnonymous()) {
				location = graph.getLabelOrDisplayId(cellularLocation.asOWLClass());
			}
			else {
				location = owlpp.render(cellularLocation);
			}
			line.append("<TD BGCOLOR=\""+CC_COLOR+"\">").append(location).append("</TD>");
		}
	}
	line.append("</TR>");
	Collection<OWLClassExpression> unknowns = node.getUnknowns();
	if (unknowns != null && !unknowns.isEmpty()) {
		line.append("<TR><TD COLSPAN=\"2\">");
		line.append("<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">");
		for (OWLClassExpression expression : unknowns) {
			renderAdditionalNodeExpression(line, expression, owlpp);
		}
		line.append("</TABLE>");
		line.append("</TD></TR>");
	}
	line.append("</TABLE>");
	line.append('>'); // end HTML markup
	line.append("];");
	
	appendLine("");
	if (node.isMf() || node.isCmf()) {
		appendLine("// annoton", 1);
	}
	else {
		appendLine("// context", 1);
	}
	appendLine(line, 1);
	
}
 
Example 18
Source File: GCIUtil.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Generates trivial SVF axioms from existing GCIs
 * 
 * <pre>
 * For each GCI of the form CX SubClassOf R some DX
 *  for each C that is an inferred direct subclass of or equivalent to CX
 *     for each D that is an inferred direct superclass of or equivalent to DX
 *       add an axiom C SubClassOf R some D
 * </pre>
 * @param ontology
 * @param reasoner
 * @return axioms
 */
public static Set<OWLSubClassOfAxiom> getSubClassOfSomeValuesFromAxioms(OWLOntology ontology,
		OWLReasoner reasoner) {

	Set<OWLSubClassOfAxiom> axioms = new HashSet<OWLSubClassOfAxiom>();
	OWLDataFactory df = ontology.getOWLOntologyManager().getOWLDataFactory();
	for (OWLSubClassOfAxiom ax : ontology.getAxioms(AxiomType.SUBCLASS_OF)) {
		OWLClassExpression c = ax.getSubClass();
		if (ax.getSuperClass() instanceof OWLObjectSomeValuesFrom) {
			OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom)ax.getSuperClass();
			OWLObjectPropertyExpression p = svf.getProperty();
			OWLClassExpression filler = svf.getFiller();
			if (filler.isAnonymous() || c.isAnonymous()) {
				if (c.isBottomEntity())
					continue;
				if (filler.isTopEntity())
					continue;
				
				Set<OWLClass> childSet = reasoner.getEquivalentClasses(c).getEntities();
				if (childSet.size() == 0) {
					childSet = reasoner.getSubClasses(c, true).getFlattened();
				}
				for (OWLClass childClass : childSet) {
					if (childClass.isBottomEntity())
						continue;
					Set<OWLClass> childClassSuperClasses = 
							reasoner.getSuperClasses(childClass, false).getFlattened();
					childClassSuperClasses.addAll(reasoner.getEquivalentClasses(childClass).getEntities());
					Set<OWLClass> parentSet = reasoner.getEquivalentClasses(filler).getEntities();
					if (parentSet.size() == 0) {
						parentSet = reasoner.getSuperClasses(filler, true).getFlattened();
					}
					// TODO: remove additional redundancy (R some X) SubClassOf (R some Y)
					// Elk cannot test arbitrary entailments
						for (OWLClass parentClass : parentSet) {
						if (parentClass.isTopEntity())
							continue;
						
						// do not assert C SubClassOf part-of some D, if C SubClassOf D is entailed
						if (childClassSuperClasses.contains(parentClass))
							continue;
						axioms.add(df.getOWLSubClassOfAxiom(childClass, 
								df.getOWLObjectSomeValuesFrom(p, parentClass)));
					}
				}
			}

		}
	}
	LOG.info("Inferred SVFs: "+axioms.size());
	return axioms;
}
 
Example 19
Source File: AbstractOWLOntologyLoader.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
protected  void indexSubclassRelations(OWLClass owlClass) throws OWLOntologyCreationException {

        OWLReasoner reasoner = getOWLReasoner(ontology);

        // use reasoner to check if root
        if (reasoner.getSubClasses(getFactory().getOWLThing(), true).getFlattened().contains(owlClass)) {
            addRootsTerms(owlClass.getIRI());
        }

        if (reasoner.getSubClasses(getFactory().getOWLClass(Namespaces.OBOINOWL.createIRI("ObsoleteClass")), false).getFlattened().contains(owlClass)) {
            addObsoleteTerms(owlClass.getIRI());
        }

        // get direct children

        Set<IRI> ct = removeExcludedIRI(
                reasoner.getSubClasses(owlClass, true).getFlattened().stream()
                        .map(OWLNamedObject::getIRI)
                        .collect(Collectors.toSet()),
                        owlVocabulary);
        if (ct.size() >0) addDirectChildren(owlClass.getIRI(), ct) ;

        // get all children
        Set<IRI> act = removeExcludedIRI(
                reasoner.getSubClasses(owlClass, false).getFlattened().stream()
                        .map(OWLNamedObject::getIRI)
                        .collect(Collectors.toSet()),
                owlVocabulary);
        if (act.size() >0) addAllChildren(owlClass.getIRI(), act);

        // get parents
        Set<IRI> dp =
                removeExcludedIRI(
                        reasoner.getSuperClasses(owlClass, true).getFlattened().stream()
                                .map(OWLNamedObject::getIRI)
                                .collect(Collectors.toSet()),
                        owlVocabulary);
        if (dp.size()>0) addDirectParents(owlClass.getIRI(), dp);

        // get all parents
        Set<IRI> ap =
                removeExcludedIRI(
                        reasoner.getSuperClasses(owlClass, false).getFlattened().stream()
                                .map(OWLNamedObject::getIRI)
                                .collect(Collectors.toSet()),
                        owlVocabulary);
        if (ap.size()>0) addAllParents(owlClass.getIRI(), ap);


        // find direct related terms
        Map<IRI, Collection<IRI>> relatedTerms = new HashMap<>();
        Set<String> relatedDescriptions = new HashSet<>();
        for (OWLClassExpression expression : owlClass.getSuperClasses(getManager().getOntologies())) {
            // only want existential with named class as filler
            if (expression.isAnonymous()) {

                if (expression instanceof OWLObjectSomeValuesFrom) {

                    OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) expression;

                    if (!someValuesFrom.getFiller().isAnonymous()) {
                        IRI propertyIRI = someValuesFrom.getProperty().asOWLObjectProperty().getIRI();
                        IRI relatedTerm = someValuesFrom.getFiller().asOWLClass().getIRI();
                        if (!relatedTerms.containsKey(propertyIRI)) {
                            relatedTerms.put(propertyIRI, new HashSet<>());
                        }
                        relatedTerms.get(propertyIRI).add(relatedTerm);
                    }
                }

                // store stringified form of class description
                relatedDescriptions.add(manSyntaxRenderer.render(expression));
            }
        }
        if (!relatedTerms.isEmpty()) {
            addRelatedTerms(owlClass.getIRI(), relatedTerms );
        }

        if (!relatedDescriptions.isEmpty()) {

            addSuperClassDescriptions(owlClass.getIRI(), relatedDescriptions);

        }
        // todo find transitive closure of related terms
    }
 
Example 20
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);
}