Java Code Examples for org.semanticweb.owlapi.reasoner.OWLReasoner#getSuperClasses()

The following examples show how to use org.semanticweb.owlapi.reasoner.OWLReasoner#getSuperClasses() . 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: RedundantAxiomTagger.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void tagRedundantAxioms(OWLReasoner reasoner) {
    OWLOntology ont = reasoner.getRootOntology();
    OWLOntologyManager mgr = ont.getOWLOntologyManager();
    OWLDataFactory df = mgr.getOWLDataFactory();
    OWLAnnotationProperty anProp = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#source"));
    for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
        if (!ax.getSuperClass().isAnonymous()) {
            OWLClass supc = (OWLClass) ax.getSuperClass();
            
            mgr.removeAxiom(ont, ax);
            reasoner.flush();
            NodeSet<OWLClass> ancs = reasoner.getSuperClasses(ax.getSubClass(), false);
            //LOG.info(ax + " ANCS="+ancs);
            if (ancs.containsEntity( supc)) {
                String direct = "indirect";
                if (reasoner.getSuperClasses(ax.getSubClass(), true).containsEntity( supc)) {
                    direct = "direct";
                }
                LOG.info("SCA = "+ax+" D="+direct);
                OWLAnnotation ann = df.getOWLAnnotation(anProp, df.getOWLLiteral(direct));
                OWLAxiom newAxiom = changeAxiomAnnotations(ax, Collections.singleton(ann), df);
                mgr.addAxiom(ont, newAxiom);
            }
            else {
                // put it back
                mgr.addAxiom(ont, ax);
            }
        }
    }
   
}
 
Example 2
Source File: QueryingUnnamedClassExpressions.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// Load your ontology.
	OWLOntology ont = man.loadOntologyFromOntologyDocument(new File(
			"c:/ontologies/ontology.owl"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Create your desired query class expression. In this example we
	// will query ObjectIntersectionOf(A ObjectSomeValuesFrom(R B)).
	PrefixManager pm = new DefaultPrefixManager("http://example.org/");
	OWLClass A = dataFactory.getOWLClass(":A", pm);
	OWLObjectProperty R = dataFactory.getOWLObjectProperty(":R", pm);
	OWLClass B = dataFactory.getOWLClass(":B", pm);
	OWLClassExpression query = dataFactory.getOWLObjectIntersectionOf(A,
			dataFactory.getOWLObjectSomeValuesFrom(R, B));

	// Create a fresh name for the query.
	OWLClass newName = dataFactory.getOWLClass(IRI.create("temp001"));
	// Make the query equivalent to the fresh class
	OWLAxiom definition = dataFactory.getOWLEquivalentClassesAxiom(newName,
			query);
	man.addAxiom(ont, definition);

	// Remember to either flush the reasoner after the ontology change
	// or create the reasoner in non-buffering mode. Note that querying
	// a reasoner after an ontology change triggers re-classification of
	// the whole ontology which might be costly. Therefore, if you plan
	// to query for multiple complex class expressions, it will be more
	// efficient to add the corresponding definitions to the ontology at
	// once before asking any queries to the reasoner.
	reasoner.flush();

	// You can now retrieve subclasses, superclasses, and instances of
	// the query class by using its new name instead.
	reasoner.getSubClasses(newName, true);
	reasoner.getSuperClasses(newName, true);
	reasoner.getInstances(newName, false);

	// After you are done with the query, you should remove the definition
	man.removeAxiom(ont, definition);

	// You can now add new definitions for new queries in the same way

	// After you are done with all queries, do not forget to free the
	// resources occupied by the reasoner
	reasoner.dispose();
}
 
Example 3
Source File: ReasonOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Remove subClassAxioms where there is a more direct axiom, and the subClassAxiom does not have
 * any annotations.
 *
 * <p>Example: genotyping assay - asserted in dev: assay - inferred by reasoner: analyte assay -
 * asserted after fill: assay, analyte assay - asserted after removeRedundantSubClassAxioms:
 * analyte assay
 *
 * @param reasoner an OWL reasoner, initialized with a root ontology; the ontology will be
 *     modified
 * @param options map of options for reasoning
 */
public static void removeRedundantSubClassAxioms(
    OWLReasoner reasoner, Map<String, String> options) {
  logger.info("Removing redundant subclass axioms...");
  OWLOntology ontology = reasoner.getRootOntology();
  OWLOntologyManager manager = ontology.getOWLOntologyManager();
  OWLDataFactory dataFactory = manager.getOWLDataFactory();

  for (OWLClass thisClass : ontology.getClassesInSignature()) {
    if (thisClass.isOWLNothing() || thisClass.isOWLThing()) {
      continue;
    }

    // Use the reasoner to get all
    // the direct superclasses of this class.
    Set<OWLClass> inferredSuperClasses = new HashSet<>();
    for (Node<OWLClass> node : reasoner.getSuperClasses(thisClass, true)) {
      for (OWLClass inferredSuperClass : node) {
        inferredSuperClasses.add(inferredSuperClass);
      }
    }

    // For each subClassAxiom,
    // if the subclass axiom does not have any annotations,
    // and the superclass is named (not anonymous),
    // and the superclass is not in the set of inferred super classes,
    // then remove that axiom.
    for (OWLSubClassOfAxiom subClassAxiom : ontology.getSubClassAxiomsForSubClass(thisClass)) {
      if (OptionsHelper.optionIsTrue(options, "preserve-annotated-axioms")) {

        if (subClassAxiom.getAnnotations().size() > 0) {
          // TODO make this configurable
          continue;
        }
      }
      if (subClassAxiom.getSuperClass().isAnonymous()) {
        continue;
      }
      OWLClass assertedSuperClass = subClassAxiom.getSuperClass().asOWLClass();
      if (inferredSuperClasses.contains(assertedSuperClass)) {
        continue;
      }
      manager.removeAxiom(
          ontology, dataFactory.getOWLSubClassOfAxiom(thisClass, assertedSuperClass));
    }
  }
  logger.info("Ontology now has {} axioms.", ontology.getAxioms().size());
}
 
Example 4
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();
	}
	
}