/// <summary> /// Counts the results /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingIDs">Binding IDs over which the Aggregate applies</param> /// <returns></returns> public override INode Apply(SparqlEvaluationContext context, IEnumerable <int> bindingIDs) { int c; List <INode> values = new List <INode>(); if (this._varname != null) { //Ensure the COUNTed variable is in the Variables of the Results if (!context.Binder.Variables.Contains(this._varname)) { throw new RdfQueryException("Cannot use the Variable " + this._expr.ToString() + " in a COUNT Aggregate since the Variable does not occur in a Graph Pattern"); } //Just Count the number of results where the variable is bound VariableExpressionTerm varExpr = (VariableExpressionTerm)this._expr; foreach (int id in bindingIDs) { INode temp = varExpr.Value(context, id); if (temp != null) { values.Add(temp); } } c = values.Distinct().Count(); } else { //Count the distinct non-null results foreach (int id in bindingIDs) { try { INode temp = this._expr.Value(context, id); if (temp != null) { values.Add(temp); } } catch { //Ignore errors } } c = values.Distinct().Count(); } LiteralNode count = new LiteralNode(null, c.ToString(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeInteger)); return(count); }
/// <summary> /// Creates a new MEDIAN Aggregate /// </summary> /// <param name="expr">Variable Expression</param> /// <param name="distinct">Whether a DISTINCT modifier applies</param> public MedianAggregate(VariableExpressionTerm expr, bool distinct) : base(expr, distinct) { this._varname = expr.ToString().Substring(1); }
/// <summary> /// Creates a new NMIN Aggregate /// </summary> /// <param name="expr">Variable Expression</param> public NumericMinAggregate(VariableExpressionTerm expr) : this(expr, false) { }
/// <summary> /// Creates a new MEDIAN Aggregate /// </summary> /// <param name="expr">Variable Expression</param> public MedianAggregate(VariableExpressionTerm expr) : this(expr, false) { }
/// <summary> /// Creates a new MODE Aggregate /// </summary> /// <param name="expr">Variable Expression</param> public ModeAggregate(VariableExpressionTerm expr) : base(expr, false) { }
/// <summary> /// Creates a new Bound Filter /// </summary> /// <param name="varTerm">Variable Expression</param> public BoundFilter(VariableExpressionTerm varTerm) : base(varTerm) { }
/// <summary> /// Creates a new AVG Aggregate /// </summary> /// <param name="expr">Variable Expression</param> public AverageAggregate(VariableExpressionTerm expr) : this(expr, false) { }
/// <summary> /// Creates a new COUNT(DISTINCT ?x) Aggregate /// </summary> /// <param name="expr">Variable Expression</param> public CountDistinctAggregate(VariableExpressionTerm expr) : base(expr) { this._varname = expr.ToString().Substring(1); }
/// <summary> /// Creates a new Bound() function expression /// </summary> /// <param name="varExpr">Variable Expression</param> public BoundFunction(VariableExpressionTerm varExpr) : base(varExpr) { }