Java Code Examples for org.eclipse.rdf4j.repository.RepositoryConnection#prepareUpdate()

The following examples show how to use org.eclipse.rdf4j.repository.RepositoryConnection#prepareUpdate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SPARQLUpdateOperation.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void execute(RepositoryConnection con) throws RepositoryException {
	try {
		Update preparedUpdate = con.prepareUpdate(QueryLanguage.SPARQL, getUpdateString(), getBaseURI());
		preparedUpdate.setIncludeInferred(isIncludeInferred());
		preparedUpdate.setDataset(getDataset());

		if (getBindings() != null) {
			for (Binding binding : getBindings()) {
				preparedUpdate.setBinding(binding.getName(), binding.getValue());
			}
		}

		preparedUpdate.execute();
	} catch (MalformedQueryException | UpdateExecutionException e) {
		throw new RepositoryException(e);
	}

}
 
Example 2
Source File: SPARQLUpdateConformanceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void runTest() throws Exception {
	RepositoryConnection con = dataRep.getConnection();
	RepositoryConnection erCon = expectedResultRepo.getConnection();
	try {
		String updateString = readUpdateString();

		con.begin();

		Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
		update.setDataset(dataset);
		update.execute();

		con.commit();

		// check default graph
		logger.info("checking default graph");
		compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource) null)),
				Iterations.asList(erCon.getStatements(null, null, null, true, (Resource) null)));

		for (String namedGraph : inputNamedGraphs.keySet()) {
			logger.info("checking named graph {}", namedGraph);
			IRI contextURI = con.getValueFactory().createIRI(namedGraph.replaceAll("\"", ""));
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
					Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
		}
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
		erCon.close();
	}
}
 
Example 3
Source File: SPARQL11UpdateComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void runTest() throws Exception {

	logger.debug("running {}", getName());

	RepositoryConnection con = dataRep.getConnection();
	RepositoryConnection erCon = expectedResultRepo.getConnection();
	try {
		String updateString = readUpdateString();

		con.begin();

		Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFile);
		update.setDataset(dataset);
		update.execute();

		con.commit();

		// check default graph
		logger.info("checking default graph");
		compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource) null)),
				Iterations.asList(erCon.getStatements(null, null, null, true, (Resource) null)));

		for (String namedGraph : inputNamedGraphs.keySet()) {
			logger.info("checking named graph {}", namedGraph);
			IRI contextURI = con.getValueFactory().createIRI(namedGraph.replaceAll("\"", ""));
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
					Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
		}
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
		erCon.close();
	}
}
 
Example 4
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 5 votes vote down vote up
public void testInsertDeleteData() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "INSERT DATA\n" +
            "{ <http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());

    String delete = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "\n" +
            "DELETE DATA\n" +
            "{ <http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, delete);
    update.execute();

    query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}
 
Example 5
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 5 votes vote down vote up
public void testClearAllGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G2 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}
 
Example 6
Source File: SPARQLUpdateTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = MalformedQueryException.class)
public void testInvalidInsertUpdate() {
	RepositoryConnection connection = rep.getConnection();
	Update update = connection.prepareUpdate(QueryLanguage.SPARQL, "insert data { ?s ?p ?o }");
}
 
Example 7
Source File: SPARQLUpdateTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expected = MalformedQueryException.class)
public void testInvalidDeleteUpdate() {
	RepositoryConnection connection = rep.getConnection();
	Update delete = connection.prepareUpdate(QueryLanguage.SPARQL, "delete data { ?s ?p ?o }");
}
 
Example 8
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testUpdateWAuthOnConfig() throws Exception {
        String sparqlUpdate = getSparqlUpdate();

        RdfCloudTripleStore tstore = new MockRdfCloudStore();
        NamespaceManager nm = new NamespaceManager(tstore.getRyaDAO(), tstore.getConf());
        tstore.setNamespaceManager(nm);
        SailRepository repo = new SailRepository(tstore);
        tstore.getRyaDAO().getConf().setCv("1|2");
        repo.initialize();

        RepositoryConnection conn = repo.getConnection();
        Update u = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate);
        u.execute();

        String query =
                "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" +
                "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" +
                "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" +
                "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "\n" +
                "SELECT * \n" +
//              "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
                "WHERE\n" +
                "{\n" +
                "  GRAPH ex:G1\n" +
                "  {\n" +
                "    ?m voc:name ?name ;\n" +
                "           voc:homepage ?hp .\n" +
                "  } .\n" +
                " GRAPH ex:G2\n" +
                "  {\n" +
                "    ?m voc:hasSkill ?skill .\n" +
                "  } .\n" +
                "}";
        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, VF.createLiteral("2"));
        CountTupleHandler tupleHandler = new CountTupleHandler();
        tupleQuery.evaluate(tupleHandler);
        assertEquals(1, tupleHandler.getCount());

        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); //no auth
        tupleHandler = new CountTupleHandler();
        tupleQuery.evaluate(tupleHandler);
        assertEquals(0, tupleHandler.getCount());

        conn.close();

        repo.shutDown();
    }
 
Example 9
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testUpdateData() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());

    String insdel = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "\n" +
            "WITH <http://example/addresses#G1>\n" +
            "DELETE { ?book dc:title ?title }\n" +
            "INSERT { ?book dc:title \"A newer book\"." +
            "         ?book dc:add \"Additional Info\" }\n" +
            "WHERE\n" +
            "  { ?book dc:creator \"A.N.Other\" ;\n" +
            "        dc:title ?title .\n" +
            "  }";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insdel);
    update.execute();

    query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "select * where { GRAPH ex:G1 {<http://example/book3> ?p ?o. } }";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(3, tupleHandler.getCount());

    conn.close();
}
 
Example 10
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testClearGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G2 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear(VF.createIRI("http://example/addresses#G2"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    conn.clear(VF.createIRI("http://example/addresses#G1"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}
 
Example 11
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testDropGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();

    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G1 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "PREFIX ex: <http://example/addresses#>\n" +
            "INSERT DATA\n" +
            "{ GRAPH ex:G2 {\n" +
            "<http://example/book3> dc:title    \"A new book\" ;\n" +
            "                         dc:creator  \"A.N.Other\" .\n" +
            "}\n" +
            "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();

    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
            "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    String drop = "PREFIX ex: <http://example/addresses#>\n" +
            "DROP GRAPH ex:G2 ";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, drop);
    update.execute();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());

    tupleHandler = new CountTupleHandler();
    drop = "PREFIX ex: <http://example/addresses#>\n" +
            "DROP GRAPH ex:G1 ";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, drop);
    update.execute();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());

    conn.close();
}