Java-使用Jena APi-从RDF文件获取数据 [英] Java - Using Jena APi - Get data from RDF file

查看:314
本文介绍了Java-使用Jena APi-从RDF文件获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与具有数据类型属性hasFirstNamehasLastNamehasDateOfBirthhasGender的类Person有关. 我正在使用Java和Jena API.

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

这是我的RDF文件中代表一个人的方式.

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+"]

因此,例如,如果有3位母文件,则该文件必须包含3行具有该格式的行.形状值(然后是输出线)将取决于性别,这就是为什么我不能对两种性别都使用同一条线.如果女性是Shape2,而男性则是shape.

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**+"]

我遇到的问题是,他只输出一行对应的一行中的一个女人和一个男人.但是,我的rdf文件中有不止一个男人和女人.

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

推荐答案

在不知道耶拿的情况下,我看不到代码中仅选择男性条目的任何地方. 检查该while (instances.hasNext()) {循环以查看其循环通过的实例.

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.

因为您为每个实例都写了一行,所以writeLine()方法同时写入了男性和女性条目,因此可能是

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.

此外,示例RDF条目的性别值为H,但是在您的代码中,您正在使用MF进行检查.

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

这篇关于Java-使用Jena APi-从RDF文件获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆