Java Code Examples for org.openrdf.query.BindingSet#iterator()

The following examples show how to use org.openrdf.query.BindingSet#iterator() . 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: RDFStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
protected int verifyQueryResult(
		CloseableIteration<? extends BindingSet, QueryEvaluationException> resultIter,
		int expectedBindingCount)
	throws QueryEvaluationException
{
	int resultCount = 0;

	while (resultIter.hasNext()) {
		BindingSet resultBindings = resultIter.next();
		resultCount++;

		assertEquals("Wrong number of binding names for binding set", expectedBindingCount,
				resultBindings.getBindingNames().size());

		int bindingCount = 0;
		Iterator<Binding> bindingIter = resultBindings.iterator();
		while (bindingIter.hasNext()) {
			bindingIter.next();
			bindingCount++;
		}

		assertEquals("Wrong number of bindings in binding set", expectedBindingCount, bindingCount);
	}

	return resultCount;
}
 
Example 2
Source File: StressTest_ClosedByInterrupt_RW.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {

    RepositoryConnection conn = null;
    TupleQueryResult result = null;
    int loop = 0;

    while (stopRequested == false) {

        try {

            System.out.println("[Read      ] snooze");
            snooze(MILLIS_BETWEEN_QUERY_BURSTS);
            System.out.println("[Read      ] enter loop " + loop);

            for (int invocation = 0; invocation < NUM_SELECTS; ++invocation) {

                conn = repo.getReadOnlyConnection();
                conn.setAutoCommit(false);

                final String sparql = "SELECT ?s WHERE { ?s ?p ?o } LIMIT "
                        + NUM_STATEMENTS_PER_SELECT;
                final TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
                result = query.evaluate();

                final List<String> duds = new ArrayList<String>();

                while (result.hasNext()) {
                    final BindingSet bindingSet = result.next();
                    for (final Iterator<Binding> i = bindingSet.iterator(); i.hasNext();) {
                        final Binding b = i.next();
                        if (b.getValue() != null) {
                            duds.add(b.getValue().stringValue());
                        }
                    }
                }

                result.close();
                result = null;

                conn.close();
                conn = null;

            }

        } catch (Throwable t) {
            printError("Read Only threw in loop " + loop, t);
        } finally {
            closeNoException(result);
            closeNoException(conn);
        }

        System.out.println("[Read      ] leave loop " + loop);
        ++loop;

    }
}
 
Example 3
Source File: RangeQueryOptimizationTest.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
@Test
public void queryOrderAscTest() throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	Iterator<String> iter = _orderByAscQueries.iterator();

	while (iter.hasNext()) {
		String query = iter.next();

		TupleQuery cq = CUMULUS_CONNECTION.prepareTupleQuery(QueryLanguage.SPARQL, query);
		TupleQueryResult cRes = cq.evaluate();
		String last = null;
		String current = null;
		while (cRes.hasNext()) {
			BindingSet bs = cRes.next();
			Iterator<Binding> bindings = bs.iterator();
			while (bindings.hasNext()) {
				Binding b = bindings.next();
				Value v = b.getValue();
				if (v instanceof Literal) {
					current = v.stringValue();
					try {
						double currDouble = Double.parseDouble(current);
						double lastDouble;
						if (last == null) {
							lastDouble = -Double.MAX_VALUE;
						} else {
							lastDouble = Double.parseDouble(last);
						}
						assertTrue(currDouble >= lastDouble);
						last = current;
					} catch (NumberFormatException ne) {
						if (last == null) {
							last = "";
						}
						assertTrue(last.compareTo(current) <= 0);
						last = current;
					}
				}
			}
		}
	}
}
 
Example 4
Source File: RangeQueryOptimizationTest.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
@Test
public void queryOrderDescTest() throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	Iterator<String> iter = _orderByDescQueries.iterator();

	while (iter.hasNext()) {
		String query = iter.next();

		TupleQuery cq = CUMULUS_CONNECTION.prepareTupleQuery(QueryLanguage.SPARQL, query);
		TupleQueryResult cRes = cq.evaluate();
		String last = null;
		String current = null;
		while (cRes.hasNext()) {
			BindingSet bs = cRes.next();
			Iterator<Binding> bindings = bs.iterator();
			while (bindings.hasNext()) {
				Binding b = bindings.next();
				Value v = b.getValue();
				if (v instanceof Literal) {
					current = v.stringValue();
					try {
						double currDouble = Double.parseDouble(current);
						double lastDouble;
						if (last == null) {
							lastDouble = Double.MAX_VALUE;
						} else {
							lastDouble = Double.parseDouble(last);
						}
						assertTrue(currDouble <= lastDouble);
						last = current;
					} catch (NumberFormatException ne) {
						if (last == null) {
							last = "_";
						}
						assertTrue(last.compareTo(current) >= 0);
						last = current;
					}
				}
			}
		}
	}
}
 
Example 5
Source File: ASTEvalHelper.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convert a Sesame {@link BindingSet} into a bigdata {@link IBindingSet}.
 * 
 * @param src
 *            The {@link BindingSet} (optional).
 * 
 * @return The {@link IBindingSet}. When the source is null or empty, an
 *         empty {@link ListBindingSet} is returned.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static IBindingSet[] toBindingSet(final BindingSet src) {
    
    if (src == null || src.size() == 0) {
        
        return new IBindingSet[] { new ListBindingSet() };

    }

    final ListBindingSet bindingSet = new ListBindingSet();

    final Iterator<Binding> itr = src.iterator();

    while (itr.hasNext()) {

        final Binding binding = itr.next();

        final IVariable<IV> var = com.bigdata.bop.Var.var(binding.getName());
        
        final IV iv = ((BigdataValue) binding.getValue()).getIV();
        
        final IConstant<IV> val = new Constant<IV>(iv);
        
        bindingSet.set(var, val);
        
    }
    
    return new IBindingSet[]{ bindingSet };

}
 
Example 6
Source File: ServiceCallUtility.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Convert an openrdf {@link BindingSet} into a bigdata {@link IBindingSet}.
 * The {@link BindingSet} MUST contain {@link BigdataValue}s and the
 * {@link IV}s for those {@link BigdataValue}s MUST have been resolved
 * against the database and the {@link IVCache} association set.
 * 
 * @param vars
 *            The variables to be projected (optional). When given, only the
 *            projected variables are in the returned {@link IBindingSet}.
 * @param in
 *            The openrdf {@link BindingSet}
 * 
 * @return The bigdata {@link IBindingSet}.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
static private IBindingSet openrdf2Bigdata(//
        final Set<IVariable<?>> vars,//
        final BindingSet in//
        ) {

    final IBindingSet out = new ListBindingSet();
    
    final Iterator<Binding> itr = in.iterator();
    
    while(itr.hasNext()) {
    
        final Binding e = itr.next();

        final String name = e.getName();

        final IVariable<?> var = Var.var(name);
        
        if (vars != null && !vars.contains(var)) {

            // This variable is not being projected.
            continue;

        }

        // Note: MUST already be BigdataValues.
        final BigdataValue value = (BigdataValue) e.getValue();

        // Note: IVs MUST already be resolved.
        final IV<?,?> iv = value.getIV();
        
        if(iv == null)
            throw new AssertionError();

        // IV must have cached Value.
        if (!iv.hasValue())
            throw new AssertionError();
        
        // The cached Value must be the Value (objects point at each other)
        if(iv.getValue() != value)
            throw new AssertionError();
        
        out.set(var, new Constant(iv));

    }
    
    return out;

}