Question

I'm using Java and Jena API. I have a class Marriage which have 3 Object Properties called "hasHusband", "Haswife" and "dateOfMarriage". The first two are associated with a class Person which has the datatypeproperties hasFirstName, hasLastName, dateOfBirth....

Through the code below I can access the hasFirstName property of the husband.

StmtIterator iter = onto.model.listStatements(null,onto.hasHusband,(RDFNode)null);  
while (iter.hasNext()) {
    Statement stmt = iter.nextStatement();  
    Resource P = ((Resource) stmt.getObject());

    StmtIterator iter2 = onto.model.listStatements(((Resource) P),onto.hasFirstName,(RDFNode)null);  
    while (iter2.hasNext()) {
         Statement stmt2 = iter2.nextStatement();  
         firstnameHusband = stmt2.getObject().toString();
    }}

I would like to modify this line

 StmtIterator iter2 = onto.model.listStatements(((Resource) P),onto.hasFirstName,(RDFNode)null);  

in order to access also the hasLastName and hasDateofBirth...

Can you explain me how can I do this?

Thanks

EDITED: @Pierre Now it concerns only the class Person. In case of womans I want to output in a new file (text file) this line below for each woman:

[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape2+"]

And for each man this:

[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape+"]

The diference is in the value of shape. The problem I have is that he only outputs one woman and one man.

A person is represented like this in my rdf file:

  <rdf:Description rdf:about="http://www.fam.com/FAM#Bruno04/02/1980 ">
    <j.0:FAMhasGender>H</j.0:FAMhasGender>
    <j.0:FAMhasDateOfBirth>04/02/1980</j.0:FAMhasDateOfBirth>
    <j.0:FAMhasLastName>DS </j.0:FAMhasLastName>
    <j.0:FAMhasFirstName> Bruno</j.0:FAMhasFirstName>
  </rdf:Description>

Here is the relevant code:

public void accessProp() {

    readFile(inputFile); // rdf
    String fname;
    String dd;
    String gen;

    ExtendedIterator instances = onto.person.listInstances();
    Individual instance = null;
    Individual firstInstance = null;
    while (instances.hasNext()) {
        instance = (Individual) instances.next();

        gen = instance.getPropertyValue(onto.hasGender).toString();
        fname = instance.getPropertyValue(onto.hasFirstName).toString();
        dd = instance.getPropertyValue(onto.hasDateOfBirth).toString();

        writeFile(fname, dd, genr);
    }
}

// Write text file
public void writeFile(String fn, String dbir, String gn) {
    String fileout = "D:/file1.txt";
    String firstName = fn;
    String dateB = dbir;
    String gender = gn;

    BufferedWriter out;
    try {
        out = new BufferedWriter(new FileWriter(fileout, true));

        if (gender.equals("F")) {
            out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape + "]");
        }

        else if (gender.equals("M")) {
            out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape2 + "]");
        }

        out.newLine();

        // flushes and closes the stream
        out.close();
    } catch (IOException e) {
        System.out.println("There was a problem:" + e);
    }
}

Can you tell me what should I do to solve my problem?

Thanks

Was it helpful?

Solution

create a helper function to get a specific property:

public RDFNode getProperty(Resource subject,Property prop)
   {
   RDFNode object=null;
   StmtIterator iter2 = this.onto.model.listStatements(subject,prop,(RDFNode)null);  
    while (iter2.hasNext()) {
         object = iter2.nextStatement().getObject();
         break;
    }}
   iter2.close();
   return object;
   }

public String getPropertyAsString(Resource subject,Property prop)
   {
   RDFNode object=getProperty(subject,prop);
   return object==null?null:object.toString();
   }

(...)
String s1=getPropertyAsString(P,onto.hasFirstName);
String s2=getPropertyAsString(P,onto.hasLastName);
(...)

OTHER TIPS

If I understand the intent of the question, you want an iterator that gives you the first name, last name, date of birth etc for each Resource. The iterator would be one row per Resource.

This is what SPARQL can do for you.

In outline:

PREFIX  : ...
SELECT *
{   ?z :hasHusband      ?p .
    ?p :hasFirstName    ?firstName ;
       :hasLastName     ?lastName ;
       :hasDateOfBirth  ?dob ;
    .
 }

and query onto.model.

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