Java源码示例:org.semanticweb.owlapi.reasoner.Node

示例1
@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());
}
 
示例2
@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");
}
 
示例3
/**
 * 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");
}
 
示例4
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);
}
 
示例5
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;
				}

			});
}
 
示例6
@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));
	}
}
 
示例7
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);
				}
			}

		}
	}
}
 
示例8
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;
}
 
示例9
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;
}
 
示例10
@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;
}
 
示例11
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;
	}
}
 
示例12
private boolean isNodeProcessed(final Node<OWLClass> node) {
	for (final OWLClass owlClass : node) {
		if (!OntologyHelper.isConceptClass(owlClass)) {
			continue;
		}

		final long storageKey = OntologyHelper.getConceptId(owlClass);
		if (!processedConceptIds.contains(storageKey)) {
			return false;
		}
	}

	return true;
}
 
示例13
private Collection<String> getUrisFromNodeSet(NodeSet<OWLClass> nodeSet) {
	Set<String> uris = new HashSet<>();

	for (Node<OWLClass> node : nodeSet) {
		uris.addAll(extractIris(node));
	}

	return uris;
}
 
示例14
public Set<Node<OWLClass>> getNamedCommonSubsumers(OWLClass c, OWLClass d) {
	EWAHCompressedBitmap bmc = ancsBitmapCachedModifiable(c);
	EWAHCompressedBitmap bmd = ancsBitmapCachedModifiable(d);
	EWAHCompressedBitmap cad = bmc.and(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;
}
 
示例15
/**
 * @param args
 * @throws OWLOntologyCreationException
 */
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();

	// Load your ontology.
	OWLOntology ont = man
			.loadOntologyFromOntologyDocument(new File(args[0]));
	
	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);
	
	// Precompute instances for each named class in the ontology
	reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);

	// List representative instances for each class.
	for (OWLClass clazz : ont.getClassesInSignature()) {
		for (Node<OWLNamedIndividual> individual : reasoner.getInstances(
				clazz, true)) {
			System.out.println(clazz + "("
					+ individual.getRepresentativeElement() + ")");
		}
	}

	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
示例16
public Node<OWLClass> getEquivalentClasses(OWLClassExpression ce)
throws InconsistentOntologyException,
ClassExpressionNotInProfileException, FreshEntitiesException,
ReasonerInterruptedException, TimeOutException {
	// TODO Auto-generated method stub
	return null;
}
 
示例17
@Override
public Node<OWLDataProperty> getEquivalentDataProperties(
		OWLDataProperty arg0)
		throws InconsistentOntologyException, FreshEntitiesException,
		ReasonerInterruptedException, TimeOutException {
	LOGGER_.trace("getEquivalentDataProperties(OWLDataProperty)");

	checkInterrupted();
	// TODO Provide implementation
	throw unsupportedOwlApiMethod(
			"getEquivalentDataProperties(OWLDataProperty)");
}
 
示例18
@Override
public Node<OWLNamedIndividual> getSameIndividuals(OWLNamedIndividual arg0)
		throws InconsistentOntologyException, FreshEntitiesException,
		ReasonerInterruptedException, TimeOutException {
	LOGGER_.trace("getSameIndividuals(OWLNamedIndividual)");
	checkInterrupted();
	// TODO This needs to be updated when we support nominals
	return new OWLNamedIndividualNode(arg0);
}
 
示例19
@Override
public Node<OWLDataProperty> getTopDataPropertyNode() {
	LOGGER_.trace("getTopDataPropertyNode()");
	checkInterrupted();
	// TODO Provide implementation
	throw unsupportedOwlApiMethod("getTopDataPropertyNode()");
}
 
示例20
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);
}
 
示例21
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, " | "));
}
 
示例22
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;
}
 
示例23
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;
}
 
示例24
/**
 * @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();
}
 
示例25
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;
}
 
示例26
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));
}
 
示例27
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;
}
 
示例28
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;
}
 
示例29
public Node<OWLObjectPropertyExpression> getInverseObjectProperties(
		OWLObjectPropertyExpression pe)
		throws InconsistentOntologyException, FreshEntitiesException,
		ReasonerInterruptedException, TimeOutException {
	// TODO Auto-generated method stub
	return null;
}
 
示例30
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
	}
}