Question

I'm trying create an Ontology usuing Jena API and the output will be in OWL as pasted down.

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class BandsOntology {

    public static void main(String[] args) 
    {
        // Create an empty ontology model
        OntModel m = ModelFactory.createOntologyModel();
        String baseURI = "http://www.bands.fr";
        String NSBand = baseURI + "#";

        m.setNsPrefix("Band", NSBand);

        OntClass Band = m.createClass(NSBand + "Band");
        OntClass Guitarist = m.createClass(NSBand + "Guitarist");
        OntClass Bassist = m.createClass(NSBand + "Bassist");
        OntClass Keyboardist = m.createClass(NSBand + "Keyboardist");
        OntClass Vocalist  = m.createClass(NSBand + "Vocalist ");

        ObjectProperty bandComposedBy = m.createObjectProperty(NSBand +"composedBy");
        bandComposedBy.hasDomain(Band);
        bandComposedBy.hasRange(Guitarist);
        bandComposedBy.hasRange(Bassist);
        bandComposedBy.hasRange(Keyboardist);       
        bandComposedBy.hasRange(Vocalist);      

        Individual queen = Band.createIndividual(NSBand + "Queen");
        Individual guitarist01 = Guitarist.createIndividual(NSBand + "guita01");        

        m.add(queen, bandComposedBy, guitarist01);

        m.write(System.out, Constants.TYPE_RDF_XML_ABBREV);

    }

}

And the output on console :

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:Band="http://www.bands.fr#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Class rdf:about="http://www.bands.fr#Bassist"/>
  <owl:Class rdf:about="http://www.bands.fr#Band"/>
  <owl:Class rdf:about="http://www.bands.fr#Vocalist "/>
  <owl:Class rdf:about="http://www.bands.fr#Keyboardist"/>
  <owl:Class rdf:about="http://www.bands.fr#Guitarist"/>
  <owl:ObjectProperty rdf:about="http://www.bands.fr#composedBy"/>
  <Band:Band rdf:about="http://www.bands.fr#Queen">
    <Band:composedBy>
      <Band:Guitarist rdf:about="http://www.bands.fr#guita01"/>
    </Band:composedBy>
  </Band:Band>
</rdf:RDF>

I'm asking why after declaring the property, I didn't get the "composedBy" property declared as I coded.

<owl:ObjectProperty rdf:about="http://www.bands.fr#composedBy">
    <rdfs:domain rdf:resource="http://www.bands.fr#Band"/>
    <rdfs:range rdf:resource="http://www.bands.fr#Bassist"/>
    <rdfs:range rdf:resource="http://www.bands.fr#Guitarist"/>
    ...
</owl:ObjectProperty>
Was it helpful?

Solution

hasDomain and hasRange don't add domains and ranges, they just return true if the propert has the specified domain and range. This is explained in the Javadoc for these methods, and follows the typical naming convention in Java that methods beginning with has- or is- return true if an object satisfies some condition.

You should use addDomain and addRange instead. That is, instead of

bandComposedBy.hasDomain(Band);
bandComposedBy.hasRange(Guitarist);
bandComposedBy.hasRange(Bassist);
bandComposedBy.hasRange(Keyboardist);       
bandComposedBy.hasRange(Vocalist);   

you should use:

bandComposedBy.addDomain(Band);
bandComposedBy.addRange(Guitarist);
bandComposedBy.addRange(Bassist);
bandComposedBy.addRange(Keyboardist);       
bandComposedBy.addRange(Vocalist);   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top