Java Code Examples for org.openrdf.query.TupleQuery#setIncludeInferred()

The following examples show how to use org.openrdf.query.TupleQuery#setIncludeInferred() . 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: SampleBlazegraphSesameEmbedded.java    From blazegraph-samples with GNU General Public License v2.0 6 votes vote down vote up
public static TupleQueryResult executeSelectQuery(final Repository repo, final String query,
		final QueryLanguage ql) throws OpenRDFException  {

	RepositoryConnection cxn;
	if (repo instanceof BigdataSailRepository) {
		cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
	} else {
		cxn = repo.getConnection();
	}

	try {

		final TupleQuery tupleQuery = cxn.prepareTupleQuery(ql, query);
		tupleQuery.setIncludeInferred(true /* includeInferred */);
		return tupleQuery.evaluate();
		
	} finally {
		// close the repository connection
		cxn.close();
	}
}
 
Example 2
Source File: Utils.java    From blazegraph-samples with GNU General Public License v2.0 6 votes vote down vote up
public static TupleQueryResult executeSelectQuery(Repository repo, String query,
		QueryLanguage ql) throws OpenRDFException  {

	RepositoryConnection cxn;
	if (repo instanceof BigdataSailRepository) {
		cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
	} else {
		cxn = repo.getConnection();
	}

	try {

		final TupleQuery tupleQuery = cxn.prepareTupleQuery(ql, query);
		tupleQuery.setIncludeInferred(true /* includeInferred */);
		return tupleQuery.evaluate();
		
	} finally {
		// close the repository connection
		cxn.close();
	}
}
 
Example 3
Source File: Concurrency.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Issue the query.
 * 
 * @throws Exception
 */
private void doQuery() throws Exception {
   
    RepositoryConnection cxn = repo.getReadOnlyConnection();
    try {

        final TupleQuery tupleQuery = 
            cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        tupleQuery.setIncludeInferred(true /* includeInferred */);
        TupleQueryResult result = tupleQuery.evaluate();
        // do something with the results
        int resultCount = 0;
        while (result.hasNext()) {
            BindingSet bindingSet = result.next();
            // log.info(bindingSet);
            resultCount++;
        }
        log.info(resultCount + " results");
        
    } finally {
        // close the repository connection
        cxn.close();
    }
        
}
 
Example 4
Source File: BigdataSailRemoteRepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testTupleQueryIncludeInferred() throws Exception {
	final TupleQuery tq = con.prepareTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p ?o}");
	tq.setIncludeInferred(false);
	tq.evaluate();
	assertEquals("false", remote.data.opts.getRequestParam(RemoteRepositoryDecls.INCLUDE_INFERRED));
	assertEquals("false", remote.data.request.getParams().get(RemoteRepositoryDecls.INCLUDE_INFERRED).getValue());
	
	tq.setIncludeInferred(true);
	final TupleQueryResult tqr = tq.evaluate();
	try {
		assertEquals("true", remote.data.opts.getRequestParam(RemoteRepositoryDecls.INCLUDE_INFERRED));
		assertEquals("true", remote.data.request.getParams().get(RemoteRepositoryDecls.INCLUDE_INFERRED).getValue());
	} finally {
		tqr.close();
	}
}
 
Example 5
Source File: LoadPdb.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public static void executeSelectQuery(Repository repo, String query) throws Exception {

		RepositoryConnection cxn = repo.getConnection();
		int counter = 0;
		try {

			final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
			tupleQuery.setIncludeInferred(true /* includeInferred */);
			TupleQueryResult result = tupleQuery.evaluate();
			// do something with the results
			while (result.hasNext()) {
				BindingSet bindingSet = result.next();
				LOG.info(bindingSet);
				counter++;
			}

		} finally {
			// close the repository connection
			cxn.close();
//			LOG.info
            System.err.println
            ("Number of results: " + counter);
		}

	}
 
Example 6
Source File: SampleCode.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute a "select" query.
 * 
 * @param repo
 * @param query
 * @param ql
 * @throws Exception
 */
public void executeSelectQuery(Repository repo, String query, 
    QueryLanguage ql) throws Exception {
    
    /*
     * With MVCC, you read from a historical state to avoid blocking and
     * being blocked by writers.  BigdataSailRepository.getQueryConnection
     * gives you a view of the repository at the last commit point.
     */
    RepositoryConnection cxn;
    if (repo instanceof BigdataSailRepository) { 
        cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
    } else {
        cxn = repo.getConnection();
    }
    
    try {

        final TupleQuery tupleQuery = cxn.prepareTupleQuery(ql, query);
        tupleQuery.setIncludeInferred(true /* includeInferred */);
        TupleQueryResult result = tupleQuery.evaluate();
        // do something with the results
        while (result.hasNext()) {
            BindingSet bindingSet = result.next();
            log.info(bindingSet);
        }
        
    } finally {
        // close the repository connection
        cxn.close();
    }
    
}
 
Example 7
Source File: ScaleOut.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Issue the query.
 * 
 * @throws Exception
 */
private void doQuery() throws Exception {
   
    // this is how you get a read-only transaction.  MUST be
    // committed or aborted later, see below.
    long transactionId = 
        fed.getTransactionService().newTx(ITx.READ_COMMITTED);
    
    try {

        // open the read-only triple store
        final AbstractTripleStore tripleStore = 
            openTripleStore(fed, transactionId);
        
        // wrap it in a Sesame SAIL
        final BigdataSail sail = new BigdataSail(tripleStore);
        final Repository repo = new BigdataSailRepository(sail);
        repo.initialize();
        
        RepositoryConnection cxn = repo.getConnection();
        try {

            final TupleQuery tupleQuery = 
                cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
            tupleQuery.setIncludeInferred(true /* includeInferred */);
            TupleQueryResult result = tupleQuery.evaluate();
            // do something with the results
            int resultCount = 0;
            while (result.hasNext()) {
                BindingSet bindingSet = result.next();
                // log.info(bindingSet);
                resultCount++;
            }
            log.info(resultCount + " results");
            
        } finally {
            // close the repository connection
            cxn.close();
        }
        
        repo.shutDown();
        
    } finally {
        
        // MUST close the transaction, abort is sufficient
        fed.getTransactionService().abort(transactionId);
        
    }
    
}
 
Example 8
Source File: TestOptionals.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Tests mapping of left joins in SPARQL onto optionals in bigdata rules.
     * 
     * @throws Exception 
     */
    public void testLeftJoins() throws Exception {

        final BigdataSail sail = getSail();
        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        final BigdataSailRepositoryConnection cxn = 
            (BigdataSailRepositoryConnection) repo.getConnection();
        cxn.setAutoCommit(false);
        
        try {
    
            final URI book1 = new URIImpl("http://www.bigdata.com/rdf#book1");
            final URI book2 = new URIImpl("http://www.bigdata.com/rdf#book2");
            final URI book3 = new URIImpl("http://www.bigdata.com/rdf#book3");
            final URI price = new URIImpl("http://www.bigdata.com/rdf#price");
            final URI XSD_INTEGER = new URIImpl("http://www.w3.org/2001/XMLSchema#integer");
/**/            
            cxn.add(
                    book1,
                    DC_TITLE,
                    new LiteralImpl("TITLE 1")
                    );
            cxn.add(
                    book1,
                    price,
                    new LiteralImpl("10", XSD_INTEGER)
                    );
            cxn.add(
                    book2,
                    DC_TITLE,
                    new LiteralImpl("TITLE 2")
                    );
            cxn.add(
                    book2,
                    price,
                    new LiteralImpl("20", XSD_INTEGER)
                    );
            cxn.add(
                    book3,
                    DC_TITLE,
                    new LiteralImpl("TITLE 3")
                    );
/**/

            /*
             * Note: The either flush() or commit() is required to flush the
             * statement buffers to the database before executing any operations
             * that go around the sail.
             */
//            cxn.flush();//commit();
            cxn.commit();
            
/**/            
            if (log.isInfoEnabled()) {
                log.info(cxn.getTripleStore().dumpStore());
            }

            String query = 
                "SELECT ?title ?price " +
                "WHERE { " +
                "?book <"+DC_TITLE+"> ?title . " +
                "OPTIONAL { ?book <"+price+"> ?price . } . " +
                "}";
            
            final TupleQuery tupleQuery = 
                cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
            tupleQuery.setIncludeInferred(true /* includeInferred */);
            TupleQueryResult result = tupleQuery.evaluate();

            Collection<BindingSet> answer = new LinkedList<BindingSet>();
            answer.add(createBindingSet(
                    new BindingImpl("title", new LiteralImpl("TITLE 1")), new BindingImpl("price", new LiteralImpl("10", XSD_INTEGER))));
            answer.add(createBindingSet(
                    new BindingImpl("title", new LiteralImpl("TITLE 2")), new BindingImpl("price", new LiteralImpl("20", XSD_INTEGER))));
            answer.add(createBindingSet(
                    new BindingImpl("title", new LiteralImpl("TITLE 3"))));
            
            compare(result, answer);
            
            
        } finally {
            cxn.close();
            sail.__tearDownUnitTest();
        }

    }
 
Example 9
Source File: TestUnions.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Tests mapping of UNIONS in SPARQL onto unions in bigdata rules.
     * 
     * @throws Exception 
     */
    public void testSesameFilters() throws Exception {

        final BigdataSail sail = getSail();
        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        final BigdataSailRepositoryConnection cxn = 
            (BigdataSailRepositoryConnection) repo.getConnection();
        cxn.setAutoCommit(false);
        
        try {
    
            final URI jack = new URIImpl("_:Jack");
            final URI jill = new URIImpl("_:Jill");
            final URI person = new URIImpl("_:Person");
            final URI age = new URIImpl("_:age");
            final URI integer = new URIImpl("http://www.w3.org/2001/XMLSchema#integer");
/**/            
            cxn.add(
                    jack,
                    RDF.TYPE,
                    person
                    );
            cxn.add(
                    jill,
                    RDF.TYPE,
                    person
                    );
            cxn.add(
                    jack,
                    age,
                    new LiteralImpl("40", integer)
                    );
            cxn.add(
                    jill,
                    age,
                    new LiteralImpl("30", integer)
                    );
/**/

            /*
             * Note: The either flush() or commit() is required to flush the
             * statement buffers to the database before executing any operations
             * that go around the sail.
             */
            cxn.flush();//commit();
            
/**/            
            log.info("hello");
            if (log.isInfoEnabled()) {
                log.info(cxn.getTripleStore().dumpStore());
            }

            {
                
                String query = 
                    "SELECT * " +
                    "WHERE { " +
                    "  { " +
                    "    ?x <"+RDF.TYPE+"> <"+person+"> . " +
                    "    ?x <"+age+"> ?age1 . " +
                    "    FILTER( ?age1 > 35 ) . " +
                    "  } " +
                    "  UNION " +
                    "  { " +
                    "    ?x <"+RDF.TYPE+"> <"+person+"> . " +
                    "    ?x <"+age+"> ?age2 . " +
                    "    FILTER( ?age2 > 25 ) . " +
                    "  } " +
                    "}";
                
                final StringWriter sw = new StringWriter();
                
                final TupleQuery tupleQuery = 
                    cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
                tupleQuery.setIncludeInferred(true /* includeInferred */);
                tupleQuery.evaluate(new SPARQLResultsXMLWriter(new XMLWriter(sw)));
    
                if(log.isInfoEnabled())log.info(sw.toString());
            }
            
        } finally {
            cxn.close();
            sail.__tearDownUnitTest();
        }

    }
 
Example 10
Source File: TestQuadsAPI.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Matt, do you recognize this unit test: TestQuadsAPI#testSCequality()? I
     * think that this may have been something from you dealing with the binding
     * on the S and C positions. Bryan
     * <p>
     * mooseroy: If i remember correctly, early on there was a problem when you
     * had something like graph ?s{?s ?p ?o} where it wasn't ensuring the ?s in
     * the context and subject position where equal.
     */
    public void testSCequality() throws Exception {

        final BigdataSail sail = getSail();
        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        final BigdataSailRepositoryConnection cxn = 
            (BigdataSailRepositoryConnection) repo.getConnection();
        cxn.setAutoCommit(false);
        
        try {
    
            assertEquals(0, cxn.getTripleStore().getNamedGraphCount());
            
            assertFalse(cxn.getContextIDs().hasNext());
            
//            final BNode a = new BNodeImpl("_:a");
//            final BNode b = new BNodeImpl("_:b");
            final URI graphA = new URIImpl("http://www.bigdata.com/rdf#graphA");
            final URI graphB = new URIImpl("http://www.bigdata.com/rdf#graphB");
            final URI s = new URIImpl("http://www.bigdata.com/rdf#s");
            final URI p1 = new URIImpl("http://www.bigdata.com/rdf#p1");
            final URI o1 = new URIImpl("http://www.bigdata.com/rdf#o1");
            final URI p2 = new URIImpl("http://www.bigdata.com/rdf#p2");
            final URI o2 = new URIImpl("http://www.bigdata.com/rdf#o2");

            cxn.add(graphA, p1, o1, graphA);

            cxn.add(     s, p1, o1, graphA);
            
            cxn.add(graphA, p2, o2, graphA);
            
            cxn.add(     s, p2, o2, graphB);

            /*
             * Note: The either flush() or commit() is required to flush the
             * statement buffers to the database before executing any operations
             * that go around the sail.
             */
//            cxn.flush();//commit();
            cxn.commit();
            
            assertEquals(2, cxn.getTripleStore().getNamedGraphCount());
            
            assertSameIterationAnyOrder(new Resource[] { graphA, graphB }, cxn
                    .getContextIDs());

/**/            
            if (log.isInfoEnabled()) {
                log.info("\n" + cxn.getTripleStore().dumpStore());
            }
            
            final String query = 
                "SELECT  * \n" +
                "WHERE { \n"+
                "  GRAPH ?g { \n" +
                "    ?g <"+p1+"> <"+o1+"> . \n" +
                "    ?g <"+p2+"> <"+o2+"> . \n" +
                "  }\n" +
                "}";
            
            final TupleQuery tupleQuery = 
                cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
            
            tupleQuery.setIncludeInferred(true /* includeInferred */);
            
            final TupleQueryResult result = tupleQuery.evaluate();

            final Collection<BindingSet> answer = new LinkedList<BindingSet>();
            
            answer.add(createBindingSet(
                    new BindingImpl("g", graphA)));
            
            compare(result, answer);
            

        } finally {
            cxn.close();
            sail.__tearDownUnitTest();
        }

    }