Java Code Examples for org.openrdf.query.TupleQueryResult#close()

The following examples show how to use org.openrdf.query.TupleQueryResult#close() . 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: SPARQLUpdateTestv2.java    From database with GNU General Public License v2.0 6 votes vote down vote up
protected long debugPrintSolutions(final String query)
        throws QueryEvaluationException, RepositoryException,
        MalformedQueryException {
    TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SPARQL,
            query).evaluate();
    try {
        long n = 0;
        while (result.hasNext()) {
            System.err.println("==> NEXT SOLUTION");
            final BindingSet bset = result.next();
            for (final String bindingName : bset.getBindingNames()) {
                final Binding b = bset.getBinding(bindingName);
                System.err.println(bindingName + " -> " + b);
            }
            n++;
        }
        return n;
    } finally {
        result.close();
    }
}
 
Example 2
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSES2024PropertyPathAnonVarSharing() throws Exception {
	loadTestData("/testdata-query/dataset-ses2024.trig");
	String query = "PREFIX : <http://example.org/> SELECT * WHERE { ?x1 :p/:lit ?l1 . ?x1 :diff ?x2 . ?x2 :p/:lit ?l2 . }" ;

	TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);

	try {
		TupleQueryResult result = tq.evaluate();
		assertNotNull(result);

		BindingSet bs = result.next();
		Literal l1 = (Literal)bs.getValue("l1");
		Literal l2 = (Literal)bs.getValue("l2");

		assertNotNull(l1);
		assertFalse(l1.equals(l2));

		result.close();
	}
	catch (QueryEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 3
Source File: TestTicket4249.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void executeQuery(final RepositoryConnection conn, final Literal string, final int start, final int length, final Literal expected)
		throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	final ValueFactory vf = conn.getValueFactory();
	final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start, ?length) as ?substring ) . }";
	final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
	q.setBinding("string", string);
	q.setBinding("start", vf.createLiteral(start));
	q.setBinding("length", vf.createLiteral(length));
	final TupleQueryResult tqr = q.evaluate();
	try {
		while (tqr.hasNext()) {
			final BindingSet bindings = tqr.next();
			// assert expected value
			assertEquals(expected, bindings.getBinding("substring").getValue());
		}
	} finally {
		tqr.close();
	}
}
 
Example 4
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSES1991NOWEvaluation()
	throws Exception
{
	loadTestData("/testdata-query/defaultgraph.ttl");
	String query = "SELECT ?d WHERE {?s ?p ?o . BIND(NOW() as ?d) } LIMIT 2";

	TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);

	try {
		TupleQueryResult result = tq.evaluate();
		assertNotNull(result);

		Literal d1 = (Literal)result.next().getValue("d");
		Literal d2 = (Literal)result.next().getValue("d");

		assertNotNull(d1);
		assertEquals(d1, d2);

		result.close();
	}
	catch (QueryEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 5
Source File: TestNoExceptions.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void executeQuery(final SailRepository repo, final String query)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException, RDFParseException, IOException,
		RDFHandlerException {
	try {
		repo.initialize();
		final RepositoryConnection conn = repo.getConnection();
		conn.setAutoCommit(false);
		try {
			final ValueFactory vf = conn.getValueFactory();
			conn.commit();
			TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
			TupleQueryResult tqr = tq.evaluate();
			tqr.close();
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 6
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSES1991RANDEvaluation()
	throws Exception
{
	loadTestData("/testdata-query/defaultgraph.ttl");
	String query = "SELECT ?r WHERE {?s ?p ?o . BIND(RAND() as ?r) } LIMIT 3";

	TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);

	try {
		TupleQueryResult result = tq.evaluate();
		assertNotNull(result);

		Literal r1 = (Literal)result.next().getValue("r");
		Literal r2 = (Literal)result.next().getValue("r");
		Literal r3 = (Literal)result.next().getValue("r");

		assertNotNull(r1);

		// there is a small chance that two successive calls to the random
		// number generator will generate the exact same value, so we check for
		// three successive calls (still theoretically possible to be
		// identical, but phenomenally unlikely).
		assertFalse(r1.equals(r2) && r1.equals(r3));

		result.close();
	}
	catch (QueryEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 7
Source File: SampleBlazegraphSesameEmbedded.java    From blazegraph-samples with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, OpenRDFException {

		// load journal properties from resources
		final Properties props = loadProperties("/blazegraph.properties");

		// instantiate a sail
		final BigdataSail sail = new BigdataSail(props);
		final Repository repo = new BigdataSailRepository(sail);

		try{
			repo.initialize();
			
			/*
			 * Load data from resources 
			 * src/main/resources/data.n3
			 */
	
			loadDataFromResources(repo, "/data.n3", "");
			
			final String query = "select * {<http://blazegraph.com/blazegraph> ?p ?o}";
			final TupleQueryResult result = executeSelectQuery(repo, query, QueryLanguage.SPARQL);
			
			try {
				while(result.hasNext()){
					
					final BindingSet bs = result.next();
					log.info(bs);
					
				}
			} finally {
				result.close();
			}
		} finally {
			repo.shutDown();
		}
	}
 
Example 8
Source File: TestTicket275.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void executeQuery(final SailRepository repo)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException, RDFParseException, IOException {
	try {
		repo.initialize();
		final RepositoryConnection conn = repo.getConnection();
		conn.setAutoCommit(false);
		try {
			conn.add(getClass().getResourceAsStream("TestTicket275.ttl"), "",
					RDFFormat.TURTLE);
			conn.commit();

			final String query = "SELECT ?lookup WHERE { ?lookup <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <os:class/Lookup> . ?lookup <os:prop/lookup/majorType> ?majorType . OPTIONAL{?lookup <os:prop/lookup/minorType> ?minorType}. FILTER(STR(?majorType) = ?argMajorType). FILTER(!bound(?minorType))}";
			final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL,
					query);
			q.setBinding("argMajorType", conn.getValueFactory()
					.createLiteral("majoor"));
			final TupleQueryResult tqr = q.evaluate();
			while (tqr.hasNext()) {
			    final Set<String> bindingNames = tqr.next().getBindingNames();
			    if(log.isInfoEnabled())
			        log.info("bindingNames="+bindingNames);
			}
			tqr.close();
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 9
Source File: SampleBlazegraphCustomFunctionEmbedded.java    From blazegraph-samples with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws OpenRDFException, IOException {
	
	final Repository repo = createRepository();
	
	registerCustomFunction(repo);
		
	try{
		repo.initialize();
		
		/*
		 * Load data from resources 
		 * src/main/resources/data.n3
		 */

		Utils.loadDataFromResources(repo, "data.n3", "");
										
		final TupleQueryResult result = Utils.executeSelectQuery(repo, QUERY, QueryLanguage.SPARQL);
		
		try {
			while(result.hasNext()){
				
				BindingSet bs = result.next();
				log.info(bs);
				
			}
		} finally {
			result.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 10
Source File: BigdataSailRemoteRepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testTupleQueryBaseURI() throws Exception {
    final String baseURI = ":baseURI";
    final TupleQuery tq = con.prepareTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p ?o}", baseURI);
    final TupleQueryResult tqr = tq.evaluate();
	try {
		assertEquals(baseURI, remote.data.opts.getRequestParam(RemoteRepositoryDecls.BASE_URI));
		assertEquals(baseURI,remote.data.opts.getRequestParam(RemoteRepositoryDecls.BASE_URI));
	} finally {
		tqr.close();
	}
}
 
Example 11
Source File: ObjectQueryTest.java    From anno4j with Apache License 2.0 5 votes vote down vote up
public void testTupleQueryBinding() throws Exception {
	Person jamie = con.addDesignation(con.getObjectFactory().createObject(),
			Person.class);
	jamie.getFoafNames().add("Jamie");
	jamie.getFoafFamily_names().add("Leigh");
	String q = PREFIX + "Select ?name where { ?person foaf:name ?name }";
	TupleQuery query = con.prepareTupleQuery(q);
	query.setBinding("person", ((RDFObject)jamie).getResource());
	TupleQueryResult result = query.evaluate();
	assertTrue(result.hasNext());
	assertEquals("Jamie", result.next().getValue("name").stringValue());
	result.close();
}
 
Example 12
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPreparedTupleQuery()
	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(" select ?name ?mbox ");
       queryBuilder.append(" where { ?s foaf:name ?name . ?s foaf:mbox ?mbox . }");
	TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);
	TupleQueryResult result = query.evaluate();
	try {
		assertThat(result, is(notNullValue()));
		assertThat(result.hasNext(), is(equalTo(true)));
		while (result.hasNext()) {
			BindingSet solution = result.next();
			assertThat(solution.hasBinding(NAME), is(equalTo(true)));
			assertThat(solution.hasBinding(MBOX), is(equalTo(true)));
			Value nameResult = solution.getValue(NAME);
			Value mboxResult = solution.getValue(MBOX);
			assertEquals("unexpected value for name: " + nameResult, nameBob, nameResult);
			assertEquals("unexpected value for mbox: " + mboxResult, mboxBob, mboxResult);
		}
	}
	finally {
		result.close();
	}
}
 
Example 13
Source File: TestTicket1682.java    From database with GNU General Public License v2.0 4 votes vote down vote up
private void executeQuery(final SailRepository repo)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException, RDFParseException, IOException {
	try {
		repo.initialize();
		final RepositoryConnection conn = repo.getConnection();
		conn.setAutoCommit(false);
		try {
			conn.add(getClass().getResourceAsStream("TestTicket1682.nt"), "",
					RDFFormat.TURTLE);
			conn.commit();

			final String query = "select ?s with { " +
			        "   select ?s where {" +
                       "       ?s <http://p> ?o" +
                       "   } VALUES (?o) {" +
                       "       (\"a\") (\"b\")" +
                       "   }" +
                       "} AS %sub1 " +
                       "where {" +
                       "   INCLUDE %sub1" +
                       "}";
			final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL,
					query);
			final TupleQueryResult tqr = q.evaluate();
			int cnt = 0;
			while (tqr.hasNext()) {
			    final Set<String> bindingNames = tqr.next().getBindingNames();
			    cnt++;
			    if(log.isInfoEnabled())
			        log.info("bindingNames="+bindingNames);
			}
			tqr.close();
			assertEquals(2, cnt);
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 14
Source File: HelloBlazegraph.java    From blazegraph-samples with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws OpenRDFException {

		final Properties props = new Properties();
		
		/*
		 * For more configuration parameters see
		 * http://www.blazegraph.com/docs/api/index.html?com/bigdata/journal/BufferMode.html
		 */
		props.put(Options.BUFFER_MODE, BufferMode.DiskRW); // persistent file system located journal
		props.put(Options.FILE, "/tmp/blazegraph/test.jnl"); // journal file location

		final BigdataSail sail = new BigdataSail(props); // instantiate a sail
		final Repository repo = new BigdataSailRepository(sail); // create a Sesame repository

		repo.initialize();

		try {
			// prepare a statement
			final URIImpl subject = new URIImpl("http://blazegraph.com/Blazegraph");
			final URIImpl predicate = new URIImpl("http://blazegraph.com/says");
			final Literal object = new LiteralImpl("hello");
			final Statement stmt = new StatementImpl(subject, predicate, object);

			// open repository connection
			RepositoryConnection cxn = repo.getConnection();

			// upload data to repository
			try {
				cxn.begin();
				cxn.add(stmt);
				cxn.commit();
			} catch (OpenRDFException ex) {
				cxn.rollback();
				throw ex;
			} finally {
				// close the repository connection
				cxn.close();
			}

			// open connection
			if (repo instanceof BigdataSailRepository) {
				cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
			} else {
				cxn = repo.getConnection();
			}

			// evaluate sparql query
			try {

				final TupleQuery tupleQuery = cxn
						.prepareTupleQuery(QueryLanguage.SPARQL,
								"select ?p ?o where { <http://blazegraph.com/Blazegraph> ?p ?o . }");
				final TupleQueryResult result = tupleQuery.evaluate();
				try {
					while (result.hasNext()) {
						final BindingSet bindingSet = result.next();
						System.err.println(bindingSet);
					}
				} finally {
					result.close();
				}

			} finally {
				// close the repository connection
				cxn.close();
			}

		} finally {
			repo.shutDown();
		}
	}
 
Example 15
Source File: TestDataLoaderServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void test_load01() throws Exception {

		final String kbPropsURL = this.getClass()
				.getResource("dataloader.props").getFile();
		final String dataURL = this.getClass().getResource("sample-data.ttl")
				.getFile();

		final PropertiesFormat format = PropertiesFormat.XML;
		final PropertiesParserFactory parserFactory = PropertiesParserRegistry
				.getInstance().get(format);

		final Properties loaderProps = parserFactory.getParser().parse(
				this.getClass().getResourceAsStream("dataloader.xml"));

		final String randomNS = "kb" + UUID.randomUUID();

		{ // verify does not exist.
			try {
				m_mgr.getRepositoryProperties(randomNS);
				fail("Should not exist: " + randomNS);
			} catch (HttpException ex) {
				// Expected status code.
				assertEquals(404, ex.getStatusCode());
			}
		}

		// Set the random namespace and the correct resource paths
		loaderProps.setProperty("namespace", randomNS);
		loaderProps.setProperty("quiet", "true");
		loaderProps.setProperty("verbose", "0");
		loaderProps.setProperty("propertyFile", kbPropsURL);
		loaderProps.setProperty("fileOrDirs", dataURL);

		m_mgr.doDataLoader(loaderProps);

		RemoteRepository repo = m_mgr.getRepositoryForNamespace(randomNS);

		{ // verify it was created by the data loader.
			final Properties p = m_mgr.getRepositoryProperties(randomNS);
			assertNotNull(p);

			log.warn("Found properties for namespace " + randomNS);
		}

		final BigdataSailRemoteRepositoryConnection cxn = (BigdataSailRemoteRepositoryConnection) repo
				.getBigdataSailRemoteRepository().getConnection();

		try {
			String queryStr = "select * where { ?s ?p ?o }";
			final org.openrdf.query.TupleQuery tq = cxn.prepareTupleQuery(
					QueryLanguage.SPARQL, queryStr);
			final TupleQueryResult tqr = tq.evaluate();
			try {
				int cnt = 0;
				while (tqr.hasNext()) {
					tqr.next();
					cnt++;
				}
				if (cnt == 0) {
					fail("DataLoaderServlet did not add any statements.");
				}
				assertTrue(cnt > 0);
			} finally {
				tqr.close();
			}
		} finally {
			cxn.close();
		}

	}
 
Example 16
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testSameTermRepeatInUnionAndOptional()
	throws Exception
{
	loadTestData("/testdata-query/dataset-query.trig");

	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("SELECT * {\n");
	query.append("    {\n");
	query.append("        ex:a ?p ?prop1\n");
	query.append("        FILTER (?p = ex:prop1)\n");
	query.append("    } UNION {\n");
	query.append("          ?s ex:p ex:A ; ");
	query.append("          { ");
	query.append("              { ");
	query.append("                 ?s ?p ?l .");
	query.append("                 FILTER(?p = rdfs:label) ");
	query.append("              } ");
	query.append("              OPTIONAL { ");
	query.append("                 ?s ?p ?opt1 . ");
	query.append("                 FILTER (?p = ex:prop1) ");
	query.append("              } ");
	query.append("              OPTIONAL { ");
	query.append("                 ?s ?p ?opt2 . ");
	query.append("                 FILTER (?p = ex:prop2) ");
	query.append("              } ");
	query.append("          }");
	query.append("    }\n");
	query.append("}");

	TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString());

	try {
		TupleQueryResult result = tq.evaluate();
		assertNotNull(result);

		int count = 0;
		while (result.hasNext()) {
			BindingSet bs = result.next();
			count++;
			assertNotNull(bs);

			System.out.println(bs);

			Value prop1 = bs.getValue("prop1");
			Value l = bs.getValue("l");

			assertTrue(prop1 instanceof Literal || l instanceof Literal);
			if (l instanceof Literal) {
				Value opt1 = bs.getValue("opt1");
				assertNull(opt1);

				Value opt2 = bs.getValue("opt2");
				assertNull(opt2);
			}
		}
		result.close();

		assertEquals(2, count);
	}
	catch (QueryEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

}
 
Example 17
Source File: TestBOps.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testConcat() throws Exception {

        final BigdataSail sail = getSail();
        try {
        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        final BigdataSailRepositoryConnection cxn = 
            (BigdataSailRepositoryConnection) repo.getConnection();
        cxn.setAutoCommit(false);
        
        try {
    
            final String ns = BD.NAMESPACE;            
          
            final URI foo = new URIImpl(ns+"foo");
            final URI bar = new URIImpl(ns+"bar");
            final URI plain = new URIImpl(ns+"plain");
            final URI language = new URIImpl(ns+"language");
            final URI string = new URIImpl(ns+"string");
            
            final Literal fooPlain = new LiteralImpl("foo");
            final Literal fooLanguage = new LiteralImpl("foo", "en");
            final Literal fooString = new LiteralImpl("foo", XMLSchema.STRING);
            final Literal barPlain = new LiteralImpl("bar");
            final Literal barLanguage = new LiteralImpl("bar", "en");
            final Literal barString = new LiteralImpl("bar", XMLSchema.STRING);
            final Literal foobarPlain = new LiteralImpl("foobar");
            final Literal foobarLanguage = new LiteralImpl("foobar", "en");
            final Literal foobarString = new LiteralImpl("foobar", XMLSchema.STRING);
            
/**/
            cxn.setNamespace("ns", ns);
            
            cxn.add(foo, plain, fooPlain);
            cxn.add(bar, plain, barPlain);
            cxn.add(foo, language, fooLanguage);
            cxn.add(bar, language, barLanguage);
            cxn.add(foo, string, fooString);
            cxn.add(bar, string, barString);
            cxn.add(plain, plain, foobarPlain);
            cxn.add(language, language, foobarLanguage);
            cxn.add(string, string, foobarString);
            cxn.add(plain, string, foobarPlain);
            cxn.add(language, plain, foobarPlain);
            cxn.add(language, string, foobarPlain);

            /*
             * 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("\n" + cxn.getTripleStore().dumpStore());
            }

            {
                 
                String query = 
                    "select ?o1 ?o2 ?o3 " +
                    "WHERE { " +
                    "  ?s1 ?p1 ?o1 . " +
                    "  ?s2 ?p2 ?o2 . " +
                    "  ?p1 ?p2 ?o3 . " +
                    "  FILTER(concat(?o1, ?o2) = ?o3)"+
                    "}";
                
               final TupleQuery tupleQuery = 
                    cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
                final TupleQueryResult result = tupleQuery.evaluate();
                
                int cnt = 0;
                try {
                	while(result.hasNext()) {
                		cnt++;
                	}
                } finally {
                	result.close();
                }
                
                assertEquals(6, cnt);

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

    }
 
Example 18
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testSameTermRepeatInOptional()
	throws Exception
{
	loadTestData("/testdata-query/dataset-query.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append(" SELECT ?l ?opt1 ?opt2 ");
	query.append(" FROM ex:optional-sameterm-graph ");
	query.append(" WHERE { ");
	query.append("          ?s ex:p ex:A ; ");
	query.append("          { ");
	query.append("              { ");
	query.append("                 ?s ?p ?l .");
	query.append("                 FILTER(?p = rdfs:label) ");
	query.append("              } ");
	query.append("              OPTIONAL { ");
	query.append("                 ?s ?p ?opt1 . ");
	query.append("                 FILTER (?p = ex:prop1) ");
	query.append("              } ");
	query.append("              OPTIONAL { ");
	query.append("                 ?s ?p ?opt2 . ");
	query.append("                 FILTER (?p = ex:prop2) ");
	query.append("              } ");
	query.append("          }");
	query.append(" } ");

	TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString());

	try {
		TupleQueryResult result = tq.evaluate();
		assertNotNull(result);

		int count = 0;
		while (result.hasNext()) {
			BindingSet bs = result.next();
			count++;
			assertNotNull(bs);

			System.out.println(bs);

			Value l = bs.getValue("l");
			assertTrue(l instanceof Literal);
			assertEquals("label", ((Literal)l).getLabel());

			Value opt1 = bs.getValue("opt1");
			assertNull(opt1);

			Value opt2 = bs.getValue("opt2");
			assertNull(opt2);
		}
		result.close();

		assertEquals(1, count);
	}
	catch (QueryEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

}
 
Example 19
Source File: SampleBlazegraphCustomFunctionRemote.java    From blazegraph-samples with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	final String namespace = "test";
	
	final Properties journalProperties = new Properties();
       {
           journalProperties.setProperty(Journal.Options.BUFFER_MODE,
                   BufferMode.MemStore.name());
           
       }
		
	final RemoteRepositoryManager repo = new RemoteRepositoryManager(
			serviceURL , false /* useLBS */);
	
	repo.createRepository(namespace, journalProperties);
	
	try {

		/*
		 * Load data from file located in the resource folder
		 * src/main/resources/data.n3
		 */
		final String resource = "/data.n3";
		loadDataFromResource(repo, namespace, resource);

		// execute query
		final TupleQueryResult result = repo.getRepositoryForNamespace(namespace)
				.prepareTupleQuery(QUERY)
				.evaluate();
		
		//result processing
		try {
			while (result.hasNext()) {
				final BindingSet bs = result.next();
				log.info(bs);
			}
		} finally {
			result.close();
		}

	} finally {
		repo.close();
	}
	

}
 
Example 20
Source File: SampleBlazegraphCustomFunctionRemoteTest.java    From blazegraph-samples with GNU General Public License v2.0 2 votes vote down vote up
@Test
public void testCustomFunctionRemote() throws Exception{
	
	final String namespace = "test";
	
	final Properties journalProperties = new Properties();
       {
           journalProperties.setProperty(Journal.Options.BUFFER_MODE,
                   BufferMode.MemStore.name());
           
       }
       
       Journal m_indexManager = new Journal(journalProperties);
		
	Server nss = NanoSparqlServer.newInstance(9999, "jettyMavenTest.xml", m_indexManager, null);
	
	final RemoteRepositoryManager m_repo = new RemoteRepositoryManager(
			SampleBlazegraphCustomFunctionRemote.serviceURL , false /* useLBS */);
	
	try {
		
		nss.start();
		
		m_repo.createRepository(namespace, journalProperties);
		
		final String resource = "/data.n3";
		SampleBlazegraphCustomFunctionRemote.loadDataFromResource(m_repo, namespace, resource);

		// execute query
		final TupleQueryResult result = m_repo.getRepositoryForNamespace(namespace)
				.prepareTupleQuery(SampleBlazegraphCustomFunctionRemote.QUERY)
				.evaluate();
		
		int countResults = 0;
		String expected = "http://www.example.com/document1";
		String actual = null;
		
		while(result.hasNext()){
			
			BindingSet bs = result.next();
		
			actual = bs.getBinding("doc").getValue().stringValue();
			
			countResults++;

		}
		
		result.close();
		
		Assert.assertEquals(1, countResults);
		
		Assert.assertEquals(expected, actual);
		
		
		
		
	} finally {
		
		nss.stop();
		
	}
	
}