我尝试使用jena和sparql查询将数据插入到rdf文件中。但是它提供了com.hp.hpl.jena.query.QueryParseException。我想使用java将“”用户“和他的详细信息插入到rdf文件中。我可以使用文件编写器在rdf文件中写入数据,但我认为这不是将数据插入到rdf文件中的最佳方法。我试着这样做使用以下答案
- Sparql query doesn't upadate when insert some data through java code
但它不能正常工作。我找不到我在这里做的错误。
public class insertdata {
public static void main(String[] args) {
new insertdata().addLecturerTriples("user5");
}
public boolean addLecturerTriples(String user) {
try {
Model m = ModelFactory.createDefaultModel();
m.read("/Users/heshanjayasinghe/Documents/A-enigmaProject/jena_Enigma/src/jena_enigma/Enigma.RDF", "RDF/XML");
ArrayList<String> subject = new ArrayList<String>();
ArrayList<String> predicate = new ArrayList<String>();
ArrayList<String> object = new ArrayList<String>();
// subject.add("<http://people.brunel.ac.uk/~csstnns/university.owl#"+fName+">");
subject.add("http://www.heshjayasinghe.webatu.com/Enigma.RDF#" + user + ">");
// predicate.add("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
predicate.add("http://www.heshjayasinghe.webatu.com/Enigma.RDF#type");
// object.add("<http://people.brunel.ac.uk/~csstnns/university.owl#Lecturer>");
object.add("http://www.heshjayasinghe.webatu.com/Enigma.RDF#User");
for (int i = 0; i < subject.size(); i++) {
String qry = "INSERT DATA"
+ "{"
+ subject.get(i) + "\n"
+ predicate.get(i) + "\n"
+ object.get(i) + "\n"
+ "}";
UpdateRequest update = UpdateFactory.create(qry);
UpdateProcessor processor = UpdateExecutionFactory.create(update, GraphStoreFactory.create(m));
processor.execute();
// UpdateProcessor qexec = UpdateExecutionFactory.createRemote(update, "http://localhost:3030/ds/update");
// Save the updated model
m.write(new FileOutputStream("/Users/heshanjayasinghe/Documents/A-enigmaProject/jena_Enigma/src/jena_enigma/Enigma.RDF"), "RDF/XML");
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
rdf文件
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<!ENTITY Enigma "http://www.heshjayasinghe.webatu.com/Enigma.RDF#" >
]>
<rdf:RDF xmlns="http://www.heshjayasinghe.webatu.com/Enigma.RDF#"
xml:base="http://www.heshjayasinghe.webatu.com/Enigma.RDF"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:Enigma="http://www.heshjayasinghe.webatu.com/Enigma.RDF#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://www.heshjayasinghe.webatu.com/Enigma.RDF"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#AskQuestion -->
<owl:ObjectProperty rdf:about="&Enigma;AskQuestion"/>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#type -->
<owl:ObjectProperty rdf:about="&Enigma;type">
<rdfs:domain rdf:resource="&Enigma;User"/>
</owl:ObjectProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#email -->
<owl:DatatypeProperty rdf:about="&Enigma;email">
<rdfs:domain rdf:resource="&Enigma;User"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#fname -->
<owl:DatatypeProperty rdf:about="&Enigma;fname">
<rdfs:domain rdf:resource="&Enigma;User"/>
<rdfs:range rdf:resource="&xsd;Name"/>
</owl:DatatypeProperty>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#lname -->
<owl:DatatypeProperty rdf:about="&Enigma;lname">
<rdfs:domain rdf:resource="&Enigma;User"/>
<rdfs:range rdf:resource="&xsd;Name"/>
</owl:DatatypeProperty>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#password -->
<owl:DatatypeProperty rdf:about="&Enigma;password">
<rdfs:domain rdf:resource="&Enigma;User"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#uname -->
<owl:DatatypeProperty rdf:about="&Enigma;uname">
<rdfs:domain rdf:resource="&Enigma;User"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#Question -->
<owl:Class rdf:about="&Enigma;Question"/>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#User -->
<owl:Class rdf:about="&Enigma;User"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#Question1 -->
<owl:NamedIndividual rdf:about="&Enigma;Question1"/>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#User1 -->
<owl:NamedIndividual rdf:about="&Enigma;User1">
<rdf:type rdf:resource="&Enigma;User"/>
<password rdf:datatype="&xsd;string">hesh</password>
<fname rdf:datatype="&xsd;string">heshan</fname>
<email rdf:datatype="&xsd;string">heshjay@hotmail.com</email>
<lname rdf:datatype="&xsd;Name">jayasinghe</lname>
<AskQuestion rdf:resource="&Enigma;Question1"/>
</owl:NamedIndividual>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#User2 -->
<owl:NamedIndividual rdf:about="&Enigma;User2">
<rdf:type rdf:resource="&Enigma;User"/>
<password rdf:datatype="&xsd;string">123</password>
<fname rdf:datatype="&xsd;string">hesh</fname>
<email rdf:datatype="&xsd;string">heshjse@gmail.com</email>
<lname rdf:datatype="&xsd;string">jay</lname>
</owl:NamedIndividual>
<!-- http://www.heshjayasinghe.webatu.com/Enigma.RDF#user3 -->
<owl:NamedIndividual rdf:about="&Enigma;user3">
<rdf:type rdf:resource="&Enigma;User"/>
<password rdf:datatype="&xsd;string">123</password>
<fname rdf:datatype="&xsd;string">hesh</fname>
<email rdf:datatype="&xsd;string">hhh@gmail.com</email>
</owl:NamedIndividual>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->