protected boolean isSuccessorRepresented( DomainNode<?> from, DomainNode<?> to, OWLObjectProperty property) { if (from.getId() instanceof OWLClassExpression && to.getId() instanceof OWLClassExpression) { Set<DomainNode<?>> successors = from.getSuccessors(property); // StatStore.getInstance().enterValue("subclass accessing", 1.0); // NodeSet<OWLClass> subClasses = // m_ontologyOperator.getReasoner().getSubClasses((OWLClass)to.getId(), false); NodeSet<OWLClass> subClasses = currentIdSubClasses; Node<OWLClass> eqClasses = currentIdEqClasses; for (DomainNode<?> succ : successors) { // only compare successors to other class domain elements if (succ.getId() instanceof OWLClass) { // if(succ.getInstantiators().containsAll(to.getInstantiators())){ // could be done // with reasoner if (m_useBuffer) { if (m_subClassBuffer.get((OWLClass) to.getId()).contains((OWLClass) succ.getId()) || m_equivalentClassBuffer .get((OWLClass) to.getId()) .contains((OWLClass) succ.getId())) { return true; } } else { if (subClasses.containsEntity((OWLClass) succ.getId()) || eqClasses.contains( (OWLClass) succ.getId())) { // if there exists a successors more (or equally) specific // than the new one return true; } } } } } return false; }
private void addEntailedKBSuccessors(OWLObjectSomeValuesFrom some) { TRACKER.start(StaticValues.TIME_KB_SUCCESSORS, BlockOutputMode.COMPLETE, true); // add all role-successors entailed by the TBox addEntailedTBoxSuccessors(some, m_normalize); // for KB mode only normalize TBox contained roles DomainNode<?> toNode = getDomainElement(some.getFiller()); OWLClass someClass = m_ontologyOperator.getFlatteningTransformer().getIntermediary(some); long start = System.currentTimeMillis(); // StatStore.getInstance().enterValue("instance accessing", 1.0); NodeSet<OWLNamedIndividual> instances = m_ontologyOperator.getReasoner().getInstances(someClass, false); sum += System.currentTimeMillis() - start; // NodeSet<OWLNamedIndividual> instances = // m_ontologyOperator.getReasoner().getInstances(getClassRepresentation(some), false); // LOG.fine(" creating successors to " + some + " from instances: " + instances.toString()); Iterator<Node<OWLNamedIndividual>> it1 = instances.iterator(); while (it1.hasNext()) { Iterator<OWLNamedIndividual> it2 = it1.next().iterator(); while (it2.hasNext()) { DomainNode<?> from = m_domain.getDomainNode(it2.next()); if (from != null) { // if(!isSuccessorRepresented(from, getDomainElement(some.getFiller()), // (OWLObjectProperty)some.getProperty())){ from.addSuccessor((OWLObjectProperty) some.getProperty(), toNode); // } } } } TRACKER.stop(StaticValues.TIME_KB_SUCCESSORS); }
public Set<OWLNamedIndividual> getInstances(String classExpressionString, boolean direct) { if (classExpressionString.trim().length() == 0) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(classExpression, direct); return individuals.getFlattened(); }
public Set<OWLClass> getSubClasses(String classExpressionString, boolean direct) { if (classExpressionString.trim().length() == 0) { return Collections.emptySet(); } OWLClassExpression classExpression = parser.parseClassExpression(classExpressionString); NodeSet<OWLClass> subClasses = reasoner.getSubClasses(classExpression, direct); return subClasses.getFlattened(); }
/** * Gather information about all Activities. Individuals are placed in a HashMap (individualsHM: * Key=URI, value = PROVIndividual) */ public void addActivity(OWLNamedIndividual ind) { // Add list of Object Property values associated with ind. addAllActivityOPs(ind); // Add list of Data Property values associated with ind. addAllActivityDPs(ind); // get Types NodeSet<OWLClass> types = PROVGraph.reasoner.getTypes(ind, true); OWLClass[] typesArray = types.getFlattened().toArray(new OWLClass[0]); setTypes(typesArray); }
/** * Adds Object Properties to the current Individual, and adds corresponding predicate to the list * (parallel arraylists) */ public void addObjectPropertiesByPredicateType(String predicateURI, OWLNamedIndividual ind) { OWLObjectProperty objectProp = PROVGraph.manager.getOWLDataFactory().getOWLObjectProperty(IRI.create(predicateURI)); NodeSet<OWLNamedIndividual> owlObjectIndividuals = PROVGraph.reasoner.getObjectPropertyValues(ind, objectProp); System.out.println(predicateURI + " Nodes: " + owlObjectIndividuals.getNodes().size()); for (Node<OWLNamedIndividual> owlSameInd : owlObjectIndividuals) { OWLNamedIndividual currInd = owlSameInd.getRepresentativeElement(); if (currInd != null) { String owlIndURI = currInd.toStringID(); System.out.println("owlIndURI: " + owlIndURI); // if the target individual has been previously created, point to that PROVIndividual object if (PROVGraph.individualsHM.containsKey(owlIndURI)) { PROVIndividual targetProvInd = PROVGraph.individualsHM.get(owlSameInd.getRepresentativeElement().toStringID()); OPconnectionObjectInds.add(targetProvInd); OPconnections.add(predicateURI); } else // otherwise create a new object with the URI, and add it to the pool of collected // PROVIndividuals { PROVIndividual targetProvInd = new PROVIndividual(owlSameInd.getRepresentativeElement().toStringID()); PROVGraph.individualsHM.put(targetProvInd.uri, targetProvInd); OPconnectionObjectInds.add(targetProvInd); OPconnections.add(predicateURI); } } else { PROVIndividual anonProvInd = new PROVIndividual("Anonymous Individual " + (++anonIndsCntr)); // **anonProvInd.uri not unique PROVGraph.individualsHM.put(anonProvInd.uri, anonProvInd); OPconnectionObjectInds.add(anonProvInd); OPconnections.add(predicateURI); } } }
protected Set<DomainNode<OWLClassExpression>> removeIncludedSuccessors( OWLObjectProperty property, DomainNode<?> d, OWLClassExpression newSucc) { Set<DomainNode<OWLClassExpression>> removed = new HashSet<DomainNode<OWLClassExpression>>(); Set<DomainNode<?>> successors = d.getSuccessors(property); // StatStore.getInstance().enterValue("superclass accessing", 1.0); // NodeSet<OWLClass> superClasses = m_ontologyOperator.getReasoner().getSuperClasses(newSucc, // false); // Node<OWLClass> eqClasses = m_ontologyOperator.getReasoner().getEquivalentClasses(newSucc); NodeSet<OWLClass> superClasses = currentIdSuperClasses; if (successors != null) { // Set<DomainNode<?>> mark_removed = new HashSet<DomainNode<?>>(); Iterator<DomainNode<?>> it = successors.iterator(); // for(DomainNode<?> succ : successors){ while (it.hasNext()) { DomainNode<?> succ = it.next(); if (succ.getId() instanceof OWLClassExpression) { // only inspect class to class relations OWLClass succClass = getClassRepresentation((OWLClassExpression) succ.getId()); // if newSucc is more specific than succ, remove succ if (m_useBuffer) { if (m_superClassBuffer.get(newSucc).contains(succClass)) { it.remove(); removed.add(m_domain.getDomainNode((OWLClassExpression) succ.getId())); } } else { if (superClasses.containsEntity(succClass)) { it.remove(); removed.add(m_domain.getDomainNode((OWLClassExpression) succ.getId())); // mark_removed.add(succ); } } } // else what about individual domain elements } // successors.removeAll(mark_removed); } return removed; }
public static void main(String[] args) { try { // Create our ontology manager in the usual way. OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); // Load a copy of the people+pets ontology. We'll load the ontology from the web (it's // acutally located // in the TONES ontology repository). IRI docIRI = IRI.create(DOCUMENT_IRI); // We load the ontology from a document - our IRI points to it directly OWLOntology ont = manager.loadOntologyFromOntologyDocument(docIRI); System.out.println("Loaded " + ont.getOntologyID()); // We need to create an instance of OWLReasoner. An OWLReasoner provides the basic // query functionality that we need, for example the ability obtain the subclasses // of a class etc. To do this we use a reasoner factory. // Create a reasoner factory. In this case, we will use HermiT, but we could also // use FaCT++ (http://code.google.com/p/factplusplus/) or // Pellet(http://clarkparsia.com/pellet) // Note that (as of 03 Feb 2010) FaCT++ and Pellet OWL API 3.0.0 compatible libraries are // expected to be available in the near future). // For now, we'll use HermiT // HermiT can be downloaded from http://hermit-reasoner.com // Make sure you get the HermiT library and add it to your class path. You can then // instantiate the HermiT reasoner factory: // Comment out the first line below and uncomment the second line below to instantiate // the HermiT reasoner factory. You'll also need to import the // org.semanticweb.HermiT.Reasoner // package. OWLReasonerFactory reasonerFactory = null; // OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory(); // We'll now create an instance of an OWLReasoner (the implementation being provided by HermiT // as // we're using the HermiT reasoner factory). The are two categories of reasoner, Buffering // and // NonBuffering. In our case, we'll create the buffering reasoner, which is the default kind // of reasoner. // We'll also attach a progress monitor to the reasoner. To do this we set up a configuration // that // knows about a progress monitor. // Create a console progress monitor. This will print the reasoner progress out to the // console. ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor(); // Specify the progress monitor via a configuration. We could also specify other setup // parameters in // the configuration, and different reasoners may accept their own defined parameters this // way. OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor); // Create a reasoner that will reason over our ontology and its imports closure. Pass in the // configuration. OWLReasoner reasoner = reasonerFactory.createReasoner(ont, config); // Ask the reasoner to do all the necessary work now reasoner.precomputeInferences(); // We can determine if the ontology is actually consistent (in this case, it should be). boolean consistent = reasoner.isConsistent(); System.out.println("Consistent: " + consistent); System.out.println("\n"); // We can easily get a list of unsatisfiable classes. (A class is unsatisfiable if it // can't possibly have any instances). Note that the getUnsatisfiableClasses method // is really just a convenience method for obtaining the classes that are equivalent // to owl:Nothing. In our case there should be just one unsatisfiable class - "mad_cow" // We ask the reasoner for the unsatisfiable classes, which returns the bottom node // in the class hierarchy (an unsatisfiable class is a subclass of every class). Node<OWLClass> bottomNode = reasoner.getUnsatisfiableClasses(); // This node contains owl:Nothing and all the classes that are equivalent to owl:Nothing - // i.e. the unsatisfiable classes. // We just want to print out the unsatisfiable classes excluding owl:Nothing, and we can // used a convenience method on the node to get these Set<OWLClass> unsatisfiable = bottomNode.getEntitiesMinusBottom(); if (!unsatisfiable.isEmpty()) { System.out.println("The following classes are unsatisfiable: "); for (OWLClass cls : unsatisfiable) { System.out.println(" " + cls); } } else { System.out.println("There are no unsatisfiable classes"); } System.out.println("\n"); // Now we want to query the reasoner for all descendants of vegetarian. Vegetarians are // defined in the // ontology to be animals that don't eat animals or parts of animals. OWLDataFactory fac = manager.getOWLDataFactory(); // Get a reference to the vegetarian class so that we can as the reasoner about it. // The full IRI of this class happens to be: // <http://owl.man.ac.uk/2005/07/sssw/people#vegetarian> OWLClass vegPizza = fac.getOWLClass(IRI.create("http://owl.man.ac.uk/2005/07/sssw/people#vegetarian")); // Now use the reasoner to obtain the subclasses of vegetarian. // We can ask for the direct subclasses of vegetarian or all of the (proper) subclasses of // vegetarian. // In this case we just want the direct ones (which we specify by the "true" flag). NodeSet<OWLClass> subClses = reasoner.getSubClasses(vegPizza, true); // The reasoner returns a NodeSet, which represents a set of Nodes. // Each node in the set represents a subclass of vegetarian pizza. A node of classes contains // classes, // where each class in the node is equivalent. For example, if we asked for the // subclasses of some class A and got back a NodeSet containing two nodes {B, C} and {D}, then // A would have // two proper subclasses. One of these subclasses would be equivalent to the class D, and the // other would // be the class that is equivalent to class B and class C. // In this case, we don't particularly care about the equivalences, so we will flatten this // set of sets and print the result Set<OWLClass> clses = subClses.getFlattened(); System.out.println("Subclasses of vegetarian: "); for (OWLClass cls : clses) { System.out.println(" " + cls); } System.out.println("\n"); // In this case, we should find that the classes, cow, sheep and giraffe are vegetarian. Note // that in this // ontology only the class cow had been stated to be a subclass of vegetarian. The fact that // sheep and // giraffe are subclasses of vegetarian was implicit in the ontology (through other things we // had said) // and this illustrates why it is important to use a reasoner for querying an ontology. // We can easily retrieve the instances of a class. In this example we'll obtain the // instances of // the class pet. This class has a full IRI of <http://owl.man.ac.uk/2005/07/sssw/people#pet> // We need to obtain a reference to this class so that we can ask the reasoner about it. OWLClass country = fac.getOWLClass(IRI.create("http://owl.man.ac.uk/2005/07/sssw/people#pet")); // Ask the reasoner for the instances of pet NodeSet<OWLNamedIndividual> individualsNodeSet = reasoner.getInstances(country, true); // The reasoner returns a NodeSet again. This time the NodeSet contains individuals. // Again, we just want the individuals, so get a flattened set. Set<OWLNamedIndividual> individuals = individualsNodeSet.getFlattened(); System.out.println("Instances of pet: "); for (OWLNamedIndividual ind : individuals) { System.out.println(" " + ind); } System.out.println("\n"); // Again, it's worth noting that not all of the individuals that are returned were explicitly // stated // to be pets. // Finally, we can ask for the property values (property assertions in OWL speak) for a given // individual // and property. // Let's get the property values for the individual Mick, the full IRI of which is // <http://owl.man.ac.uk/2005/07/sssw/people#Mick> // Get a reference to the individual Mick OWLNamedIndividual mick = fac.getOWLNamedIndividual(IRI.create("http://owl.man.ac.uk/2005/07/sssw/people#Mick")); // Let's get the pets of Mick // Get hold of the has_pet property which has a full IRI of // <http://owl.man.ac.uk/2005/07/sssw/people#has_pet> OWLObjectProperty hasPet = fac.getOWLObjectProperty(IRI.create("http://owl.man.ac.uk/2005/07/sssw/people#has_pet")); // Now ask the reasoner for the has_pet property values for Mick NodeSet<OWLNamedIndividual> petValuesNodeSet = reasoner.getObjectPropertyValues(mick, hasPet); Set<OWLNamedIndividual> values = petValuesNodeSet.getFlattened(); System.out.println("The has_pet property values for Mick are: "); for (OWLNamedIndividual ind : values) { System.out.println(" " + ind); } // Notice that Mick has a pet Rex, which wasn't asserted in the ontology. // Finally, let's print out the class hierarchy. // Get hold of the top node in the class hierarchy (containing owl:Thing) // Now print the hierarchy out Node<OWLClass> topNode = reasoner.getTopClassNode(); print(topNode, reasoner, 0); } catch (UnsupportedOperationException exception) { System.out.println("Unsupported reasoner operation."); } catch (OWLOntologyCreationException e) { System.out.println("Could not load the pizza ontology: " + e.getMessage()); } }
private void addEntailedTBoxSuccessors(OWLObjectSomeValuesFrom some, boolean doNormalizing) { // StatStore.getInstance().enterValue("lookup " + some.getFiller(), 1.0); // StatStore.getInstance().enterValue("restrictions handled", 1.0); TRACKER.start(StaticValues.TIME_TBOX_SUCCESSORS, BlockOutputMode.COMPLETE, true); // TRACKER.start("fetch domain element and intermediary", BlockOutputMode.COMPLETE, true); DomainNode<?> toNode = getDomainElement(some.getFiller()); // the super class, intermediary stands for (some r B) OWLClass superClass = m_ontologyOperator.getFlatteningTransformer().getIntermediary(some); // TRACKER.stop("fetch domain element and intermediary"); // add successors from all // TRACKER.start("query elk", BlockOutputMode.COMPLETE, true); // StatStore.getInstance().enterValue("subclass accessing", 1.0); NodeSet<OWLClass> classes = m_ontologyOperator.getReasoner().getSubClasses(superClass, false); // fill current class relations from Id // StatStore.getInstance().enterValue("subclass accessing", 1.0); currentIdSubClasses = m_ontologyOperator.getReasoner().getSubClasses(some.getFiller(), false); // StatStore.getInstance().enterValue("superclass accessing", 1.0); currentIdSuperClasses = m_ontologyOperator.getReasoner().getSuperClasses(some.getFiller(), false); // StatStore.getInstance().enterValue("equivalent classes accessing", 1.0); currentIdEqClasses = m_ontologyOperator.getReasoner().getEquivalentClasses(some.getFiller()); // TRACKER.stop("query elk"); // LOG.fine(" creating successors to " + some + ": " + classes.toString()); Iterator<Node<OWLClass>> nodeIt = classes.iterator(); while (nodeIt.hasNext()) { Iterator<OWLClass> it = nodeIt.next().iterator(); while (it.hasNext()) { DomainNode<?> node = m_domain.getDomainNode(it.next()); // if it.next() yields no domain node, there exists an ABox identity for it // and this case is covered by addEntailedKBSuccessors if (node != null) { if (doNormalizing) { // if(!isSuccessorRepresented(node, (OWLObjectProperty)some.getProperty(), // some.getFiller())){ TRACKER.start("normalizing and adding", BlockOutputMode.COMPLETE, true); if (!isSuccessorRepresented(node, toNode, (OWLObjectProperty) some.getProperty())) { removeIncludedSuccessors( (OWLObjectProperty) some.getProperty(), node, some.getFiller()); node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode); } TRACKER.stop("normalizing and adding"); } else { node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode); } } } } // add all equivalent class successors // TRACKER.start("query elk", BlockOutputMode.COMPLETE, true); Iterator<OWLClass> cIt = m_ontologyOperator.getReasoner().getEquivalentClasses(superClass).iterator(); // TRACKER.stop("query elk"); while (cIt.hasNext()) { DomainNode<?> node = m_domain.getDomainNode(cIt.next()); if (node != null) { if (doNormalizing) { // if(!isSuccessorRepresented(node, (OWLObjectProperty)some.getProperty(), // some.getFiller())){ TRACKER.start("normalizing and adding", BlockOutputMode.COMPLETE, true); if (!isSuccessorRepresented(node, toNode, (OWLObjectProperty) some.getProperty())) { removeIncludedSuccessors( (OWLObjectProperty) some.getProperty(), node, some.getFiller()); node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode); } TRACKER.stop("normalizing and adding"); } else { node.addSuccessor((OWLObjectProperty) some.getProperty(), toNode); } } } TRACKER.stop(StaticValues.TIME_TBOX_SUCCESSORS); }