Java Code Examples for org.semanticweb.owlapi.model.OWLClass#equals()

The following examples show how to use org.semanticweb.owlapi.model.OWLClass#equals() . 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: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private EWAHCompressedBitmap ancsProperBitmapCachedModifiable(OWLClass c) {
	if (properSuperclassBitmapMap != null && properSuperclassBitmapMap.containsKey(c)) {
		return properSuperclassBitmapMap.get(c);
	}

	Set<Integer> ancsInts = new HashSet<Integer>();
	for (Node<OWLClass> anc : reasoner.getSuperClasses(c, false)) {
		// TODO - verify robust for non-Rep elements
		OWLClass ac = getDeterministicRepresentative(anc);
		if (ac.equals(thing))
			continue;
		ancsInts.add(classIndex.get(ac));
	}

	EWAHCompressedBitmap bm = convertIntsToBitmap(ancsInts);
	if (properSuperclassBitmapMap == null)
		properSuperclassBitmapMap = new HashMap<OWLClass,EWAHCompressedBitmap>();
	properSuperclassBitmapMap.put(c, bm);
	return bm;		
}
 
Example 4
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
Double getInformationContentForAttribute(int cix) throws UnknownOWLClassException {
	// check if present in cache; if so, use cached value
	if (icClassArray != null && icClassArray[cix] != null) {
		return icClassArray[cix];
	}

	// not cached - retrieve IC using the class
	OWLClass c = classArray[cix];
	Double ic = getInformationContentForAttribute(c);
	if (debugClass != null && c.equals(debugClass)) {
		LOG.info("DEBUG "+c+" IX:"+cix+" IC= "+ic);
	}
	// place results in cache, creating a new cache if none exists
	if (icClassArray == null) {
		icClassArray = new Double[classArray.length];
	}
	icClassArray[cix] = ic;
	return ic;
}
 
Example 5
Source File: SimSpeedTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public EWAHCompressedBitmap ancsProperBitmapCachedModifiable(OWLClass c) {
	if (properSuperclassBitmapMap != null && properSuperclassBitmapMap.containsKey(c)) {
		return properSuperclassBitmapMap.get(c);
	}
	
	Set<Integer> ancsInts = new HashSet<Integer>();
	for (Node<OWLClass> anc : reasoner.getSuperClasses(c, false)) {
		// TODO - verify robust for non-Rep elements
		OWLClass ac = anc.getRepresentativeElement();
		if (ac.equals(thing))
			continue;
		Integer ix = classIndex.get(ac);
		if (ix == null) {
			msg("??"+anc);
		}
		ancsInts.add(ix.intValue());
	}


	//msg(c + " ancs = "+caints.size());
	EWAHCompressedBitmap bm = bm(ancsInts);
	if (properSuperclassBitmapMap == null)
		properSuperclassBitmapMap = new HashMap<OWLClass,EWAHCompressedBitmap>();
	properSuperclassBitmapMap.put(c, bm);
	return bm;		
}
 
Example 6
Source File: LinkMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Check that the given subClass does not already has a matching subClass axiom.
 * 
 * @param subCls
 * @param p
 * @param superCls
 * @return existing axiom or null
 */
private OWLAxiom hasLinks(OWLClass subCls, OWLObjectProperty p, OWLClass superCls) {
	for(OWLOntology o : allOntologies) {
		Set<OWLSubClassOfAxiom> subClsAxioms = o.getSubClassAxiomsForSubClass(subCls);
		for (OWLSubClassOfAxiom subClsAxiom : subClsAxioms) {
			OWLClassExpression ce = subClsAxiom.getSuperClass();
			if (ce instanceof OWLObjectSomeValuesFrom) {
				OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) ce;
				OWLObjectPropertyExpression property = someValuesFrom.getProperty();
				if (p.equals(property)) {
					OWLClassExpression filler = someValuesFrom.getFiller();
					if (superCls.equals(filler)) {
						return subClsAxiom;
					}
				}
			}
		}
	}
	return null;
}
 
Example 7
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param populationClass
 * @param pc1 - sample set class
 * @param pc2 - enriched set class
 * @return enrichment
 * @throws MathException
 */
public List<EnrichmentResult> calculateAllByAllEnrichment(OWLClass populationClass,
		OWLClass pc1,
		OWLClass pc2) throws MathException {
	List<EnrichmentResult> results = new Vector<EnrichmentResult>();
	OWLClass nothing = this.owlDataFactory.getOWLNothing();
	for (OWLClass sampleSetClass : getReasoner().getSubClasses(pc1, false).getFlattened()) {
		if (sampleSetClass.equals(nothing))
			continue;
		LOG.info("sample set class:"+sampleSetClass);
		List<EnrichmentResult> resultsInner = new Vector<EnrichmentResult>();
		for (OWLClass enrichedClass : this.getReasoner().getSubClasses(pc2, false).getFlattened()) {
			if (enrichedClass.equals(nothing))
				continue;
			if (sampleSetClass.equals(enrichedClass) ||
					this.getNamedSubsumers(enrichedClass).contains(sampleSetClass) ||
					this.getNamedSubsumers(sampleSetClass).contains(enrichedClass)) {
				continue;
			}
			EnrichmentResult result = calculatePairwiseEnrichment(populationClass,
					sampleSetClass, enrichedClass);
			addEnrichmentResult(result, resultsInner);			
		}
		//LOG.info("sorting results:"+resultsInner.size());
		Collections.sort(resultsInner);
		//LOG.info("sorted results:"+resultsInner.size());
		results.addAll(resultsInner);
	}
	LOG.info("enrichment completed");
	//Collections.sort(results);
	return results;
}
 
Example 8
Source File: AbstractSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<OWLAxiom> createPropertyView(OWLObjectProperty viewProperty, Set<OWLClass> classes, String labelFormat) {
	OWLAnnotationProperty rdfsLabel = getOWLDataFactory().getRDFSLabel();
	Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>();
	propertyToFormatMap.put(viewProperty, labelFormat);
	for (OWLClass c : classes) {
		if (c.equals(getOWLDataFactory().getOWLNothing())) {
			continue;
		}
		OWLClass vc = getOWLDataFactory().getOWLClass(makeViewClassIRI(c.getIRI(), viewProperty.getIRI(), "-", false, false));
		OWLObjectSomeValuesFrom vx = getOWLDataFactory().getOWLObjectSomeValuesFrom(viewProperty, c);
		newAxioms.add(
				getOWLDataFactory().getOWLEquivalentClassesAxiom(vc, vx)
		);
		newAxioms.add(
				getOWLDataFactory().getOWLDeclarationAxiom(vc)
		);
		String label = getAnyLabel(c);
		String viewLabel = String.format(labelFormat, label);
		newAxioms.add(
				getOWLDataFactory().getOWLAnnotationAssertionAxiom(rdfsLabel, vc.getIRI(), 
						getOWLDataFactory().getOWLLiteral(viewLabel))
		);
		LOG.info("VIEW_CLASS:"+vc+" "+viewLabel+" = P some "+c);
		addViewMapping(c, viewProperty, vc);
		newClasses.add(vc);
	}	
	LOG.info("Num new classes:"+newClasses.size());
	addAxiomsToOutput(newAxioms, true);
	return newAxioms;
}
 
Example 9
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 10
Source File: AbstractOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param populationClass
 * @param pc1
 *          - sample set root class
 * @param pc2
 *          - enriched set root class
 * @return enrichment results
 * @throws MathException
 * @throws UnknownOWLClassException 
 */
public List<EnrichmentResult> calculateAllByAllEnrichment(
		OWLClass populationClass, OWLClass pc1, OWLClass pc2)
				throws MathException, UnknownOWLClassException {
	List<EnrichmentResult> results = new Vector<EnrichmentResult>();
	OWLClass nothing = getSourceOntology().getOWLOntologyManager().getOWLDataFactory().getOWLNothing();
	for (OWLClass sampleSetClass : getReasoner().getSubClasses(pc1, false)
			.getFlattened()) {
		if (sampleSetClass.equals(nothing)) {
			continue;
		}
		int sampleSetSize = getNumElementsForAttribute(sampleSetClass); // todo - optimize
		LOG.info("sample set class:" + sampleSetClass + " size="+sampleSetSize);
		if (sampleSetSize < 2)
			continue;
		if (sampleSetSize > enrichmentConfig.maximumClassSize) {
			LOG.info("  skipping sample set, larger than max size");
			continue;
		}
		List<EnrichmentResult> resultsInner = new Vector<EnrichmentResult>();
		for (OWLClass enrichedClass : this.getReasoner()
				.getSubClasses(pc2, false).getFlattened()) {
			if (enrichedClass.equals(nothing)) continue;
			//LOG.info(" population class:" + enrichedClass + " size="+getNumElementsForAttribute(enrichedClass));
			if (getNumElementsForAttribute(enrichedClass) < 2)
				continue;
			if (getNumElementsForAttribute(enrichedClass) > enrichmentConfig.maximumClassSize) {
				//LOG.info("  skipping test set, larger than max size");
				continue;
			}
			if (sampleSetClass.equals(enrichedClass)
					|| this.getNamedSubsumers(enrichedClass).contains(sampleSetClass)
					|| this.getNamedSubsumers(sampleSetClass).contains(enrichedClass)) {
				continue;
			}
			EnrichmentResult result = calculatePairwiseEnrichment(populationClass,
					sampleSetClass, enrichedClass);
			addEnrichmentResult(result, resultsInner);
		}
		// LOG.info("sorting results:"+resultsInner.size());
		Collections.sort(resultsInner);
		// LOG.info("sorted results:"+resultsInner.size());
		results.addAll(resultsInner);
	}
	LOG.info("enrichment completed");
	// Collections.sort(results);
	results = filterEnrichmentResults(results);
	return results;
}
 
Example 11
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ConsistencyReport performConsistencyChecks(){

		if(graph == null){
			return new ConsistencyReport("The ontology is not set.");
		}

		OWLOntology ont = graph.getSourceOntology();
		reasoner = getReasoner(ont);
		long t1 = System.currentTimeMillis();

		logInfo("Consistency check started............");
		boolean consistent = reasoner.isConsistent();

		logInfo("Is the ontology consistent ....................." + consistent + ", " + (System.currentTimeMillis()-t1)/100);

		List<String> errors = new ArrayList<String>();
		Set<OWLEntity> unsatisfiable = new HashSet<OWLEntity>();
		if(!consistent){
			errors.add("The ontology '" + graph.getOntologyId() + " ' is not consistent");
		}

		// We can easily get a list of unsatisfiable classes.  (A class is unsatisfiable if it
		// can't possibly have any instances).  Note that the getunsatisfiableClasses method
		// is really just a convenience method for obtaining the classes that are equivalent
		// to owl:Nothing.
		OWLClass nothing = graph.getDataFactory().getOWLNothing();
		Node<OWLClass> unsatisfiableClasses = reasoner.getUnsatisfiableClasses();
		if (unsatisfiableClasses.getSize() > 0) {
			for(OWLClass cls : unsatisfiableClasses.getEntities()) {
				logInfo("unsat: "+cls.getIRI());
				if (cls.equals(nothing)) {
					// nothing to see here, move along
					continue;
				}
				StringBuilder sb = new StringBuilder();
				sb.append("Unsatisfiable: ").append(graph.getIdentifier(cls));
				String lbl = graph.getLabel(cls);
				if (lbl != null) {
					sb.append(" '").append(lbl).append("'");
				}
				errors.add(sb.toString());
				unsatisfiable.add(cls);
			}
		}


		return new ConsistencyReport(errors, unsatisfiable);

	}
 
Example 12
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 13
Source File: LinkMaker.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Start search for matches of the given patterns. Annotate new axioms with
 * the given source annotation. Furthermore create a new axiom for existing
 * annotations, if they do not have a matching source annotation.<br>
 * <br>
 * This method does not modify the ontology, it only returns the axioms. 
 * 
 * @param patterns
 * @param sourceAnnotation
 * @return result
 */
public LinkMakerResult makeLinks(List<LinkPattern> patterns, OWLAnnotation sourceAnnotation, boolean updateExisting) {
	final Set<OWLAxiom> resultAxioms = new HashSet<OWLAxiom>();
	final Set<OWLAxiom> existingAxioms = new HashSet<OWLAxiom>();
	final Set<OWLAxiom> modified = new HashSet<OWLAxiom>();
	for (LinkPattern linkPattern : patterns) {
		for (OWLClass currentSubClass : getRelevantClasses(linkPattern.subGenus)) {
			OWLClass differentiaCls = hasMatchingIntersection(currentSubClass, linkPattern.subGenus, linkPattern.differentiaRelation);
			if (differentiaCls != null) {
				// found a matching class, now search for the corresponding one.
				for(OWLClass currentSuperClass : getRelevantClasses(linkPattern.superGenus)) {
					OWLClass potentialMatch = hasMatchingIntersection(currentSuperClass, linkPattern.superGenus, linkPattern.differentiaRelation);
					if (differentiaCls.equals(potentialMatch)) {
						// the class has the required xp
						// now check that the link does not already exist
						OWLAxiom existing = hasLinks(currentSubClass, linkPattern.newRelation, currentSuperClass);
						OWLObjectSomeValuesFrom someValuesFrom = f.getOWLObjectSomeValuesFrom(linkPattern.newRelation, currentSuperClass);
						if (existing == null) {
							OWLSubClassOfAxiom a = f.getOWLSubClassOfAxiom(currentSubClass, someValuesFrom, Collections.singleton(sourceAnnotation));
							resultAxioms.add(a);
						}
						else {
							if (updateExisting) {
								existingAxioms.add(existing);
								Set<OWLAnnotation> existingAnnotations = existing.getAnnotations();
								if (existingAnnotations.contains(sourceAnnotation)) {
									modified.add(existing);
								}
								else {
									Set<OWLAnnotation> mergedAnnotations = new HashSet<OWLAnnotation>();
									mergedAnnotations.add(sourceAnnotation);
									mergedAnnotations.addAll(existingAnnotations);
									OWLSubClassOfAxiom mod = f.getOWLSubClassOfAxiom(currentSubClass, someValuesFrom, mergedAnnotations);
									modified.add(mod);
								}
							}
						}
					}
				}
			}
		}
	}
	return new LinkMakerResult(resultAxioms, existingAxioms, modified);
}
 
Example 14
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;
}