How to create instances of a class using jena?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create instances of a class using jena?

anuj kumar
Hi,

I am trying to manipulate the owl file using jena api. My requirement is to add instances to a class and then populate the values of the properties of that instance.
Suppose I have the below class structure:-

classA    (instanceA)
    dataprop1  ("value")
    dataprop2   (5)
    hasClassB1  (instanceB)     {this is an object property}
classB  (instanceB)
   dataprop3  ("value")
   dataprop4   (10)

Sample Code that I am executing


OntModel base = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);

base.read(in, ""); // in the input stream of the owl file
String OWL_NS = base.getNsPrefixURI("owl");
String RDF_NS = base.getNsPrefixURI("rdf");
String DEFAULT_NS = base.getNsPrefixURI("");
OntClass c = base.getOntClass(DEFAULT_NS + "claasA" );
Individual ind1 = c.createIndividual( DEFAULT_NS + "instanceA" );

After executing the above code the owl file does not change. Also how to add property values to the class instance?

Also I got to know that you can insert ,update or delete data from an owl file using sparql.
In general which is a better practice to modify an owl file : use jena or use sparql and what advantage would each option have?
Is there any sample code to modify the owl file using sparql with jena api.(Using pellet as the inference engine)

Thanks
Anuj

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

James Howison-3

On Sep 21, 2008, at 9:10 AM, anuj kumar wrote:

> Hi,
>
> I am trying to manipulate the owl file using jena api. My  
> requirement is to
> add instances to a class and then populate the values of the  
> properties of
> that instance.
> Suppose I have the below class structure:-
>
> classA    (instanceA)
>    dataprop1  ("value")
>    dataprop2   (5)
>    hasClassB1  (instanceB)     {this is an object property}
> classB  (instanceB)
>   dataprop3  ("value")
>   dataprop4   (10)
> *
> Sample Code that I am executing*
>
> OntModel base =
> ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
> base.read(in, ""); // in the input stream of the owl file
> String OWL_NS = base.getNsPrefixURI("owl");
> String RDF_NS = base.getNsPrefixURI("rdf");
> String DEFAULT_NS = base.getNsPrefixURI("");
> OntClass c = base.getOntClass(DEFAULT_NS + "claasA" );

typo above (claasA)?  I think you might want base.createClass here,  
unless the Class is already defined (eg in Protege) in the file you  
read in.  In which case you need to ensure that you spell the request  
right, otherwise you get null and an Exception :)

> Individual ind1 = c.createIndividual( DEFAULT_NS + "instanceA" );

You are expecting the model to hold (in addition to the statements  
read in):

:instanceA rdf:type :classA .

> After executing the above code the owl file does not change.

You do need to save the model, see:

base.write(OutputStream out, String lang ) // eg. "Turtle"
base.writeAll() // includes inferences

> Also how to add
> property values to the class instance?

An Individual is a Resource, so you can use the .addProperty methods  
of Resource.

> Also I got to know that you can insert ,update or delete data from  
> an owl
> file using sparql.
> In general which is a better practice to modify an owl file : use  
> jena or
> use sparql and what advantage would each option have?

An interesting question; I'm not aware of any guides.  SPARQL/Update  
would allow access to the model of the network?

> Is there any sample code to modify the owl file using sparql with jena
> api.(Using pellet as the inference engine)

I find the Model interface methods (createResource, createProperty)  
more convenient than the OntModel (OntClass, Individual etc) methods,  
but I'm only adding and manipulating individual data, not class data.  
These are demonstrated in the tutorials:

eg
http://jena.sourceforge.net/tutorial/RDF_API/#ch-Navigating%20a%20Model

--J
_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

anuj kumar
James,

typo above (claasA)?  I think you might want base.createClass here,
unless the Class is already defined (eg in Protege) in the file you
read in.  In which case you need to ensure that you spell the request
right, otherwise you get null and an Exception :)

Actually the module is to populate/add data(instances and its property values) for an already existing owl file. So the user will just specify the instance name and select the class name which is populated after reading the owl file. So I guess this should not be an issue. Any way thanks for pointing this.

You do need to save the model, see:

base.write(OutputStream out, String lang ) // eg. "Turtle"
base.writeAll() // includes inferences

I think I should use base.write(OutputStream out, String lang) as I am reading from a base owl file which I want to update with the added data. So in this case the outputstream will be pointing to the file which I want to update correct? And this would result in the contents of the old file being deleted and the new contents being added to the file. Would this not cause a performance issue in case the owl file is very large. Suppose all I want to do is add one instance of a class and some of its property values, then in this case it would be a bit strange to delete the full old file and re-write it again.

An Individual is a Resource, so you can use the .addProperty methods
of Resource.

Actually I donot want to create a property , all I want to do is add a value to a the property of a particular instance of a class. So i guess I have 2 options
1)inst.addProperty(Property prop, String str)
   This would add the following statement to the file

   :inst :prop :str  
   What if the property does not exists in the owl file , will it also add the definition of the property and the above statement as well or does it throw an exception?
   In case the property has a value for the instance then executing it would result in the instance having 2 values for the particular property

2)inst.ind1.setPropertyValue(Property property, RDFNode value)  (set the value of the property with the specified value or replaces it if it exists )

I find the Model interface methods (createResource, createProperty)
more convenient than the OntModel (OntClass, Individual etc) methods,
but I'm only adding and manipulating individual data, not class data.

This may seem to basic a question but what is the difference between Model interface and OntModel interface? When would you use Model interface and when OntModel.


Thanks
Anuj

On Sun, Sep 21, 2008 at 10:24 AM, James Howison <[hidden email]> wrote:

On Sep 21, 2008, at 9:10 AM, anuj kumar wrote:

> Hi,
>
> I am trying to manipulate the owl file using jena api. My
> requirement is to
> add instances to a class and then populate the values of the
> properties of
> that instance.
> Suppose I have the below class structure:-
>
> classA    (instanceA)
>    dataprop1  ("value")
>    dataprop2   (5)
>    hasClassB1  (instanceB)     {this is an object property}
> classB  (instanceB)
>   dataprop3  ("value")
>   dataprop4   (10)
> *
> Sample Code that I am executing*
>
> OntModel base =
> ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
> base.read(in, ""); // in the input stream of the owl file
> String OWL_NS = base.getNsPrefixURI("owl");
> String RDF_NS = base.getNsPrefixURI("rdf");
> String DEFAULT_NS = base.getNsPrefixURI("");
> OntClass c = base.getOntClass(DEFAULT_NS + "claasA" );

typo above (claasA)?  I think you might want base.createClass here,
unless the Class is already defined (eg in Protege) in the file you
read in.  In which case you need to ensure that you spell the request
right, otherwise you get null and an Exception :)

> Individual ind1 = c.createIndividual( DEFAULT_NS + "instanceA" );

You are expecting the model to hold (in addition to the statements
read in):

:instanceA rdf:type :classA .

> After executing the above code the owl file does not change.

You do need to save the model, see:

base.write(OutputStream out, String lang ) // eg. "Turtle"
base.writeAll() // includes inferences

> Also how to add
> property values to the class instance?

An Individual is a Resource, so you can use the .addProperty methods
of Resource.

> Also I got to know that you can insert ,update or delete data from
> an owl
> file using sparql.
> In general which is a better practice to modify an owl file : use
> jena or
> use sparql and what advantage would each option have?

An interesting question; I'm not aware of any guides.  SPARQL/Update
would allow access to the model of the network?

> Is there any sample code to modify the owl file using sparql with jena
> api.(Using pellet as the inference engine)

I find the Model interface methods (createResource, createProperty)
more convenient than the OntModel (OntClass, Individual etc) methods,
but I'm only adding and manipulating individual data, not class data.
These are demonstrated in the tutorials:

eg
http://jena.sourceforge.net/tutorial/RDF_API/#ch-Navigating%20a%20Model

--J
_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03


_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

James Howison-3

On Sep 21, 2008, at 8:47 PM, anuj kumar wrote:

> James,
>
>> typo above (claasA)?  I think you might want base.createClass here,
>> unless the Class is already defined (eg in Protege) in the file you
>> read in.  In which case you need to ensure that you spell the request
>> right, otherwise you get null and an Exception :)
>
> Actually the module is to populate/add data(instances and its property
> values) for an already existing owl file. So the user will just  
> specify the
> instance name and select the class name which is populated after  
> reading the
> owl file. So I guess this should not be an issue. Any way thanks for
> pointing this.
>
>> You do need to save the model, see:
>>
>> base.write(OutputStream out, String lang ) // eg. "Turtle"
>> base.writeAll() // includes inferences
>
> I think I should use base.write(OutputStream out, String lang) as I am
> reading from a base owl file which I want to update with the added  
> data. So
> in this case the outputstream will be pointing to the file which I  
> want to
> update correct? And this would result in the contents of the old  
> file being
> deleted and the new contents being added to the file. Would this not  
> cause a
> performance issue in case the owl file is very large. Suppose all I  
> want to
> do is add one instance of a class and some of its property values,  
> then in
> this case it would be a bit strange to delete the full old file and  
> re-write
> it again.

When working with files, Jena always loads the whole Model into memory  
(it has to to parse the XML or Turtle serializations), so there is no  
way to make incremental changes to a file.  If your models won't fit  
in memory you need to look to the 'persistent storage' methods such as  
SDB and TDB.  I mostly use SDB, but I'd start again with TDB if I had  
the choice.

Recognize that inference always happens in main memory, so you are  
limited by RAM as to the size of models you can use a reasoner with.

>> An Individual is a Resource, so you can use the .addProperty methods
>> of Resource.
>
> Actually I donot want to create a property , all I want to do is add  
> a value
> to a the property of a particular instance of a class. So i guess I  
> have 2
> options
> 1)inst.addProperty(Property prop, String str)
>   This would add the following statement to the file
>   :inst :prop :str
>   What if the property does not exists in the owl file , will it  
> also add
> the definition of the property and the above statement as well or  
> does it
> throw an exception?

If you use addProperty(model.getProperty(yourNS+"propname"), str) you  
will get:

:ind :propname "str" .

depending on the inference of the model you might get statements  
defining :prop as, for example an OntProperty, I'm not too clear on  
that. If you stick with the getProperty and getClass methods of  
OntModel you can avoid introducing typos.  Eyeball is another way to  
check typos (it checks for the use of classes and properties that  
aren't described as owl:Class etc).

>   In case the property has a value for the instance then executing  
> it would
> result in the instance having 2 values for the particular property

yes. If the property is defined as owl:functional and you are using  
inference, though an OntModel, this will result an inconsistency  
(unless :str is the same :)

> 2)inst.ind1.setPropertyValue(Property property, RDFNode value)  (set  
> the
> value of the property with the specified value or replaces it if it  
> exists )

Yes, I don't use that interface so I can't comment.  You'd still need  
to use model.getProperty(yourNS+"prop") to get the Property as the  
first argument.  You'd also have to make the right kind of RDFNode (eg  
model.createTypedLiteral() as the second argument.

>> I find the Model interface methods (createResource, createProperty)
>> more convenient than the OntModel (OntClass, Individual etc) methods,
>> but I'm only adding and manipulating individual data, not class data.
>
> This may seem to basic a question but what is the difference between  
> Model
> interface and OntModel interface? When would you use Model interface  
> and
> when OntModel.

OntModel usually, but not always, has inference with it (created using  
one of the OntModel specs).  The language of the interface is much  
more OWL conscious (eg Individual Class) than Model, which is more RDF  
centric.  You can also make sure that you don't accidentally introduce  
new properties and classes into the ontology, byt always using the  
getClass or getProperty methods of an OntModel.

I find thinking in terms of triples more intuitive, which is why I  
favor the Model interface (always available on OntModel too), but  
others may find the OWLy Individual and Class methods more intuitive,  
they are certainly closer to the intent of OWL.

Good luck and feel free to post more questions as you make your way  
through.  Any comments you have on the tutorials and documentation  
would also be appreciated, I'm sure.

--J

>
>
> Thanks
> Anuj
>
> On Sun, Sep 21, 2008 at 10:24 AM, James Howison <[hidden email]>  
> wrote:
>
>>
>> On Sep 21, 2008, at 9:10 AM, anuj kumar wrote:
>>
>>> Hi,
>>>
>>> I am trying to manipulate the owl file using jena api. My
>>> requirement is to
>>> add instances to a class and then populate the values of the
>>> properties of
>>> that instance.
>>> Suppose I have the below class structure:-
>>>
>>> classA    (instanceA)
>>>   dataprop1  ("value")
>>>   dataprop2   (5)
>>>   hasClassB1  (instanceB)     {this is an object property}
>>> classB  (instanceB)
>>>  dataprop3  ("value")
>>>  dataprop4   (10)
>>> *
>>> Sample Code that I am executing*
>>>
>>> OntModel base =
>>> ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
>>> base.read(in, ""); // in the input stream of the owl file
>>> String OWL_NS = base.getNsPrefixURI("owl");
>>> String RDF_NS = base.getNsPrefixURI("rdf");
>>> String DEFAULT_NS = base.getNsPrefixURI("");
>>> OntClass c = base.getOntClass(DEFAULT_NS + "claasA" );
>>
>> typo above (claasA)?  I think you might want base.createClass here,
>> unless the Class is already defined (eg in Protege) in the file you
>> read in.  In which case you need to ensure that you spell the request
>> right, otherwise you get null and an Exception :)
>>
>>> Individual ind1 = c.createIndividual( DEFAULT_NS + "instanceA" );
>>
>> You are expecting the model to hold (in addition to the statements
>> read in):
>>
>> :instanceA rdf:type :classA .
>>
>>> After executing the above code the owl file does not change.
>>
>> You do need to save the model, see:
>>
>> base.write(OutputStream out, String lang ) // eg. "Turtle"
>> base.writeAll() // includes inferences
>>
>>> Also how to add
>>> property values to the class instance?
>>
>> An Individual is a Resource, so you can use the .addProperty methods
>> of Resource.
>>
>>> Also I got to know that you can insert ,update or delete data from
>>> an owl
>>> file using sparql.
>>> In general which is a better practice to modify an owl file : use
>>> jena or
>>> use sparql and what advantage would each option have?
>>
>> An interesting question; I'm not aware of any guides.  SPARQL/Update
>> would allow access to the model of the network?
>>
>>> Is there any sample code to modify the owl file using sparql with  
>>> jena
>>> api.(Using pellet as the inference engine)
>>
>> I find the Model interface methods (createResource, createProperty)
>> more convenient than the OntModel (OntClass, Individual etc) methods,
>> but I'm only adding and manipulating individual data, not class data.
>> These are demonstrated in the tutorials:
>>
>> eg
>> http://jena.sourceforge.net/tutorial/RDF_API/#ch-Navigating%20a%20Model
>>
>> --J
>> _______________________________________________
>> protege-owl mailing list
>> [hidden email]
>> https://mailman.stanford.edu/mailman/listinfo/protege-owl
>>
>> Instructions for unsubscribing:
>> http://protege.stanford.edu/doc/faq.html#01a.03
>>
> _______________________________________________
> protege-owl mailing list
> [hidden email]
> https://mailman.stanford.edu/mailman/listinfo/protege-owl
>
> Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

Thomas Russ
In reply to this post by anuj kumar

On Sep 21, 2008, at 6:10 AM, anuj kumar wrote:

> In general which is a better practice to modify an owl file : use  
> jena or use sparql and what advantage would each option have?

Use Jena.  Or the OWLAPI.  Or the Protege OWL API.

Avoid using SPARQL.

The reason is that any of the first three methods understand OWL and  
thus will operate on the knowledge model.  SPARQL is purely syntactic  
and will operate on the surface syntax of the representation.  But  
there are many semantically equivalent ways to represent OWL models,  
so you can't be completely sure exactly which tuples will be present  
in an RDF serialization of an OWL model.  That makes it very difficult  
to write a set of complete SPARQL rules for doing the updates.  You  
might not have found the appropriate serialization.

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

JMiller

Thomas,

Is "Avoid using SPARQL" a recommendation only for modification, or also for queries?

Jim




Thomas Russ <[hidden email]>
Sent by: [hidden email]

09/22/2008 07:17 PM

Please respond to
User support for the Protege-OWL editor        <[hidden email]>

To
User support for the Protege-OWL editor <[hidden email]>
cc
Subject
Re: [protege-owl] How to create instances of a class using jena?






On Sep 21, 2008, at 6:10 AM, anuj kumar wrote:

> In general which is a better practice to modify an owl file : use  
> jena or use sparql and what advantage would each option have?

Use Jena.  Or the OWLAPI.  Or the Protege OWL API.

Avoid using SPARQL.

The reason is that any of the first three methods understand OWL and  
thus will operate on the knowledge model.  SPARQL is purely syntactic  
and will operate on the surface syntax of the representation.  But  
there are many semantically equivalent ways to represent OWL models,  
so you can't be completely sure exactly which tuples will be present  
in an RDF serialization of an OWL model.  That makes it very difficult  
to write a set of complete SPARQL rules for doing the updates.  You  
might not have found the appropriate serialization.

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03




_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03 
Reply | Threaded
Open this post in threaded view
|

Re: How to create instances of a class using jena?

James Howison-3
Jim, to understand Thomas' argument it might help to search the  
mailing list for messages by Thomas discussing this issue.  It comes  
up often.  I'm afraid that I've never fully understood the risks; a  
working paper length explanation of Thomas' point would be greatly  
appreciated.

Until then we are limited to discussions like the one I quote below:

On May 21, 2008, at 1:27 PM, Thomas Russ wrote:

>
> On May 21, 2008, at 4:36 AM, James Howison wrote:
>
>>
>> On May 20, 2008, at 7:13 PM, Martin O'Connor wrote:
>>
>>> anuj kumar wrote:
>>>
>>>> SPARQL is used to query RDF , so does that mean OWL is first
>>>> converted
>>>> to an RFD representation while implementing SPARQL queries with  
>>>> jena
>>>> and since RDF is not as rich as OWL we don't get good results using
>>>> SPARQL with OWL. Is my understanding correct?
>>>
>>> Yes. When applied to OWL, SPARQL is typically used to query the
>>> serialized RDF representation of an OWL ontology. This is  
>>> problematic
>>> because this serialization can be extremely complex and is not
>>> deterministic.
>>
>> Thanks, this is interesting.  I had thought that the RDF
>> representation of OWL was deterministic and that SPARQL against a
>> 'fully-reasoned' model, while not always as convenient as one would
>> want, was at least fully capable.  Are there any resources around  
>> that
>> explain the non-determinacy of OWL as RDF?
>
> Well, I suppose in a strictly technical sense, the serialization of
> OWL into RDF is deterministic, in that a given program will always
> produce the same serialization for a given ontology.  But any change
> to the ontology can result in a radically different serialization, so
> that it is not trivially predictable.
>
> Part of this is related to options available in the serialized form of
> OWL.  For example, the following are both valid serializations of
> "Fred is a Person":
>
> <Person rdf:id="Fred"/>
>
> <owl:Thing rdf:about="#Fred">
>   <rdf:type rdf:resource="#Person">
> </owl:Individual>
>
> But working on the RDF, you would have to use different SPARQL queries
> to get the semantic results you would expect if you wanted to get all
> of the instances of Person.

What I never understood about this point is that these are equivalent  
in terms of triples, where SPARQL operates, just convert them to  
Turtle to see that:

:Fred rdf:type :Person .

So, as far as I can see, this example of different serializations  
doesn't pose a problem.

An example of where a change to an OWL ontology would result in a  
different set of RDF triples (as opposed to a differently serialized  
model) would be greatly appreciated.  I'm not suggesting that they  
don't exist---I don't know enough to say that---but I haven't come  
across any (which admittedly is not saying much!).

Cheers,
James

ps.  none of this is to suggest that SQWRL isn't helpful or that a  
more OWL aware SPARQL wouldn't be fantastic (for example specifying  
levels of inference in a query).

On Sep 23, 2008, at 11:44 AM, James A Miller wrote:

> Thomas,
>
> Is "Avoid using SPARQL" a recommendation only for modification, or  
> also
> for queries?
>
> Jim
>
>
>
>
>
> Thomas Russ <[hidden email]>
> Sent by: [hidden email]
> 09/22/2008 07:17 PM
> Please respond to
> User support for the Protege-OWL editor <[hidden email]
> >
>
>
> To
> User support for the Protege-OWL editor <[hidden email]
> >
> cc
>
> Subject
> Re: [protege-owl] How to create instances of a class using jena?
>
>
>
>
>
>
>
> On Sep 21, 2008, at 6:10 AM, anuj kumar wrote:
>
>> In general which is a better practice to modify an owl file : use
>> jena or use sparql and what advantage would each option have?
>
> Use Jena.  Or the OWLAPI.  Or the Protege OWL API.
>
> Avoid using SPARQL.
>
> The reason is that any of the first three methods understand OWL and
> thus will operate on the knowledge model.  SPARQL is purely syntactic
> and will operate on the surface syntax of the representation.  But
> there are many semantically equivalent ways to represent OWL models,
> so you can't be completely sure exactly which tuples will be present
> in an RDF serialization of an OWL model.  That makes it very difficult
> to write a set of complete SPARQL rules for doing the updates.  You
> might not have found the appropriate serialization.
>
> _______________________________________________
> protege-owl mailing list
> [hidden email]
> https://mailman.stanford.edu/mailman/listinfo/protege-owl
>
> Instructions for unsubscribing:
> http://protege.stanford.edu/doc/faq.html#01a.03
>
>
>
> _______________________________________________
> protege-owl mailing list
> [hidden email]
> https://mailman.stanford.edu/mailman/listinfo/protege-owl
>
> Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03