Java Code Examples for org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet#addBinding()

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet#addBinding() . 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: StatementPatternMatcherTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void matchesSubject() throws Exception {
    // Create the matcher against a pattern that matches a specific subject.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp(
            "SELECT * WHERE {" +
                "<urn:Alice> ?p ?o ." +
            "}"));

    // Create a statement that matches the pattern.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"), vf.createIRI("urn:testGraph"));

    // Create the expected resulting Binding Set.
    final QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("p", vf.createIRI("urn:talksTo"));
    expected.addBinding("o", vf.createIRI("urn:Bob"));

    // Show the expected Binding Set matches the resulting Binding Set.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertEquals(expected, bs.get());
}
 
Example 2
Source File: StatementMetadataNode.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the BindingSet from the specified RyaStatement by using the
 * StatementPattern for this class. This method checks whether
 * StatementPattern has a {@link Value} for each position
 * {@link org.eclipse.rdf4j.query.algebra.Var} (Subject, Predicate, Object).
 * If it doesn't have a Value, a Binding is created from the
 * RyaStatement using the {@link RyaType} for the corresponding position
 * (Subject, Predicate, Object).
 *
 * @param statement
 * @return BindingSet
 */
private BindingSet buildBindingSetFromStatementPattern(final RyaStatement statement) {
    final Var subjVar = sp.getSubjectVar();
    final Var predVar = sp.getPredicateVar();
    final Var objVar = sp.getObjectVar();
    final Var contextVar = sp.getContextVar();
    final QueryBindingSet bs = new QueryBindingSet();

    if (subjVar.getValue() == null) {
        bs.addBinding(subjVar.getName(), RyaToRdfConversions.convertValue(statement.getSubject()));
    }

    if (predVar.getValue() == null) {
        bs.addBinding(predVar.getName(), RyaToRdfConversions.convertValue(statement.getPredicate()));
    }

    if (objVar.getValue() == null) {
        bs.addBinding(objVar.getName(), RyaToRdfConversions.convertValue(statement.getObject()));
    }

    if (contextVar != null && contextVar.getValue() == null) {
        bs.addBinding(contextVar.getName(), RyaToRdfConversions.convertValue(statement.getContext()));
    }

    return bs;
}
 
Example 3
Source File: AccumuloIndexSetColumnVisibilityTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void variableInstantiationTest() 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 bs = new QueryBindingSet();
    bs.addBinding("name", VF.createIRI("http://Alice"));
    final QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding("name", VF.createIRI("http://Bob"));

    final Set<BindingSet> bSets = Sets.newHashSet(bs, 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 4
Source File: AccumuloPcjSerializerTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void basicMixUriLiteralBsTest() throws BindingSetConversionException {
	final QueryBindingSet bs = new QueryBindingSet();
	bs.addBinding("X", VF.createLiteral("literal1"));
	bs.addBinding("Y", VF.createLiteral("5", VF.createIRI("http://www.w3.org/2001/XMLSchema#integer")));
	bs.addBinding("Z", VF.createLiteral("5.0", VF.createIRI("http://www.w3.org/2001/XMLSchema#double")));
	bs.addBinding("W", VF.createLiteral("1000", VF.createIRI("http://www.w3.org/2001/XMLSchema#long")));
	bs.addBinding("A", VF.createIRI("http://uri1"));
	bs.addBinding("B", VF.createIRI("http://uri2"));
	bs.addBinding("C", VF.createIRI("http://uri3"));
	final VariableOrder varOrder = new VariableOrder("A","W","X","Y","Z","B","C");

	BindingSetConverter<byte[]> converter = new AccumuloPcjSerializer();
	final byte[] byteVal = converter.convert(bs, varOrder);
	final BindingSet newBs = converter.convert(byteVal, varOrder);
	assertEquals(bs, newBs);
}
 
Example 5
Source File: VOIDBase.java    From semagrow with Apache License 2.0 5 votes vote down vote up
protected Set<Resource> getMatchingDatasetsOfPredicate(IRI pred) {
    String q = "SELECT ?dataset { ?dataset <" + VOID.PROPERTY + "> ?prop. \n" +
                      "?dataset <"+ VOID.TRIPLES + "> ?triples. FILTER (?triples > 0) .  }";
    //String q = "SELECT ?dataset { ?dataset ?p ?p1. }";
    QueryBindingSet bindings = new QueryBindingSet();
    bindings.addBinding("prop", pred);
    return evalQuerySet(q, bindings, "dataset");
}
 
Example 6
Source File: MongoDbSmartUriIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testStorage() throws SmartUriException, RuntimeException {
    smartUriConverter.storeEntity(BOB_ENTITY);

    final String sparql = "SELECT * WHERE { " +
        "<" + BOB.getData() + "> <" + RDF.TYPE + "> <" + PERSON_TYPE.getId().getData() + "> . " +
        "<" + BOB.getData() + "> <" + HAS_SSN.getData() + "> ?ssn . " +
        "<" + BOB.getData() + "> <" + HAS_AGE.getData() + "> ?age . " +
        "<" + BOB.getData() + "> <" + HAS_WEIGHT.getData() + "> ?weight . " +
        "<" + BOB.getData() + "> <" + HAS_ADDRESS.getData() + "> ?address . " +
    "}";

    final StatementPatternCollector spCollector = new StatementPatternCollector();
    new SPARQLParser().parseQuery(sparql, null).getTupleExpr().visit(spCollector);
    final List<StatementPattern> patterns = spCollector.getStatementPatterns();
    final EntityQueryNode entityQueryNode = new EntityQueryNode(PERSON_TYPE, patterns, smartUriConverter.getEntityStorage());
    final QueryBindingSet queryBindingSet = new QueryBindingSet();
    final Property ssnProperty = BOB_ENTITY.lookupTypeProperty(PERSON_TYPE, HAS_SSN).get();
    queryBindingSet.addBinding(HAS_SSN.getData(), RyaToRdfConversions.convertValue(ssnProperty.getValue()));

    final CloseableIteration<BindingSet, QueryEvaluationException> iter = entityQueryNode.evaluate(queryBindingSet);
    int count = 0;
    // These should match what was used in the SPARQL query.
    final List<String> queryParamNames = Lists.newArrayList("ssn", "age", "weight", "address");
    while (iter.hasNext()) {
        final BindingSet bs = iter.next();
        assertTrue(bs.getBindingNames().containsAll(queryParamNames));
        count++;
    }
    assertEquals(count, 1);
}
 
Example 7
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryNoBindingSetConstantContext()
        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:context2"), "", new StatementMetadata());
    dao.add(statement2);
    
    RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"),
            new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
    dao.add(statement3);

    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    
    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:Joe"));
    
    Assert.assertEquals(expected, bsList.get(0));
    
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
 
Example 8
Source File: GraphToBindingSetConversionIteration.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected BindingSet convert(Statement st) {
	QueryBindingSet result = new QueryBindingSet();
	result.addBinding("subject", st.getSubject());
	result.addBinding("predicate", st.getPredicate());
	result.addBinding("object", st.getObject());
	if (st.getContext() != null) {
		result.addBinding("context", st.getContext());
	}

	return result;
}
 
Example 9
Source File: FederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Evaluate a projection containing additional values, e.g. set from a filter expression
 * 
 * @return
 * @throws QueryEvaluationException
 */
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateProjectionWithBindings(ProjectionWithBindings projection, BindingSet bindings) throws QueryEvaluationException {
	QueryBindingSet actualBindings = new QueryBindingSet(bindings);
	for (Binding b : projection.getAdditionalBindings())
		actualBindings.addBinding(b);
	return evaluate((Projection)projection, actualBindings);
}
 
Example 10
Source File: SimpleBindingSetOps.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * Project a binding set to a potentially smaller binding set that
 * contain only the variable bindings that are in vars set.
 * @param bindings
 * @param vars
 * @return
 */
@Override
public BindingSet project(Collection<String> vars, BindingSet bindings) {
    QueryBindingSet q = new QueryBindingSet();

    for (String varName : vars) {
        Binding b = bindings.getBinding(varName);
        if (b != null) {
            q.addBinding(b);
        }
    }
    return q;
}
 
Example 11
Source File: BindingSetHashJoinIterator.java    From rya with Apache License 2.0 5 votes vote down vote up
private BindingSet removeConstants(BindingSet bs) {
	QueryBindingSet bSet = new QueryBindingSet();
	for (String s : bs.getBindingNames()) {
		if (!VarNameUtils.isConstant(s)) {
			bSet.addBinding(bs.getBinding(s));
		}
	}
	return bSet;
}
 
Example 12
Source File: BindingHashShardingFunctionTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void bindingSetRowTest() {
    String nodeId = NodeType.generateNewFluoIdForType(NodeType.STATEMENT_PATTERN);
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("entity", VF.createIRI("urn:entity"));
    bs.addBinding("location", VF.createLiteral("location_1"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
    VariableOrder varOrder = new VariableOrder("entity","location");
    Bytes row = RowKeyUtil.makeRowKey(nodeId, varOrder, vBs);
    Bytes shardedRow = BindingHashShardingFunction.addShard(nodeId, varOrder, vBs);
    BindingSetRow expected = BindingSetRow.make(row);
    BindingSetRow actual = BindingSetRow.makeFromShardedRow(Bytes.of(SP_PREFIX), shardedRow);
    Assert.assertEquals(expected, actual);
}
 
Example 13
Source File: AccumuloStatementMetadataNodeTest.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.
 * 
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetCollection()
        throws MalformedQueryException, QueryEvaluationException, RyaDAOException {

    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"));

    RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
            new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
    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);

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

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

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

    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);

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

    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", VF.createLiteral("CoffeeShop"));
    expected1.addBinding("y", VF.createLiteral("Joe"));
    expected1.addBinding("z", VF.createLiteral("Virginia"));

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

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

    Assert.assertEquals(2, bsList.size());
    Assert.assertEquals(expected1, bsList.get(1));
    Assert.assertEquals(expected2, bsList.get(0));

    dao.delete(statement1, conf);
    dao.delete(statement2, conf);
}
 
Example 14
Source File: AccumuloIndexSetTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void accumuloIndexSetTestWithTwoDirectProductBindingSet() 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("location",VF.createIRI("http://Virginia"));

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

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

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

    final QueryBindingSet alice1 = new QueryBindingSet();
    alice1.addBinding("name", VF.createIRI("http://Alice"));
    alice1.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));
    alice1.addAll(bs);

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

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

    final QueryBindingSet alice2 = new QueryBindingSet();
    alice2.addBinding("name", VF.createIRI("http://Alice"));
    alice2.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));
    alice2.addAll(bs2);

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

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

    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(alice1,bob1,charlie1,alice2,bob2,charlie2), fetchedResults);
}
 
Example 15
Source File: PeriodicNotificationProcessorIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void periodicProcessorTest() throws Exception {
    
    String id = UUID.randomUUID().toString().replace("-", "");
    BlockingQueue<TimestampedNotification> notifications = new LinkedBlockingQueue<>();
    BlockingQueue<NodeBin> bins = new LinkedBlockingQueue<>();
    BlockingQueue<BindingSetRecord> bindingSets = new LinkedBlockingQueue<>();
    
    TimestampedNotification ts1 = new TimestampedNotification(
            PeriodicNotification.builder().id(id).initialDelay(0).period(2000).timeUnit(TimeUnit.SECONDS).build());  
    long binId1 = (ts1.getTimestamp().getTime()/ts1.getPeriod())*ts1.getPeriod();
    
    Thread.sleep(2000);
    
    TimestampedNotification ts2 = new TimestampedNotification(
            PeriodicNotification.builder().id(id).initialDelay(0).period(2000).timeUnit(TimeUnit.SECONDS).build());  
    long binId2 = (ts2.getTimestamp().getTime()/ts2.getPeriod())*ts2.getPeriod();
    
    Set<NodeBin> expectedBins = new HashSet<>();
    expectedBins.add(new NodeBin(id, binId1));
    expectedBins.add(new NodeBin(id, binId2));
    
    Set<BindingSet> expected = new HashSet<>();
    Set<VisibilityBindingSet> storageResults = new HashSet<>();
    
    QueryBindingSet bs1 = new QueryBindingSet();
    bs1.addBinding("periodicBinId", VF.createLiteral(binId1));
    bs1.addBinding("id", VF.createLiteral(1));
    expected.add(bs1);
    storageResults.add(new VisibilityBindingSet(bs1));
    
    QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding("periodicBinId", VF.createLiteral(binId1));
    bs2.addBinding("id", VF.createLiteral(2));
    expected.add(bs2);
    storageResults.add(new VisibilityBindingSet(bs2));
    
    QueryBindingSet bs3 = new QueryBindingSet();
    bs3.addBinding("periodicBinId", VF.createLiteral(binId2));
    bs3.addBinding("id", VF.createLiteral(3));
    expected.add(bs3);
    storageResults.add(new VisibilityBindingSet(bs3));
    
    QueryBindingSet bs4 = new QueryBindingSet();
    bs4.addBinding("periodicBinId", VF.createLiteral(binId2));
    bs4.addBinding("id", VF.createLiteral(4));
    expected.add(bs4);
    storageResults.add(new VisibilityBindingSet(bs4));
    
    PeriodicQueryResultStorage periodicStorage = new AccumuloPeriodicQueryResultStorage(super.getAccumuloConnector(),
            RYA_INSTANCE_NAME);
    periodicStorage.createPeriodicQuery(id, "select ?id where {?obs <urn:hasId> ?id.}", new VariableOrder("periodicBinId", "id"));
    periodicStorage.addPeriodicQueryResults(id, storageResults);

    NotificationProcessorExecutor processor = new NotificationProcessorExecutor(periodicStorage, notifications, bins, bindingSets, 1);
    processor.start();
    
    notifications.add(ts1);
    notifications.add(ts2);

    Thread.sleep(5000);
    
    Assert.assertEquals(expectedBins.size(), bins.size());
    Assert.assertEquals(true, bins.containsAll(expectedBins));
    
    Set<BindingSet> actual = new HashSet<>();
    bindingSets.forEach(x -> actual.add(x.getBindingSet()));
    Assert.assertEquals(expected, actual);
    
    processor.stop();
}
 
Example 16
Source File: AccumuloIndexSetTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void accumuloIndexSetTestWithTwoDirectProductBindingSetsWithConstantMapping() throws Exception {
    // 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 " +
            "{" +
              "?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 String sparql2 =
            "SELECT ?x " +
            "{" +
              "?x <http://hasAge> 16 ." +
              "?x <http://playsSport> \"Soccer\" " +
            "}";

    final SPARQLParser p = new SPARQLParser();
    final ParsedQuery pq1 = p.parseQuery(sparql, null);
    final ParsedQuery pq2 = p.parseQuery(sparql2, null);

    final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);
    ais.setProjectionExpr((Projection) QueryVariableNormalizer.getNormalizedIndex(pq2.getTupleExpr(), pq1.getTupleExpr()).get(0));

    final QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("birthDate",VF.createLiteral("1983-03-17",VF.createIRI("http://www.w3.org/2001/XMLSchema#date")));
    bs.addBinding("x",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("x",VF.createIRI("http://Bob"));

    final Set<BindingSet> bSets = Sets.newHashSet(bs,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);
    }

    Assert.assertEquals(Sets.<BindingSet>newHashSet(bs2), fetchedResults);
}
 
Example 17
Source File: HashJoinIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected BindingSet getNextElement() throws QueryEvaluationException {
	Map<BindingSetHashKey, List<BindingSet>> nextHashTable = hashTable;
	if (nextHashTable == null) {
		synchronized (this) {
			nextHashTable = hashTable;
			if (nextHashTable == null) {
				nextHashTable = hashTable = setupHashTable();
			}
		}
	}
	Iterator<BindingSet> nextHashTableValues = hashTableValues;

	while (currentScanElem == null) {
		if (scanList.hasNext()) {
			currentScanElem = nextFromCache(scanList);
		} else {
			disposeCache(scanList); // exhausted so can free

			if (restIter.hasNext()) {
				currentScanElem = restIter.next();
			} else {
				// no more elements available
				return null;
			}
		}

		if (currentScanElem != null) {
			if (currentScanElem instanceof EmptyBindingSet) {
				// the empty bindingset should be merged with all bindingset in the
				// hash table
				Collection<List<BindingSet>> values = nextHashTable.values();
				boolean empty = values.isEmpty() || values.size() == 1 && values.contains(null);
				nextHashTableValues = hashTableValues = empty ? new EmptyIterator<>() : new UnionIterator<>(values);
				if (!nextHashTableValues.hasNext()) {
					currentScanElem = null;
					closeHashValue(nextHashTableValues);
					nextHashTableValues = hashTableValues = null;
				}
			} else {
				BindingSetHashKey key = BindingSetHashKey.create(joinAttributes, currentScanElem);
				List<BindingSet> hashValue = nextHashTable.get(key);
				if (hashValue != null && !hashValue.isEmpty()) {
					nextHashTableValues = hashTableValues = hashValue.iterator();
				} else if (leftJoin) {
					nextHashTableValues = hashTableValues = Collections.singletonList(EmptyBindingSet.getInstance())
							.iterator();
				} else {
					currentScanElem = null;
					closeHashValue(nextHashTableValues);
					nextHashTableValues = hashTableValues = null;
				}
			}
		}
	}

	if (nextHashTableValues != null) {
		BindingSet nextHashTableValue = nextHashTableValues.next();

		QueryBindingSet result = new QueryBindingSet(currentScanElem);

		for (String name : nextHashTableValue.getBindingNames()) {
			if (!result.hasBinding(name)) {
				Value v = nextHashTableValue.getValue(name);
				if (v != null) {
					result.addBinding(name, v);
				}
			}
		}

		if (!nextHashTableValues.hasNext()) {
			// we've exhausted the current scanlist entry
			currentScanElem = null;
			closeHashValue(nextHashTableValues);
			nextHashTableValues = hashTableValues = null;
		}
		return result;
	}

	return EmptyBindingSet.getInstance();
}
 
Example 18
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleQueryWithBindingSets()
        throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    //query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph ?c  {?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:context2"), "", new StatementMetadata());
    dao.add(statement2);
    
    RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"),
            new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
    dao.add(statement3);

    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("c", VF.createIRI("uri:context2"));
    
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("c", VF.createIRI("uri:context1"));

    
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));

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

    Assert.assertEquals(2, bsList.size());
    
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", VF.createIRI("uri:Joe"));
    expected1.addBinding("c", VF.createIRI("uri:context1"));

    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", VF.createIRI("uri:Doug"));
    expected2.addBinding("c", VF.createIRI("uri:context2"));
    
    Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2));
    Set<BindingSet> actual = new HashSet<>(bsList);
    
    Assert.assertEquals(expected, actual);
    
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
 
Example 19
Source File: BatchIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleJoinAdd() throws Exception {
    final String sparql = "SELECT ?subject ?object1 ?object2 WHERE { ?subject <urn:predicate_1> ?object1; "
            + " <urn:predicate_2> ?object2 } ";
    try (FluoClient fluoClient = new FluoClientImpl(getFluoConfiguration())) {

        RyaIRI subj = new RyaIRI("urn:subject_1");
        RyaStatement statement2 = new RyaStatement(subj, new RyaIRI("urn:predicate_2"), null);
        Set<RyaStatement> statements2 = getRyaStatements(statement2, 5);

        // Create the PCJ table.
        final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(getAccumuloConnector(), getRyaInstanceName());
        final String pcjId = pcjStorage.createPcj(sparql);

        // Tell the Fluo app to maintain the PCJ.
        String queryId = new CreateFluoPcj()
                .withRyaIntegration(pcjId, pcjStorage, fluoClient, getAccumuloConnector(), getRyaInstanceName()).getQueryId();

        List<String> ids = getNodeIdStrings(fluoClient, queryId);
        String joinId = ids.get(2);
        String rightSp = ids.get(4);
        QueryBindingSet bs = new QueryBindingSet();
        bs.addBinding("subject", VF.createIRI("urn:subject_1"));
        bs.addBinding("object1", VF.createIRI("urn:object_0"));
        VisibilityBindingSet vBs = new VisibilityBindingSet(bs);

        IRI iri = VF.createIRI("urn:subject_1");
        Bytes prefixBytes = BindingHashShardingFunction.getShardedScanPrefix(rightSp, iri);
        Span span = Span.prefix(prefixBytes);

        // Stream the data into Fluo.
        InsertTriples inserter = new InsertTriples();
        inserter.insert(fluoClient, statements2, Optional.absent());

        getMiniFluo().waitForObservers();
        verifyCounts(fluoClient, ids, Arrays.asList(0, 0, 0, 0, 5));

        JoinBatchInformation batch = JoinBatchInformation.builder().setBatchSize(1)
                .setColumn(FluoQueryColumns.STATEMENT_PATTERN_BINDING_SET).setSpan(span).setTask(Task.Add)
                .setJoinType(JoinType.NATURAL_JOIN).setSide(Side.LEFT).setBs(vBs).build();
        // Verify the end results of the query match the expected results.
        createSpanBatch(fluoClient, joinId, batch);

        getMiniFluo().waitForObservers();
        verifyCounts(fluoClient, ids, Arrays.asList(5, 5, 5, 0, 5));
    }
}
 
Example 20
Source File: StarQueryTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetContrainedQuery() throws RyaTypeResolverException, MalformedQueryException {
  
    String q1 = "" //
            + "SELECT ?X ?Y1 ?Y2 " //
            + "{"//
            + "GRAPH <http://joe> { " //
            +  "?X <uri:cf1> ?Y1 ."//
            +  "?X <uri:cf2> ?Y2 ."//
            +  "?X <uri:cf3> ?Y3 ."//
            +  "}" //
            +  "}";
    
    
    SPARQLParser parser = new SPARQLParser();
    
    ParsedQuery pq1 = null;
    pq1 = parser.parseQuery(q1, null);

    TupleExpr te1 = pq1.getTupleExpr();
    
    System.out.println(te1);
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    
    StarQuery sq1 = new StarQuery(spList1);
    
    QueryBindingSet bs1 = new QueryBindingSet();
    QueryBindingSet bs2 = new QueryBindingSet();
    
    Value v1 = VF.createIRI("uri:hank");
    Value v2 = VF.createIRI("uri:bob");
    
    bs1.addBinding("X",v1);
    bs2.addBinding("X", v1);
    bs2.addBinding("Y3", v2);
    
   StarQuery sq2 = StarQuery.getConstrainedStarQuery(sq1, bs1);
   StarQuery sq3 = StarQuery.getConstrainedStarQuery(sq1, bs2);
   
   Assert.assertTrue(sq2.commonVarHasValue());
   Assert.assertEquals(sq2.getCommonVarValue(), "uri:hank");
   
   Assert.assertTrue(sq3.commonVarHasValue());
   Assert.assertEquals(sq3.getCommonVarValue(), "uri:hank");
   
   
   TextColumn[] tc1 = sq1.getColumnCond();
   TextColumn[] tc2 = sq2.getColumnCond();
   TextColumn[] tc3 = sq3.getColumnCond();
   
   for(int i = 0; i < tc1.length; i++) {
       
       Assert.assertTrue(tc1[i].equals(tc2[i]));
       if(i != 2) {
           Assert.assertTrue(tc1[i].equals(tc3[i]));
       } else {
           Assert.assertEquals(tc3[i].getColumnFamily(), new Text("uri:cf3"));
           RyaType objType = RdfToRyaConversions.convertValue(v2);
           byte[][] b1 = null;
            b1 = RyaContext.getInstance().serializeType(objType);
           byte[] b2 = Bytes.concat("object".getBytes(),
                   "\u0000".getBytes(), b1[0], b1[1]);
           Assert.assertEquals(tc3[i].getColumnQualifier(), new Text(b2));
           Assert.assertTrue(!tc3[i].isPrefix());
       }
   }
    
    
    
}