Java Code Examples for org.openrdf.query.Update#execute()

The following examples show how to use org.openrdf.query.Update#execute() . 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: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDropGraph()
	throws Exception
{
	logger.debug("executing testDropGraph");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("DROP GRAPH <" + graph1.stringValue() + "> ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	operation.execute();
	assertFalse(con.hasStatement(null, null, null, false, graph1));
	assertTrue(con.hasStatement(null, null, null, false, graph2));
	assertTrue(con.hasStatement(null, null, null, false));
}
 
Example 2
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCreateNewGraph()
	throws Exception
{
	logger.debug("executing testCreateNewGraph");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());

	URI newGraph = f.createURI(EX_NS, "new-graph");

	update.append("CREATE GRAPH <" + newGraph + "> ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	operation.execute();
	assertTrue(con.hasStatement(null, null, null, false, graph1));
	assertTrue(con.hasStatement(null, null, null, false, graph2));
	assertFalse(con.hasStatement(null, null, null, false, newGraph));
	assertTrue(con.hasStatement(null, null, null, false));
}
 
Example 3
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInsertWhereWithBindings()
	throws Exception
{
	logger.debug("executing test testInsertWhereWithBindings");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("INSERT { ?x rdfs:comment ?z . } WHERE { ?x foaf:name ?y }");

	Literal comment = f.createLiteral("Bob has a comment");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());
	operation.setBinding("x", bob);
	operation.setBinding("z", comment);

	assertFalse(con.hasStatement(null, RDFS.COMMENT, comment, true));

	operation.execute();

	assertTrue(con.hasStatement(bob, RDFS.COMMENT, comment, true));
	assertFalse(con.hasStatement(alice, RDFS.COMMENT, comment, true));

}
 
Example 4
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testClearGraph()
	throws Exception
{
	logger.debug("executing testClearGraph");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("CLEAR GRAPH <" + graph1.stringValue() + "> ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	operation.execute();
	assertFalse(con.hasStatement(null, null, null, false, graph1));
	assertTrue(con.hasStatement(null, null, null, false, graph2));
	assertTrue(con.hasStatement(null, null, null, false));
}
 
Example 5
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCopyFromDefaultToDefault()
	throws Exception
{
	logger.debug("executing testCopyFromDefaultToDefault");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("COPY DEFAULT TO DEFAULT");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	assertTrue(con.hasStatement(graph1, DC.PUBLISHER, null, false, (Resource)null));
	assertTrue(con.hasStatement(graph2, DC.PUBLISHER, null, false, (Resource)null));
	operation.execute();
	assertTrue(con.hasStatement(graph1, DC.PUBLISHER, null, false, (Resource)null));
	assertTrue(con.hasStatement(graph2, DC.PUBLISHER, null, false, (Resource)null));
}
 
Example 6
Source File: SelectorFactoryTest.java    From anno4j with Apache License 2.0 6 votes vote down vote up
private void clear() throws RepositoryException, UpdateExecutionException {
    String deleteUpdate = "DELETE {?s ?p ?o}\n" +
            "WHERE {?s ?p ?o}";

    ObjectConnection connection = this.anno4j.getObjectRepository().getConnection();

    Update update;
    try {
        update = connection.prepareUpdate(deleteUpdate);
    } catch (MalformedQueryException e) {
        e.printStackTrace();
        return;
    }

    update.execute();
}
 
Example 7
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCreateExistingGraph()
	throws Exception
{
	logger.debug("executing testCreateExistingGraph");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("CREATE GRAPH <" + graph1 + "> ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	try {
		operation.execute();

		fail("creation of existing graph should have resulted in error.");
	}
	catch (UpdateExecutionException e) {
		// expected behavior
		con.rollback();
	}
}
 
Example 8
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAddToNewNamed()
	throws Exception
{
	logger.debug("executing testAddToNewNamed");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("ADD GRAPH ex:graph1 TO ex:graph3");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	operation.execute();
	assertTrue(con.hasStatement(bob, FOAF.NAME, null, false, f.createURI(EX_NS, "graph3")));
	assertTrue(con.hasStatement(bob, FOAF.NAME, null, false, graph1));
}
 
Example 9
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInsertDataInGraph()
	throws Exception
{
	logger.debug("executing testInsertDataInGraph");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("INSERT DATA { GRAPH ex:graph1 { ex:book1 dc:title \"book 1\" ; dc:creator \"Ringo\" . } } ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	URI book1 = f.createURI(EX_NS, "book1");

	assertFalse(con.hasStatement(book1, DC.TITLE, f.createLiteral("book 1"), true, graph1));
	assertFalse(con.hasStatement(book1, DC.CREATOR, f.createLiteral("Ringo"), true, graph1));

	operation.execute();

	String msg = "two new statements about ex:book1 should have been inserted in graph1";
	assertTrue(msg, con.hasStatement(book1, DC.TITLE, f.createLiteral("book 1"), true, graph1));
	assertTrue(msg, con.hasStatement(book1, DC.CREATOR, f.createLiteral("Ringo"), true, graph1));
}
 
Example 10
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInsertEmptyWhere()
	throws Exception
{
	logger.debug("executing test testInsertEmptyWhere");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("INSERT { <" + bob + "> rdfs:label \"Bob\" . } WHERE { }");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	assertFalse(con.hasStatement(bob, RDFS.LABEL, f.createLiteral("Bob"), true));

	operation.execute();

	assertTrue(con.hasStatement(bob, RDFS.LABEL, f.createLiteral("Bob"), true));
}
 
Example 11
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDeleteWhereOptional()
	throws Exception
{
	logger.debug("executing test testDeleteWhereOptional");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append(" DELETE { ?x foaf:name ?y; foaf:mbox ?mbox. } ");
	update.append(" WHERE {?x foaf:name ?y. ");
	update.append(" OPTIONAL { ?x foaf:mbox ?mbox. FILTER (str(?mbox) = \"[email protected]\") } }");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	Literal mboxBob = f.createLiteral("[email protected]");
	Literal mboxAlice = f.createLiteral("[email protected]");
	assertTrue(con.hasStatement(bob, FOAF.MBOX, mboxBob, true));
	assertTrue(con.hasStatement(alice, FOAF.MBOX, mboxAlice, true));

	assertTrue(con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertTrue(con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

	operation.execute();

	assertFalse(con.hasStatement(bob, FOAF.MBOX, mboxBob, true));
	assertTrue(con.hasStatement(alice, FOAF.MBOX, mboxAlice, true));

	assertFalse(con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertFalse(con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

}
 
Example 12
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testInsertWhereWithOptional()
	throws Exception
{
	logger.debug("executing testInsertWhereWithOptional");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append(" INSERT { ?s ex:age ?incAge } ");
	// update.append(" DELETE { ?s ex:age ?age } ");
	update.append(" WHERE { ?s foaf:name ?name . ");
	update.append(" OPTIONAL {?s ex:age ?age . BIND ((?age + 1) as ?incAge)  } ");
	update.append(" } ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	URI age = f.createURI(EX_NS, "age");

	assertFalse(con.hasStatement(alice, age, null, true));
	assertTrue(con.hasStatement(bob, age, null, true));

	operation.execute();

	RepositoryResult<Statement> result = con.getStatements(bob, age, null, true);

	while (result.hasNext()) {
		System.out.println(result.next().toString());
	}

	assertTrue(con.hasStatement(bob, age, f.createLiteral("43", XMLSchema.INTEGER), true));

	result = con.getStatements(alice, age, null, true);

	while (result.hasNext()) {
		System.out.println(result.next());
	}
	assertFalse(con.hasStatement(alice, age, null, true));
}
 
Example 13
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testUpdateSequenceInsertDelete()
	throws Exception
{
	logger.debug("executing testUpdateSequenceInsertDelete");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("INSERT {?x foaf:name \"foo\" } WHERE {?y ex:containsPerson ?x}; ");
	update.append(getNamespaceDeclarations());
	update.append("DELETE {?y foaf:name ?n } WHERE {?x ex:containsPerson ?y. ?y foaf:name ?n . } ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	assertTrue(con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertTrue(con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

	operation.execute();

	String msg = "foaf:name properties should have been deleted";
	assertFalse(msg, con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertFalse(msg, con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

	msg = "foaf:name properties with value 'foo' should not have been added";
	assertFalse(msg, con.hasStatement(bob, FOAF.NAME, f.createLiteral("foo"), true));
	assertFalse(msg, con.hasStatement(alice, FOAF.NAME, f.createLiteral("foo"), true));
}
 
Example 14
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testUpdateSequenceDeleteInsert()
	throws Exception
{
	logger.debug("executing testUpdateSequenceDeleteInsert");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("DELETE {?y foaf:name ?n } WHERE {?x ex:containsPerson ?y. ?y foaf:name ?n . }; ");
	update.append(getNamespaceDeclarations());
	update.append("INSERT {?x foaf:name \"foo\" } WHERE {?y ex:containsPerson ?x} ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	assertTrue(con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertTrue(con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

	operation.execute();

	String msg = "foaf:name properties should have been deleted";
	assertFalse(msg, con.hasStatement(bob, FOAF.NAME, f.createLiteral("Bob"), true));
	assertFalse(msg, con.hasStatement(alice, FOAF.NAME, f.createLiteral("Alice"), true));

	msg = "foaf:name properties with value 'foo' should have been added";
	assertTrue(msg, con.hasStatement(bob, FOAF.NAME, f.createLiteral("foo"), true));
	assertTrue(msg, con.hasStatement(alice, FOAF.NAME, f.createLiteral("foo"), true));
}
 
Example 15
Source File: TestTicket1753.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void executeQuery(final BigdataSailRepository repo)
		throws UpdateExecutionException, RepositoryException, MalformedQueryException {
	try {
		repo.initialize();
		final BigdataSailRepositoryConnection conn = repo.getConnection();
		try {
			String update = "insert\r\n" + 
					"{ <http://dbpedia.org/resource/Jules_Verne> <http://ll.com.br/related> ?ss}\r\n" + 
					"where { {select distinct ?ss where { \r\n" + 
					"	?b <http://ll.com.br/author> ?ss . ?ss <http://purl.org/dc/terms/subject> ?x . \r\n" + 
					"	filter(?ss != <http://dbpedia.org/resource/Jules_Verne>)\r\n" + 
					"	{select (count(distinct ?s) as ?c) ?x where\r\n" + 
					"	{ <http://dbpedia.org/resource/Jules_Verne> <http://purl.org/dc/terms/subject> ?x . \r\n" +
					"	?s <http://purl.org/dc/terms/subject> ?x . \r\n" + 
					"	?b <http://ll.com.br/author> ?s . \r\n" + 
					"	filter( !contains(str(?x),\"University\") \r\n" + 
					"		&& !contains(str(?x),\"People\") \r\n" + 
					"		&& !contains(str(?x),\"Convert\") \r\n" + 
					"		&& !contains(str(?x),\"people\") )} \r\n" + 
					"	group by ?x having(?c < 500) } \r\n" + 
					"}order by asc(?c) limit 20} }"; 
			Update preparedUpdate = conn.prepareUpdate(QueryLanguage.SPARQL, update);
			preparedUpdate.execute();
			// no exception should occur on execution, overwise test will fail
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 16
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDeleteInsertWhereLoopingBehavior()
	throws Exception
{
	logger.debug("executing test testDeleteInsertWhereLoopingBehavior");
	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append(" DELETE { ?x ex:age ?y } INSERT {?x ex:age ?z }");
	update.append(" WHERE { ");
	update.append("   ?x ex:age ?y .");
	update.append("   BIND((?y + 1) as ?z) ");
	update.append("   FILTER( ?y < 46 ) ");
	update.append(" } ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	URI age = f.createURI(EX_NS, "age");
	Literal originalAgeValue = f.createLiteral("42", XMLSchema.INTEGER);
	Literal correctAgeValue = f.createLiteral("43", XMLSchema.INTEGER);
	Literal inCorrectAgeValue = f.createLiteral("46", XMLSchema.INTEGER);

	assertTrue(con.hasStatement(bob, age, originalAgeValue, true));

	operation.execute();

	assertFalse(con.hasStatement(bob, age, originalAgeValue, true));
	assertTrue(con.hasStatement(bob, age, correctAgeValue, true));
	assertFalse(con.hasStatement(bob, age, inCorrectAgeValue, true));
}
 
Example 17
Source File: OWLSchemaPersistingManager.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * Persists the information that a property is functional to the default graph of the connected triplestore.
 * All properties with {@link Functional} or {@link Bijective} annotation are considered.
 * @param annotatedObjects The {@link Iri} annotated objects that should be considered.
 * @throws RepositoryException Thrown on error regarding the connected triplestore.
 * @throws UpdateExecutionException Thrown if an error occurred while executing the update.
 */
private void persistFunctional(Collection<AccessibleObject> annotatedObjects) throws RepositoryException, UpdateExecutionException {
    // Get those methods and fields that have the @Functional annotation:
    Collection<AccessibleObject> functionalObjects = filterObjectsWithAnnotation(annotatedObjects, Functional.class);
    // All those objects that are declared bijective are also functional:
    functionalObjects.addAll(filterObjectsWithAnnotation(annotatedObjects, Bijective.class));

    // Prepare the update query and execute it:
    try {
        Update update = buildInstanceUpdate(getIrisFromObjects(functionalObjects), OWL.FUNCTIONAL_PROPERTY);
        update.execute();
    } catch (MalformedQueryException e) {
        throw new UpdateExecutionException();
    }
}
 
Example 18
Source File: BigdataSPARQLUpdateConformanceTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
    protected void runTest()
        throws Exception
    {
        BigdataSailRepositoryConnection con = (BigdataSailRepositoryConnection) dataRep.getConnection();
        BigdataSailRepositoryConnection erCon = (BigdataSailRepositoryConnection) expectedResultRepo.getConnection();
        try {
            String updateString = readUpdateString();
            
            con.begin();
//            con.setReadContexts((URI)null);
            
            Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
//            if (this.dataset != null) {
//                update.setDataset(this.dataset);
//            }
            update.execute();

            con.commit();
            
            // check default graph
            compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource)null)),
                    Iterations.asList(erCon.getStatements(null, null, null, true, (Resource)null)),
                    (BigdataSailRepositoryConnection) con);

            for (String namedGraph : inputNamedGraphs.keySet()) {
                URI contextURI = con.getValueFactory().createURI(namedGraph.replaceAll("\"", ""));
                compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
                        Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)),
                        (BigdataSailRepositoryConnection) con);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            if(con.isActive()) {
                con.rollback();
            }
            throw e;
        }
        finally {
            con.close();
            erCon.close();
        }
    }
 
Example 19
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testInsertData2()
	throws Exception
{
	logger.debug("executing testInsertData2");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append("INSERT DATA { ex:book1 dc:title \"the number four\"^^<http://www.w3.org/2001/XMLSchema#integer> . }; ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	URI book1 = f.createURI(EX_NS, "book1");

	assertFalse(con.hasStatement(book1, DC.TITLE, f.createLiteral("the number four", XMLSchema.INTEGER), true));

	operation.execute();

	String msg = "new statement about ex:book1 should have been inserted";

	assertTrue(msg, con.hasStatement(book1, DC.TITLE, f.createLiteral("the number four", XMLSchema.INTEGER), true));
}
 
Example 20
Source File: SPARQLUpdateTest.java    From database with GNU General Public License v2.0 2 votes vote down vote up
@Test
public void testInsertWhereWithBlankNode()
	throws Exception
{
	logger.debug("executing testInsertWhereWithBlankNode");

	StringBuilder update = new StringBuilder();
	update.append(getNamespaceDeclarations());
	update.append(" INSERT { ?s ex:complexAge [ rdf:value ?age; rdfs:label \"old\" ] . } ");
	update.append(" WHERE { ?s ex:age ?age . ");
	update.append(" } ");

	Update operation = con.prepareUpdate(QueryLanguage.SPARQL, update.toString());

	URI age = f.createURI(EX_NS, "age");
	URI complexAge = f.createURI(EX_NS, "complexAge");

	assertTrue(con.hasStatement(bob, age, null, true));

	operation.execute();

	RepositoryResult<Statement> sts = con.getStatements(bob, complexAge, null, true);

	assertTrue(sts.hasNext());

	Value v1 = sts.next().getObject();

	sts.close();

	sts = con.getStatements(null, RDF.VALUE, null, true);

	assertTrue(sts.hasNext());

	Value v2 = sts.next().getSubject();

	assertEquals(v1, v2);

	sts.close();

	String query = getNamespaceDeclarations()
			+ " SELECT ?bn ?age ?l WHERE { ex:bob ex:complexAge ?bn. ?bn rdf:value ?age. ?bn rdfs:label ?l .} ";

	TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate();

	assertTrue(result.hasNext());

	BindingSet bs = result.next();

	assertFalse(result.hasNext());

}