/// <summary> /// Substitutes a primary expression which is a Node term for a virtual Node term /// </summary> /// <param name="expr">Expression</param> /// <returns></returns> protected ISparqlExpression SubstitutePrimaryExpression(ISparqlExpression expr) { if (expr.GetType().Equals(this._exprType)) { NodeExpressionTerm term = (NodeExpressionTerm)expr; INode curr = term.Value(null, 0); TNodeID id = this._provider.GetID(curr); if (id == null || id.Equals(this._provider.NullID)) { throw new RdfQueryException("Cannot transform the Expression to use Virtual Nodes"); } INode virt = this.CreateVirtualNode(id, curr); return(new NodeExpressionTerm(virt)); } else { return(expr); } }
/// <summary> /// Creates a new Same Term Filter /// </summary> /// <param name="pattern">Algebra the Filter applies over</param> /// <param name="var">Variable to restrict on</param> /// <param name="term">Expression Term</param> public SameTermFilter(ISparqlAlgebra pattern, String var, NodeExpressionTerm term) : base(pattern, var, term, new UnaryExpressionFilter(new SameTermFunction(new VariableExpressionTerm(var), term))) { }
/// <summary> /// Creates a new Single Value Restriction Filter /// </summary> /// <param name="pattern">Algebra the filter applies over</param> /// <param name="var">Variable to restrict on</param> /// <param name="term">Value to restrict to</param> /// <param name="filter">Filter to use</param> public SingleValueRestrictionFilter(ISparqlAlgebra pattern, String var, NodeExpressionTerm term, ISparqlFilter filter) : base(pattern, var, filter) { this._term = term; }
/// <summary> /// Creates a new Identity Filter /// </summary> /// <param name="pattern">Algebra the Filter applies over</param> /// <param name="var">Variable to restrict on</param> /// <param name="term">Expression Term</param> public IdentityFilter(ISparqlAlgebra pattern, String var, NodeExpressionTerm term) : base(pattern, var, term, new UnaryExpressionFilter(new EqualsExpression(new VariableExpressionTerm(var), term))) { }
/// <summary> /// Formats a SPARQL Expression /// </summary> /// <param name="expr">SPARQL Expression</param> /// <returns></returns> protected virtual String FormatExpression(ISparqlExpression expr) { StringBuilder output = new StringBuilder(); try { switch (expr.Type) { case SparqlExpressionType.Aggregate: if (expr is AggregateExpressionTerm) { AggregateExpressionTerm agg = (AggregateExpressionTerm)expr; output.Append(this.FormatAggregate(agg.Aggregate)); } else if (expr is NonNumericAggregateExpressionTerm) { NonNumericAggregateExpressionTerm nonNumAgg = (NonNumericAggregateExpressionTerm)expr; output.Append(this.FormatAggregate(nonNumAgg.Aggregate)); } else { output.Append(expr.ToString()); } break; case SparqlExpressionType.BinaryOperator: ISparqlExpression lhs = expr.Arguments.First(); ISparqlExpression rhs = expr.Arguments.Skip(1).First(); //Format the Expression wrapping the LHS and/or RHS in brackets if required //to ensure that ordering of operators is preserved if (lhs.Type == SparqlExpressionType.BinaryOperator) { output.Append('('); output.Append(this.FormatExpression(lhs)); output.Append(')'); } else { output.Append(this.FormatExpression(lhs)); } output.Append(' '); output.Append(expr.Functor); output.Append(' '); if (rhs.Type == SparqlExpressionType.BinaryOperator) { output.Append('('); output.Append(this.FormatExpression(rhs)); output.Append(')'); } else { output.Append(this.FormatExpression(rhs)); } break; case SparqlExpressionType.Function: //Show either a Keyword/URI/QName as appropriate if (SparqlSpecsHelper.IsFunctionKeyword(expr.Functor)) { output.Append(expr.Functor); } else { String funcQname; if (this._qnameMapper.ReduceToQName(expr.Functor, out funcQname)) { output.Append(funcQname); } else { output.Append('<'); output.Append(this.FormatUri(expr.Functor)); output.Append('>'); } } //Add Arguments list output.Append('('); List <ISparqlExpression> args = expr.Arguments.ToList(); for (int i = 0; i < args.Count; i++) { output.Append(this.FormatExpression(args[i])); if (i < args.Count - 1) { output.Append(", "); } } output.Append(')'); break; case SparqlExpressionType.GraphOperator: output.Append(expr.Functor); output.Append(' '); List <ISparqlExpression> gArgs = expr.Arguments.ToList(); if (gArgs.Count > 1) { throw new RdfOutputException("Error Formatting SPARQL Expression - Expressions of type GraphOperator are only allowed a single argument"); } for (int i = 0; i < gArgs.Count; i++) { output.Append(this.FormatExpression(gArgs[i])); if (i < gArgs.Count - 1) { output.Append(", "); } } break; case SparqlExpressionType.Primary: //If Node/Numeric Term then use Node Formatting otherwise use ToString() on the expression if (expr is NodeExpressionTerm) { NodeExpressionTerm nodeTerm = (NodeExpressionTerm)expr; output.Append(this.Format(nodeTerm.Value(null, 0))); } else if (expr is NumericExpressionTerm) { NumericExpressionTerm numTerm = (NumericExpressionTerm)expr; output.Append(this.Format(numTerm.Value(null, 0))); } else if (expr is GraphPatternExpressionTerm) { GraphPatternExpressionTerm gp = (GraphPatternExpressionTerm)expr; output.Append(this.Format(gp.Pattern)); } else { output.Append(expr.ToString()); } break; case SparqlExpressionType.SetOperator: //Add First Argument and Set Operator output.Append(this.FormatExpression(expr.Arguments.First())); output.Append(' '); output.Append(expr.Functor); //Add Set output.Append(" ("); List <ISparqlExpression> set = expr.Arguments.Skip(1).ToList(); for (int i = 0; i < set.Count; i++) { output.Append(this.FormatExpression(set[i])); if (i < set.Count - 1) { output.Append(", "); } } output.Append(')'); break; case SparqlExpressionType.UnaryOperator: //Just Functor then Expression output.Append(expr.Functor); output.Append(this.FormatExpression(expr.Arguments.First())); break; } } catch (RdfOutputException) { throw; } catch (Exception ex) { throw new RdfOutputException("Error formatting a SPARQL Expression - the Expression may have the wrong number of arguments for the reported expression type", ex); } return(output.ToString()); }