/** * Constructs an instance of {@link AggregationState}. */ public AggregationState() { this.visibility = ""; this.bindingSet = new MapBindingSet(); this.avgStates = new HashMap<>(); }
@Override public CloseableIteration<RyaStatement, RyaDAOException> query( final RyaStatement stmt, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { checkNotNull(stmt); checkNotNull(conf); Entry<RyaStatement, BindingSet> entry = new AbstractMap.SimpleEntry<>(stmt, new MapBindingSet()); Collection<Entry<RyaStatement, BindingSet>> collection = Collections.singleton(entry); return new RyaStatementCursorIterator(queryWithBindingSet(collection, conf)); }
@Override public CloseableIteration<RyaStatement, RyaDAOException> batchQuery( final Collection<RyaStatement> stmts, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { final Map<RyaStatement, BindingSet> queries = new HashMap<>(); for (final RyaStatement stmt : stmts) { queries.put(stmt, new MapBindingSet()); } return new RyaStatementCursorIterator(queryWithBindingSet(queries.entrySet(), conf)); }
@Override public CloseableIterable<RyaStatement> query(final BatchRyaQuery batchRyaQuery) throws RyaDAOException { Preconditions.checkNotNull(batchRyaQuery); final Map<RyaStatement, BindingSet> queries = new HashMap<>(); for (final RyaStatement stmt : batchRyaQuery.getQueries()) { queries.put(stmt, new MapBindingSet()); } Iterator<RyaStatement> iterator = new RyaStatementCursorIterator(queryWithBindingSet(queries.entrySet(), getConf())); return CloseableIterables.wrap(() -> iterator); }
/** * Create a new {@link BindingSet} that only includes the bindings whose names appear within the {@code variableOrder}. * If no binding is found for a variable, then that binding is just omitted from the resulting object. * * @param variableOrder - Defines which bindings will be kept. (not null) * @param bindingSet - Contains the source {@link Binding}s. (not null) * @return A new {@link BindingSet} containing only the specified bindings. */ public static BindingSet keepBindings(final VariableOrder variableOrder, final BindingSet bindingSet) { requireNonNull(variableOrder); requireNonNull(bindingSet); final MapBindingSet result = new MapBindingSet(); for(final String bindingName : variableOrder) { if(bindingSet.hasBinding(bindingName)) { final Binding binding = bindingSet.getBinding(bindingName); result.addBinding(binding); } } return result; } }
@Override public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException { Set<String> names = arg0.getBindingNames(); MapBindingSet sol = new MapBindingSet(names.size()); for (String n : names) { Value v = AGHttpRepoClient.getApplicationValue(arg0.getValue(n), vf); if (v != null) { sol.addBinding(n, v); } } handler.handleSolution(sol); }
public static synchronized CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateQuery(final ParsedQuery query, final SailConnection sc) throws QueryException { MapBindingSet bindings = new MapBindingSet(); boolean includeInferred = false; try { return sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, includeInferred); } catch (SailException e) { throw new QueryException(e); } }
@Override public BindingSet next() { final Document bs = resultsIter.next(); final MapBindingSet binding = new MapBindingSet(); for (final String key : bs.keySet()) { if (key.equals(VISIBILITIES_FIELD)) { // has auths, is a visibility binding set. } else if (!key.equals("_id") && !key.equals(PCJ_ID)) { // is the binding value. final Document typeDoc = (Document) bs.get(key); final IRI dataType = VF.createIRI(typeDoc.getString(BINDING_TYPE)); final RyaType type = new RyaType(dataType, typeDoc.getString(BINDING_VALUE)); final Value value = RyaToRdfConversions.convertValue(type); binding.addBinding(key, value); } } return binding; }
private void reportSolution(Resource solutionNode, List<String> bindingNames) throws RDFHandlerException, GraphUtilException { MapBindingSet bindingSet = new MapBindingSet(bindingNames.size()); Iterator<Value> bindingIter = GraphUtil.getObjectIterator(graph, solutionNode, BINDING); while (bindingIter.hasNext()) { Value bindingNode = bindingIter.next(); if (bindingNode instanceof Resource) { Binding binding = getBinding((Resource)bindingNode); bindingSet.addBinding(binding); } else { throw new RDFHandlerException("Value for " + BINDING + " is not a resource: " + bindingNode); } } try { tqrHandler.handleSolution(bindingSet); } catch (TupleQueryResultHandlerException e) { throw new RDFHandlerException(e.getMessage(), e); } }
private void reportSolution(Resource solutionNode, List<String> bindingNames) throws RDFHandlerException, GraphUtilException { MapBindingSet bindingSet = new MapBindingSet(bindingNames.size()); Iterator<Value> bindingIter = GraphUtil.getObjectIterator(graph, solutionNode, BINDING); while (bindingIter.hasNext()) { Value bindingNode = bindingIter.next(); if (bindingNode instanceof Resource) { Binding binding = getBinding((Resource)bindingNode); bindingSet.addBinding(binding); } else { throw new RDFHandlerException("Value for " + BINDING + " is not a resource: " + bindingNode); } } try { tqrHandler.handleSolution(bindingSet); } catch (TupleQueryResultHandlerException e) { throw new RDFHandlerException(e.getMessage(), e); } }
private void reportSolution(Resource solutionNode, List<String> bindingNames) throws RDFHandlerException, GraphUtilException { MapBindingSet bindingSet = new MapBindingSet(bindingNames.size()); Iterator<Value> bindingIter = GraphUtil.getObjectIterator(graph, solutionNode, BINDING); while (bindingIter.hasNext()) { Value bindingNode = bindingIter.next(); if (bindingNode instanceof Resource) { Binding binding = getBinding((Resource)bindingNode); bindingSet.addBinding(binding); } else { throw new RDFHandlerException("Value for " + BINDING + " is not a resource: " + bindingNode); } } try { tqrHandler.handleSolution(bindingSet); } catch (TupleQueryResultHandlerException e) { throw new RDFHandlerException(e.getMessage(), e); } }
private Set<VisibilityBindingSet> readGroupedResults(final String pcjId, final VariableOrder groupByVars) { requireNonNull(pcjId); // Read the results from the Kafka topic. The last one for each set of Group By values is an aggregation result. // The key in this map is a Binding Set containing only the group by variables. final Map<BindingSet, VisibilityBindingSet> results = new HashMap<>(); try(final KafkaConsumer<String, VisibilityBindingSet> consumer = makeConsumer(pcjId)) { final ConsumerRecords<String, VisibilityBindingSet> records = consumer.poll(5000); final Iterator<ConsumerRecord<String, VisibilityBindingSet>> recordIterator = records.iterator(); while (recordIterator.hasNext()) { final VisibilityBindingSet visBindingSet = recordIterator.next().value(); final MapBindingSet key = new MapBindingSet(); for(final String groupByBar : groupByVars) { key.addBinding( visBindingSet.getBinding(groupByBar) ); } results.put(key, visBindingSet); } } return Sets.newHashSet( results.values() ); } }
@Override public BindingSet next() throws QueryEvaluationException { if (!hasNext() || isClosed) { throw new NoSuchElementException(); } Statement statment = statementIt.next(); MapBindingSet bset = new MapBindingSet(); if (!subjectBinding.startsWith("-const")) bset.addBinding(subjectBinding, statment.getSubject()); if (!predicateBinding.startsWith("-const")) bset.addBinding(predicateBinding, statment.getPredicate()); if (!objectBinding.startsWith("-const")) bset.addBinding(objectBinding, statment.getObject()); if (contextBinding != null && !contextBinding.startsWith("-const")) bset.addBinding(contextBinding, statment.getContext()); // merge with other bindings. for (String name : bindings.getBindingNames()) { bset.addBinding(name, bindings.getValue(name)); } return bset; }
@Override public VisibilityBindingSet next() { final MapBindingSet bs = new MapBindingSet(); for (final Binding binding : newResult) { bs.addBinding(binding); } final VisibilityBindingSet joinResult = joinedResults.next(); for (final Binding binding : joinResult) { bs.addBinding(binding); } // We want to make sure the visibilities are always written the same way, // so figure out which are on the left side and which are on the right side. final String leftVisi; final String rightVisi; if (newResultSide == Side.LEFT) { leftVisi = newResult.getVisibility(); rightVisi = joinResult.getVisibility(); } else { leftVisi = joinResult.getVisibility(); rightVisi = newResult.getVisibility(); } final String visibility = VisibilitySimplifier.unionAndSimplify(leftVisi, rightVisi); return new VisibilityBindingSet(bs, visibility); }
@Nonnull private Record createMockRecord() { final Repository repository = mock(Repository.class); final MapBindingSet bindingSet = new MapBindingSet(); final Id recordId = new Id("urn:bluemarine:record:the_record"); bindingSet.addBinding("record", literalFor(recordId)); bindingSet.addBinding("label", literalFor("The record")); return new RepositoryRecord(repository, bindingSet); }
@Nonnull private MusicArtist createMockArtist() { final Repository repository = mock(Repository.class); final MapBindingSet bindingSet = new MapBindingSet(); final Id artistId = new Id("urn:bluemarine:artist:john_doe"); bindingSet.addBinding("artist", literalFor(artistId)); bindingSet.addBinding("label", literalFor("John Doe")); bindingSet.addBinding("artist_type", literalFor(1)); return new RepositoryMusicArtist(repository, bindingSet); }
@Test public void min() throws Exception { // A query that finds the minimum price for an item within the inventory. final String sparql = "SELECT (min(?price) as ?minPrice) { " + "?item <urn:price> ?price . " + "}"; // Create the Statements that will be loaded into Rya. final ValueFactory vf = SimpleValueFactory.getInstance(); final Collection<Statement> statements = Sets.newHashSet( vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:price"), vf.createLiteral(2.50)), vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:price"), vf.createLiteral(0.99)), vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:price"), vf.createLiteral(4.99))); // Create the PCJ in Fluo and load the statements into Rya. final String pcjId = loadDataAndCreateQuery(sparql, statements); // Create the expected results of the SPARQL query once the PCJ has been computed. final MapBindingSet expectedResult = new MapBindingSet(); expectedResult.addBinding("minPrice", vf.createLiteral(0.99)); // Ensure the last result matches the expected result. final VisibilityBindingSet result = readLastResult(pcjId); assertEquals(expectedResult, result); }
@Test public void sum() throws Exception { // A query that sums the counts of all of the items that are in the inventory. final String sparql = "SELECT (sum(?count) as ?itemSum) { " + "?item <urn:count> ?count . " + "}"; // Create the Statements that will be loaded into Rya. final ValueFactory vf = SimpleValueFactory.getInstance(); final Collection<Statement> statements = Sets.newHashSet( vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:count"), vf.createLiteral(5)), vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:count"), vf.createLiteral(7)), vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:count"), vf.createLiteral(2))); // Create the PCJ in Fluo and load the statements into Rya. final String pcjId = loadDataAndCreateQuery(sparql, statements); // Create the expected results of the SPARQL query once the PCJ has been computed. final MapBindingSet expectedResult = new MapBindingSet(); expectedResult.addBinding("itemSum", vf.createLiteral("14", XMLSchema.INTEGER)); // Ensure the last result matches the expected result. final VisibilityBindingSet result = readLastResult(pcjId); assertEquals(expectedResult, result); }
@Test public void aggregateWithFilter() throws Exception { // A query that filters results from a statement pattern before applying the aggregation function. final String sparql = "SELECT (min(?price) as ?minPrice) { " + "FILTER(?price > 1.00) " + "?item <urn:price> ?price . " + "}"; // Create the Statements that will be loaded into Rya. final ValueFactory vf = SimpleValueFactory.getInstance(); final Collection<Statement> statements = Sets.newHashSet( vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:price"), vf.createLiteral(2.50)), vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:price"), vf.createLiteral(0.99)), vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:price"), vf.createLiteral(4.99))); // Create the PCJ in Fluo and load the statements into Rya. final String pcjId = loadDataAndCreateQuery(sparql, statements); // Create the expected results of the SPARQL query once the PCJ has been computed. final MapBindingSet expectedResult = new MapBindingSet(); expectedResult.addBinding("minPrice", vf.createLiteral(2.50)); // Ensure the last result matches the expected result. final VisibilityBindingSet result = readLastResult(pcjId); assertEquals(expectedResult, result); }
@Test public void max() throws Exception { // A query that finds the maximum price for an item within the inventory. final String sparql = "SELECT (max(?price) as ?maxPrice) { " + "?item <urn:price> ?price . " + "}"; // Create the Statements that will be loaded into Rya. final ValueFactory vf = SimpleValueFactory.getInstance(); final Collection<Statement> statements = Sets.newHashSet( vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:price"), vf.createLiteral(2.50)), vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:price"), vf.createLiteral(0.99)), vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:price"), vf.createLiteral(4.99))); // Create the PCJ in Fluo and load the statements into Rya. final String pcjId = loadDataAndCreateQuery(sparql, statements); // Create the expected results of the SPARQL query once the PCJ has been computed. final MapBindingSet expectedResult = new MapBindingSet(); expectedResult.addBinding("maxPrice", vf.createLiteral(4.99)); // Ensure the last result matches the expected result. final VisibilityBindingSet result = readLastResult(pcjId); assertEquals(expectedResult, result); }