use of org.apache.jena.rdf.model.Property in project jena by apache.
the class ExampleConstructQuads method main.
public static void main(String[] args) throws FileNotFoundException, IOException {
// create testing data :
// 1) default graph data
Model model = ModelFactory.createDefaultModel();
Resource s = model.createResource("http://eg.com/s");
Property p = model.createProperty("http://eg.com/p");
Resource o = model.createResource("http://eg.com/o");
model.add(s, p, o);
Dataset dataset = DatasetFactory.create(model);
// 2) named graph data
Model model1 = ModelFactory.createDefaultModel();
Resource s1 = model.createResource("http://eg.com/s1");
Property p1 = model.createProperty("http://eg.com/p1");
Resource o1 = model.createResource("http://eg.com/o1");
model1.add(s1, p1, o1);
dataset.addNamedModel("http://eg.com/g1", model1);
PrintUtil.printOut(dataset.asDatasetGraph().find());
// construct named graph
System.out.println("construct named graph:");
String queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// construct default graph 1
System.out.println("construct default graph 1:");
queryString = "CONSTRUCT { {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// construct default graph 2
System.out.println("construct default graph 2:");
queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o} WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// construct triples
System.out.println("construct default graph 2:");
queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o} WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Triple> triples = qexec.execConstructTriples();
PrintUtil.printOut(triples);
}
System.out.println(query);
// construct dataset
System.out.println("construct dataset:");
queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Dataset d = qexec.execConstructDataset();
RDFDataMgr.write(System.out, d, Lang.TRIG);
}
System.out.println(query);
// short form 1
System.out.println("short form 1:");
queryString = "CONSTRUCT WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// short form 2
System.out.println("short form 2:");
queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// short form 3
System.out.println("short form 3:");
queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Triple> triples = qexec.execConstructTriples();
PrintUtil.printOut(triples);
}
System.out.println(query);
// short form 4
System.out.println("short form 4:");
queryString = "CONSTRUCT WHERE{ {<http://eg.com/s> <http://eg.com/p> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Quad> quads = qexec.execConstructQuads();
PrintUtil.printOut(quads);
}
System.out.println(query);
// short form 4
System.out.println("default graph projection:");
queryString = "CONSTRUCT { {<http://eg.com/s> <http://eg.com/p> ?o} GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
query = QueryFactory.create(queryString, Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
Iterator<Triple> triples = qexec.execConstructTriples();
PrintUtil.printOut(triples);
}
System.out.println(query);
// run-construct-quad-test
System.out.println("run-construct-quad-test:");
File[] tests = new File("testing/ARQ/Syntax/Syntax-ARQ").listFiles((dir, fname) -> {
if (fname.startsWith("syntax-quad-construct-")) {
return true;
}
return false;
});
for (File test : tests) {
System.out.println("======== File: " + test.getName());
try (BufferedReader br = new BufferedReader(new FileReader(test))) {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
System.out.println("==== Output of parse/print file " + test.getName());
try {
Query q = QueryFactory.read(test.getAbsolutePath(), Syntax.syntaxARQ);
System.out.println(q);
} catch (QueryParseException ex) {
System.out.println("File " + test.getAbsolutePath() + " : " + ex.getMessage());
}
}
}
use of org.apache.jena.rdf.model.Property in project jena by apache.
the class ModelHelper method modelAdd.
/**
* add to a model all the statements expressed by a string.
*
* Does not do any transaction manipulation.
*
* @param m
* the model to be updated
* @param facts
* a sequence of semicolon-separated "S P O" facts
* @return the updated model
*/
public static Model modelAdd(Model m, String facts) {
StringTokenizer semis = new StringTokenizer(facts, ";");
while (semis.hasMoreTokens()) {
StringTokenizer st = new StringTokenizer(semis.nextToken());
Resource sub = resource(st.nextToken());
Property pred = property(st.nextToken());
RDFNode obj = rdfNode(st.nextToken());
m.add(sub, pred, obj);
}
return m;
}
use of org.apache.jena.rdf.model.Property in project opentheso by miledrousset.
the class SynchroSparql method run.
@Override
public void run() {
System.out.println("dans le run saprtql");
String url = sparqlStruct.getAdresseServeur().replaceAll("http", "jdbc:virtuoso").trim() + ":1111";
VirtGraph graph = new VirtGraph(sparqlStruct.getGraph(), url, sparqlStruct.getNom_d_utilisateur(), sparqlStruct.getMot_de_passe());
String str = "CLEAR GRAPH <" + sparqlStruct.getGraph() + ">";
VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(str, graph);
vur.exec();
Model m = ModelFactory.createDefaultModel();
DownloadBean db = new DownloadBean();
db.setConnect(conn);
StreamedContent file = db.thesoToFile(sparqlStruct.getThesaurus(), liste_lang, liste_group, 0);
try {
m.read(file.getStream(), null);
} catch (Exception e) {
// graph.close();
}
StmtIterator iter = m.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
Triple tri = new Triple(subject.asNode(), predicate.asNode(), object.asNode());
graph.add(tri);
}
graph.close();
}
Aggregations