我正在使用 Java 和 Jena API。我有一个类婚姻,它有 3 个对象属性,称为"hasHusband"、"Haswife"和"dateOfMarriage"。前两个与具有数据类型属性 hasFirstName、hasLastName、dateOfBirth ...的类 Person相关联。
通过下面的代码,我可以访问丈夫的 hasFirstName 属性。
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();
}}
我想修改这一行
StmtIterator iter2 = onto.model.listStatements(((Resource) P),onto.hasFirstName,(RDFNode)null);
为了访问 hasLastName 和 hasDateofBirth...
你能解释一下我该怎么做吗?
谢谢
已编辑:@Pierre 现在它只涉及 Person 类。如果是女性,我想在一个新文件(文本文件)中为每个女性输出以下这一行:
[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape2+"]
对于每个人来说:
[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape+"]
区别在于形状的价值。我的问题是他只输出一个女人和一个男人。
一个人在我的 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>
以下是相关代码:
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);
}
}
你能告诉我该怎么做才能解决我的问题吗?
谢谢