org.semanticweb.owlapi.reasoner.Node Java Examples

The following examples show how to use org.semanticweb.owlapi.reasoner.Node. 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: Sim2CommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@CLIMethod("--sim-lcs")
public void simLCS(Opts opts) throws Exception {
	opts.info("C1 C2", "find LCS of two classes");
	loadProperties(opts);
	OWLClass c1 = (OWLClass) this.resolveClass(opts.nextOpt());
	OWLClass c2 = (OWLClass) this.resolveClass(opts.nextOpt());
	OWLPrettyPrinter owlpp = getPrettyPrinter();
	if (owlsim == null) {
		owlsim = getOwlSimFactory().createOwlSim(g.getSourceOntology());
		owlsim.createElementAttributeMapFromOntology();
	}
	owlsim.createElementAttributeMapFromOntology();
	Set<Node<OWLClass>> lcsSet = owlsim.getNamedLowestCommonSubsumers(c1, c2);

	for (Node<OWLClass> lcsNode : lcsSet) {
		System.out.println(owlpp.render(lcsNode.getRepresentativeElement()));
	}

	ScoreAttributeSetPair sap = owlsim.getLowestCommonSubsumerWithIC(c1, c2);
	System.out.println("Score="+sap.score);
	for (OWLClass lcs : sap.attributeClassSet) {
		System.out.println(owlpp.render(lcs));
	}
}
 
Example #2
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Set<Node<OWLClass>> getInferredAttributes(OWLNamedIndividual a) {
	// TODO - refactor this method - in previous versions of Elk it was
	// not possible to ask for the types an instance instantiates, now
	// with Elk 0.4 it is
	if (elementToInferredAttributesMap.containsKey(a))
		return new HashSet<Node<OWLClass>>(elementToInferredAttributesMap.get(a));
	Set<Node<OWLClass>> nodes = new HashSet<Node<OWLClass>>();
	for (OWLClass c : getAttributesForElement(a)) {
		// if nodes contains c, it also contains all subsumers of c
		if (nodes.contains(c)) continue;
		nodes.addAll(getNamedReflexiveSubsumers(c));
		// nodes.addAll(getReasoner().getSuperClasses(c, false).getNodes());
	}
	elementToInferredAttributesMap.put(a, nodes);
	return new HashSet<Node<OWLClass>>(nodes);
}
 
Example #3
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private <T extends OWLNamedObject> T getDeterministicRepresentative(Node<T> n) {
	T rep = null;
	// find lexical maximal entry as representative node
	for (T entry : n) {
		if (rep == null) {
			rep = entry;
		}
		else {
			// use IRI for comparison
			IRI repIRI = rep.getIRI();
			IRI entryIRI = entry.getIRI();
			if (entryIRI.compareTo(repIRI) > 0) {
				rep = entry;
			}
		}
	}
	return rep;
}
 
Example #4
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void precomputeAttributeElementCount() {
	if (attributeElementCount != null)
		return;
	attributeElementCount = new HashMap<OWLClass, Integer>();
	for (OWLEntity e : this.getAllElements()) {
		LOG.info("Adding 1 to all attributes of "+e);
		for (OWLClass dc : getAttributesForElement(e)) {
			for (Node<OWLClass> n : this.getNamedReflexiveSubsumers(dc)) {
				for (OWLClass c : n.getEntities()) {
					if (!attributeElementCount.containsKey(c))
						attributeElementCount.put(c, 1);
					else
						attributeElementCount.put(c, attributeElementCount.get(c)+1);
				}
			}

		}
	}
}
 
Example #5
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Mapping between an attribute (e.g. phenotype class) and the number of
 * instances it classifies
 */
protected void precomputeAttributeElementCount() {
	if (attributeElementCount != null) return;
	attributeElementCount = new HashMap<OWLClass, Integer>();
	// some high level attributes will classify all or most of the ABox;
	// this way may be faster...
	for (OWLNamedIndividual e : this.getAllElements()) {
		LOG.info("Incrementing count all attributes of " + e);
		LOG.info(" DIRECT ATTS: " + getAttributesForElement(e).size());
		for (Node<OWLClass> n : this.getInferredAttributes(e)) {
			for (OWLClass c : n.getEntities()) {
				if (!attributeElementCount.containsKey(c))
					attributeElementCount.put(c, 1);
				else
					attributeElementCount.put(c, attributeElementCount.get(c) + 1);
			}
		}
	}
	LOG.info("Finished precomputing attribute element count");
}
 
Example #6
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public double getAsymmetricElementGraphInformationContentSimilarity(
		OWLNamedIndividual i, OWLNamedIndividual j) throws UnknownOWLClassException {
	// TODO - optimize
	Set<Node<OWLClass>> ci = getNamedCommonSubsumers(i, j);
	Set<Node<OWLClass>> cu = this.getInferredAttributes(j);
	double sumICboth = 0;
	double sumICunion = 0;
	for (Node<OWLClass> c : cu) {
		// TODO - we can avoid doing this twice by using xor in the bitmap
		OWLClass rep = getDeterministicRepresentative(c);
		sumICunion += getInformationContentForAttribute(rep);
		if (ci.contains(c)) {
			sumICboth += getInformationContentForAttribute(rep);
		}

	}
	return sumICboth / sumICunion;
}
 
Example #7
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ScoreAttributeSetPair getSimilarityMaxIC(OWLNamedIndividual i,
		OWLNamedIndividual j) throws UnknownOWLClassException {

	Set<Node<OWLClass>> atts = getNamedCommonSubsumers(i,j);

	ScoreAttributeSetPair best = new ScoreAttributeSetPair(0.0);
	for (Node<OWLClass> n : atts) {
		OWLClass c = getDeterministicRepresentative(n);
		Double ic = getInformationContentForAttribute(c);
		if (Math.abs(ic - best.score) < 0.000001) {
			// tie for best attribute
			best.addAttributeClass(c);
		}
		if (ic > best.score) {
			best = new ScoreAttributeSetPair(ic, c);
		}
	}
	return best;
}
 
Example #8
Source File: SimpleOntology.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Set<ObjectProperty> getAllClassObjectProperties(IRI iri) {
    org.semanticweb.owlapi.model.IRI classIRI = SimpleOntologyValues.owlapiIRI(iri);
    if (owlOntology.containsClassInSignature(classIRI)) {
        OWLClass owlClass = owlManager.getOWLDataFactory().getOWLClass(classIRI);
        Node<OWLClass> equivalentClasses = owlReasoner.getEquivalentClasses(owlClass);
        NodeSet<OWLClass> superClasses = owlReasoner.getSuperClasses(owlClass);
        return owlOntology.objectPropertiesInSignature(Imports.INCLUDED)
                .filter(property -> {
                    Set<OWLObjectPropertyDomainAxiom> domains = owlOntology.axioms(
                            OWLObjectPropertyDomainAxiom.class, OWLObjectPropertyExpression.class, property,
                            Imports.INCLUDED, Navigation.IN_SUB_POSITION).collect(Collectors.toSet());
                    return hasClassAsDomain(domains.stream(), classIRI, equivalentClasses, superClasses)
                            || hasNoDomain(domains.stream());
                })
                .map(SimpleOntologyValues::mobiObjectProperty)
                .collect(Collectors.toSet());
    }
    throw new IllegalArgumentException("Class not found in ontology");
}
 
Example #9
Source File: OwlApiClassExpressionEquivalentClassesQueryTest.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
public OwlApiClassExpressionEquivalentClassesQueryTest(
		final QueryTestManifest<OWLClassExpression, EquivalentEntitiesTestOutput<OWLClass>> manifest) {
	super(manifest,
			new OwlApiReasoningTestDelegate<EquivalentEntitiesTestOutput<OWLClass>>(
					manifest) {

				@Override
				public EquivalentEntitiesTestOutput<OWLClass> getActualOutput()
						throws Exception {
					final Node<OWLClass> equivalent = getReasoner()
							.getEquivalentClasses(
									manifest.getInput().getQuery());
					return new OwlApiEquivalentEntitiesTestOutput(
							equivalent);
				}

				@Override
				public Class<? extends Exception> getInterruptionExceptionClass() {
					return ReasonerInterruptedException.class;
				}

			});
}
 
Example #10
Source File: BigFastOwlSimTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLCS() throws OWLOntologyCreationException, OBOFormatParserException, IOException, UnknownOWLClassException {
	String idspace="mp";
	load(idspace);
	ABoxUtils.makeDefaultIndividuals(getOntology());
	precompute();
	OWLClass c1 = getCls("MP_0003737"); // ossification of pinnae
	OWLClass c2 = getCls("MP_0002895"); // abnormal otolithic membrane morphology
	Set<Node<OWLClass>> cs = owlsim.getNamedCommonSubsumers(c1, c2);
	msg("CS="+cs);
	// note: this test is inherently fragile, if MP changes drastically the test may need recofnigured
	assertTrue(cs.size() > 2);
	Set<Node<OWLClass>> lcs = owlsim.getNamedLowestCommonSubsumers(c1, c2);
	msg("LCS="+lcs);
	assertTrue(cs.size() > lcs.size());
}
 
Example #11
Source File: Sim2CommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String renderAttributes(Set<Node<OWLClass>> atts,
		OWLPrettyPrinter owlpp) {
	List<String> s = new ArrayList<String>();
	for (Node<OWLClass> n : atts) {
		OWLClass c = n.getRepresentativeElement();
		s.add(owlpp.render(c));
	}
	return (StringUtils.join(s, " | "));
}
 
Example #12
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ScoreAttributePair getLowestCommonSubsumerIC(OWLClass a,
		OWLClass b, Double minimumIC) {

	ClassExpressionPair pair = new ClassExpressionPair(a, b);

	// lookup cache to see if this has been calculated already
	if (!this.isNoLookupForLCSCache()) {
		if (lcsICcache.containsKey(pair)) {
			return lcsICcache.get(pair); // don't make a copy, assume unmodified
		}
		ClassExpressionPair pairRev = new ClassExpressionPair(b, a);
		if (lcsICcache.containsKey(pairRev)) {
			return lcsICcache.get(pairRev); // don't make a copy, assume unmodified
		}

		if (this.isLCSCacheFullyPopulated) {
			// entry not found in cache and the cache is fully populated;
			// this means that the IC was below threshold
			return null;
		}
	}
	// TODO: test whether it is more efficient to get redundant common subsumers too,
	// then simply keep the ones with the highest.
	// removing redundant may be better as those deeper in the hierarchy may
	// have the same IC as a parent
	Set<Node<OWLClass>> lcsSet = getNamedLowestCommonSubsumers(a, b);
	return getLowestCommonSubsumerIC(pair, lcsSet, minimumIC);
}
 
Example #13
Source File: ElkReasoner.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
@Override
public Node<OWLDataProperty> getTopDataPropertyNode() {
	LOGGER_.trace("getTopDataPropertyNode()");
	checkInterrupted();
	// TODO Provide implementation
	throw unsupportedOwlApiMethod("getTopDataPropertyNode()");
}
 
Example #14
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<Node<OWLClass>> getNamedSubsumers(OWLNamedIndividual a) {
	Set<Node<OWLClass>> nodes = new HashSet<Node<OWLClass>>();
	// for now we do not use reasoners for the first step (Elk does not support ABoxes)
	for (OWLClass c: this.getAttributesForElement(a)) {
		nodes.addAll(getReasoner().getSuperClasses(c, false).getNodes());
	}
	return nodes;
}
 
Example #15
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<Node<OWLClass>> getNamedReflexiveSubsumers(OWLClassExpression a) {
	if (superclassMap != null && superclassMap.containsKey(a)) {
		return new HashSet<Node<OWLClass>>(superclassMap.get(a));
	}
	Set<Node<OWLClass>> nodes =  getReasoner().getSuperClasses(a, false).getNodes();
	nodes.add(getReasoner().getEquivalentClasses(a));
	if (superclassMap == null) {
		superclassMap = new HashMap<OWLClassExpression,Set<Node<OWLClass>>>();
	}
	superclassMap.put(a, new HashSet<Node<OWLClass>>(nodes));
	return nodes;
}
 
Example #16
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param a
 * @param b
 * @return SimJ of two attribute classes
 */
public float getAttributeJaccardSimilarity(OWLClassExpression a, OWLClassExpression b) {
	Set<Node<OWLClass>> ci = getNamedCommonSubsumers(a,b);
	Set<Node<OWLClass>> cu = getNamedReflexiveSubsumers(a);
	cu.addAll(getNamedReflexiveSubsumers(b));
	return ci.size() / (float)cu.size();
}
 
Example #17
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<Node<OWLClass>> getNamedCommonSubsumers(OWLNamedIndividual a,
		OWLNamedIndividual b) {
	// we don't cache this as we assume it will be called at most once
	Set<Node<OWLClass>> nodes = getInferredAttributes(a);
	nodes.retainAll(getInferredAttributes(b));
	return nodes;
}
 
Example #18
Source File: ReasonerTaxonomyWalker.java    From snomed-owl-toolkit with Apache License 2.0 5 votes vote down vote up
private NodeSet<OWLClass> computeNextNodeSet(final Node<OWLClass> node) {
	final NodeSet<OWLClass> subClasses = reasoner.getSubClasses(node.getRepresentativeElement(), true);

	if (!subClasses.isBottomSingleton()) {
		return subClasses;
	}

	if (nothingProcessed) {
		return EMPTY_NODE_SET;
	} else {
		nothingProcessed = true;
		return subClasses;
	}
}
 
Example #19
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLClass getIndexedClass(Node<OWLClass> n) throws UnknownOWLClassException {
	if (representativeClassMap == null)
		representativeClassMap = new HashMap<Node<OWLClass>, OWLClass>();
	else if (representativeClassMap.containsKey(n))
		return representativeClassMap.get(n);
	for (OWLClass c : n.getEntities()) {
		if (classIndex.containsKey(c)) {
			representativeClassMap.put(n,c);
			return c;				
		}
	}
	throw new UnknownOWLClassException(getDeterministicRepresentative(n));
}
 
Example #20
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Set<Node<OWLClass>> ancsCachedModifiable(OWLClass c) {
	if (superclassMap != null && superclassMap.containsKey(c)) {
		return superclassMap.get(c);
	}
	Set<Node<OWLClass>> a = ancs(c);
	if (superclassMap == null)
		superclassMap = new HashMap<OWLClass,Set<Node<OWLClass>>>();
	superclassMap.put(c, a);
	return a;
}
 
Example #21
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Set<Node<OWLClass>> ancsCachedModifiable(OWLNamedIndividual i) {
	if (inferredTypesMap != null && inferredTypesMap.containsKey(i)) {
		return inferredTypesMap.get(i);
	}
	Set<Node<OWLClass>> a = ancs(i);
	if (inferredTypesMap == null)
		inferredTypesMap = new HashMap<OWLNamedIndividual,Set<Node<OWLClass>>>();
	inferredTypesMap.put(i, a);
	return a;
}
 
Example #22
Source File: GraphReasoner.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Node<OWLObjectPropertyExpression> getInverseObjectProperties(
		OWLObjectPropertyExpression pe)
		throws InconsistentOntologyException, FreshEntitiesException,
		ReasonerInterruptedException, TimeOutException {
	// TODO Auto-generated method stub
	return null;
}
 
Example #23
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void search(Set<OWLClass> atts, Metric metric) {
	Set<Node<OWLClass>> iatts = new HashSet<Node<OWLClass>>();
	for (OWLClass att : atts) {
		iatts.addAll(getNamedReflexiveSubsumers(att));
	}
	for (OWLNamedIndividual j : this.getAllElements()) {
		Set<Node<OWLClass>> jatts = this.getInferredAttributes(j);
		Set<Node<OWLClass>> attsInBoth = new HashSet<Node<OWLClass>>(iatts);
		iatts.retainAll(jatts);
		Set<Node<OWLClass>> attsInEither = new HashSet<Node<OWLClass>>(iatts);
		iatts.addAll(jatts);
		double simj = attsInBoth.size() / attsInEither.size();
		// TODO
	}
}
 
Example #24
Source File: OntologyHelper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private Collection<String> getUrisFromNodeSet(NodeSet<OWLClass> nodeSet) {
	Set<String> uris = new HashSet<>();

	for (Node<OWLClass> node : nodeSet) {
		for (OWLClass expr : node.getEntities()) {
			if (isClassSatisfiable(expr)) {
				uris.add(expr.getIRI().toURI().toString());
			}
		}
	}

	return uris;
}
 
Example #25
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Set<Node<OWLClass>> getNamedUnionSubsumers(OWLNamedIndividual i,
		OWLNamedIndividual j) throws UnknownOWLClassException {
	EWAHCompressedBitmap bmc = ancsBitmapCachedModifiable(i);
	EWAHCompressedBitmap bmd = ancsBitmapCachedModifiable(j);
	EWAHCompressedBitmap cad = bmc.or(bmd);
	Set<Node<OWLClass>> nodes = new HashSet<Node<OWLClass>>();
	for (int ix : cad.toArray()) {
		OWLClassNode node = new OWLClassNode(classArray[ix]);
		nodes.add(node);
	}
	return nodes;
}
 
Example #26
Source File: FastOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Set<Node<OWLClass>> getNamedLowestCommonSubsumers(int cix, int dix) throws UnknownOWLClassException {
	EWAHCompressedBitmap cad = getNamedLowestCommonSubsumersAsBitmap(cix, dix);
	Set<Node<OWLClass>> nodes = new HashSet<Node<OWLClass>>();
	// TODO - optimize this & ensure all elements of an equivalence set are included
	for (int ix : cad.toArray()) {
		OWLClassNode node = new OWLClassNode(classArray[ix]);
		nodes.add(node);
	}
	return nodes;

}
 
Example #27
Source File: SimSpeedTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@Ignore("This test requires an external resource. This can lead to false positive failures.")
public void testLCS() throws OWLOntologyCreationException, OBOFormatParserException, IOException {
	String idspace="mp";
	load(idspace);
	precompute(Method.BITMAP);
	OWLClass c1 = getCls("MP_0003737"); // ossification of pinnae
	OWLClass c2 = getCls("MP_0002895"); // abnormal otolithic membrane morphology
	Set<Node<OWLClass>> cs = this.getNamedCommonSubsumers(c1, c2);
	msg("CS="+cs);
	Set<Node<OWLClass>> lcs = this.getNamedLowestCommonSubsumers(c1, c2, Method.BITMAP);
	msg("LCS="+lcs);
}
 
Example #28
Source File: LCSEnabledSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<Node<OWLClass>> getNamedLowestCommonSubsumers(OWLClassExpression a, OWLClassExpression b) {
	Set<Node<OWLClass>> nodes = getNamedCommonSubsumers(a, b);
	Set<Node<OWLClass>> rNodes = new HashSet<Node<OWLClass>>();
	for (Node<OWLClass> node : nodes) {
		// all ancestors (non-reflexive) of node are redundant
		rNodes.addAll(getReasoner().getSuperClasses(node.getRepresentativeElement(), false).getNodes());
	}
	nodes.removeAll(rNodes);
	return nodes;
}
 
Example #29
Source File: LCSEnabledSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private float getMinAsymmetricJaccardIndex(OWLClassExpression a, OWLClassExpression b) {
	// if a subsumes b, then SimMAJ(a,b) = 1
	// 'rewards' pairs that are distant from eachother
	Set<Node<OWLClass>> ci = getNamedCommonSubsumers(a,b); // a /\ b
	int div = Math.min(getNamedReflexiveSubsumers(a).size(), getNamedReflexiveSubsumers(b).size());
	return ci.size() / (float)div;
}
 
Example #30
Source File: SimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int getNamedCommonSubsumersCount(OWLNamedIndividual a,
		OWLNamedIndividual b) {
	// we don't cache this as we assume it will be called at most once
	Set<Node<OWLClass>> nodes = getInferredAttributes(a);
	nodes.retainAll(getInferredAttributes(b));
	return nodes.size();
}