2

I'm using java and the Jena API. I have a class Marriage which has 3 Object Properties called "hasHusband", "Haswife" and "dateOfMarriage". The first two are associated with a class Person which has the datatype properties like first name, last name, date of birth...

I'd like to retrieve the wife's first name and the husband's first name. Could you explain me how can I do this?

Here is the relevant part of my rdf file:

(...)

<j.0:FAMmariage rdf:about=http://www.fam.com/FAM#BrunoCatherine> 

 <j.0:FAMaDateMariage>25/07/2011</j.0:FAMaDateMariage>

 <j.0:FAMhasWife>
    <rdf:Description rdf:about="http://www.fam.com/FAM#Catherine20/03/1982">
    <j.0:FAMDateOfBirth>20/03/1980</j.0:FAMDateOfBirth>
    <j.0:FAMHasName>Gomez</j.0:FAMHasName>
    <j.0:FAMHasFirstName>Catherine</j.0:FAMHasFirstName>
  </rdf:Description>
 </j.0:FAMHasWife>

 <j.0:FAMHusband>
  <rdf:Description rdf:about="http://www.fam.com/FAM# Bruno15/06/1980 ">
    <j.0:FAMaDateOfBirth>15/06/1980 </j.0:FAMDateOfBirth>
    <j.0:FAMHasName>Jeandet </j.0:FAMHasName>
    <j.0:FAMHasFirstName>Bruno</j.0:FAMHasFirstName>
  </rdf:Description>
 </j.0:FAMHusband>

</j.0:FAMmariage>
(...)

Thanks

EDITED Through the code below I can get the hasWife object property. What should I add to get the firstName datatypeproperty?

//Object Property hasWife

     StmtIterator iter = onto.model.listStatements(null,onto.hasWife,(RDFNode)null);  

     while (iter.hasNext()) 
     {
        Statement stmt = iter.nextStatement();  
        System.out.println(stmt.getObject().toString());                          
     }
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
user1632278
  • 111
  • 6

1 Answers1

2

This is what SPARQL is for. Have a look at Jena's SPARQL Tutorial.

As an alternative you could can programatically search the model, which is described in Querying a Model. For instance by using listStatements(Subject, Predicate, Object) where you can decide which parameters should be null.

In your case you could use listStatements(null, hasWifeProperty, null) and listStatements(null, hasHusbandProperty, null) and access the corresponding object of the result.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • Thanks for the answer. I tried but it still does not work, please look the code below. Can you tell me what's wrong? Thanks StmtIterator iter = onto.model.listStatements(); while (iter.hasNext()) { Statement stmt = iter.nextStatement(); Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject(); if (predicate.equals(onto.hasWifeProperty)){ System.out.print(" " + object.toString() + " "); }} – user1632278 Aug 30 '12 at 07:13
  • @user1632278 Since you have the RDF data already, take the URI identifier of the property then you can get the property object by invoking: `model.getProperty("http://...")` – Konrad Reiche Aug 30 '12 at 10:30
  • I edited my question. Please see the code I added, I can already get the property object hasWife and hasHusband, but not the firstName. Can you tell me what should I add to get the firstName through hasWife and hasHusband. Thanks again. – user1632278 Aug 30 '12 at 15:34
  • @user1632278 You can cast your object to a resource: `((Resource) stmt.getObject())` and access with the method that seems the most convenient to you, [see the API for that](http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Resource.html). For instance with `listProperties()`. – Konrad Reiche Aug 30 '12 at 15:49