MarkLogicTupleQuery extends MarkLogicQuery implements TupleQuery,MarkLogicQueryDependent { @Override public TupleQueryResult evaluate() throws QueryEvaluationException { return evaluate(this.start,this.pageLength); } MarkLogicTupleQuery(MarkLogicClient client, SPARQLQueryBindingSet bindingSet, String baseUri, String queryString, GraphPermissions graphPerms, QueryDefinition queryDef, SPARQLRuleset[] rulesets); @Override TupleQueryResult evaluate(); TupleQueryResult evaluate(long start, long pageLength); @Override void evaluate(TupleQueryResultHandler resultHandler); void evaluate(TupleQueryResultHandler resultHandler,long start, long pageLength);  }
@Test public void testPrepareTupleQueryQueryStringMethod() throws Exception{ String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 10 "; TupleQuery tupleQuery = conn.prepareTupleQuery(queryString); tupleQuery = conn.prepareTupleQuery(queryString,"http: TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); results.close(); }
@Test public void testSPARQLQueryWithDefaultInferred() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 2 "; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setIncludeInferred(true); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); BindingSet bindingSet1 = results.next(); Value sV1 = bindingSet1.getValue("s"); Value pV1 = bindingSet1.getValue("p"); Value oV1 = bindingSet1.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV1.stringValue()); results.close(); }
@Test public void testSPARQLQueryDistinct() throws Exception { try { String queryString = "SELECT DISTINCT ?_ WHERE { GRAPH ?ctx { ?s ?p ?o . } }"; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult result = tupleQuery.evaluate(); RepositoryResult<Resource> rr = new RepositoryResult<Resource>( new ExceptionConvertingIteration<Resource, RepositoryException>( new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(result) { @Override protected Resource convert(BindingSet bindings) throws QueryEvaluationException { return (Resource) bindings.getValue("_"); } }) { @Override protected RepositoryException convert(Exception e) { return new RepositoryException(e); } }); Assert.assertTrue(rr.hasNext()); result.close(); } catch (MalformedQueryException e) { throw new RepositoryException(e); } catch (QueryEvaluationException e) { throw new RepositoryException(e); } }
@Test public void testSPARQLQueryWithPagination() throws Exception { try{ for(int i=0; i<101; i++){ String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 100 "; MarkLogicTupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(3, 1); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); Assert.assertFalse(results.hasNext()); results.close(); } } catch(Exception ex) { throw ex; } finally { conn.close(); Thread.sleep(1000); } }
@Test public void testSPARQLQueryCloseWait() throws Exception { try{ String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 100"; MarkLogicTupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); for(int i=0; i<101; i++){ TupleQueryResult results = tupleQuery.evaluate(3,1); results.close(); } } catch(Exception ex) { throw ex; } finally { conn.close(); Thread.sleep(1000); } }
@Test public void testSPARQLQueryWithRuleset() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 100 "; MarkLogicTupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setRulesets(SPARQLRuleset.RDFS_FULL); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); results.close(); }
@Test public void testSPARQLQueryWithMultipleRulesets() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 100 "; MarkLogicTupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setRulesets(SPARQLRuleset.RDFS_FULL,SPARQLRuleset.DOMAIN); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); results.close(); }
@Test public void testSPARQLQueryWithResultsHandler() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 10"; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.evaluate(new TupleQueryResultHandler() { @Override public void startQueryResult(List<String> bindingNames) { Assert.assertEquals(bindingNames.get(0), "s"); Assert.assertEquals(bindingNames.get(1), "p"); Assert.assertEquals(bindingNames.get(2), "o"); } @Override public void handleSolution(BindingSet bindingSet) { Assert.assertEquals(bindingSet.getBinding("o").getValue().stringValue(), "0"); } @Override public void endQueryResult() { } @Override public void handleBoolean(boolean arg0) throws QueryResultHandlerException { } @Override public void handleLinks(List<String> arg0) throws QueryResultHandlerException { } }); }
@Test public void testSPARQLQueryBindings() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o . filter (?s = ?b) filter (?p = ?c) }"; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setBinding("b", ValueFactoryImpl.getInstance().createURI("http: tupleQuery.setBinding("c", ValueFactoryImpl.getInstance().createURI("http: tupleQuery.removeBinding("c"); Assert.assertEquals(null, tupleQuery.getBindings().getBinding("c")); tupleQuery.clearBindings(); Assert.assertEquals(null, tupleQuery.getBindings().getBinding("b")); tupleQuery.setBinding("b", ValueFactoryImpl.getInstance().createURI("http: tupleQuery.setBinding("c", ValueFactoryImpl.getInstance().createURI("http: TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); logger.info(results.getBindingNames().toString()); Assert.assertTrue(results.hasNext()); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("http: results.close(); }
@Test public void testSPARQLWithWriter() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(out); String expected = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<sparql xmlns='http: "\t<head>\n" + "\t\t<variable name='s'/>\n" + "\t\t<variable name='p'/>\n" + "\t\t<variable name='o'/>\n" + "\t</head>\n" + "\t<results>\n" + "\t\t<result>\n" + "\t\t\t<binding name='s'>\n" + "\t\t\t\t<uri>http: "\t\t\t</binding>\n" + "\t\t\t<binding name='p'>\n" + "\t\t\t\t<uri>http: "\t\t\t</binding>\n" + "\t\t\t<binding name='o'>\n" + "\t\t\t\t<literal datatype='http: "\t\t\t</binding>\n" + "\t\t</result>\n" + "\t</results>\n" + "</sparql>\n"; String queryString = "select * { ?s ?p ?o . } limit 1"; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.evaluate(sparqlWriter); Assert.assertEquals(expected, out.toString()); }
@Test public void testSPARQLQueryWithEmptyResults() throws Exception { String queryString = "select * { <http: TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertFalse(results.hasNext()); results.close(); }
@Test public void testSPARQLQueryWithNullRulesets() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 100 "; MarkLogicTupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setRulesets(SPARQLRuleset.RDFS_FULL,null); tupleQuery.setRulesets(null); Assert.assertTrue(tupleQuery.getRulesets() == null); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); results.close(); }
@Test(expected=org.openrdf.query.QueryEvaluationException.class) public void testSPARQLQueryQueryEvaluationException() throws Exception { String queryString = "select * <http: TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertFalse(results.hasNext()); results.close(); }
@Test(expected=org.openrdf.query.QueryEvaluationException.class) public void testSPARQLMalformedException() throws Exception { String queryString = "select1 * <http: TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertFalse(results.hasNext()); results.close(); }
@Test public void testSPARQLQueryWithPrepareQuery() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 1 "; Query q = conn.prepareQuery(QueryLanguage.SPARQL, queryString); try { if (q instanceof TupleQuery) { TupleQueryResult result = ((TupleQuery) q).evaluate(); while (result.hasNext()) { BindingSet tuple = result.next(); Assert.assertEquals("s", tuple.getBinding("s").getName()); Assert.assertEquals("http: } result.close(); } } catch(Exception ex) { throw ex; } finally { conn.close(); Thread.sleep(1000); } }
@Test public void testSPARQLQuery() throws Exception { try { for (int i=0; i<101;i++){ String queryString = "select * { ?s ?p ?o } limit 2 "; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV.stringValue()); BindingSet bindingSet1 = results.next(); Value sV1 = bindingSet1.getValue("s"); Value pV1 = bindingSet1.getValue("p"); Value oV1 = bindingSet1.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("0", oV1.stringValue()); results.close(); } } catch(Exception ex) { throw ex; } finally { conn.close(); Thread.sleep(1000); } }
@Test public void testPrepareTupleQueryWithOptimizeLevel() throws Exception{ String queryString = "select ?s ?p ?o { ?s ?p ?o } limit 10 "; TupleQuery tupleQuery = conn.prepareTupleQuery(queryString,"http: conn.setOptimizeLevel(0); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertNotNull(results); conn.setOptimizeLevel(null); results.close(); }
@Test public void testSPARQLQueryBindings() throws Exception { String queryString = "select ?s ?p ?o { ?s ?p ?o . filter (?s = ?b) filter (?p = ?c) }"; TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); tupleQuery.setBinding("b", SimpleValueFactory.getInstance().createIRI("http: tupleQuery.setBinding("c", SimpleValueFactory.getInstance().createIRI("http: tupleQuery.removeBinding("c"); Assert.assertEquals(null, tupleQuery.getBindings().getBinding("c")); tupleQuery.clearBindings(); Assert.assertEquals(null, tupleQuery.getBindings().getBinding("b")); tupleQuery.setBinding("b", SimpleValueFactory.getInstance().createIRI("http: tupleQuery.setBinding("c", SimpleValueFactory.getInstance().createIRI("http: TupleQueryResult results = tupleQuery.evaluate(); Assert.assertEquals(results.getBindingNames().get(0), "s"); Assert.assertEquals(results.getBindingNames().get(1), "p"); Assert.assertEquals(results.getBindingNames().get(2), "o"); logger.info(results.getBindingNames().toString()); Assert.assertTrue(results.hasNext()); BindingSet bindingSet = results.next(); Value sV = bindingSet.getValue("s"); Value pV = bindingSet.getValue("p"); Value oV = bindingSet.getValue("o"); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("http: results.close(); }
@Test(expected=org.eclipse.rdf4j.query.QueryEvaluationException.class) public void testSPARQLQueryQueryEvaluationException() throws Exception { String queryString = "select * <http: TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertFalse(results.hasNext()); results.close(); }
@Test(expected=org.eclipse.rdf4j.query.QueryEvaluationException.class) public void testSPARQLMalformedException() throws Exception { String queryString = "select1 * <http: TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult results = tupleQuery.evaluate(); Assert.assertFalse(results.hasNext()); results.close(); }