简体   繁体   中英

How to create an ontology in Java?

I've some data triplets that I want to write in some sort of basic OWL ontology. I've triplets like:

Delhi is part of India 

or

India is an Asian country

Note that I've relations like "is-a", "part-of", or "related-to". What's the simplest way to build an ontology? Any working example or a reference to an example website will be great help!

There are a lot of different things mixed up in your question, I strongly suggest you take a bit of time (away from the keyboard.) to think through what you're trying to achieve here.

Firstly, geographic ontologies can get quite complex, and a lot of work has already been done in this area. Probably the obvious starting point is the GeoNames ontology , which gives names to geographic features, including cities like Dehli and countries like India. At the very least you should re-use those names for the places in your application, as that will maximise the chances that your data can be successfully joined with other available linked-data sources.

However, you probably don't want the whole of GeoNames in your application (I'm guessing), so you also need to be clear why you need an ontology at all. A good way to approach this is from the outside of your application: rather than worry about which kind of Jena model to use, start by thinking through ways to complete the sentence "using the ontology, a user of my application will be able to..." . That should then lead you on to establishing some competency questions (see, for example, section 3 of this guide ) for your ontology. Once you know what kinds of information you want to represent, and what kinds of queries you need to apply to it, your technology choices will be much clearer. I realise that these applications are typically developed iteratively, and you'll want to try some code out fairly early on, but I still advocate getting your destination more clearly in mind before you start your coding journey.

You imply that you want to use Jena to drive a web site. There are many choices here. Don't be mislead by the term semantic web - this actually means bringing web-like qualities to interlined data sets, rather than putting semantics into human readable web pages per se. While you can do so, and many people do, you'll need some additional layers in your architecture. We typically use one of two approaches: using Jena with a templating engine, such as Velocity , in a servlets container, or using a Ruby web framework and driving Jena via JRuby . There are many other ways to solve this particular problem: Jena doesn't address web publishing directly, but it can be used within any Java-based web framework.

Finally, regarding namespaces, you should really re-use existing vocabularies, and hence namespaces, where possible. Don't make up new names for things which already have representations on the web of data somewhere. Use GeoNames, or DbPedia , or any of the many other published vocabularies where they fit. If they don't fit, then you should create a new name rather than use an existing name in a non-compatible way. In this case, you should use the web domain of your application (eg your company or university) as the basis for the namespace. Ideally, you should publish your ontology at the base URL of the namespace, but this can sometimes be hard to arrange depending on local web policies.

I suggest OWL API from Manchester University. In this way you can start to create your ontology "on the fly" in Java, and with a single method invocation you can serialize it in your preferred format (RDF, Manchester Syntax etc) if you need, or directly working on the in-memory representation. In this way you can rapidly prototype and experiment your ontology in the context of your program.

For an overview of the library and its main componenets I suggest the tutorial ( code tutorial ) provided by the creator of the library, it covers 90% of the basic needs.

PS: Protégé is based on OWL Api, you can also try it as suggested, but expecially in the beginning I preferred to rapidly play with ontologies and switch to some engineering environment like Protege when my mind was clear enough. In addition, with an external ontology you would need to learn how to navigate it, that IMHO it is really not worth in the very beginning.

/**  
  - This is maven dependencies for owl-api
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-api</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-apibinding</artifactId>
    </dependency>

    * First of all you need to initialize ontology:

 **/   
                    private OWLDataFactory factory;

                    private PrefixManager pm;

                    private OWLOntology ontology;

                    private String pmString = "#";

                    private OWLOntologyManager manager;

                    private OWLReasoner reasoner;

                    private ShortFormEntityChecker entityChecker;

                    private BidirectionalShortFormProviderAdapter bidirectionalShortFormProviderAdapter;

             private void initializeOntology(String fileContent)
                            throws OWLOntologyCreationException {
                        InputStream bstream = new ByteArrayInputStream(fileContent.getBytes());
                        this.manager = OWLManager.createOWLOntologyManager();
                        this.ontology = this.manager.loadOntologyFromOntologyDocument(bstream);
                        IRI ontologyIRI = this.ontology.getOntologyID().getOntologyIRI();
                        this.pm = new DefaultPrefixManager(ontologyIRI.toString()
                                + this.pmString);
                        this.factory = this.manager.getOWLDataFactory();

                        ReasonerFactory factory = new ReasonerFactory();
                        this.reasoner = factory.createReasoner(this.ontology);

                        Set<OWLOntology> onts = new HashSet<>();
                        onts.add(this.ontology);

                        DefaultPrefixManager defaultPrefixManager = new DefaultPrefixManager(
                                this.pm);
                        ShortFormProvider shortFormProvider = new ManchesterOWLSyntaxPrefixNameShortFormProvider(
                                defaultPrefixManager);
                        this.bidirectionalShortFormProviderAdapter = new BidirectionalShortFormProviderAdapter(
                                this.manager, onts, shortFormProvider);
                        this.entityChecker = new ShortFormEntityChecker(
                                this.bidirectionalShortFormProviderAdapter);

                    }

/*
    After that you need to define your classes and the relations of the classes. These relations calls as object properties in ontology. Instance of each ontology class calls as individual and the attributies of the classes (for person name, age , adress) calls as data-property.
*/
//    To create a new individual of an ontology class : 



         public OWLClass getClass(String className) {
                 return this.factory.getOWLClass(":" + className, this.pm);
            }




            public OWLNamedIndividual createInvidual(String cls, String invname) {
                                        OWLNamedIndividual res = this.factory.getOWLNamedIndividual(":"
                                                + invname, this.pm);
                                        this.manager.addAxiom(this.ontology,
                                                this.factory.getOWLDeclarationAxiom(res));
                                        OWLClassAssertionAxiom axiom = this.factory.getOWLClassAssertionAxiom(
                                                getClass(cls), res);
                                        this.manager.addAxiom(this.ontology, axiom);
                                        return res;
            }

 //   To create an object property :

 //   This method will create an object property named prop if it is not exist.



         public OWLObjectProperty getObjectProperty(String prop) {
                    return this.factory.getOWLObjectProperty(":" + prop, this.pm);
                }

            public void addObjectProperty(String propname, OWLNamedIndividual prop,
                        OWLNamedIndividual obj) {
                    OWLObjectPropertyAssertionAxiom axiom = this.factory
                            .getOWLObjectPropertyAssertionAxiom(
                                    getObjectProperty(propname), obj, prop);
                    this.manager.addAxiom(this.ontology, axiom);
                }

  //  And finally , to add a data-property to individuals :

        public OWLDataProperty getDataProperty(String prop) {
                        return this.factory.getOWLDataProperty(":" + prop, this.pm);
                    }

        public void addDataProperty(String propname, boolean propvalue,
                        OWLNamedIndividual inv) {
                    OWLAxiom axiom = this.factory.getOWLDataPropertyAssertionAxiom(
                            getDataProperty(propname), inv, propvalue);
                    this.manager.addAxiom(this.ontology, axiom);
        }

Have a look at Stanford's Protege . It's an ontology editor.

You'd just declare a triplet class consisting of a subject, object, and predicate. "has-a" is a predicate, so your ontology elements would look like:

"Dehli", "is-in", "India"
"India", "is-in", "Asia"
"India", "is-a", "country"

This doesn't address queries, of course, but given a decent data store (even a database would do) you could start to build a flexible ontology with a decent query mechanism.

JENA is far, far more capable than what this would create, of course; it does provide the semantic query stuff, as well as far better resource definition and resolution. However, it's a lot more involved than a simple triplet structure; it all depends on what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM