我想在RdfStreamer中加载RDF。我创建了一个查询,在arrayList中提取查询Sparql的结果集,我在流中获取它以传递给主类。这是我的流光代码:
package main.java.eu.larkc.csparql.sr4ld2014.streamer;
import java.util.ArrayList;
import java.util.Random;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.util.FileManager;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream;
public class SensorsStreamer extends RdfStream implements Runnable {
private long sleepTime;
private String baseUri;
private ArrayList<String> prova=new ArrayList<String>();
public ArrayList<String> loadToFile(){
Model modelMisurazioni= FileManager.get().loadModel("misurazioni.rdf");
String s1 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX socrata: <http://www.socrata.com/rdf/terms#>\n" +
"PREFIX dcat: <http://www.w3.org/ns/dcat#>\n" +
"PREFIX ods: <http://open-data-standards.github.com/2012/01/open-data-standards#>\n" +
"PREFIX dcterm: <http://purl.org/dc/terms/>\n" +
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n" +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"PREFIX dsbase: <http://www.dati.lombardia.it/resource/>\n" +
"PREFIX ds: <http://www.dati.lombardia.it/resource/_647i-nhxk/>\n" +
"\n" +
"SELECT ?idSensore \n" +
"WHERE\n" +
" { ?s ?proprietà ?idSensore. "
+ "?s ds:idsensore ?idSensore.}\n" +
"";
Query queryM= QueryFactory.create(s1);
QueryExecution qexec=QueryExecutionFactory.create(queryM, modelMisurazioni);
System.out.println("QUERY 1");
ResultSet results = qexec.execSelect();
//ResultSetFormatter.out(System.out, results) ;
int i=0;
while(results.hasNext()){
prova.add( results.next().getLiteral("idSensore").getLexicalForm() );
System.out.println(prova.get(i++).toString() );
}
return prova;
}
public SensorsStreamer(String iri, String baseUri,long sleepTime) {
super(iri);
this.sleepTime = sleepTime;
this.baseUri = baseUri;
prova=loadToFile();
}
public void run() {
Random random = new Random();
String sensorIndex;
int subjectIndex;
int roomIndex;
int observationIndex;
int numberOfPerson;
while(true){
try{
numberOfPerson = random.nextInt(3);
subjectIndex = random.nextInt(5);
roomIndex = random.nextInt(5);
observationIndex = random.nextInt(Integer.MAX_VALUE);
RdfQuadruple q = new RdfQuadruple(baseUri + "sensor" + provva2, baseUri + "observes", baseUri + "observation" + observationIndex, System.currentTimeMillis());
this.put(q);
if(numberOfPerson < 2){
q = new RdfQuadruple(baseUri + "observation" + observationIndex, baseUri + "who", baseUri+"person" + subjectIndex, System.currentTimeMillis());
this.put(q);
} else {
for(int i = 0 ; i < numberOfPerson ; i++){
q = new RdfQuadruple(baseUri + "observation" + observationIndex, baseUri + "who", baseUri+"person" + subjectIndex, System.currentTimeMillis());
this.put(q);
subjectIndex = random.nextInt(5);
}
}
q = new RdfQuadruple(baseUri + "observation" + observationIndex, baseUri + "where", baseUri+"room" + roomIndex, System.currentTimeMillis());
this.put(q);
Thread.sleep(sleepTime);
} catch(Exception e){
e.printStackTrace();
}
}
}
}
日食的错误是这样的:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.jena.atlas.lib.Cache.getIfPresent(Ljava/lang/Object;)Ljava/lang/Object;
at org.apache.jena.enhanced.EnhGraph.getNodeAs(EnhGraph.java:133)
at org.apache.jena.rdf.model.impl.IteratorFactory.asResource(IteratorFactory.java:71)
at org.apache.jena.rdf.model.impl.IteratorFactory.asResource(IteratorFactory.java:62)
at org.apache.jena.rdf.model.impl.ModelCom.getResource(ModelCom.java:888)
at org.apache.jena.rdf.model.impl.ModelCom.createResource(ModelCom.java:1338)
at org.apache.jena.vocabulary.DC_11.<clinit>(DC_11.java:39)
at org.apache.jena.assembler.assemblers.ContentAssembler.<clinit>(ContentAssembler.java:179)
at org.apache.jena.assembler.ConstAssembler.content(ConstAssembler.java:73)
at org.apache.jena.assembler.ConstAssembler.general(ConstAssembler.java:142)
at org.apache.jena.sparql.core.assembler.AssemblerUtils.init(AssemblerUtils.java:59)
at org.apache.jena.query.ARQ.init(ARQ.java:567)
at org.apache.jena.sparql.system.InitARQ.start(InitARQ.java:29)
at org.apache.jena.system.JenaSystem.lambda$init$40(JenaSystem.java:114)
at java.util.ArrayList.forEach(Unknown Source)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:179)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:156)
at org.apache.jena.system.JenaSystem.init(JenaSystem.java:111)
at org.apache.jena.util.FileManager.<clinit>(FileManager.java:86)
at main.java.eu.larkc.csparql.sr4ld2014.streamer.SensorsStreamer.loadToFile(SensorsStreamer.java:47)
at main.java.eu.larkc.csparql.sr4ld2014.streamer.SensorsStreamer.<init>(SensorsStreamer.java:90)
at main.java.eu.larkc.csparql.sr4ld2014.SR4LD2014_Ex4.main(SR4LD2014_Ex4.java:63)
我哪里错了? 请帮帮我
答案 0 :(得分:0)
检查lib/class
org.apache.jena.atlas.lib.Cache.getIfPresent(Ljava/lang/Object;)Ljava/lang/Object
。可能是Java选择了一个旧库,或者Cache.class
没有实现这样的方法。尝试清理并重新编译。