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

The following examples show how to use org.semanticweb.owlapi.model.OWLClassExpression#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: DescriptionTreeSimilarity.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLClassExpression makeUnionWithReduction(OWLClassExpression xa, OWLClassExpression xb) {
	if (xa.equals(xb))
		return xa;
	OWLClassExpression rx = makeUnionUsingReflexiveProperty(xa,xb);
		
	if (rx == null)
		rx = makeUnionUsingReflexiveProperty(xb,xa);
	if (rx == null) {
		rx = makeUnionBasic(xa,xb);
	}
	return rx;		
}
 
Example 2
Source File: DescriptionTreeSimilarity.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * combines two class expressions to a UnionOf expression.
 * if both are identical, returns the expression.
 * does not attempt to reduce the union expression
 * 
 * @param xa
 * @param xb
 * @return class expression
 */
private OWLClassExpression makeUnionBasic(OWLClassExpression xa, OWLClassExpression xb) {
	if (xa.equals(xb)) {
		return xa;
	}
	else {
		Set<OWLClassExpression> args = new HashSet<OWLClassExpression>();
		args.add(xa);
		args.add(xb);
		return graph.getDataFactory().getOWLObjectUnionOf(args);
	}
}
 
Example 3
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * gets the LCS. If multiple named LCSs exist in the ontology, then
 * a class expression is generated from the filtered intersection
 * 
 * @param a
 * @param b
 * @return OWLClassExpression
 */
public OWLClassExpression getLowestCommonSubsumer(OWLClassExpression a, OWLClassExpression b) {
	if (a.equals(b)) {
		return a;
	}
	if (a instanceof OWLClass && b instanceof OWLClass) {
		if (getReasoner().getSuperClasses(a, false).getFlattened().contains(b)) {
			return b;
		}
		if (getReasoner().getSuperClasses(b, false).getFlattened().contains(a)) {
			return a;
		}
	}

	// returns the set of named LCSs that already exist
	Set<Node<OWLClass>> ccs = getNamedLowestCommonSubsumers(a,b);

	// single LCS - return this directly
	if (ccs.size() == 1) {
		return ccs.iterator().next().getRepresentativeElement();
	}

	// make a class expression from the filtered intersection of LCSs

	Set<OWLClass> ops = new HashSet<OWLClass>();
	Set<OWLClass> skips = new HashSet<OWLClass>();
	OWLClass best = null;
	double bestIC = 0.0;
	for (Node<OWLClass> n : ccs) {
		OWLClass c = n.getRepresentativeElement();
		// allows custom filtering; e.g. upper-level classes
		// note: should be removed at view stage			
		if (this.getVerbotenClasses().contains(c))
			continue;
		// TODO: custom filtering
		boolean skip = false;
		Double ic = getInformationContentForAttribute(c);
		if (ic == null) {
			// if the attributes classes have not been filtered, then null values
			// (ie classes with no instances) are possible
			continue;
		}
		if (ic > bestIC) {
			bestIC = ic;
			best = c;
		}
		if (ic < 2.5) { // TODO: configure
			//LOG.info("SKIPPING: "+c+" IC="+ic);
			continue;
		}
		if (this.getNamedReflexiveSubsumers(c).size() < 2) {
			LOG.info("SKIPPING: "+c+" no parents");
			// non-grouping attribute
			continue;
		}
		// don't make intersections of similar elements
		for (Node<OWLClass> n2 : ccs) {
			OWLClass c2 = n2.getRepresentativeElement();
			double ic2 = getInformationContentForAttribute(c2); 
			// prioritize the one with highest IC
			if (ic < ic2 || (ic==ic2 && c.compareTo(c2) > 0)) {
				// TODO: configure simj thresh
				if (this.getAttributeJaccardSimilarity(c, c2) > 0.75) {
					LOG.info("SKIPPING: "+c+" too similar to "+n2);
					skip = true;
				}
			}
		}
		if (skip)
			continue;
		// not filtered
		ops.add(c);
	}

	if (ops.size() == 1) {
		return ops.iterator().next();
	}
	if (ops.size() == 0) {
		// none pass: choose the best representative of the intersection
		return best;
	}
	return owlDataFactory.getOWLObjectIntersectionOf(ops);
}
 
Example 4
Source File: DescriptionTreeSimilarity.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * makes a reduced union expression.
 * 
 * Uses the following two reduction rules:
 * 
 * (r1 some X) U (r2 some Y) ==> lcs(r1,r2) some MakeUnionOf(X,Y)
 * (r1 some X) U X ==> reflexive-version-of-r1 some X
 * 
 * TODO: test for (r some r some X) u (r some X) cases. needs to be done over final expression.
 *  
 * if a reduced form cannot be made, returns null
 * 
 * @param xa
 * @param xb
 * @return class expression
 */
private OWLClassExpression makeUnionUsingReflexiveProperty(OWLClassExpression xa, OWLClassExpression xb) {
	LOG.info("testing if there is a more compact union expression for "+xa+" ++ "+xb);
	OWLDataFactory df = graph.getDataFactory();
	if (xa instanceof OWLQuantifiedRestriction) {
		// TODO - check before casting
		OWLObjectProperty prop = (OWLObjectProperty) ((OWLQuantifiedRestriction) xa).getProperty();
		OWLClassExpression xaRest = (OWLClassExpression) ((OWLQuantifiedRestriction)xa).getFiller();
		if (xb instanceof OWLQuantifiedRestriction) {
			OWLObjectPropertyExpression p2 =
				propertySubsumer(prop, 
					((OWLQuantifiedObjectRestriction) xb).getProperty());
			
			if (p2 != null) {
				OWLClassExpression xbRest = (OWLClassExpression) ((OWLQuantifiedRestriction)xb).getFiller();
				OWLClassExpression x = makeUnionWithReduction(xaRest,xbRest);
				// todo - mixing some and all
				if (xa instanceof OWLObjectSomeValuesFrom)
					return df.getOWLObjectSomeValuesFrom(p2,x);
				else if (xa instanceof OWLObjectAllValuesFrom)
					return df.getOWLObjectAllValuesFrom(p2, x);
			}
		}
		LOG.info("  test: "+xaRest+" == "+xb);
		

		if (xaRest.equals(xb)) {
			LOG.info("     TRUE: "+xaRest+" == "+xb);

			OWLObjectProperty rprop = null;
			if (graph.getIsReflexive(prop)) {
				rprop = prop;
			}
			if (forceReflexivePropertyCreation) {
				OWLOntologyManager manager = graph.getManager();
				OWLOntology ont = graph.getSourceOntology();
				rprop = 
					df.getOWLObjectProperty(IRI.create(prop.getIRI().toString()+"_reflexive"));
				manager.applyChange(new AddAxiom(ont, df.getOWLSubObjectPropertyOfAxiom(prop, rprop)));
				manager.applyChange(new AddAxiom(ont, df.getOWLTransitiveObjectPropertyAxiom(rprop)));
				LOG.info("  reflexive prop:"+rprop);

			}
			if (rprop != null) {
				if (xa instanceof OWLObjectSomeValuesFrom)
					return df.getOWLObjectSomeValuesFrom(rprop,xb);
				else if (xa instanceof OWLObjectAllValuesFrom)
					return df.getOWLObjectAllValuesFrom(rprop, xb);
			}

		}
	}
	return null;
}
 
Example 5
Source File: ReasonerDiff.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static Set<OWLAxiom> getInferences(OWLGraphWrapper graph, String reasonerName) throws OWLException {
	
	graph.mergeImportClosure();
	InferenceBuilder builder = new InferenceBuilder(graph, reasonerName);
	try {
		Set<OWLAxiom> inferredAxioms = new HashSet<OWLAxiom>();

		OWLOntology ontology = graph.getSourceOntology();
		OWLDataFactory dataFactory = ontology.getOWLOntologyManager().getOWLDataFactory();

		OWLReasoner reasoner = builder.getReasoner(ontology);
		for (OWLClass cls : ontology.getClassesInSignature()) {
			NodeSet<OWLClass> scs = reasoner.getSuperClasses(cls, true);
			for (Node<OWLClass> scSet : scs) {
				for (OWLClass sc : scSet) {
					if (sc.isOWLThing()) {
						continue; // do not report subclasses of owl:Thing
					}
					// we do not want to report inferred subclass links
					// if they are already asserted in the ontology
					boolean isAsserted = false;
					for (OWLClassExpression asc : OwlHelper.getSuperClasses(cls, ontology)) {
						if (asc.equals(sc)) {
							// we don't want to report this
							isAsserted = true;
						}
					}
					// include any inferred axiom that is NOT already asserted in the ontology
					if (!isAsserted) {						
						inferredAxioms.add(dataFactory.getOWLSubClassOfAxiom(cls, sc));
					}
				}
			}
		}
		return inferredAxioms;
	}
	finally {
		builder.dispose();
	}
	
}
 
Example 6
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Check the ontology for an existing subClassOf axiom with the given sub- and superclass. 
 * This search ignores axiom annotations (i.e. is_inferred=true).
 * 
 * WARNING: Do not use {@link OWLOntology#containsAxiomIgnoreAnnotations(OWLAxiom)}
 * In the current OWL-API, this seems to be very very slow.
 * 
 * @param subClass
 * @param superClass
 * @param ontology
 * @return true, if there is an axiom for this subClass statement.
 */
protected boolean hasAssertedSubClassAxiom(OWLClass subClass, OWLClassExpression superClass, OWLOntology ontology) {
	Set<OWLSubClassOfAxiom> existing = ontology.getSubClassAxiomsForSubClass(subClass);
	if (existing != null) {
		for (OWLSubClassOfAxiom sca : existing) {
			if (superClass.equals(sca.getSuperClass())) {
				return true;
			}
		}
	}
	return false;
}