public boolean contains(OWLDataProperty prop) { if (prop.equals(owlOntologyManager.getOWLDataFactory().getOWLThing())) { return true; } else if (prop.equals(owlOntologyManager.getOWLDataFactory().getOWLNothing())) { return true; } return owlDataProperty2DataPropertyPointerMap.containsKey(prop); }
public boolean contains(OWLClass cls) { if (cls.equals(owlOntologyManager.getOWLDataFactory().getOWLThing())) { return true; } else if (cls.equals(owlOntologyManager.getOWLDataFactory().getOWLNothing())) { return true; } return owlClass2ClassPointerMap.containsKey(cls); }
private OWLDescription createIncludeCoffeeDescription(Set<OWLClass> includeExtras) { OWLObjectProperty prop = this.getNamedObjectProperty(PREFERENCES.getExtrasPropertyName()); // has_extras Set<OWLDescription> classes = new HashSet<OWLDescription>(); classes.add(getCoffeeClass()); // get OWL class for Coffee for (OWLClass extra : includeExtras) { classes.add(manager.getOWLDataFactory().getOWLObjectSomeRestriction(prop, extra)); } return manager.getOWLDataFactory().getOWLObjectIntersectionOf(classes); }
private OWLDescription createExcludeCoffeeDescription(Set<OWLClass> excludeExtras) { OWLObjectProperty prop = this.getNamedObjectProperty(PREFERENCES.getExtrasPropertyName()); // has_extras Set<OWLDescription> classes = new HashSet<OWLDescription>(); // Everything must be a coffee for (OWLClass extra : excludeExtras) { classes.add(manager.getOWLDataFactory().getOWLObjectSomeRestriction(prop, extra)); } // are looking for. return manager.getOWLDataFactory().getOWLObjectUnionOf(classes); }
@Before public void setUp() { when(ontology.getOWLOntologyManager()).thenReturn(manager); when(manager.getOWLDataFactory()).thenReturn(dataFactory); when(dataFactory.getOWLThing()).thenReturn(owlThing); when(dataFactory.getOWLNothing()).thenReturn(owlNothing); when(dataFactory.getOWLTopObjectProperty()).thenReturn(owlTopObjectProperty); when(dataFactory.getOWLBottomObjectProperty()).thenReturn(owlBottomObjectProperty); when(dataFactory.getOWLTopDataProperty()).thenReturn(owlTopDataProperty); when(dataFactory.getOWLBottomDataProperty()).thenReturn(owlBottomDataProperty); reasoner = new NoOpReasoner(ontology, dataFactory); }
/** * Creates OWLDescription (query) by given included extras and excluded extras * * @param includeExtras * @param excludeExtras * @return */ private OWLDescription createCoffeeDescription( Set<OWLClass> includeExtras, Set<OWLClass> excludeExtras) { // Include means existential restrictions // Exclude means negated existential restrictions OWLObjectProperty prop = this.getNamedObjectProperty(PREFERENCES.getExtrasPropertyName()); // has_extras // Create a hash set to store the components (existential restrictions) // of our description Set<OWLDescription> classes = new HashSet<OWLDescription>(); // Everything must be a coffee classes.add(getCoffeeClass()); // get OWL class for Coffee // Create the existential restrictions that represent the extras // that we want to include. for (OWLClass extra : includeExtras) { // e.g. hasExtras some ex_A , hasExtras some ex_B classes.add(manager.getOWLDataFactory().getOWLObjectSomeRestriction(prop, extra)); } // Create the negated existential restrictions of the extras that we // want to exclude for (OWLClass excludeExtra : excludeExtras) { // has_topping some topping_A OWLDescription restriction = manager.getOWLDataFactory().getOWLObjectSomeRestriction(prop, excludeExtra); // not (has_topping some topping_A) OWLObjectComplementOf neg = manager.getOWLDataFactory().getOWLObjectComplementOf(restriction); classes.add(neg); } // Bind the whole thing up in an intersection class // to create a concept description of the coffee we // are looking for. return manager.getOWLDataFactory().getOWLObjectIntersectionOf(classes); }
public void visit(OWLUntypedConstant node) { try { lastDataValuePointer = owlConstant2DataValuePointerMap.get(node); if (lastDataValuePointer == null) { owlOntologyManager .getOWLDataFactory() .getOWLDataType(XSDVocabulary.STRING.getURI()) .accept((OWLDataVisitor) this); lastDataValuePointer = faCTPlusPlus.getDataValue(node.getLiteral(), getLastDataTypePointer()); owlConstant2DataValuePointerMap.put(node, lastDataValuePointer); dataValuePointerMap.put(lastDataValuePointer, node); } } catch (Exception e) { throw new FaCTPlusPlusRuntimeException(e); } }
public void visit(OWLDataType dataType) { try { lastDataTypeExpressionPointer = null; lastDataTypePointer = (DataTypePointer) owlDataRange2DataTypeExpressionPointerMap.get(dataType); if (lastDataTypePointer == null) { if (owlOntologyManager.getOWLDataFactory().getTopDataType().equals(dataType)) { lastDataTypePointer = faCTPlusPlus.getDataTop(); } else { lastDataTypePointer = faCTPlusPlus.getBuiltInDataType(dataType.getURI().toString()); } owlDataRange2DataTypeExpressionPointerMap.put(dataType, lastDataTypePointer); dataTypeExpressionPointerMap.put(lastDataTypePointer, dataType); } } catch (Exception e) { throw new FaCTPlusPlusRuntimeException(e); } }
public void loadOntology() { try { /* * --apaitoymena vimata gia th dhmioyrgia mapped ontologias: * 1.dhmioyrgoyme ena toBeMappedIRI me skopo toy thn antistoixish me * to local File. 2.dhmioyrgoyme ena File me th dieythynsh ths * ontologias sto topiko apothikeytiko meso. 3.dhmioyrgoyme enan * SimpleIRIMapper kai ton prosthetoyme mesw toy manager. o * SimpleIRIMapper syndeei to toBeMappedIRI poy dwsame arxika, me * thn fysikh topothesia ths ontologias sto topiko apothikeytiko * meso. 4.dhmioyrgoyme ena ontologyIRI me akrivws thn idia arxiki * timh me to toBeMappedIRI to opoio tha einai to IRI ths ontologias * mas 5.dhmioyrgoyme thn ontologia mas xrhsimopoiwntas to * manager.loadOntology(ontologyIRI); */ String sep = File.separator; manager = OWLManager.createOWLOntologyManager(); toBeMappedIRI = IRI.create( "http://www.semanticweb.org/ontologies/ptyxiaki_v0.6/2011/5/Ontology1308067064597.owl"); // ontFile = new File("../src/ontologyresources/ptyxiaki_v0.8.owl"); ontFile = new File("src/ontologyresources/ptyxiaki_v0.8.owl"); // in case of alternative location on load time when the application is jar'ed! if (!ontFile.canRead()) { ontFile = new File("ontologyresources/ptyxiaki_v0.8.owl"); } manager.addIRIMapper(new SimpleIRIMapper(toBeMappedIRI, IRI.create(ontFile))); ontologyIRI = IRI.create( "http://www.semanticweb.org/ontologies/ptyxiaki_v0.6/2011/5/Ontology1308067064597.owl"); topIxOnt = manager.loadOntology(ontologyIRI); OWLFactory = manager.getOWLDataFactory(); topIxFormat = manager.getOntologyFormat(topIxOnt); topIxPrefixManager = new DefaultPrefixManager(topIxOnt.getOntologyID().getOntologyIRI().toString() + '#'); System.out.println("loaded ontology: " + this.topIxOnt); System.out.println("from: " + this.manager.getOntologyDocumentIRI(this.topIxOnt)); } catch (OWLException oex) { logger.info(oex.getMessage()); } }
/** * Gets the property by given name, e.g. has_topping * * @param propName * @return */ public OWLObjectProperty getNamedObjectProperty(String propName) { if (objPropNameCache == null) { objPropNameCache = new HashMap<String, URI>(); for (OWLOntology ont : reasoner.getLoadedOntologies()) { for (OWLObjectProperty prop : ont.getReferencedObjectProperties()) { objPropNameCache.put(prop.toString(), prop.getURI()); } } } // search the HashMap OWLObjectProperty namedProp = null; URI uri = objPropNameCache.get(propName); if (uri != null) { namedProp = manager.getOWLDataFactory().getOWLObjectProperty(uri); } else { System.err.println("Cannot find object property: " + propName + " in loaded ontologies"); } return namedProp; }
/** * Gets OWL class by given name * * @param className * @return * @throws OWLReasonerException */ public OWLClass getNamedClass(String className) { if (classnameCache == null) { classnameCache = new HashMap<String, URI>(); for (OWLOntology ont : reasoner.getLoadedOntologies()) { for (OWLClass cls : ont.getReferencedClasses()) { classnameCache.put(cls.toString(), cls.getURI()); } } } OWLClass namedCls = null; URI uri = classnameCache.get(className); if (uri != null) { namedCls = manager.getOWLDataFactory().getOWLClass(uri); } else { System.err.println("Cannot find class: " + className + " in loaded ontologies"); } return namedCls; }
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()); } }