Question

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());                          
     }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top