Java Code Examples for org.eclipse.rdf4j.common.iteration.CloseableIteration#hasNext()

The following examples show how to use org.eclipse.rdf4j.common.iteration.CloseableIteration#hasNext() . 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: KafkaQueryChangeLogIT.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void readFromPosition_positionStartsBegining() throws Exception {
    final List<QueryChange> expected = write10ChangesToChangeLog().subList(5, 10);

    // set the position to some non-0 position
    final TopicPartition partition = new TopicPartition(topic, 0);
    consumer.assign(Lists.newArrayList(partition));
    consumer.seekToBeginning(Lists.newArrayList(partition));
    final CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> iter = changeLog.readFromPosition(5L);

    final List<QueryChange> actual = new ArrayList<>();
    while (iter.hasNext()) {
        final ChangeLogEntry<QueryChange> entry = iter.next();
        actual.add(entry.getEntry());
    }
    assertEquals(expected, actual);
}
 
Example 2
Source File: AccumuloIndexSetColumnVisibilityTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void accumuloIndexSetTestAttemptJoinAccrossTypes() throws Exception {
    // Setup the object that will be tested.
    final String pcjTableName = new PcjTableNameFactory().makeTableName(ryaInstanceName, pcjId);
    final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);

    // Setup the binding sets that will be evaluated.
    final QueryBindingSet bs1 = new QueryBindingSet();
    bs1.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));
    final QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));

    final Set<BindingSet> bSets = Sets.newHashSet(bs1, bs2);
    final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);

    final Set<BindingSet> fetchedResults = new HashSet<>();
    while (results.hasNext()) {
        final BindingSet next = results.next();
        fetchedResults.add(next);
    }

    final Set<BindingSet> expected = Sets.newHashSet(pcjBs1, pcjBs2);
    assertEquals(expected, fetchedResults);
}
 
Example 3
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryWithBindingSetConstantContext()
        throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    //query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph <uri:context1>  {?x <uri:talksTo> <uri:Bob>. }}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    
    RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"),
            new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
    dao.add(statement1);
    
    RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"),
            new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
    dao.add(statement2);
    
    RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"),
            new RyaType("uri:Bob"), new RyaIRI("uri:context2"), "", new StatementMetadata());
    dao.add(statement3);

    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("x", VF.createIRI("uri:Doug"));
    
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));

    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }

    Assert.assertEquals(1, bsList.size());
    
    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", VF.createIRI("uri:Doug"));

    Assert.assertEquals(expected, bsList.get(0));
    
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
 
Example 4
Source File: SparqlFederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateBoundJoinStatementPattern(
		StatementTupleExpr stmt, List<BindingSet> bindings)
{
	// we can omit the bound join handling
	if (bindings.size()==1)
		return evaluate(stmt, bindings.get(0));
			
	FilterValueExpr filterExpr = null;
	if (stmt instanceof FilterTuple)
		filterExpr = ((FilterTuple)stmt).getFilterExpr();
	
	Boolean isEvaluated = false;
	String preparedQuery = QueryStringUtil.selectQueryStringBoundUnion((StatementPattern)stmt, bindings, filterExpr, isEvaluated);
	
	CloseableIteration<BindingSet, QueryEvaluationException> result = evaluateAtStatementSources(preparedQuery, stmt.getStatementSources(), stmt.getQueryInfo());
					
	// apply filter and/or convert to original bindings
	if (filterExpr!=null && !isEvaluated) {
		result = new BoundJoinConversionIteration(result, bindings);		// apply conversion
		result = new FilteringIteration(this, filterExpr, result);				// apply filter
		if (!result.hasNext()) {
			result.close();
			return new EmptyIteration<BindingSet, QueryEvaluationException>();
		}
	} else {
		result = new BoundJoinConversionIteration(result, bindings);
	}
		
	// in order to avoid leakage of http route during the iteration
	return new BufferedCloseableIterator<BindingSet, QueryEvaluationException>(result);		
}
 
Example 5
Source File: MergeJoinTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleMergeJoin() throws Exception {
    //add data
    RyaIRI pred = new RyaIRI(litdupsNS, "pred1");
    RyaType one = new RyaType("1");
    RyaType two = new RyaType("2");
    RyaIRI subj1 = new RyaIRI(litdupsNS, "subj1");
    RyaIRI subj2 = new RyaIRI(litdupsNS, "subj2");
    RyaIRI subj3 = new RyaIRI(litdupsNS, "subj3");
    RyaIRI subj4 = new RyaIRI(litdupsNS, "subj4");

    dao.add(new RyaStatement(subj1, pred, one));
    dao.add(new RyaStatement(subj1, pred, two));
    dao.add(new RyaStatement(subj2, pred, one));
    dao.add(new RyaStatement(subj2, pred, two));
    dao.add(new RyaStatement(subj3, pred, one));
    dao.add(new RyaStatement(subj3, pred, two));
    dao.add(new RyaStatement(subj4, pred, one));
    dao.add(new RyaStatement(subj4, pred, two));
    

    //1 join
    MergeJoin mergeJoin = new MergeJoin(dao.getQueryEngine());
    CloseableIteration<RyaIRI, RyaDAOException> join = mergeJoin.join(null, new CustomEntry<RyaIRI, RyaType>(pred, one),
            new CustomEntry<RyaIRI, RyaType>(pred, two));

    Set<RyaIRI> uris = new HashSet<RyaIRI>();
    while (join.hasNext()) {
        uris.add(join.next());
    }
    assertTrue(uris.contains(subj1));
    assertTrue(uris.contains(subj2));
    assertTrue(uris.contains(subj3));
    assertTrue(uris.contains(subj4));
    join.close();
}
 
Example 6
Source File: SailFederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateBoundJoinStatementPattern(
		StatementTupleExpr stmt, List<BindingSet> bindings)
		throws QueryEvaluationException {

	// we can omit the bound join handling
	if (bindings.size() == 1) {
		return evaluate(stmt, bindings.get(0));
	}

	FilterValueExpr filterExpr = null;
	if (stmt instanceof FilterTuple) {
		filterExpr = ((FilterTuple) stmt).getFilterExpr();
	}

	Boolean isEvaluated = false;
	TupleExpr preparedQuery = QueryAlgebraUtil.selectQueryBoundUnion((StatementPattern) stmt, bindings, filterExpr,
			isEvaluated);

	CloseableIteration<BindingSet, QueryEvaluationException> result = evaluateAtStatementSources(preparedQuery,
			stmt.getStatementSources(), stmt.getQueryInfo());

	// apply filter and/or convert to original bindings
	if (filterExpr != null && !isEvaluated) {
		result = new BoundJoinConversionIteration(result, bindings); // apply conversion
		result = new FilteringIteration(filterExpr, result, this); // apply filter
		if (!result.hasNext()) {
			return new EmptyIteration<>();
		}
	} else {
		result = new BoundJoinConversionIteration(result, bindings);
	}

	return result;
}
 
Example 7
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param add
 * @param uc
 * @throws SailException
 */
protected void executeAdd(Add add, UpdateContext uc, int maxExecTime) throws SailException {
	ValueConstant sourceGraph = add.getSourceGraph();
	ValueConstant destinationGraph = add.getDestinationGraph();

	Resource source = sourceGraph != null ? (Resource) sourceGraph.getValue() : null;
	Resource destination = destinationGraph != null ? (Resource) destinationGraph.getValue() : null;

	if (source == null && destination == null || (source != null && source.equals(destination))) {
		// source and destination are the same, copy is a null-operation.
		return;
	}

	// get all statements from source and add them to destination
	CloseableIteration<? extends Statement, SailException> statements = null;
	try {
		statements = con.getStatements(null, null, null, uc.isIncludeInferred(), (Resource) source);

		if (maxExecTime > 0) {
			statements = new TimeLimitIteration<Statement, SailException>(statements, 1000L * maxExecTime) {

				@Override
				protected void throwInterruptedException() throws SailException {
					throw new SailException("execution took too long");
				}
			};
		}

		while (statements.hasNext()) {
			Statement st = statements.next();
			con.addStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) destination);
		}
	} finally {
		if (statements != null) {
			statements.close();
		}
	}
}
 
Example 8
Source File: HBaseSail.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<? extends Resource, SailException> getContextIDs() throws SailException {

    //generate an iterator over the identifiers of the contexts available in Halyard.
    final CloseableIteration<? extends Statement, SailException> scanner = getStatements(HALYARD.STATS_ROOT_NODE, SD.NAMED_GRAPH_PROPERTY, null, true, HALYARD.STATS_GRAPH_CONTEXT);
    return new CloseableIteration<Resource, SailException>() {
        @Override
        public void close() throws SailException {
            scanner.close();
        }

        @Override
        public boolean hasNext() throws SailException {
            return scanner.hasNext();
        }

        @Override
        public Resource next() throws SailException {
            return (IRI)scanner.next().getObject();
        }

        @Override
        public void remove() throws SailException {
            throw new UnsupportedOperationException();
        }

    };
}
 
Example 9
Source File: SparqlFederationEvalStrategyWithValues.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateBoundJoinStatementPattern(
		StatementTupleExpr stmt, List<BindingSet> bindings)
		throws QueryEvaluationException {
	
	// we can omit the bound join handling
	if (bindings.size()==1)
		return evaluate(stmt, bindings.get(0));
			
	FilterValueExpr filterExpr = null;
	if (stmt instanceof FilterTuple)
		filterExpr = ((FilterTuple)stmt).getFilterExpr();
	
	Boolean isEvaluated = false;
	String preparedQuery = QueryStringUtil.selectQueryStringBoundJoinVALUES((StatementPattern)stmt, bindings, filterExpr, isEvaluated);
	
	CloseableIteration<BindingSet, QueryEvaluationException> result = evaluateAtStatementSources(preparedQuery, stmt.getStatementSources(), stmt.getQueryInfo());
					
	// apply filter and/or convert to original bindings
	if (filterExpr!=null && !isEvaluated) {
		result = new BoundJoinVALUESConversionIteration(result, bindings);		// apply conversion
		result = new FilteringIteration(this, filterExpr, result);				// apply filter
		if (!result.hasNext())	
			return new EmptyIteration<BindingSet, QueryEvaluationException>();
	} else {
		result = new BoundJoinVALUESConversionIteration(result, bindings);
	}
		
	return result;		
}
 
Example 10
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if results are filtered correctly using the metadata properties. In
 * this case, the date for the ingested RyaStatement differs from the date
 * specified in the query.
 */
@Test
public void simpleQueryWithoutBindingSetInvalidProperty() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();

        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Doug"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15"));

        final RyaStatement statement = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement);

        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery pq = parser.parseQuery(query, null);
        final List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
        final StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());

        final List<BindingSet> bsList = new ArrayList<>();
        while (iteration.hasNext()) {
            bsList.add(iteration.next());
        }
        Assert.assertEquals(0, bsList.size());
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
    }
}
 
Example 11
Source File: AccumuloRyaDAOTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() throws Exception {
    RyaIRI cpu = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "cpu"));
    RyaIRI loadPerc = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "loadPerc"));
    RyaIRI uri1 = RdfToRyaConversions.convertIRI(VF.createIRI(litdupsNS, "uri1"));
    dao.add(new RyaStatement(cpu, loadPerc, uri1));

    CloseableIteration<RyaStatement, RyaDAOException> iter = dao.getQueryEngine().query(new RyaStatement(cpu, loadPerc, null), conf);
    int count = 0;
    while (iter.hasNext()) {
        assertTrue(uri1.equals(iter.next().getObject()));
        count++;
    }
    iter.close();
    assertEquals(1, count);

    dao.delete(new RyaStatement(cpu, loadPerc, null), conf);

    iter = dao.getQueryEngine().query(new RyaStatement(cpu, loadPerc, null), conf);
    count = 0;
    while (iter.hasNext()) {
        count++;
        iter.next();
    }
    iter.close();
    assertEquals(0, count);
}
 
Example 12
Source File: SailTripleSource.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> getStatements(
		StatementPattern stmt, RepositoryConnection conn,
		final BindingSet bindings, FilterValueExpr filterExpr)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException  {

	Value subjValue = QueryAlgebraUtil.getVarValue(stmt.getSubjectVar(), bindings);
	Value predValue = QueryAlgebraUtil.getVarValue(stmt.getPredicateVar(), bindings);
	Value objValue = QueryAlgebraUtil.getVarValue(stmt.getObjectVar(), bindings);			
	
	RepositoryResult<Statement> repoResult = conn.getStatements((Resource)subjValue, (IRI)predValue, objValue, true, new Resource[0]);
			
	// XXX implementation remark and TODO taken from Sesame
	// The same variable might have been used multiple times in this
	// StatementPattern, verify value equality in those cases.
	
	// an iterator that converts the statements to var bindings
	CloseableIteration<BindingSet, QueryEvaluationException> res = new StatementConversionIteration(repoResult, bindings, stmt);
			
	// if filter is set, apply it
	if (filterExpr != null) {
		CloseableIteration<BindingSet, QueryEvaluationException> fres = new FilteringIteration(strategy, filterExpr, res);
		if (!fres.hasNext())
			return new EmptyIteration<BindingSet, QueryEvaluationException>();
		return fres;
	}		
	
	return res;
}
 
Example 13
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs3_2() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.RANGE, null);

	for (Statement nt : ntIter) {
		Resource aaa = nt.getSubject();
		Value zzz = nt.getObject();

		if (aaa instanceof IRI && zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (IRI) aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value uuu = t1.getObject();
				if (uuu instanceof Resource) {
					boolean added = addInferredStatement((Resource) uuu, RDF.TYPE, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;

}
 
Example 14
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param copy
 * @param uc
 * @throws SailException
 */
protected void executeCopy(Copy copy, UpdateContext uc, int maxExecutionTime) throws SailException {
	ValueConstant sourceGraph = copy.getSourceGraph();
	ValueConstant destinationGraph = copy.getDestinationGraph();

	Resource source = sourceGraph != null ? (Resource) sourceGraph.getValue() : null;
	Resource destination = destinationGraph != null ? (Resource) destinationGraph.getValue() : null;

	if (source == null && destination == null || (source != null && source.equals(destination))) {
		// source and destination are the same, copy is a null-operation.
		return;
	}

	// clear destination
	final long start = System.currentTimeMillis();
	con.clear((Resource) destination);
	final long clearTime = (System.currentTimeMillis() - start) / 1000;

	if (maxExecutionTime > 0) {
		if (clearTime > maxExecutionTime) {
			throw new SailException("execution took too long");
		}
	}

	// get all statements from source and add them to destination
	CloseableIteration<? extends Statement, SailException> statements = null;
	try {
		statements = con.getStatements(null, null, null, uc.isIncludeInferred(), (Resource) source);

		if (maxExecutionTime > 0) {
			statements = new TimeLimitIteration<Statement, SailException>(statements,
					1000L * (maxExecutionTime - clearTime)) {

				@Override
				protected void throwInterruptedException() throws SailException {
					throw new SailException("execution took too long");
				}
			};
		}

		while (statements.hasNext()) {
			Statement st = statements.next();
			con.addStatement(uc, st.getSubject(), st.getPredicate(), st.getObject(), (Resource) destination);
		}
	} finally {
		if (statements != null) {
			statements.close();
		}
	}
}
 
Example 15
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if StatementMetadataNode joins BindingSet values correctly for
 * variables appearing as the object in one of the StatementPattern
 * statements (in the case ?x appears as the Object in the statement
 * _:blankNode rdf:object ?x). StatementPattern statements have either
 * rdf:subject, rdf:predicate, or rdf:object as the predicate.
 */
@Test
public void simpleQueryWithBindingSetCollection() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

        final RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("HardwareStore"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);

        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery pq = parser.parseQuery(query, null);
        final List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
        final StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);

        final List<BindingSet> bsCollection = new ArrayList<>();
        final QueryBindingSet bsConstraint1 = new QueryBindingSet();
        bsConstraint1.addBinding("x", VF.createLiteral("CoffeeShop"));
        bsConstraint1.addBinding("z", VF.createLiteral("Virginia"));

        final QueryBindingSet bsConstraint2 = new QueryBindingSet();
        bsConstraint2.addBinding("x", VF.createLiteral("HardwareStore"));
        bsConstraint2.addBinding("z", VF.createLiteral("Maryland"));

        final QueryBindingSet bsConstraint3 = new QueryBindingSet();
        bsConstraint3.addBinding("x", VF.createLiteral("BurgerShack"));
        bsConstraint3.addBinding("z", VF.createLiteral("Delaware"));
        bsCollection.add(bsConstraint1);
        bsCollection.add(bsConstraint2);
        bsCollection.add(bsConstraint3);

        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);

        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("x", VF.createLiteral("CoffeeShop"));
        expected1.addBinding("y", VF.createLiteral("Joe"));
        expected1.addBinding("z", VF.createLiteral("Virginia"));

        final QueryBindingSet expected2 = new QueryBindingSet();
        expected2.addBinding("x", VF.createLiteral("HardwareStore"));
        expected2.addBinding("y", VF.createLiteral("Joe"));
        expected2.addBinding("z", VF.createLiteral("Maryland"));
        expected.add(expected1);
        expected.add(expected2);

        final Set<BindingSet> bsSet = new HashSet<>();
        while (iteration.hasNext()) {
            bsSet.add(iteration.next());
        }

        Assert.assertEquals(expected, bsSet);

        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
    } finally {
        dao.destroy();
    }
}
 
Example 16
Source File: LeftJoinIterator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected BindingSet getNextElement() throws QueryEvaluationException {
	try {
		CloseableIteration<BindingSet, QueryEvaluationException> nextRightIter = rightIter;
		while (nextRightIter.hasNext() || leftIter.hasNext()) {
			BindingSet leftBindings = null;

			if (!nextRightIter.hasNext()) {
				// Use left arg's bindings in case join fails
				leftBindings = leftIter.next();

				nextRightIter.close();
				nextRightIter = rightIter = strategy.evaluate(join.getRightArg(), leftBindings);
			}

			while (nextRightIter.hasNext()) {
				BindingSet rightBindings = nextRightIter.next();

				try {
					if (join.getCondition() == null) {
						return rightBindings;
					} else {
						// Limit the bindings to the ones that are in scope for
						// this filter
						QueryBindingSet scopeBindings = new QueryBindingSet(rightBindings);
						scopeBindings.retainAll(scopeBindingNames);

						if (strategy.isTrue(join.getCondition(), scopeBindings)) {
							return rightBindings;
						}
					}
				} catch (ValueExprEvaluationException e) {
					// Ignore, condition not evaluated successfully
				}
			}

			if (leftBindings != null) {
				// Join failed, return left arg's bindings
				return leftBindings;
			}
		}
	} catch (NoSuchElementException ignore) {
		// probably, one of the iterations has been closed concurrently in
		// handleClose()
	}

	return null;
}
 
Example 17
Source File: AccumuloIndexSetTest.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * TODO doc
 * @throws QueryEvaluationException
 * @throws SailException
 * @throws MalformedQueryException
 * @throws AccumuloSecurityException
 * @throws AccumuloException
 */
@Test
public void accumuloIndexSetTestWithEmptyBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, AccumuloException, AccumuloSecurityException {
    // Load some Triples into Rya.
    final Set<Statement> triples = new HashSet<>();
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))) );
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(43)) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );

    for(final Statement triple : triples) {
        ryaConn.add(triple);
    }

    // Create a PCJ table will include those triples in its results.
    final String sparql =
            "SELECT ?name ?age " +
            "{" +
              "FILTER(?age < 30) ." +
              "?name <http://hasAge> ?age." +
              "?name <http://playsSport> \"Soccer\" " +
            "}";

    final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
    // Create and populate the PCJ table.
    PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.absent());

    final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);

    final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(new QueryBindingSet());
    final Set<BindingSet> fetchedResults = new HashSet<BindingSet>();
    while(results.hasNext()) {
        fetchedResults.add(results.next());
    }
    // Ensure the expected results match those that were stored.
    final QueryBindingSet alice = new QueryBindingSet();
    alice.addBinding("name", VF.createIRI("http://Alice"));
    alice.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));

    final QueryBindingSet bob = new QueryBindingSet();
    bob.addBinding("name", VF.createIRI("http://Bob"));
    bob.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));

    final QueryBindingSet charlie = new QueryBindingSet();
    charlie.addBinding("name", VF.createIRI("http://Charlie"));
    charlie.addBinding("age", VF.createLiteral(BigInteger.valueOf(12)));

    final Set<BindingSet> expectedResults = Sets.newHashSet(alice, bob, charlie);
    Assert.assertEquals(expectedResults, fetchedResults);
}
 
Example 18
Source File: HashJoinTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testMergeJoinMultiWay() throws Exception {
    //add data
    RyaIRI pred = new RyaIRI(litdupsNS, "pred1");
    RyaType zero = new RyaType("0");
    RyaType one = new RyaType("1");
    RyaType two = new RyaType("2");
    RyaType three = new RyaType("3");
    RyaType four = new RyaType("4");
    RyaIRI subj1 = new RyaIRI(litdupsNS, "subj1");
    RyaIRI subj2 = new RyaIRI(litdupsNS, "subj2");
    RyaIRI subj3 = new RyaIRI(litdupsNS, "subj3");
    RyaIRI subj4 = new RyaIRI(litdupsNS, "subj4");

    dao.add(new RyaStatement(subj1, pred, one));
    dao.add(new RyaStatement(subj1, pred, two));
    dao.add(new RyaStatement(subj1, pred, three));
    dao.add(new RyaStatement(subj1, pred, four));
    dao.add(new RyaStatement(subj2, pred, zero));
    dao.add(new RyaStatement(subj2, pred, one));
    dao.add(new RyaStatement(subj2, pred, two));
    dao.add(new RyaStatement(subj2, pred, three));
    dao.add(new RyaStatement(subj2, pred, four));
    dao.add(new RyaStatement(subj3, pred, one));
    dao.add(new RyaStatement(subj3, pred, two));
    dao.add(new RyaStatement(subj3, pred, four));
    dao.add(new RyaStatement(subj4, pred, one));
    dao.add(new RyaStatement(subj4, pred, two));
    dao.add(new RyaStatement(subj4, pred, three));
    dao.add(new RyaStatement(subj4, pred, four));
    

    //1 join
    HashJoin hjoin = new HashJoin(dao.getQueryEngine());
    CloseableIteration<RyaIRI, RyaDAOException> join = hjoin.join(null,
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, one),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, two),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, three),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, four)
    );

    Set<RyaIRI> uris = new HashSet<RyaIRI>();
    while (join.hasNext()) {
        uris.add(join.next());
    }
    assertTrue(uris.contains(subj1));
    assertTrue(uris.contains(subj2));
    assertTrue(uris.contains(subj4));
    join.close();
}
 
Example 19
Source File: IterativeJoinTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testIterativeJoinMultiWay() throws Exception {
    //add data
    RyaIRI pred = new RyaIRI(litdupsNS, "pred1");
    RyaType zero = new RyaType("0");
    RyaType one = new RyaType("1");
    RyaType two = new RyaType("2");
    RyaType three = new RyaType("3");
    RyaType four = new RyaType("4");
    RyaIRI subj1 = new RyaIRI(litdupsNS, "subj1");
    RyaIRI subj2 = new RyaIRI(litdupsNS, "subj2");
    RyaIRI subj3 = new RyaIRI(litdupsNS, "subj3");
    RyaIRI subj4 = new RyaIRI(litdupsNS, "subj4");

    dao.add(new RyaStatement(subj1, pred, one));
    dao.add(new RyaStatement(subj1, pred, two));
    dao.add(new RyaStatement(subj1, pred, three));
    dao.add(new RyaStatement(subj1, pred, four));
    dao.add(new RyaStatement(subj2, pred, zero));
    dao.add(new RyaStatement(subj2, pred, one));
    dao.add(new RyaStatement(subj2, pred, two));
    dao.add(new RyaStatement(subj2, pred, three));
    dao.add(new RyaStatement(subj2, pred, four));
    dao.add(new RyaStatement(subj3, pred, one));
    dao.add(new RyaStatement(subj3, pred, two));
    dao.add(new RyaStatement(subj3, pred, four));
    dao.add(new RyaStatement(subj4, pred, one));
    dao.add(new RyaStatement(subj4, pred, two));
    dao.add(new RyaStatement(subj4, pred, three));
    dao.add(new RyaStatement(subj4, pred, four));

    //1 join
    IterativeJoin iterativeJoin = new IterativeJoin(dao.getQueryEngine());
    CloseableIteration<RyaIRI, RyaDAOException> join = iterativeJoin.join(null,
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, one),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, two),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, three),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, four)
    );

    Set<RyaIRI> uris = new HashSet<RyaIRI>();
    while (join.hasNext()) {
        uris.add(join.next());
    }
    assertTrue(uris.contains(subj1));
    assertTrue(uris.contains(subj2));
    assertTrue(uris.contains(subj4));
    join.close();
}
 
Example 20
Source File: AccumuloIndexSetTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void accumuloIndexSetTestWithTwoBindingSets() throws RepositoryException, PcjException, TableNotFoundException,
RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, AccumuloException, AccumuloSecurityException {
    // Load some Triples into Rya.
    final Set<Statement> triples = new HashSet<>();
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))) );
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(43)) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );

    for(final Statement triple : triples) {
        ryaConn.add(triple);
    }

    // Create a PCJ table will include those triples in its results.
    final String sparql =
            "SELECT ?name ?age " +
            "{" +
              "FILTER(?age < 30) ." +
              "?name <http://hasAge> ?age." +
              "?name <http://playsSport> \"Soccer\" " +
            "}";

    final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");

    // Create and populate the PCJ table.
    PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.absent());

    final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);

    final QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("birthDate",VF.createLiteral("1983-03-17",VF.createIRI("http://www.w3.org/2001/XMLSchema#date")));
    bs.addBinding("name",VF.createIRI("http://Alice"));

    final QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding("birthDate",VF.createLiteral("1983-04-18",VF.createIRI("http://www.w3.org/2001/XMLSchema#date")));
    bs2.addBinding("name",VF.createIRI("http://Bob"));

    final Set<BindingSet> bSets = Sets.newHashSet(bs,bs2);

    final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);

    final QueryBindingSet alice = new QueryBindingSet();
    alice.addBinding("name", VF.createIRI("http://Alice"));
    alice.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));
    alice.addBinding("birthDate", VF.createLiteral("1983-03-17",VF.createIRI("http://www.w3.org/2001/XMLSchema#date")));

    final QueryBindingSet bob = new QueryBindingSet();
    bob.addBinding("name", VF.createIRI("http://Bob"));
    bob.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));
    bob.addBinding("birthDate", VF.createLiteral("1983-04-18",VF.createIRI("http://www.w3.org/2001/XMLSchema#date")));

    final Set<BindingSet> fetchedResults = new HashSet<>();
    while(results.hasNext()) {
        final BindingSet next = results.next();
        System.out.println(next);
        fetchedResults.add(next);
    }

    Assert.assertEquals(Sets.<BindingSet>newHashSet(alice,bob), fetchedResults);
}