org.openrdf.query.impl.DatasetImpl Java Examples

The following examples show how to use org.openrdf.query.impl.DatasetImpl. 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: Test_Ticket_789.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void test_ticket_1326_dataset() throws Exception {
	populate();
	
	BigdataSailRemoteRepository sailRepo = m_repo.getBigdataSailRemoteRepository();
	BigdataSailRemoteRepositoryConnection con = sailRepo.getConnection();
	final TupleQuery tq = con.prepareTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p ?o}");

	final DatasetImpl dataset = new DatasetImpl();
	dataset.addDefaultGraph(defaultGraph1);

	tq.setDataset(dataset);
	{
		TupleQueryResult tqr = tq.evaluate();
		try {
			int count = 0;
			while (tqr.hasNext()) {
				tqr.next();
				count++;
			}
			// there is only 1 statement in defaultGraph1 
			assertEquals(1, count);
		} finally {
			tqr.close();
		}
	}
}
 
Example #2
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDeleteDefaultGraph()
	throws Exception
{
	URI g1 = vf.createURI("urn:test:g1");
	URI g2 = vf.createURI("urn:test:g2");
	testCon.add(vf.createURI(URN_TEST_S1), vf.createURI(URN_TEST_P1), vf.createURI(URN_TEST_O1), g1);
	testCon.add(vf.createURI("urn:test:s2"), vf.createURI(URN_TEST_P2), vf.createURI("urn:test:o2"), g2);
	Update up = testCon.prepareUpdate(QueryLanguage.SPARQL, SPARQL_DEL_ALL);
	DatasetImpl ds = new DatasetImpl();
	ds.addDefaultGraph(g1);
	ds.addDefaultRemoveGraph(g1);
	up.setDataset(ds);
	up.execute();
	assertThat(size(g1), is(equalTo(0)));
	assertThat(size(g2), is(equalTo(1)));
}
 
Example #3
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private int size(URI defaultGraph)
	throws RepositoryException, MalformedQueryException, QueryEvaluationException
{
	TupleQuery qry = testCon.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { ?s ?p ?o }");
	DatasetImpl dataset = new DatasetImpl();
	dataset.addDefaultGraph(defaultGraph);
	qry.setDataset(dataset);
	TupleQueryResult result = qry.evaluate();
	try {
		int count = 0;
		while (result.hasNext()) {
			result.next();
			count++;
		}
		return count;
	}
	finally {
		result.close();
	}
}
 
Example #4
Source File: SPARQLUpdateConformanceTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public SPARQLUpdateConformanceTest(String testURI, String name, String requestFile, URI defaultGraphURI,
		Map<String, URI> inputNamedGraphs, URI resultDefaultGraphURI, Map<String, URI> resultNamedGraphs)
{
	super(name);

	this.testURI = testURI;
	this.requestFileURL = requestFile;
	this.inputDefaultGraph = defaultGraphURI;
	this.inputNamedGraphs = inputNamedGraphs;
	this.resultDefaultGraph = resultDefaultGraphURI;
	this.resultNamedGraphs = resultNamedGraphs;

	if (this.inputNamedGraphs.size() > 0) {
		DatasetImpl ds = new DatasetImpl();
		ds.addDefaultGraph(null);
		ds.addDefaultRemoveGraph(null);
		ds.setDefaultInsertGraph(null);

		for (String ng : inputNamedGraphs.keySet()) {
			URI namedGraph = new URIImpl(ng);
			ds.addNamedGraph(namedGraph);
		}
		this.dataset = ds;
	}
	else {
		this.dataset = null;
	}
}
 
Example #5
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testDataset()
	throws Exception
{
	testCon.add(alice, name, nameAlice, context2);
	testCon.add(alice, mbox, mboxAlice, context2);
	testCon.add(context2, publisher, nameAlice);
	testCon.add(bob, name, nameBob, context1);
	testCon.add(bob, mbox, mboxBob, context1);
	testCon.add(context1, publisher, nameBob);
	StringBuilder queryBuilder = new StringBuilder();
	queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> ");
	queryBuilder.append(ASK);
	queryBuilder.append("{ ?p foaf:name ?name }");
	BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);
	assertThat(query.evaluate(), is(equalTo(true)));
	DatasetImpl dataset = new DatasetImpl();

	// default graph: {context1}
	dataset.addDefaultGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));

	// default graph: {context1, context2}
	dataset.addDefaultGraph(context2);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));

	// default graph: {context2}
	dataset.removeDefaultGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));
	queryBuilder.setLength(0);
	queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> ");
	queryBuilder.append(ASK);
	queryBuilder.append("{ GRAPH ?g { ?p foaf:name ?name } }");
	query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);

	// default graph: {context2}; named graph: {}
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));

	// default graph: {context1, context2}; named graph: {context2}
	dataset.addDefaultGraph(context1);
	dataset.addNamedGraph(context2);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));

	// default graph: {context1, context2}; named graph: {context1, context2}
	dataset.addNamedGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));
}
 
Example #6
Source File: TestBigdataExprBuilder.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * A variant of the above test where one of the URIs in the default / named
     * graph declarations is not a graph in the KB.
     * 
     * @throws MalformedQueryException
     * @throws TokenMgrError
     * @throws ParseException
     */
    public void test_from_and_from_named_with_unknown_graph()
            throws MalformedQueryException, TokenMgrError, ParseException {

        final String sparql = "" + //
                "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + //
                "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + //
                "SELECT ?who ?g ?mbox\n" + //
                "FROM <http://example.org/dft.ttl>\n" + //
                "FROM NAMED <http://example.org/alice>\n" + //
                "FROM NAMED <http://example.org/bob>\n" + //
                "WHERE {\n" + //
                "    ?g dc:publisher ?who .\n" + //
                "    GRAPH ?g { ?x foaf:mbox ?mbox } \n" + //
                "}"//
        ;

        final QueryRoot expected = new QueryRoot(QueryType.SELECT);
        {

            {
                final Map<String, String> prefixDecls = new LinkedHashMap<String, String>(PrefixDeclProcessor.defaultDecls);
                prefixDecls.put("foaf", FOAFVocabularyDecl.NAMESPACE);
                prefixDecls.put("dc", DC.NAMESPACE);
                expected.setPrefixDecls(prefixDecls);
            }

            {
                final ProjectionNode projection = new ProjectionNode();
                expected.setProjection(projection);
                projection.addProjectionVar(new VarNode("who"));
                projection.addProjectionVar(new VarNode("g"));
                projection.addProjectionVar(new VarNode("mbox"));
            }

            {
                final BigdataURI uri1 = valueFactory
                        .createURI("http://example.org/dft.ttl");

                final BigdataURI uri2 = valueFactory
                        .createURI("http://example.org/alice");

                final BigdataURI uri3 = valueFactory
                        .createURI("http://example.org/bob");

                final BigdataValue[] values = new BigdataValue[] { //
                        uri1, //
                        uri2,//
//                        uri3 //
                };

                tripleStore.getLexiconRelation().addTerms(values,
                        values.length, false/* readOnly */);

                final DatasetImpl dataset = new DatasetImpl();
                dataset.addDefaultGraph(uri1);
                dataset.addNamedGraph(uri2);
                dataset.addNamedGraph(uri3);
                final DatasetNode datasetNode = new DatasetNode(dataset, false/* update */);
                expected.setDataset(datasetNode);
            }

            {
                final JoinGroupNode whereClause = new JoinGroupNode();
                expected.setWhereClause(whereClause);

                whereClause
                        .addChild(new StatementPatternNode(new VarNode("g"),
                                new ConstantNode(makeIV(valueFactory
                                        .createURI(DC.PUBLISHER.toString()))),
                                new VarNode("who"), null/* c */,
                                Scope.DEFAULT_CONTEXTS));

                final JoinGroupNode group = new JoinGroupNode();
                whereClause.addChild(group);
                group.setContext(new VarNode("g"));
                group.addChild(new StatementPatternNode(
                        new VarNode("x"),
                        new ConstantNode(makeIV(valueFactory
                                .createURI(FOAFVocabularyDecl.mbox.toString()))),
                        new VarNode("mbox"), new VarNode("g"),
                        Scope.NAMED_CONTEXTS));

            }
        }

        final QueryRoot actual = parse(sparql, baseURI);

        assertSameAST(sparql, expected, actual);

    }