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

The following examples show how to use org.semanticweb.owlapi.reasoner.OWLReasoner#getSubClasses() . 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: ReduceOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void findNonRedundant(
    Node<OWLClass> node,
    OWLReasoner reasoner,
    Map<OWLClass, Map<OWLClass, Set<OWLSubClassOfAxiom>>> assertions,
    Set<OWLSubClassOfAxiom> nonredundant,
    Set<Node<OWLClass>> alreadySeen) {
  if (!alreadySeen.contains(node)) {
    NodeSet<OWLClass> subclasses = reasoner.getSubClasses(node.getRepresentativeElement(), true);
    for (OWLClass superclass : node.getEntities()) {
      for (OWLClass subclass : subclasses.getFlattened()) {
        if (assertions.containsKey(superclass)) {
          Map<OWLClass, Set<OWLSubClassOfAxiom>> subclassAxiomsBySubclass =
              assertions.get(superclass);
          if (subclassAxiomsBySubclass.containsKey(subclass)) {
            nonredundant.addAll(subclassAxiomsBySubclass.get(subclass));
          }
        }
      }
    }
    alreadySeen.add(node);
    for (Node<OWLClass> subclassNode : subclasses.getNodes()) {
      findNonRedundant(subclassNode, reasoner, assertions, nonredundant, alreadySeen);
    }
  }
}
 
Example 2
Source File: GoIEPRestrictionsRule.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GoIEPRestrictionsRule(OWLGraphWrapper graph, TraversingEcoMapper eco) {
	this.message = MESSAGE;
	this.violationType = ViolationType.Warning;
	
	evidences = eco.getAllValidEvidenceIds("IEP", true);
	
	classSubSet = new HashSet<String>();
	
	OWLClass rootClass = graph.getOWLClassByIdentifier("GO:0008150");
	OWLReasonerFactory factory = new ElkReasonerFactory();
	OWLReasoner reasoner = factory.createReasoner(graph.getSourceOntology());
	try {
		NodeSet<OWLClass> nodeSet = reasoner.getSubClasses(rootClass, false);
		for(OWLClass cls : nodeSet.getFlattened()) {
			if (cls.isBottomEntity() || cls.isTopEntity()) {
				continue;
			}
			String oboId = graph.getIdentifier(cls);
			if (oboId != null) {
				classSubSet.add(oboId);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 3
Source File: GoIPICatalyticActivityRestrictionsRule.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GoIPICatalyticActivityRestrictionsRule(OWLGraphWrapper graph, TraversingEcoMapper eco) {
	this.message = MESSAGE;
	this.violationType = ViolationType.Warning;
	
	evidences = eco.getAllValidEvidenceIds("IPI", true);
	
	classSubSet = new HashSet<String>();
	
	OWLClass rootClass = graph.getOWLClassByIdentifier("GO:0003824"); // catalytic activity
	OWLReasonerFactory factory = new ElkReasonerFactory();
	OWLReasoner reasoner = factory.createReasoner(graph.getSourceOntology());
	try {
		NodeSet<OWLClass> nodeSet = reasoner.getSubClasses(rootClass, false);
		for(OWLClass cls : nodeSet.getFlattened()) {
			if (cls.isBottomEntity() || cls.isTopEntity()) {
				continue;
			}
			String oboId = graph.getIdentifier(cls);
			if (oboId != null) {
				classSubSet.add(oboId);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 4
Source File: DLQueryTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Execute the DL query on the given ontology graph. Uses the factory to create 
 * the {@link OWLReasoner} for an internal query ontology.
 * 
 * @param queryObject
 * @param ontology
 * @param reasonerFactory
 * @return set of {@link OWLClass} which 
 */
static Set<OWLClass> executeQuery(OWLClassExpression queryObject, OWLOntology ontology, 
		OWLReasonerFactory reasonerFactory) 
{
	Set<OWLClass> subset = new HashSet<OWLClass>();
	
	LOG.info("Create reasoner for query ontology.");
	// Create an instance of an OWL API reasoner
	OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
	try {
		LOG.info("Start evaluation for DL query subclass of: "+queryObject);
		NodeSet<OWLClass> node = reasoner.getSubClasses(queryObject, false);
		if (node != null) {
			Set<OWLClass> classes = node.getFlattened();
			for (OWLClass owlClass : classes) {
				if (!owlClass.isBottomEntity() && !owlClass.isTopEntity()) {
					subset.add(owlClass);
				}
			}
			LOG.info("Number of found classes for dl query subclass of: "+classes.size());
		}
		return subset;
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 5
Source File: TBoxUnFoldingTool.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a new instance for the given ontology graph. Use the given set of parent
 * ids to retrieve the classes which are to be unfolded.
 * 
 * @param graph ontology
 * @param parents set of OBO-style ids
 * @param reasonerName type of reasoner to be used for inferring the relevant sub classes of the parent set.
 * @throws NonDeterministicUnfoldException
 */
public TBoxUnFoldingTool(OWLGraphWrapper graph, Set<String> parents, String reasonerName) throws NonDeterministicUnfoldException {
	this.graph = graph;
	this.ontology = graph.getSourceOntology();
	
	final InferenceBuilder inferenceBuilder = new InferenceBuilder(graph, reasonerName);
	final OWLReasoner reasoner = inferenceBuilder.getReasoner(ontology);
	
	final Set<OWLClass> unfoldClasses = new HashSet<OWLClass>();
	try {
		for(String parent : parents) {
			OWLClass parentClass = graph.getOWLClassByIdentifier(parent);
			NodeSet<OWLClass> nodeSet = reasoner.getSubClasses(parentClass , false);
			if (nodeSet != null && !nodeSet.isEmpty() && !nodeSet.isBottomSingleton()) {
				unfoldClasses.addAll(nodeSet.getFlattened());
			}
		}
	}
	finally {
		inferenceBuilder.dispose();
	}
	
	if (unfoldClasses.isEmpty()) {
		throw new RuntimeException("No classes found for given parents.");
	}
	
	visitor = new UnfoldingVisitor(unfoldClasses, ontology);
}
 
Example 6
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();
}