当我单击“添加”按钮时,我有一个函数可以在 RDF 文件中添加 Person
类的实例(及其属性值)。我可以添加第一个实例,但是当我尝试添加第二个实例时,它会用第二个实例替换第一个实例。
我认为这是因为在添加第一个实例后我没有增加 Resource P1 = model.createResource(personURI+"Name")
。
如何在 Java 中将 Resource
从 P1
递增到 P2
再到 P3
等?
public class ActionAjoutPersonne implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Onto f = new Onto();
Resource p1 = f.model.createResource(f.uriBase+tabTF[0].getText());
p1.addProperty(f.aPourPrenom, tabTF[0].getText());
p1.addProperty(f.aPourNom, tabTF[1].getText());
p1.addProperty(f.aDateNaiss, tabTF[2].getText());
if (tabTF[3].getText().equals("F"))
{
p1.addProperty(f.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, f.femme);
}
else if (tabTF[3].getText().equals("H"))
{
p1.addProperty(f.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, f.homme);
}
StringWriter sw = new StringWriter();
f.model.write(sw, "RDF/XML-ABBREV");
String owlCode = sw.toString();
File file = new File("d:/teste20.rdf");
try{
FileWriter fw = new FileWriter(file);
fw.write(owlCode);
fw.close();
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
最佳答案
我假设您使用的是 Jena?
如果您使用相同的 URI 参数再次调用 createResource(uri)
方法,它将覆盖现有资源。
因此您必须确保为每个人分配不同的 URI。
您可以使用某种自动递增计数器来实现这一点,或者通过将人名作为 URI 的一部分(例如 using URLEncoder.encode(name, "utf-8")
) ).
关于java - 如何增加资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009857/