/** * Constructs an instance of {@link AggregationState}. */ public AggregationState() { this.visibility = ""; this.bindingSet = new MapBindingSet(); this.avgStates = new HashMap<>(); }
@Override public void setBinding(String name, Value value) { assert value instanceof Literal || value instanceof IRI; bindings.addBinding(name, value); }
@Override public void clearBindings() { bindings.clear(); }
protected String getQueryString() { if (bindings.size() == 0) return operation; String qry = operation; int b = qry.indexOf('{'); String select = qry.substring(0, b); String where = qry.substring(b); for (String name : bindings.getBindingNames()) { String replacement = getReplacement(bindings.getValue(name)); if (replacement != null) { String pattern = "[\\?\\$]" + name + "(?=\\W)"; select = select.replaceAll(pattern, ""); where = where.replaceAll(pattern, replacement); } } return select + where; }
/** * 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 update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) { checkArgument(aggregation.getAggregationType() == AggregationType.COUNT, "The CountFunction only accepts COUNT AggregationElements."); requireNonNull(state); requireNonNull(childBindingSet); // Only add one to the count if the child contains the binding that we are counting. final String aggregatedName = aggregation.getAggregatedBindingName(); if(childBindingSet.hasBinding(aggregatedName)) { final MapBindingSet result = state.getBindingSet(); final String resultName = aggregation.getResultBindingName(); final boolean newBinding = !result.hasBinding(resultName); if(newBinding) { // Initialize the binding. result.addBinding(resultName, VF.createLiteral(BigInteger.ONE)); } else { // Update the existing binding. final Literal count = (Literal) result.getValue(resultName); final BigInteger updatedCount = count.integerValue().add( BigInteger.ONE ); result.addBinding(resultName, VF.createLiteral(updatedCount)); } } } }
final MapBindingSet result = state.getBindingSet(); final String resultName = aggregation.getResultBindingName(); final boolean newBinding = !result.hasBinding(resultName); final Literal countLiteral = VF.createLiteral(count); final Literal average = MathUtil.compute(sumLiteral, countLiteral, MathOp.DIVIDE); result.addBinding(resultName, average);
@Override public void removeBinding(String s) { bindings.removeBinding(s); }
@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 void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) { checkArgument(aggregation.getAggregationType() == AggregationType.MIN, "The MinFunction only accepts MIN AggregationElements."); requireNonNull(state); requireNonNull(childBindingSet); // Only update the min if the child contains the binding that we are finding the min value for. final String aggregatedName = aggregation.getAggregatedBindingName(); if(childBindingSet.hasBinding(aggregatedName)) { final MapBindingSet result = state.getBindingSet(); final String resultName = aggregation.getResultBindingName(); final boolean newBinding = !result.hasBinding(resultName); Value min; if(newBinding) { min = childBindingSet.getValue(aggregatedName); } else { final Value oldMin = result.getValue(resultName); final Value chidlMin = childBindingSet.getValue(aggregatedName); min = compare.compare(chidlMin, oldMin) < 0 ? chidlMin : oldMin; } result.addBinding(resultName, min); } } }
protected String getQueryString() { if (bindings.size() == 0) return queryString; String qry = queryString; int b = qry.indexOf('{'); String select = qry.substring(0, b); String where = qry.substring(b); for (String name : bindings.getBindingNames()) { String replacement = getReplacement(bindings.getValue(name)); if (replacement != null) { String pattern = "[\\?\\$]" + name + "(?=\\W)"; select = select.replaceAll(pattern, ""); where = where.replaceAll(pattern, replacement); } } return select + where; }
@Override public void removeBinding(String s) { bindings.removeBinding(s); }
@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); }
@Override public void accept(final Binding binding) { resultSet.addBinding(binding); } });
@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 void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) { checkArgument(aggregation.getAggregationType() == AggregationType.MAX, "The MaxFunction only accepts MAX AggregationElements."); requireNonNull(state); requireNonNull(childBindingSet); // Only update the max if the child contains the binding that we are finding the max value for. final String aggregatedName = aggregation.getAggregatedBindingName(); if(childBindingSet.hasBinding(aggregatedName)) { final MapBindingSet result = state.getBindingSet(); final String resultName = aggregation.getResultBindingName(); final boolean newBinding = !result.hasBinding(resultName); Value max; if(newBinding) { max = childBindingSet.getValue(aggregatedName); } else { final Value oldMax = result.getValue(resultName); final Value childMax = childBindingSet.getValue(aggregatedName); max = compare.compare(childMax, oldMax) > 0 ? childMax : oldMax; } result.addBinding(resultName, max); } } }
protected String getQueryString() { if (bindings.size() == 0) return operation; String qry = operation; int b = qry.indexOf('{'); String select = qry.substring(0, b); String where = qry.substring(b); for (String name : bindings.getBindingNames()) { String replacement = getReplacement(bindings.getValue(name)); if (replacement != null) { String pattern = "[\\?\\$]" + name + "(?=\\W)"; select = select.replaceAll(pattern, ""); where = where.replaceAll(pattern, replacement); } } return select + where; }
@Override public void clearBindings() { bindings.clear(); }
@Override public void removeBinding(String name) { bindings.removeBinding(name); }
@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); }