You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Luis Eufrasio Teixeira Neto <lu...@gmail.com> on 2013/09/04 15:00:51 UTC

Help loading Ontology

Dear All,

I am trying to load an Ontology (attached) where there are classes with the
same datatype property. For example:

foaf:Document  a       owl:Class , rdfs:Class ;
      rdfs:label "foaf:Document" ;
      rdfs:subClassOf [ a owl:Restriction ;
            owl:onProperty dc:creator ;
            owl:maxCardinality "2"^^<http://www.w3.org/2001/XMLSchema#int>
      ] .

dc:title
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain foaf:Document;
      rdfs:label "dc:title" ;
      rdfs:range xsd:string .

*dc:date*
*      a       rdf:Property , owl:DatatypeProperty ;*
*      rdfs:domain foaf:Document;*
*      rdfs:label "dc:date" ;*
*      rdfs:range xsd:string .*

and

conf:Conference  a       owl:Class , rdfs:Class ;
      rdfs:label "conf:Conference" .

rdfs:label
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain conf:Conference;
      rdfs:label "rdfs:label" ;
      rdfs:range xsd:string .

*dc:date*
*      a       rdf:Property , owl:DatatypeProperty ;*
*      rdfs:domain conf:Conference;*
*      rdfs:label "dc:date" ;*
*      rdfs:range xsd:string .*
*
*
So datatype property dc:date is reused into classes foaf:Document and
conf:Conference.

The problem is: when I load the ontology the datatype porperty dc:date only
appears one time with conf:Conference as domain class.

I need that it appears for the two classes. Below follows the code I wrote
for loading it:

        OntModel ontModel = ModelFactory
                .createOntologyModel(OntModelSpec.OWL_MEM);
        String filePath = mc.getOntologyFilePath();
        if (filePath != null && !"".equals(filePath)) {
            ontModel.read(new FileInputStream(new File(filePath)),
                    "http://ufc.br/rdb2rdfmb/", mc.getOntologyLang());
        } else {
            ontModel.read(mc.getOntologyURL(), mc.getOntologyLang());
        }

        ExtendedIterator<OntClass> i = ontModel.listClasses();
        while (i.hasNext()) {
            OntClass ontClass = (OntClass) i.next();
            String prefix =
ontModel.getNsURIPrefix(ontClass.getNameSpace());
            String name = ontClass.getLocalName();

            if (classes.get(name + prefix) == null) {
                Class_ c = new Class_(prefix, name);
                classes.put(prefix + name, c);
                mapPrefixes.put(prefix, ontClass.getNameSpace());

                OntClass superClass = ontClass.getSuperClass();
                if (superClass != null) {
                    String superName = superClass.getLocalName();
                    String superPrefix =
ontModel.getNsURIPrefix(superClass.getNameSpace());
                    Class_ superC = classes.get(superPrefix + superName);

                    if (superC == null) {
                        superC = new Class_(superPrefix, superName);
                        classes.put(superPrefix + superName, superC);
                        mapPrefixes.put(superPrefix,
superClass.getNameSpace());
                    }

                    c.setSuperClass(superC);
                }
            }
        }

        ExtendedIterator<DatatypeProperty> i2 = ontModel
                .listDatatypeProperties();
        while (i2.hasNext()) {
            DatatypeProperty datatypeProperty = (DatatypeProperty)
i2.next();

            if (datatypeProperty.getDomain() != null &&
datatypeProperty.getRange() != null) {
                String dClassPrefix =
ontModel.getNsURIPrefix(datatypeProperty.getDomain().getNameSpace());
                String dClassName =
datatypeProperty.getDomain().getLocalName();
                Class_ dClass = classes.get(dClassPrefix + dClassName);
                String dpName = datatypeProperty.getLocalName();
                String dpPrefix =
ontModel.getNsURIPrefix(datatypeProperty.getNameSpace());
                String rangeName =
datatypeProperty.getRange().getLocalName();

                DataProperty dp = new DataProperty(dpPrefix, dpName,
dClass, rangeName);
                if (dClass != null) {
                    dClass.getdProperties().add(dp);
                    mapPrefixes.put(dpPrefix,
datatypeProperty.getNameSpace());
                }
            }
        }

-- 
Kind Regards,
Luís Eufrasio T. Neto
MSN: luis_eufrasio@hotmail.com

"Ser LIVRE é ser capaz de AMAR sem exigir nada em troca."

Re: Help loading Ontology

Posted by Luis Eufrasio Teixeira Neto <lu...@gmail.com>.
Thanks for all responses,

listDomain sugested by Dave Reynolds solved my problem.


2013/9/4 Chris Dollin <ch...@epimorphics.com>

> On Wednesday, September 04, 2013 10:00:51 AM Luis Eufrasio Teixeira Neto
> wrote:
>
> Non-date remarks:
>
> >         ExtendedIterator<OntClass> i = ontModel.listClasses();
> >         while (i.hasNext()) {
> >             OntClass ontClass = (OntClass) i.next();
> >             String prefix =
> > ontModel.getNsURIPrefix(ontClass.getNameSpace());
> >             String name = ontClass.getLocalName();
> >
> >             if (classes.get(name + prefix) == null) {
> >                 Class_ c = new Class_(prefix, name);
> >                 classes.put(prefix + name, c);
> >                 mapPrefixes.put(prefix, ontClass.getNameSpace());
>
> (a) Rather than using mapPrefixes, just use the PrefixMapping of
>     the model, or even the model itself.
>
> (b) If you want a short name for the class, you can use Model.shortForm,
>     which applies any available prefix from the model.
>
> >                 OntClass superClass = ontClass.getSuperClass();
>
> (c) Note that a class can have multiple distinct superclasses. Assuming
>    that there's only one will trip you up.
>
> >                 if (superClass != null) {
> >                     String superName = superClass.getLocalName();
> >                     String superPrefix =
> > ontModel.getNsURIPrefix(superClass.getNameSpace());
> >                     Class_ superC = classes.get(superPrefix + superName);
> >
> >                     if (superC == null) {
> >                         superC = new Class_(superPrefix, superName);
> >                         classes.put(superPrefix + superName, superC);
> >                         mapPrefixes.put(superPrefix,
> > superClass.getNameSpace());
> >                     }
> >
> >                     c.setSuperClass(superC);
> >                 }
> >             }
> >         }
>
> Exactly why are you extracting all this information from the model
> when the model holds it quite nicely already?
>
> Chris
>
> --
> "I know it was late, but Mountjoy never bothers,                /Archer's
> Goon/
>  so long as it's the full two thousand words."
>
> Epimorphics Ltd, http://www.epimorphics.com
> Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20
> 6PT
> Epimorphics Ltd. is a limited company registered in England (number
> 7016688)
>
>


-- 
Um abraço,
Luís Eufrasio T. Neto
MSN: luis_eufrasio@hotmail.com

"Ser LIVRE é ser capaz de AMAR sem exigir nada em troca."

Re: Help loading Ontology

Posted by Chris Dollin <ch...@epimorphics.com>.
On Wednesday, September 04, 2013 10:00:51 AM Luis Eufrasio Teixeira Neto wrote:

Non-date remarks:

>         ExtendedIterator<OntClass> i = ontModel.listClasses();
>         while (i.hasNext()) {
>             OntClass ontClass = (OntClass) i.next();
>             String prefix =
> ontModel.getNsURIPrefix(ontClass.getNameSpace());
>             String name = ontClass.getLocalName();
> 
>             if (classes.get(name + prefix) == null) {
>                 Class_ c = new Class_(prefix, name);
>                 classes.put(prefix + name, c);
>                 mapPrefixes.put(prefix, ontClass.getNameSpace());

(a) Rather than using mapPrefixes, just use the PrefixMapping of
    the model, or even the model itself.

(b) If you want a short name for the class, you can use Model.shortForm,
    which applies any available prefix from the model.

>                 OntClass superClass = ontClass.getSuperClass();

(c) Note that a class can have multiple distinct superclasses. Assuming
   that there's only one will trip you up.

>                 if (superClass != null) {
>                     String superName = superClass.getLocalName();
>                     String superPrefix =
> ontModel.getNsURIPrefix(superClass.getNameSpace());
>                     Class_ superC = classes.get(superPrefix + superName);
> 
>                     if (superC == null) {
>                         superC = new Class_(superPrefix, superName);
>                         classes.put(superPrefix + superName, superC);
>                         mapPrefixes.put(superPrefix,
> superClass.getNameSpace());
>                     }
> 
>                     c.setSuperClass(superC);
>                 }
>             }
>         }

Exactly why are you extracting all this information from the model
when the model holds it quite nicely already?

Chris

-- 
"I know it was late, but Mountjoy never bothers,                /Archer's Goon/
 so long as it's the full two thousand words."

Epimorphics Ltd, http://www.epimorphics.com
Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20 6PT
Epimorphics Ltd. is a limited company registered in England (number 7016688)


RE: Help loading Ontology

Posted by Ed Swing <Ed...@sas.com>.
There are a couple of possibilities you might consider:


1)      Create subproperties of date for use with each different class. This is probably the best approach, as you won’t get confused with your properties, and the property name (publishDate, eventDate) would indicate what it’s used for.

2)      Create a union of the classes (Datable?), and define the domain to be the union class.


From: Luis Eufrasio Teixeira Neto [mailto:luis.eufrasio@gmail.com]
Sent: Wednesday, September 04, 2013 9:01 AM
To: users@jena.apache.org
Subject: Help loading Ontology

Dear All,

I am trying to load an Ontology (attached) where there are classes with the same datatype property. For example:

foaf:Document  a       owl:Class , rdfs:Class ;
      rdfs:label "foaf:Document" ;
      rdfs:subClassOf [ a owl:Restriction ;
            owl:onProperty dc:creator ;
            owl:maxCardinality "2"^^<http://www.w3.org/2001/XMLSchema#int>
      ] .

dc:title
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain foaf:Document;
      rdfs:label "dc:title" ;
      rdfs:range xsd:string .

dc:date
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain foaf:Document;
      rdfs:label "dc:date" ;
      rdfs:range xsd:string .

and

conf:Conference  a       owl:Class , rdfs:Class ;
      rdfs:label "conf:Conference" .

rdfs:label
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain conf:Conference;
      rdfs:label "rdfs:label" ;
      rdfs:range xsd:string .

dc:date
      a       rdf:Property , owl:DatatypeProperty ;
      rdfs:domain conf:Conference;
      rdfs:label "dc:date" ;
      rdfs:range xsd:string .

So datatype property dc:date is reused into classes foaf:Document and conf:Conference.

The problem is: when I load the ontology the datatype porperty dc:date only appears one time with conf:Conference as domain class.

I need that it appears for the two classes. Below follows the code I wrote for loading it:

        OntModel ontModel = ModelFactory
                .createOntologyModel(OntModelSpec.OWL_MEM);
        String filePath = mc.getOntologyFilePath();
        if (filePath != null && !"".equals(filePath)) {
            ontModel.read(new FileInputStream(new File(filePath)),
                    "http://ufc.br/rdb2rdfmb/", mc.getOntologyLang());
        } else {
            ontModel.read(mc.getOntologyURL(), mc.getOntologyLang());
        }

        ExtendedIterator<OntClass> i = ontModel.listClasses();
        while (i.hasNext()) {
            OntClass ontClass = (OntClass) i.next();
            String prefix = ontModel.getNsURIPrefix(ontClass.getNameSpace());
            String name = ontClass.getLocalName();

            if (classes.get(name + prefix) == null) {
                Class_ c = new Class_(prefix, name);
                classes.put(prefix + name, c);
                mapPrefixes.put(prefix, ontClass.getNameSpace());

                OntClass superClass = ontClass.getSuperClass();
                if (superClass != null) {
                    String superName = superClass.getLocalName();
                    String superPrefix = ontModel.getNsURIPrefix(superClass.getNameSpace());
                    Class_ superC = classes.get(superPrefix + superName);

                    if (superC == null) {
                        superC = new Class_(superPrefix, superName);
                        classes.put(superPrefix + superName, superC);
                        mapPrefixes.put(superPrefix, superClass.getNameSpace());
                    }

                    c.setSuperClass(superC);
                }
            }
        }

        ExtendedIterator<DatatypeProperty> i2 = ontModel
                .listDatatypeProperties();
        while (i2.hasNext()) {
            DatatypeProperty datatypeProperty = (DatatypeProperty) i2.next();

            if (datatypeProperty.getDomain() != null && datatypeProperty.getRange() != null) {
                String dClassPrefix = ontModel.getNsURIPrefix(datatypeProperty.getDomain().getNameSpace());
                String dClassName = datatypeProperty.getDomain().getLocalName();
                Class_ dClass = classes.get(dClassPrefix + dClassName);
                String dpName = datatypeProperty.getLocalName();
                String dpPrefix = ontModel.getNsURIPrefix(datatypeProperty.getNameSpace());
                String rangeName = datatypeProperty.getRange().getLocalName();

                DataProperty dp = new DataProperty(dpPrefix, dpName, dClass, rangeName);
                if (dClass != null) {
                    dClass.getdProperties().add(dp);
                    mapPrefixes.put(dpPrefix, datatypeProperty.getNameSpace());
                }
            }
        }

--
Kind Regards,
Luís Eufrasio T. Neto
MSN: luis_eufrasio@hotmail.com<ma...@hotmail.com>

"Ser LIVRE é ser capaz de AMAR sem exigir nada em troca."

Re: Help loading Ontology

Posted by Dave Reynolds <da...@gmail.com>.
On 04/09/13 14:00, Luis Eufrasio Teixeira Neto wrote:
> Dear All,
>
> I am trying to load an Ontology (attached)

This list doesn't support attachments but see below ...

> where there are classes with
> the same datatype property. For example:
>
> foaf:Document  a       owl:Class , rdfs:Class ;
>        rdfs:label "foaf:Document" ;
>        rdfs:subClassOf [ a owl:Restriction ;
>              owl:onProperty dc:creator ;
>              owl:maxCardinality "2"^^<http://www.w3.org/2001/XMLSchema#int>
>        ] .
> dc:title
>        a       rdf:Property , owl:DatatypeProperty ;
>        rdfs:domain foaf:Document;
>        rdfs:label "dc:title" ;
>        rdfs:range xsd:string .
> *dc:date*
> *      a       rdf:Property , owl:DatatypeProperty ;*
> *      rdfs:domain foaf:Document;*
> *      rdfs:label "dc:date" ;*
> *      rdfs:range xsd:string .*
>
> and
>
> conf:Conference  a       owl:Class , rdfs:Class ;
>        rdfs:label "conf:Conference" .
> rdfs:label
>        a       rdf:Property , owl:DatatypeProperty ;
>        rdfs:domain conf:Conference;
>        rdfs:label "rdfs:label" ;
>        rdfs:range xsd:string .
> *dc:date*
> *      a       rdf:Property , owl:DatatypeProperty ;*
> *      rdfs:domain conf:Conference;*
> *      rdfs:label "dc:date" ;*
> *      rdfs:range xsd:string .*
> *
> *
> So datatype property dc:date is reused into classes foaf:Document and
> conf:Conference.

Ugh. That's not a good idea and doesn't mean what you think it means. 
The net effect is that semantically you are saying that anything with a 
dc:date is both a conf:Conference and a foaf:Document. Whereas probably 
there is nothing in the intersection of those two.

Ideally don't redeclare dc:date at all. If you do want to express that 
e.g. a conf:Conference can have a dc:date then use an OWL restriction.

> The problem is: when I load the ontology the datatype porperty dc:date
> only appears one time with conf:Conference as domain class.

No, both declarations will be in the model, you can check this by 
listing all the properties of dc:date in your model.

You are only seeing one in your code because ...

>              if (datatypeProperty.getDomain() != null &&
> datatypeProperty.getRange() != null) {
>                  String dClassPrefix =
> ontModel.getNsURIPrefix(datatypeProperty.getDomain().getNameSpace());

... here you are calling getDomain which can only return a single 
domain, if there are multiple ones then it will just pick one.

Use OntClass.listDomain instead.

Dave