Examples of OWLOntologyManager


Examples of org.semanticweb.owlapi.model.OWLOntologyManager

    }

    @Test
    public void testShallow() throws OWLOntologyCreationException {
        OWLOntology o = loadOntologyFromString(s);
        OWLOntologyManager m2 = o.getOWLOntologyManager();
        OWLOntology copy = m1.copyOntology(o, OntologyCopy.SHALLOW);
        assertEquals(m1, copy.getOWLOntologyManager());
        assertTrue(m2.contains(o));
        assertTrue(m1.contains(copy));
        assertNotNull(m2.getOntologyFormat(o));
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

    }

    @Test
    public void testDeep() throws OWLOntologyCreationException {
        OWLOntology o = loadOntologyFromString(s);
        OWLOntologyManager m2 = o.getOWLOntologyManager();
        OWLOntology copy = m1.copyOntology(o, OntologyCopy.DEEP);
        assertEquals(m1, copy.getOWLOntologyManager());
        assertTrue(m2.contains(o));
        assertTrue(m1.contains(copy));
        assertNotNull(m2.getOntologyFormat(o));
        assertNotNull(m1.getOntologyFormat(o));
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

        // In order to say that an individual is an instance of a class (in an
        // ontology), we can add a ClassAssertion to the ontology. For example,
        // suppose we wanted to specify that :Mary is an instance of the class
        // :Person. First we need to obtain the individual :Mary and the class
        // :Person Create an ontology manager to work with
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory dataFactory = manager.getOWLDataFactory();
        // The IRIs used here are taken from the OWL 2 Primer
        String base = "http://example.com/owl/families/";
        PrefixManager pm = new DefaultPrefixManager(null, null, base);
        // Get the reference to the :Person class (the full IRI will be
        // <http://example.com/owl/families/Person>)
        OWLClass person = dataFactory.getOWLClass(":Person", pm);
        // Get the reference to the :Mary class (the full IRI will be
        // <http://example.com/owl/families/Mary>)
        OWLNamedIndividual mary = dataFactory
                .getOWLNamedIndividual(":Mary", pm);
        // Now create a ClassAssertion to specify that :Mary is an instance of
        // :Person
        OWLClassAssertionAxiom classAssertion = dataFactory
                .getOWLClassAssertionAxiom(person, mary);
        // We need to add the class assertion to the ontology that we want
        // specify that :Mary is a :Person
        OWLOntology ontology = manager.createOntology(IRI.create(base));
        // Add the class assertion
        manager.addAxiom(ontology, classAssertion);
        // Dump the ontology to stdout
        manager.saveOntology(ontology, new StreamDocumentTarget(
                new ByteArrayOutputStream()));
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     * @throws Exception
     *         exception
     */
    @Test
    public void shouldCreateAndSaveOntology() throws Exception {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        // Let's create an ontology and name it
        // "http://www.co-ode.org/ontologies/testont.owl" We need to set up a
        // mapping which points to a concrete file where the ontology will be
        // stored. (It's good practice to do this even if we don't intend to
        // save the ontology).
        IRI ontologyIRI = IRI
                .create("http://www.co-ode.org/ontologies/testont.owl");
        // Create a document IRI which can be resolved to point to where our
        // ontology will be saved.
        IRI documentIRI = IRI.create("file:/tmp/SWRLTest.owl");
        // Set up a mapping, which maps the ontology to the document IRI
        SimpleIRIMapper mapper = new SimpleIRIMapper(ontologyIRI, documentIRI);
        manager.getIRIMappers().add(mapper);
        // Now create the ontology - we use the ontology IRI (not the physical
        // IRI)
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLDataFactory factory = manager.getOWLDataFactory();
        // Get hold of references to class A and class B. Note that the ontology
        // does not contain class A or classB, we simply get references to
        // objects from a data factory that represent class A and class B
        OWLClass clsA = factory.getOWLClass(IRI.create(ontologyIRI + "#A"));
        OWLClass clsB = factory.getOWLClass(IRI.create(ontologyIRI + "#B"));
        SWRLVariable var = factory.getSWRLVariable(IRI.create(ontologyIRI
                + "#x"));
        SWRLRule rule = factory.getSWRLRule(
                singleton(factory.getSWRLClassAtom(clsA, var)),
                singleton(factory.getSWRLClassAtom(clsB, var)));
        manager.applyChange(new AddAxiom(ontology, rule));
        OWLObjectProperty prop = factory.getOWLObjectProperty(IRI
                .create(ontologyIRI + "#propA"));
        OWLObjectProperty propB = factory.getOWLObjectProperty(IRI
                .create(ontologyIRI + "#propB"));
        SWRLObjectPropertyAtom propAtom = factory.getSWRLObjectPropertyAtom(
                prop, var, var);
        SWRLObjectPropertyAtom propAtom2 = factory.getSWRLObjectPropertyAtom(
                propB, var, var);
        Set<SWRLAtom> antecedent = new HashSet<SWRLAtom>();
        antecedent.add(propAtom);
        antecedent.add(propAtom2);
        SWRLRule rule2 = factory.getSWRLRule(antecedent,
                Collections.singleton(propAtom));
        manager.applyChange(new AddAxiom(ontology, rule2));
        // Now save the ontology. The ontology will be saved to the location
        // where we loaded it from, in the default ontology format
        manager.saveOntology(ontology);
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     * @throws Exception
     *         exception
     */
    @Test
    public void shouldAddObjectPropertyAssertions() throws Exception {
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        String base = "http://www.semanticweb.org/ontologies/individualsexample";
        OWLOntology ont = man.createOntology(IRI.create(base));
        OWLDataFactory dataFactory = man.getOWLDataFactory();
        // In this case, we would like to state that matthew has a father who is
        // peter. We need a subject and object - matthew is the subject and
        // peter is the object. We use the data factory to obtain references to
        // these individuals
        OWLIndividual matthew = dataFactory.getOWLNamedIndividual(IRI
                .create(base + "#matthew"));
        OWLIndividual peter = dataFactory.getOWLNamedIndividual(IRI.create(base
                + "#peter"));
        // We want to link the subject and object with the hasFather property,
        // so use the data factory to obtain a reference to this object
        // property.
        OWLObjectProperty hasFather = dataFactory.getOWLObjectProperty(IRI
                .create(base + "#hasFather"));
        // Now create the actual assertion (triple), as an object property
        // assertion axiom matthew --> hasFather --> peter
        OWLObjectPropertyAssertionAxiom assertion = dataFactory
                .getOWLObjectPropertyAssertionAxiom(hasFather, matthew, peter);
        // Finally, add the axiom to our ontology and save
        AddAxiom addAxiomChange = new AddAxiom(ont, assertion);
        man.applyChange(addAxiomChange);
        // We can also specify that matthew is an instance of Person. To do this
        // we use a ClassAssertion axiom. First we need a reference to the
        // person class
        OWLClass personClass = dataFactory.getOWLClass(IRI.create(base
                + "#Person"));
        // Now we will create out Class Assertion to specify that matthew is an
        // instance of Person (or rather that Person has matthew as an instance)
        OWLClassAssertionAxiom ax = dataFactory.getOWLClassAssertionAxiom(
                personClass, matthew);
        // Add this axiom to our ontology. We can use a short cut method -
        // instead of creating the AddAxiom change ourselves, it will be created
        // automatically and the change applied
        man.addAxiom(ont, ax);
        // Save our ontology
        man.saveOntology(ont, IRI.create("file:/tmp/example.owl"));
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

    public void shouldDeleteIndividuals() throws Exception {
        // The pizza ontology contains several individuals that represent
        // countries, which describe the country of origin of various pizzas and
        // ingredients. In this example we will delete them all. First off, we
        // start by loading the pizza ontology.
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        OWLOntology ont = loadPizza(man);
        // We can't directly delete individuals, properties or classes from an
        // ontology because ontologies don't directly contain entities -- they
        // are merely referenced by the axioms that the ontology contains. For
        // example, if an ontology contained a subclass axiom SubClassOf(A, B)
        // which stated A was a subclass of B, then that ontology would contain
        // references to classes A and B. If we essentially want to "delete"
        // classes A and B from this ontology we have to remove all axioms that
        // contain class A and class B in their SIGNATURE (in this case just one
        // axiom SubClassOf(A, B)). To do this, we can use the OWLEntityRemove
        // utility class, which will remove an entity (class, property or
        // individual) from a set of ontologies. Create the entity remover - in
        // this case we just want to remove the individuals from the pizza
        // ontology, so pass our reference to the pizza ontology in as a
        // singleton set.
        OWLEntityRemover remover = new OWLEntityRemover(singleton(ont));
        // System.out.println("Number of individuals: "
        // + ont.getIndividualsInSignature().size());
        // Loop through each individual that is referenced in the pizza
        // ontology, and ask it to accept a visit from the entity remover. The
        // remover will automatically accumulate the changes which are necessary
        // to remove the individual from the ontologies (the pizza ontology)
        // which it knows about
        for (OWLNamedIndividual ind : ont.getIndividualsInSignature()) {
            ind.accept(remover);
        }
        // Now we get all of the changes from the entity remover, which should
        // be applied to remove all of the individuals that we have visited from
        // the pizza ontology. Notice that "batch" deletes can essentially be
        // performed - we simply visit all of the classes, properties and
        // individuals that we want to remove and then apply ALL of the changes
        // after using the entity remover to collect them
        man.applyChanges(remover.getChanges());
        // System.out.println("Number of individuals: "
        // + ont.getIndividualsInSignature().size());
        // At this point, if we wanted to reuse the entity remover, we would
        // have to reset it
        remover.reset();
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     * @throws Exception
     *         exception
     */
    @Test
    public void shouldCreateRestrictions() throws Exception {
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        String base = "http://org.semanticweb.restrictionexample";
        OWLOntology ont = man.createOntology(IRI.create(base));
        // In this example we will add an axiom to state that all Heads have
        // parts that are noses (in fact, here we merely state that a Head has
        // at least one nose!). We do this by creating an existential (some)
        // restriction to describe the class of things which have a part that is
        // a nose (hasPart some Nose), and then we use this restriction in a
        // subclass axiom to state that Head is a subclass of things that have
        // parts that are Noses SubClassOf(Head, hasPart some Nose) -- in other
        // words, Heads have parts that are noses! First we need to obtain
        // references to our hasPart property and our Nose class
        OWLDataFactory factory = man.getOWLDataFactory();
        OWLObjectProperty hasPart = factory.getOWLObjectProperty(IRI
                .create(base + "#hasPart"));
        OWLClass nose = factory.getOWLClass(IRI.create(base + "#Nose"));
        // Now create a restriction to describe the class of individuals that
        // have at least one part that is a kind of nose
        OWLClassExpression hasPartSomeNose = factory
                .getOWLObjectSomeValuesFrom(hasPart, nose);
        // Obtain a reference to the Head class so that we can specify that
        // Heads have noses
        OWLClass head = factory.getOWLClass(IRI.create(base + "#Head"));
        // We now want to state that Head is a subclass of hasPart some Nose, to
        // do this we create a subclass axiom, with head as the subclass and
        // "hasPart some Nose" as the superclass (remember, restrictions are
        // also classes - they describe classes of individuals -- they are
        // anonymous classes).
        OWLSubClassOfAxiom ax = factory.getOWLSubClassOfAxiom(head,
                hasPartSomeNose);
        // Add the axiom to our ontology
        AddAxiom addAx = new AddAxiom(ont, ax);
        man.applyChange(addAx);
    }
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     *         exception
     */
    @Test
    public void shouldUseReasoner() throws Exception {
        // Create our ontology manager in the usual way.
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ont = loadPizza(manager);
        // 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 = new StructuralReasonerFactory();
        // 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);
        OWLReasoner reasoner = reasonerFactory.createReasoner(ont);
        // 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);
        // 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");
        }
        // 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"));
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     *         exception
     */
    @Test
    public void shouldLookAtRestrictions() throws Exception {
        // Create our manager
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        // Load the pizza ontology
        OWLOntology ont = loadPizza(man);
        // We want to examine the restrictions on margherita pizza. To do this,
        // we need to obtain a reference to the margherita pizza class. In this
        // case, we know the URI for margherita pizza (it happens to be the
        // ontology URI - the base URI plus #Margherita - note that this isn't
        // always the case. A class may have a URI that bears no resemblance to
        // the ontology URI which contains axioms about the class).
        IRI margheritaPizzaIRI = IRI.create(ont.getOntologyID()
                .getOntologyIRI().get()
                + "#Margherita");
        OWLClass margheritaPizza = man.getOWLDataFactory().getOWLClass(
                margheritaPizzaIRI);
        // Now we want to collect the properties which are used in existential
        // restrictions on the class. To do this, we will create a utility class
        // - RestrictionVisitor, which acts as a filter for existential
        // restrictions. This uses the Visitor Pattern (google Visitor Design
View Full Code Here

Examples of org.semanticweb.owlapi.model.OWLOntologyManager

     *         exception
     */
    @Test
    public void shouldCreateAndReadAnnotations() throws Exception {
        // Create our manager
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        // Load the pizza ontology
        OWLOntology ont = loadPizza(man);
        // We want to add a comment to the pizza class. First, we need to obtain
        // a reference to the pizza class
        OWLClass pizzaCls = df.getOWLClass(IRI.create(ont.getOntologyID()
                .getOntologyIRI().get()
                + "#Pizza"));
        // Now we create the content of our comment. In this case we simply want
        // a plain string literal. We'll attach a language to the comment to
        // specify that our comment is written in English (en).
        OWLAnnotation commentAnno = df.getOWLAnnotation(df.getRDFSComment(),
                df.getOWLLiteral("A class which represents pizzas", "en"));
        // Specify that the pizza class has an annotation - to do this we attach
        // an entity annotation using an entity annotation axiom (remember,
        // classes are entities)
        OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(pizzaCls.getIRI(),
                commentAnno);
        // Add the axiom to the ontology
        man.applyChange(new AddAxiom(ont, ax));
        // Now lets add a version info annotation to the ontology. There is no
        // 'standard' OWL 1.1 annotation object for this, like there is for
        // comments and labels, so the creation of the annotation is a bit more
        // involved. First we'll create a constant for the annotation value.
        // Version info should probably contain a version number for the
        // ontology, but in this case, we'll add some text to describe why the
        // version has been updated
        OWLLiteral lit = df.getOWLLiteral("Added a comment to the pizza class");
        // The above constant is just a plain literal containing the version
        // info text/comment we need to create an annotation, which pairs a URI
        // with the constant
        OWLAnnotation anno = df.getOWLAnnotation(df
                .getOWLAnnotationProperty(OWLRDFVocabulary.OWL_VERSION_INFO
                        .getIRI()), lit);
        // Now we can add this as an ontology annotation Apply the change in the
        // usual way
        man.applyChange(new AddOntologyAnnotation(ont, anno));
        // The pizza ontology has labels attached to most classes which are
        // translations of class names into Portuguese (pt) we can access these
        // and print them out. At this point, it is worth noting that constants
        // can be typed or untyped. If constants are untyped then they can have
        // language tags, which are optional - typed constant cannot have
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.