Search in sources :

Example 26 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class PipelineResultIterationTest method testIteration.

@Test
public void testIteration() throws QueryEvaluationException {
    final Map<String, String> nameMap = new HashMap<>();
    nameMap.put("bName", "b");
    nameMap.put("eName", "e");
    try (final PipelineResultIteration iter = new PipelineResultIteration(documentIterator(new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Beth")), new Document("<VALUES>", new Document("a", "urn:Alice").append("bName", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("c", "urn:Carol")), new Document("<VALUES>", new Document("cName", "urn:Carol").append("d", "urn:Dan"))), nameMap, new QueryBindingSet())) {
        assertTrue(iter.hasNext());
        BindingSet bs = iter.next();
        assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
        assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
        assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
        assertTrue(iter.hasNext());
        bs = iter.next();
        assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
        assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
        assertEquals("urn:Beth", bs.getBinding("b").getValue().stringValue());
        assertTrue(iter.hasNext());
        bs = iter.next();
        assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
        assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
        assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
        assertTrue(iter.hasNext());
        bs = iter.next();
        assertEquals(Sets.newHashSet("a", "c"), bs.getBindingNames());
        assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
        assertEquals("urn:Carol", bs.getBinding("c").getValue().stringValue());
        bs = iter.next();
        assertEquals(Sets.newHashSet("cName", "d"), bs.getBindingNames());
        assertEquals("urn:Carol", bs.getBinding("cName").getValue().stringValue());
        assertEquals("urn:Dan", bs.getBinding("d").getValue().stringValue());
        assertFalse(iter.hasNext());
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) HashMap(java.util.HashMap) Document(org.bson.Document) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 27 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class PCJKeyToCrossProductBindingSetIterator method getBindingSet.

/**
 * @param key
 *            - Accumulo key obtained from scan
 * @return - BindingSet satisfying any specified constant constraints
 * @throws BindingSetConversionException
 * @throws QueryEvaluationException
 */
private BindingSet getBindingSet(Key key) throws BindingSetConversionException, QueryEvaluationException {
    byte[] row = key.getRow().getBytes();
    String[] varOrder = key.getColumnFamily().toString().split(ExternalTupleSet.VAR_ORDER_DELIM);
    BindingSet bindingSet = converter.convert(row, new VariableOrder(varOrder));
    QueryBindingSet bs = new QueryBindingSet();
    for (String var : bindingSet.getBindingNames()) {
        String mappedVar = null;
        if (pcjVarMap.containsKey(var)) {
            mappedVar = pcjVarMap.get(var);
        } else {
            throw new QueryEvaluationException("PCJ Variable has no mapping to query variable.");
        }
        if (constantConstraintsExist) {
            if (VarNameUtils.isConstant(mappedVar) && constantConstraints.containsKey(mappedVar) && !constantConstraints.get(mappedVar).equals(bindingSet.getValue(var))) {
                return EMPTY_BINDINGSET;
            }
        }
        if (!VarNameUtils.isConstant(mappedVar)) {
            bs.addBinding(mappedVar, bindingSet.getValue(var));
        }
    }
    return bs;
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 28 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class EntityTupleSet method evaluate.

@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) throws QueryEvaluationException {
    // into the remainder of the star query to be evaluated
    if (minCard < 1000 && starQuery.size() > 2 && numberOfSpVars(minSp) == 1 && !starQuery.commonVarConstant()) {
        try {
            RdfCloudTripleStoreConnection conn = getRyaSailConnection();
            CloseableIteration<BindingSet, QueryEvaluationException> sol = (CloseableIteration<BindingSet, QueryEvaluationException>) conn.evaluate(minSp, null, bindings, false);
            Set<BindingSet> bSet = Sets.newHashSet();
            while (sol.hasNext()) {
                // TODO this is not optimal - should check if bindings variables intersect minSp variables
                // creating the following QueryBindingSet is only necessary if no intersection occurs
                QueryBindingSet bs = new QueryBindingSet();
                bs.addAll(sol.next());
                bs.addAll(bindings);
                bSet.add(bs);
            }
            List<StatementPattern> spList = starQuery.getNodes();
            spList.remove(minSp);
            StarQuery sq = new StarQuery(spList);
            conn.close();
            return new EntityTupleSet(sq, conf, true).evaluate(bSet);
        } catch (Exception e) {
            throw new QueryEvaluationException(e);
        }
    } else {
        this.evalOptUsed = true;
        return this.evaluate(Collections.singleton(bindings));
    }
}
Also used : CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) CardinalityStatementPattern(org.apache.rya.indexing.accumulo.entity.StarQuery.CardinalityStatementPattern) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RdfCloudTripleStoreConnection(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) SailException(org.eclipse.rdf4j.sail.SailException) AccumuloException(org.apache.accumulo.core.client.AccumuloException)

Example 29 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class AccumuloDocIdIndexer method queryDocIndex.

@Override
public CloseableIteration<BindingSet, QueryEvaluationException> queryDocIndex(final StarQuery query, final Collection<BindingSet> constraints) throws TableNotFoundException, QueryEvaluationException {
    final StarQuery starQ = query;
    final Iterator<BindingSet> bs = constraints.iterator();
    final Iterator<BindingSet> bs2 = constraints.iterator();
    final Set<String> unCommonVarNames;
    final Set<String> commonVarNames;
    if (bs2.hasNext()) {
        final BindingSet currBs = bs2.next();
        commonVarNames = StarQuery.getCommonVars(query, currBs);
        unCommonVarNames = Sets.difference(currBs.getBindingNames(), commonVarNames);
    } else {
        commonVarNames = Sets.newHashSet();
        unCommonVarNames = Sets.newHashSet();
    }
    if (commonVarNames.size() == 1 && !query.commonVarConstant() && commonVarNames.contains(query.getCommonVarName())) {
        final HashMultimap<String, BindingSet> map = HashMultimap.create();
        final String commonVar = starQ.getCommonVarName();
        final Iterator<Entry<Key, Value>> intersections;
        final BatchScanner scan;
        final Set<Range> ranges = Sets.newHashSet();
        while (bs.hasNext()) {
            final BindingSet currentBs = bs.next();
            if (currentBs.getBinding(commonVar) == null) {
                continue;
            }
            final String row = currentBs.getBinding(commonVar).getValue().stringValue();
            ranges.add(new Range(row));
            map.put(row, currentBs);
        }
        scan = runQuery(starQ, ranges);
        intersections = scan.iterator();
        return new CloseableIteration<BindingSet, QueryEvaluationException>() {

            private QueryBindingSet currentSolutionBs = null;

            private boolean hasNextCalled = false;

            private boolean isEmpty = false;

            private Iterator<BindingSet> inputSet = new ArrayList<BindingSet>().iterator();

            private BindingSet currentBs;

            private Key key;

            @Override
            public boolean hasNext() throws QueryEvaluationException {
                if (!hasNextCalled && !isEmpty) {
                    while (inputSet.hasNext() || intersections.hasNext()) {
                        if (!inputSet.hasNext()) {
                            key = intersections.next().getKey();
                            inputSet = map.get(key.getRow().toString()).iterator();
                        }
                        currentBs = inputSet.next();
                        currentSolutionBs = deserializeKey(key, starQ, currentBs, unCommonVarNames);
                        if (currentSolutionBs.size() == unCommonVarNames.size() + starQ.getUnCommonVars().size() + 1) {
                            hasNextCalled = true;
                            return true;
                        }
                    }
                    isEmpty = true;
                    return false;
                } else {
                    return !isEmpty;
                }
            }

            @Override
            public BindingSet next() throws QueryEvaluationException {
                if (hasNextCalled) {
                    hasNextCalled = false;
                } else if (isEmpty) {
                    throw new NoSuchElementException();
                } else {
                    if (this.hasNext()) {
                        hasNextCalled = false;
                    } else {
                        throw new NoSuchElementException();
                    }
                }
                return currentSolutionBs;
            }

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

            @Override
            public void close() throws QueryEvaluationException {
                scan.close();
            }
        };
    } else {
        return new CloseableIteration<BindingSet, QueryEvaluationException>() {

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

            private Iterator<Entry<Key, Value>> intersections = null;

            private QueryBindingSet currentSolutionBs = null;

            private boolean hasNextCalled = false;

            private boolean isEmpty = false;

            private boolean init = false;

            private BindingSet currentBs;

            private StarQuery sq = new StarQuery(starQ);

            private final Set<Range> emptyRangeSet = Sets.newHashSet();

            private BatchScanner scan;

            @Override
            public BindingSet next() throws QueryEvaluationException {
                if (hasNextCalled) {
                    hasNextCalled = false;
                } else if (isEmpty) {
                    throw new NoSuchElementException();
                } else {
                    if (this.hasNext()) {
                        hasNextCalled = false;
                    } else {
                        throw new NoSuchElementException();
                    }
                }
                return currentSolutionBs;
            }

            @Override
            public boolean hasNext() throws QueryEvaluationException {
                if (!init) {
                    if (intersections == null && bs.hasNext()) {
                        currentBs = bs.next();
                        sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
                        scan = runQuery(sq, emptyRangeSet);
                        intersections = scan.iterator();
                    // binding set empty
                    } else if (intersections == null && !bs.hasNext()) {
                        currentBs = new QueryBindingSet();
                        scan = runQuery(starQ, emptyRangeSet);
                        intersections = scan.iterator();
                    }
                    init = true;
                }
                if (!hasNextCalled && !isEmpty) {
                    while (intersections.hasNext() || bs.hasNext()) {
                        if (!intersections.hasNext()) {
                            scan.close();
                            currentBs = bs.next();
                            sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
                            scan = runQuery(sq, emptyRangeSet);
                            intersections = scan.iterator();
                        }
                        if (intersections.hasNext()) {
                            currentSolutionBs = deserializeKey(intersections.next().getKey(), sq, currentBs, unCommonVarNames);
                        } else {
                            continue;
                        }
                        if (sq.commonVarConstant() && currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size()) {
                            hasNextCalled = true;
                            return true;
                        } else if (currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size() + 1) {
                            hasNextCalled = true;
                            return true;
                        }
                    }
                    isEmpty = true;
                    return false;
                } else {
                    return !isEmpty;
                }
            }

            @Override
            public void close() throws QueryEvaluationException {
                scan.close();
            }
        };
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) Set(java.util.Set) BatchScanner(org.apache.accumulo.core.client.BatchScanner) ArrayList(java.util.ArrayList) Range(org.apache.accumulo.core.data.Range) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) Entry(java.util.Map.Entry) DocumentIndexIntersectingIterator(org.apache.rya.accumulo.documentIndex.DocumentIndexIntersectingIterator) Iterator(java.util.Iterator) Value(org.apache.accumulo.core.data.Value) Key(org.apache.accumulo.core.data.Key) NoSuchElementException(java.util.NoSuchElementException)

Example 30 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class AccumuloDocIndexerTest method testNoContextUnCommonVarBs.

@Test
public void testNoContextUnCommonVarBs() throws Exception {
    BatchWriter bw = null;
    RyaTableMutationsFactory rtm = new RyaTableMutationsFactory(RyaTripleContext.getInstance(conf));
    bw = accCon.createBatchWriter(tableName, 500L * 1024L * 1024L, Long.MAX_VALUE, 30);
    for (int i = 0; i < 30; i++) {
        RyaStatement rs1 = new RyaStatement(new RyaIRI("uri:" + i), new RyaIRI("uri:cf1"), new RyaType(XMLSchema.STRING, "cq1"));
        RyaStatement rs2 = new RyaStatement(new RyaIRI("uri:" + i), new RyaIRI("uri:cf2"), new RyaType(XMLSchema.STRING, "cq2"));
        RyaStatement rs3 = null;
        if (i == 5 || i == 10 || i == 15 || i == 20 || i == 25) {
            rs3 = new RyaStatement(new RyaIRI("uri:" + i), new RyaIRI("uri:cf3"), new RyaType(XMLSchema.INTEGER, Integer.toString(i)));
        }
        Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, Collection<Mutation>> serialize1 = rtm.serialize(rs1);
        Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, Collection<Mutation>> serialize2 = rtm.serialize(rs2);
        Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, Collection<Mutation>> serialize3 = null;
        if (rs3 != null) {
            serialize3 = rtm.serialize(rs3);
        }
        Collection<Mutation> m1 = EntityCentricIndex.createMutations(rs1);
        for (Mutation m : m1) {
            bw.addMutation(m);
        }
        Collection<Mutation> m2 = EntityCentricIndex.createMutations(rs2);
        for (Mutation m : m2) {
            bw.addMutation(m);
        }
        if (serialize3 != null) {
            Collection<Mutation> m3 = EntityCentricIndex.createMutations(rs3);
            for (Mutation m : m3) {
                bw.addMutation(m);
            }
        }
    }
    String q1 = // 
    "" + // 
    "SELECT ?X ?Y1 ?Y2 " + // 
    "{" + // 
    "?X <uri:cf1> ?Y1 ." + // 
    "?X <uri:cf2> ?Y2 ." + // 
    "?X <uri:cf3> ?Y3 ." + "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(q1, null);
    TupleExpr te1 = pq1.getTupleExpr();
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    Assert.assertTrue(StarQuery.isValidStarQuery(spList1));
    StarQuery sq1 = new StarQuery(spList1);
    AccumuloDocIdIndexer adi = new AccumuloDocIdIndexer(conf);
    Value v1 = RyaToRdfConversions.convertValue(new RyaType(XMLSchema.INTEGER, Integer.toString(5)));
    Value v2 = RyaToRdfConversions.convertValue(new RyaType(XMLSchema.INTEGER, Integer.toString(25)));
    List<BindingSet> bsList = Lists.newArrayList();
    QueryBindingSet b1 = (new QueryBindingSet());
    b1.addBinding("Y3", v1);
    QueryBindingSet b2 = (new QueryBindingSet());
    b2.addBinding("Y3", v2);
    bsList.add(b1);
    bsList.add(b2);
    CloseableIteration<BindingSet, QueryEvaluationException> sol1 = adi.queryDocIndex(sq1, bsList);
    System.out.println("**********************TEST 4***********************");
    int results = 0;
    while (sol1.hasNext()) {
        System.out.println(sol1.next());
        results++;
    }
    Assert.assertEquals(2, results);
    adi.close();
}
Also used : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) RyaIRI(org.apache.rya.api.domain.RyaIRI) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RyaTableMutationsFactory(org.apache.rya.accumulo.RyaTableMutationsFactory) Value(org.eclipse.rdf4j.model.Value) Collection(java.util.Collection) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Test(org.junit.Test)

Aggregations

QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)111 Test (org.junit.Test)84 BindingSet (org.eclipse.rdf4j.query.BindingSet)83 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)56 RyaIRI (org.apache.rya.api.domain.RyaIRI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)42 SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)42 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)16 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.eclipse.rdf4j.model.Statement)15 Value (org.eclipse.rdf4j.model.Value)14 Collection (java.util.Collection)13 BatchWriter (org.apache.accumulo.core.client.BatchWriter)12