Apache Jena - 生成 RDF 的问题
Apache Jena - issue on generating RDF
我尝试用 Apache Jena 制作一个 RDF 文档,我想在其中获取一些关于博物馆的信息。我制作了资源和模型,但是当我打印出最终输出时,它会将所有博物馆的属性提供给第一个博物馆资源。
我在那段代码中做错了什么? (输出在底部)。
这是 Jena RDF 生成器:
public class RDFTesting2 {
public static void main(String[] args) {
MuseInfo info = new MuseInfo();
ArrayList<HashMap<String, String>> rdfData = info.getMuseInfo();
StringWriter out = new StringWriter();
String outStr = "";
String ns = "http://opendata.cs.pub.ro/resource/";
String nsVCard = "http://www.w3.org/2006/vcard/ns#";
String nsProp = "http://opendata.cs.pub.ro/property/";
String nsDbPedia = "http://dbpedia.org/ontology/";
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("vCard", nsVCard);
model.setNsPrefix("opendata", nsProp);
model.setNsPrefix("dbPedia", nsDbPedia);
for (int i = 0; i < rdfData.size(); i++) {
String museId = rdfData.get(i).get("id");
Resource museContacts = model.createResource(ns + "contacts");
Resource museContact = model.createResource(ns + "contact");
Resource museLocation = model.createResource(ns + "location");
Resource museInfo = model.createResource(ns + "info");
Property contactProp = model.createProperty(nsProp + "contacts");
Property contactInfo = model.createProperty(nsProp + "contact");
Property locationInfo = model.createProperty(nsProp + "location");
Property genInfo = model.createProperty(nsProp + "info");
Property contactPerson = model.createProperty(nsDbPedia + "contactPerson");
Resource museum = model.createResource(ns + "museum_" + museId + "/");
museContact
.addProperty(VCARD2.hasTelephone, rdfData.get(i).get("telefon"))
.addProperty(contactPerson, rdfData.get(i).get("persoana contact"));
museLocation
.addProperty(VCARD2.hasLocality, rdfData.get(i).get("localitatea"));
museInfo
.addProperty(VCARD2.hasUID, rdfData.get(i).get("id"))
.addProperty(VCARD2.hasTitle, rdfData.get(i).get("denumire"), "ro");
model
.add(museContacts, contactInfo, museContact)
.add(museContacts, locationInfo, museLocation)
.add(museum, contactProp, museContacts)
.add(museum, genInfo, museInfo);
}
model.write(out, "RDF/XML");
String result = out.toString();
model.removeAll(); // Curata modelul
outStr += result;
out.getBuffer().setLength(0); // Curata bufferul
System.out.println(outStr);
}
}
这是class博物馆信息生成器:
public class MuseInfo {
public ArrayList<HashMap<String, String>> getMuseInfo() {
ArrayList<HashMap<String, String>> rdfData = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm;
hm = new HashMap<String, String>();
hm.put("denumire", "Centrul folcloric Grădiniţa, Galicea Mare");
hm.put("id", "0");
hm.put("localitatea", "Galicea Mare");
hm.put("persoana contact", "Gologan Veronica");
hm.put("telefon", "0241.542.277");
rdfData.add(hm);
hm = new HashMap<String, String>();
hm.put("denumire", "Acvariul Constanţa");
hm.put("id", "1");
hm.put("localitatea", "Constanţa");
hm.put("persoana contact", "E. Şerbănescu");
hm.put("telefon", "0241/481.461");
rdfData.add(hm);
hm = new HashMap<String, String>();
hm.put("denumire", "Ansamblul brâncovenesc de la Potlogi");
hm.put("id", "2");
hm.put("localitatea", "Potlogi");
hm.put("persoana contact", "Gheorghe Olteanu");
hm.put("telefon", "0245.61 39 46");
rdfData.add(hm);
return rdfData;
}
}
输出是:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:opendata="http://opendata.cs.pub.ro/property/"
xmlns:vCard="http://www.w3.org/2006/vcard/ns#"
xmlns:dbPedia="http://dbpedia.org/ontology/">
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_0/">
<opendata:info>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/info">
<vCard:hasTitle xml:lang="ro">Ansamblul brâncovenesc de la Potlogi</vCard:hasTitle>
<vCard:hasUID>2</vCard:hasUID>
<vCard:hasTitle xml:lang="ro">Acvariul Constanţa</vCard:hasTitle>
<vCard:hasUID>1</vCard:hasUID>
<vCard:hasTitle xml:lang="ro">Centrul folcloric Grădiniţa, Galicea Mare</vCard:hasTitle>
<vCard:hasUID>0</vCard:hasUID>
</rdf:Description>
</opendata:info>
<opendata:contacts>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/contacts">
<opendata:location>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/location">
<vCard:hasLocality>Potlogi</vCard:hasLocality>
<vCard:hasLocality>Constanţa</vCard:hasLocality>
<vCard:hasLocality>Galicea Mare</vCard:hasLocality>
</rdf:Description>
</opendata:location>
<opendata:contact>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/contact">
<dbPedia:contactPerson>Gheorghe Olteanu</dbPedia:contactPerson>
<vCard:hasTelephone>0245.61 39 46</vCard:hasTelephone>
<dbPedia:contactPerson>E. Şerbănescu</dbPedia:contactPerson>
<vCard:hasTelephone>0241/481.461</vCard:hasTelephone>
<dbPedia:contactPerson>Gologan Veronica</dbPedia:contactPerson>
<vCard:hasTelephone>0241.542.277</vCard:hasTelephone>
</rdf:Description>
</opendata:contact>
</rdf:Description>
</opendata:contacts>
</rdf:Description>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_1/">
<opendata:info rdf:resource="http://opendata.cs.pub.ro/resource/info"/>
<opendata:contacts rdf:resource="http://opendata.cs.pub.ro/resource/contacts"/>
</rdf:Description>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_2/">
<opendata:info rdf:resource="http://opendata.cs.pub.ro/resource/info"/>
<opendata:contacts rdf:resource="http://opendata.cs.pub.ro/resource/contacts"/>
</rdf:Description>
</rdf:RDF>
解决方案:
Resource museContacts = model.createResource();
Resource museContact = model.createResource();
Resource museLocation = model.createResource();
Resource museInfo = model.createResource();
我尝试用 Apache Jena 制作一个 RDF 文档,我想在其中获取一些关于博物馆的信息。我制作了资源和模型,但是当我打印出最终输出时,它会将所有博物馆的属性提供给第一个博物馆资源。
我在那段代码中做错了什么? (输出在底部)。
这是 Jena RDF 生成器:
public class RDFTesting2 {
public static void main(String[] args) {
MuseInfo info = new MuseInfo();
ArrayList<HashMap<String, String>> rdfData = info.getMuseInfo();
StringWriter out = new StringWriter();
String outStr = "";
String ns = "http://opendata.cs.pub.ro/resource/";
String nsVCard = "http://www.w3.org/2006/vcard/ns#";
String nsProp = "http://opendata.cs.pub.ro/property/";
String nsDbPedia = "http://dbpedia.org/ontology/";
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("vCard", nsVCard);
model.setNsPrefix("opendata", nsProp);
model.setNsPrefix("dbPedia", nsDbPedia);
for (int i = 0; i < rdfData.size(); i++) {
String museId = rdfData.get(i).get("id");
Resource museContacts = model.createResource(ns + "contacts");
Resource museContact = model.createResource(ns + "contact");
Resource museLocation = model.createResource(ns + "location");
Resource museInfo = model.createResource(ns + "info");
Property contactProp = model.createProperty(nsProp + "contacts");
Property contactInfo = model.createProperty(nsProp + "contact");
Property locationInfo = model.createProperty(nsProp + "location");
Property genInfo = model.createProperty(nsProp + "info");
Property contactPerson = model.createProperty(nsDbPedia + "contactPerson");
Resource museum = model.createResource(ns + "museum_" + museId + "/");
museContact
.addProperty(VCARD2.hasTelephone, rdfData.get(i).get("telefon"))
.addProperty(contactPerson, rdfData.get(i).get("persoana contact"));
museLocation
.addProperty(VCARD2.hasLocality, rdfData.get(i).get("localitatea"));
museInfo
.addProperty(VCARD2.hasUID, rdfData.get(i).get("id"))
.addProperty(VCARD2.hasTitle, rdfData.get(i).get("denumire"), "ro");
model
.add(museContacts, contactInfo, museContact)
.add(museContacts, locationInfo, museLocation)
.add(museum, contactProp, museContacts)
.add(museum, genInfo, museInfo);
}
model.write(out, "RDF/XML");
String result = out.toString();
model.removeAll(); // Curata modelul
outStr += result;
out.getBuffer().setLength(0); // Curata bufferul
System.out.println(outStr);
}
}
这是class博物馆信息生成器:
public class MuseInfo {
public ArrayList<HashMap<String, String>> getMuseInfo() {
ArrayList<HashMap<String, String>> rdfData = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm;
hm = new HashMap<String, String>();
hm.put("denumire", "Centrul folcloric Grădiniţa, Galicea Mare");
hm.put("id", "0");
hm.put("localitatea", "Galicea Mare");
hm.put("persoana contact", "Gologan Veronica");
hm.put("telefon", "0241.542.277");
rdfData.add(hm);
hm = new HashMap<String, String>();
hm.put("denumire", "Acvariul Constanţa");
hm.put("id", "1");
hm.put("localitatea", "Constanţa");
hm.put("persoana contact", "E. Şerbănescu");
hm.put("telefon", "0241/481.461");
rdfData.add(hm);
hm = new HashMap<String, String>();
hm.put("denumire", "Ansamblul brâncovenesc de la Potlogi");
hm.put("id", "2");
hm.put("localitatea", "Potlogi");
hm.put("persoana contact", "Gheorghe Olteanu");
hm.put("telefon", "0245.61 39 46");
rdfData.add(hm);
return rdfData;
}
}
输出是:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:opendata="http://opendata.cs.pub.ro/property/"
xmlns:vCard="http://www.w3.org/2006/vcard/ns#"
xmlns:dbPedia="http://dbpedia.org/ontology/">
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_0/">
<opendata:info>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/info">
<vCard:hasTitle xml:lang="ro">Ansamblul brâncovenesc de la Potlogi</vCard:hasTitle>
<vCard:hasUID>2</vCard:hasUID>
<vCard:hasTitle xml:lang="ro">Acvariul Constanţa</vCard:hasTitle>
<vCard:hasUID>1</vCard:hasUID>
<vCard:hasTitle xml:lang="ro">Centrul folcloric Grădiniţa, Galicea Mare</vCard:hasTitle>
<vCard:hasUID>0</vCard:hasUID>
</rdf:Description>
</opendata:info>
<opendata:contacts>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/contacts">
<opendata:location>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/location">
<vCard:hasLocality>Potlogi</vCard:hasLocality>
<vCard:hasLocality>Constanţa</vCard:hasLocality>
<vCard:hasLocality>Galicea Mare</vCard:hasLocality>
</rdf:Description>
</opendata:location>
<opendata:contact>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/contact">
<dbPedia:contactPerson>Gheorghe Olteanu</dbPedia:contactPerson>
<vCard:hasTelephone>0245.61 39 46</vCard:hasTelephone>
<dbPedia:contactPerson>E. Şerbănescu</dbPedia:contactPerson>
<vCard:hasTelephone>0241/481.461</vCard:hasTelephone>
<dbPedia:contactPerson>Gologan Veronica</dbPedia:contactPerson>
<vCard:hasTelephone>0241.542.277</vCard:hasTelephone>
</rdf:Description>
</opendata:contact>
</rdf:Description>
</opendata:contacts>
</rdf:Description>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_1/">
<opendata:info rdf:resource="http://opendata.cs.pub.ro/resource/info"/>
<opendata:contacts rdf:resource="http://opendata.cs.pub.ro/resource/contacts"/>
</rdf:Description>
<rdf:Description rdf:about="http://opendata.cs.pub.ro/resource/museum_2/">
<opendata:info rdf:resource="http://opendata.cs.pub.ro/resource/info"/>
<opendata:contacts rdf:resource="http://opendata.cs.pub.ro/resource/contacts"/>
</rdf:Description>
</rdf:RDF>
解决方案:
Resource museContacts = model.createResource();
Resource museContact = model.createResource();
Resource museLocation = model.createResource();
Resource museInfo = model.createResource();