简体   繁体   English

Java - Jena API - 输出文件

[英]Java - Jena API - Output file

I'm using Java and Jena API. 我正在使用Java和Jena API。

I have a class Person with the datatype properties hasFirstName , hasLastName , hasDateOfBirth , hasGender . 我有一类Person与数据类型属性hasFirstNamehasLastNamehasDateOfBirthhasGender

Here is how one person is represented in my RDF file. 以下是我在RDF文件中表示一个人的方式。

<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>

For each person whose gender is female I want to generate a text file with this line below: 对于性别为女性的每个人,我想生成一个文本文件,如下所示:

[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. 因此,如果有3个女性,则该文件必须包含具有该格式的3行。 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. 形状值(然后是输出线)将取决于性别,这就是为什么我不能对两种性别使用相同的线。

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 in the output file i only see one woman and one man with the corresponding line. 我的问题是在输出文件中我只看到一个女人和一个男人有相应的行。 I have more than one woman and man but he only outputs one for each gender. 我有一个以上的女人和男人,但他只为每个性别输出一个。 That's my problem... 那是我的问题......

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 modify to solve my problem? 你能告诉我应该修改什么来解决我的问题吗?

Thanks 谢谢

Is my humble opinion that you don't know RDF altogether, and your are struggling to get things working by asking random questions here on SO when you are not able to figure out what's wrong by yourself. 我谦虚地认为你完全不了解RDF,当你无法弄清楚自己出了什么问题时,你正在努力通过在这里提出随机问题来解决问题。 There's nothing wrong with this attitude, however it would be way better if you tried to carefully read the documentation (OWL, RDF and Jena) . 这种态度没有任何问题,但是如果你仔细阅读文档(OWL,RDF和Jena)会更好。

I suspect that the problem is not on the Java side , but instead it's an issue with your ontology (unless you can prove it's not true). 我怀疑问题不在Java方面 ,而是你的本体问题(除非你能证明它不是真的)。

I never used RDF and Jena at all, however I spent a couple of hours reading the docs, so even if the following may not be the holy way to do things, it's certainly a way better than yours. 我从来没有使用过RDF和Jena,但是我花了几个小时阅读文档,所以即使以下可能不是神圣的做事方式,它肯定比你的方式更好。 Feel free to ask questions (to me or to the SO community) when you can't understand something. 当你无法理解某些东西时,请随意向我(或我或SO社区)提问。 First, let's write our ontology in OWL (I'm new to this, so this may contain errors) Note: You are not supposed to build your ontology in Java code at every program run. 首先,让我们在OWL中编写我们的本体(我是新手,所以这可能包含错误) 注意:你不应该在每次程序运行时都用Java代码构建你的本体。 Likely you'll load your ontology from some serialized form like the following 可能你会从如下的序列化形式加载你的本体

<?xml version="1.0"?>
<!-- See http://www.w3.org/TR/2004/REC-owl-guide-20040210/#Namespaces -->
<!DOCTYPE rdf:RDF [
<!ENTITY fam "http://zybnet.com/fam#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
]>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="&xsd;"

    xmlns    ="&fam;" 
    xmlns:fam="&fam;" 
    xml:base ="&fam;" >

    <owl:Class rdf:about="Person" />

    <!-- Properties name, gender, and birth -->
    <owl:DatatypeProperty rdf:about="&fam;name">
        <rdfs:domain rdf:resource="Person" />
        <rdfs:range  rdf:resource="&xsd;string" />
    </owl:DatatypeProperty>
    <owl:DatatypeProperty rdf:about="&fam;birth">
        <rdfs:domain rdf:resource="Person" />
        <rdfs:range  rdf:resource="&xsd;date" />
    </owl:DatatypeProperty>
    <owl:DatatypeProperty rdf:about="&fam;gender">
        <rdfs:domain rdf:resource="Person" />
        <rdfs:range  rdf:resource="&xsd;string"/>
    </owl:DatatypeProperty>

    <rdf:Description rdf:about="user/7192">
        <fam:name>Bruno</fam:name>
        <fam:gender>M</fam:gender>
        <fam:birth>19/08/1987</fam:birth>
    </rdf:Description>
    <rdf:Description rdf:about="user/3023">
        <fam:name>Raffaele</fam:name>
        <fam:gender>M</fam:gender>
        <fam:birth>09/02/1927</fam:birth>
    </rdf:Description>
    <rdf:Description rdf:about="user/9283">
        <fam:name>Angela</fam:name>
        <fam:gender>F</fam:gender>
        <fam:birth>01/06/1957</fam:birth>
    </rdf:Description>
</rdf:RDF>

Then comes the Java code 然后是Java代码

public class RDF {
    public static void main(String[] args) {
        Model model = ModelFactory.createOntologyModel();
        model.read(RDF.class.getResourceAsStream("family.rdf"), null);

        System.out.println("All triples in file");

        outQuery("SELECT ?subject ?predicate ?object " +
                "WHERE { ?subject ?predicate ?object }", model);

        System.out.println("A simple query");

        outQuery("SELECT ?subject ?name ?gender " +
                "WHERE { " +
                " ?subject <http://zybnet.com/fam#name> ?name ." +
                " ?subject <http://zybnet.com/fam#gender> ?gender ." +
                " }", model);
    }

    private static void outQuery(String q, Model model) {
        Query query = QueryFactory.create(q);
        QueryExecution execution = QueryExecutionFactory.create(query, model);
        ResultSet results = execution.execSelect();
        ResultSetFormatter.out(System.out, results, query);
        execution.close();
    }

}

And finally a bit of log4j configuration (this must be put in a log4j.properties on the classpath, or loaded from Java code with PropertyConfigurator.configure ) 最后是一些log4j配置(这必须放在类路径的log4j.properties ,或者用PropertyConfigurator.configure从Java代码加载)

log4j.logger.com.hp.hpl.jena=debug, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p] %m%n

This should be your starting code. 这应该是您的起始代码。 By running this, you'll see what's going on. 通过运行它,你会看到发生了什么。 By understanding this code, you'll be able to refactor your application and find ouy what to study to get it up and running. 通过理解此代码,您将能够重构您的应用程序并找到要学习的内容以使其运行起来。 Good luck! 祝好运!

Sample output: (text removed from brevity) 示例输出:(文字从简洁中删除)

[Lots of text here...]
A simple query
[DEBUG] Lock : main
-------------------------------------------------------
| uri                           | name       | gender |
=======================================================
| <http://zybnet.com/user/9230> | "Angela"   | "F"    |
| <http://zybnet.com/user/0239> | "Raffaele" | "M"    |
| <http://zybnet.com/user/0001> | "Bruno"    | "M"    |
-------------------------------------------------------

You close your output file in writeFile so no more data can be written after the first invocation of this method. 您在writeFile关闭输出文件,因此在第一次调用此方法后无法再写入数据。 Close the file after your main loop in accessProp . accessProp的主循环之后关闭文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM