Search in sources :

Example 46 with Property

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());
        }
    }
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Resource(org.apache.jena.rdf.model.Resource) Triple(org.apache.jena.graph.Triple) Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property)

Example 47 with Property

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 48 with Property

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();
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) VirtuosoUpdateRequest(virtuoso.jena.driver.VirtuosoUpdateRequest) DownloadBean(mom.trd.opentheso.SelectedBeans.DownloadBean) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) Triple(org.apache.jena.graph.Triple) VirtGraph(virtuoso.jena.driver.VirtGraph) Model(org.apache.jena.rdf.model.Model) StreamedContent(org.primefaces.model.StreamedContent) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

Property (org.apache.jena.rdf.model.Property)48 Resource (org.apache.jena.rdf.model.Resource)35 Model (org.apache.jena.rdf.model.Model)24 Test (org.junit.Test)11 Literal (org.apache.jena.rdf.model.Literal)9 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)6 RDFNode (org.apache.jena.rdf.model.RDFNode)6 Node (org.apache.jena.graph.Node)4 Triple (org.apache.jena.graph.Triple)4 Dataset (org.apache.jena.query.Dataset)4 InfModel (org.apache.jena.rdf.model.InfModel)4 Statement (org.apache.jena.rdf.model.Statement)4 Store (org.apache.jena.sdb.Store)4 StmtIterator (org.apache.jena.rdf.model.StmtIterator)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 Graph (org.apache.jena.graph.Graph)2 OntModel (org.apache.jena.ontology.OntModel)2 SecuredProperty (org.apache.jena.permissions.model.SecuredProperty)2 StringReader (java.io.StringReader)1