user1910188
user1910188

Reputation: 11

Java - Using Jena APi - Get data from RDF file

My question concerns the class Person with the datatype properties hasFirstName, hasLastName, hasDateOfBirth, hasGender. I'm using Java and Jena API.

Here is how one person is represented 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>

I want to write this line below if gender is female :

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

so if there is, for example, 3 females the file must contain 3 lines with that format. The shape value( and then the output line) will depend on the gender, that's why i cannot not use the same line for both genders. Shape2 if female and shape if male.

For each person whose gender is male I want to output this line below:

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

The problem I have is that he outputs only one woman and one man with the corresponding line. However, I have more than one woman and man in my rdf file.

Here is the relevant code. Can you tell me what should I modify to solve this? Thank you very much.

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);
    }
}

Upvotes: 1

Views: 1040

Answers (1)

Udo Klimaschewski
Udo Klimaschewski

Reputation: 5315

Without knowing Jena, I do not see any place in your code where you only select the male entries. Check that while (instances.hasNext()) { loop to see what instances it loops through.

Because you write for each of that instances a line, the writeLine() method writes both, male and female entries, it might be that

ExtendedIterator instances = onto.person.listInstances();

returns the two male and female entries you see in your file.

Also, your example RDF entry has a value of H for gender, but in your code you are using M and Fto check it.

Upvotes: 1

Related Questions