/// <summary> /// Gets the value of a member of the Group for concatenating as part of the result for the Group /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> protected virtual String ValueInternal(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = this._expr.Evaluate(context, bindingID); if (temp == null) { throw new RdfQueryException("Cannot do an XPath string-join on a null"); } if (temp.NodeType == NodeType.Literal) { ILiteralNode l = (ILiteralNode)temp; if (l.DataType != null) { if (l.DataType.AbsoluteUri.Equals(XmlSpecsHelper.XmlSchemaDataTypeString)) { return(temp.AsString()); } else { throw new RdfQueryException("Cannot do an XPath string-join on a Literal which is not typed as a String"); } } else { return(temp.AsString()); } } else { throw new RdfQueryException("Cannot do an XPath string-join on a non-Literal Node"); } }
/// <summary> /// Gets the value of the function in the given Evaluation Context for the given Binding ID /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = this._expr.Evaluate(context, bindingID); if (temp != null) { switch (temp.NodeType) { case NodeType.Blank: throw new RdfQueryException("Cannot calculate the Hash of a Blank Node"); case NodeType.GraphLiteral: throw new RdfQueryException("Cannot calculate the Hash of a Graph Literal"); case NodeType.Literal: return(new StringNode(null, this.Hash(((ILiteralNode)temp).Value))); case NodeType.Uri: return(new StringNode(null, this.Hash(temp.AsString()))); default: throw new RdfQueryException("Cannot calculate the Hash of an Unknown Node Type"); } } else { throw new RdfQueryException("Cannot calculate the Hash of a null"); } }
public static List <Pokemon> resultsToObject(SparqlResultSet results, IEnumerable <string> names) { List <Pokemon> pokemons = new List <Pokemon>(); results.Results.ForEach(row => { string pkName = row.ElementAt(0).Value.AsValuedNode().AsString(); if (!pokemons.Exists(pk => pk.name == pkName)) { Pokemon pokemon = new Pokemon(pkName, new List <Values>()); pokemons.Add(pokemon); } for (int i = 1; i < row.Count; i++) { Pokemon pokemon = pokemons.Find(pk => pk.name == pkName); string valName = names.ElementAt(i - 1); if (!pokemon.values.Exists(val => val.name == valName)) { pokemon.values.Add(new Values(valName, new List <string>())); } Values values = pokemon.values.Find(val => val.name == valName); IValuedNode value = row[i].AsValuedNode(); string val = (value != null) ? value.AsString() : ""; if (!values.vals.Exists(v => v == val)) { values.vals.Add(val); } } }); return(pokemons); }
/// <summary> /// Configures the Options for the Regular Expression /// </summary> /// <param name="n">Node detailing the Options</param> /// <param name="throwErrors">Whether errors should be thrown or suppressed</param> private void ConfigureOptions(IValuedNode n, bool throwErrors) { // Start by resetting to no options this._options = RegexOptions.None; if (n == null) { if (throwErrors) { throw new RdfQueryException("REGEX Options Expression does not produce an Options string"); } } else { if (n.NodeType == NodeType.Literal) { string ops = n.AsString(); foreach (char c in ops.ToCharArray()) { switch (c) { case 'i': this._options |= RegexOptions.IgnoreCase; break; case 'm': this._options |= RegexOptions.Multiline; break; case 's': this._options |= RegexOptions.Singleline; break; case 'x': this._options |= RegexOptions.IgnorePatternWhitespace; break; default: if (throwErrors) { throw new RdfQueryException("Invalid flag character '" + c + "' in Options string"); } break; } } } else { if (throwErrors) { throw new RdfQueryException("REGEX Options Expression does not produce an Options string"); } } } }
/// <summary> /// Casts the results of the inner expression to a Literal Node typed xsd:string. /// </summary> /// <param name="context">Evaluation Context.</param> /// <param name="bindingID">Binding ID.</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode n = _expr.Evaluate(context, bindingID); if (n == null) { throw new RdfQueryException("Cannot cast a Null to a xsd:string"); } return(new StringNode(null, n.AsString(), UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString))); }
/// <summary> /// Creates a new SPARQL Replace function. /// </summary> /// <param name="text">Text Expression.</param> /// <param name="find">Search Expression.</param> /// <param name="replace">Replace Expression.</param> /// <param name="options">Options Expression.</param> public ReplaceFunction(ISparqlExpression text, ISparqlExpression find, ISparqlExpression replace, ISparqlExpression options) { _textExpr = text; // Get the Pattern if (find is ConstantTerm) { // If the Pattern is a Node Expression Term then it is a fixed Pattern IValuedNode n = find.Evaluate(null, 0); if (n.NodeType == NodeType.Literal) { // Try to parse as a Regular Expression try { string p = n.AsString(); Regex temp = new Regex(p); // It's a Valid Pattern _fixedPattern = true; _find = p; } catch { // No catch actions } } } _findExpr = find; // Get the Replace if (replace is ConstantTerm) { // If the Replace is a Node Expresison Term then it is a fixed Pattern IValuedNode n = replace.Evaluate(null, 0); if (n.NodeType == NodeType.Literal) { _replace = n.AsString(); _fixedReplace = true; } } _replaceExpr = replace; // Get the Options if (options != null) { if (options is ConstantTerm) { ConfigureOptions(options.Evaluate(null, 0), false); } _optionExpr = options; } }
/// <summary> /// Calculates the value of the function in the given Evaluation Context for the given Binding ID. /// </summary> /// <param name="context">Evaluation Context.</param> /// <param name="bindingID">Binding ID.</param> /// <returns></returns> public virtual IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = _expr.Evaluate(context, bindingID); if (temp != null) { DateTimeOffset dt = temp.AsDateTimeOffset(); // Regex based check to see if the value has a Timezone component // If not then the result is a null if (!Regex.IsMatch(temp.AsString(), "(Z|[+-]\\d{2}:\\d{2})$")) { return(null); } // Now we have a DateTime we can try and return the Timezone if (dt.Offset.Equals(TimeSpan.Zero)) { // If Zero it was specified as Z (which means UTC so zero offset) return(new StringNode(null, "PT0S", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDayTimeDuration))); } else { // If the Offset is outside the range -14 to 14 this is considered invalid if (dt.Offset.Hours < -14 || dt.Offset.Hours > 14) { return(null); } // Otherwise it has an offset which is a given number of hours and minutse string offset = "PT" + Math.Abs(dt.Offset.Hours) + "H"; if (dt.Offset.Hours < 0) { offset = "-" + offset; } if (dt.Offset.Minutes != 0) { offset = offset + Math.Abs(dt.Offset.Minutes) + "M"; } if (dt.Offset.Hours == 0 && dt.Offset.Minutes < 0) { offset = "-" + offset; } return(new StringNode(null, offset, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDayTimeDuration))); } } else { throw new RdfQueryException("Unable to evaluate an XPath Date Time function on a null argument"); } }
/// <summary> /// Gets the value of the function in the given Evaluation Context for the given Binding ID /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { StringBuilder output = new StringBuilder(); for (int i = 0; i < _exprs.Count; i++) { IValuedNode temp = _exprs[i].Evaluate(context, bindingID); if (temp == null) { throw new RdfQueryException("Cannot evaluate the ARQ string-join() function when an argument evaluates to a Null"); } switch (temp.NodeType) { case NodeType.Literal: output.Append(temp.AsString()); break; default: throw new RdfQueryException("Cannot evaluate the ARQ string-join() function when an argument is not a Literal Node"); } if (i < _exprs.Count - 1) { if (_fixedSeparator) { output.Append(_separator); } else { IValuedNode sep = _sep.Evaluate(context, bindingID); if (sep == null) { throw new RdfQueryException("Cannot evaluate the ARQ strjoin() function when the separator expression evaluates to a Null"); } if (sep.NodeType == NodeType.Literal) { output.Append(sep.AsString()); } else { throw new RdfQueryException("Cannot evaluate the ARQ strjoin() function when the separator expression evaluates to a non-Literal Node"); } } } } return(new StringNode(null, output.ToString(), UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString))); }
/// <summary> /// Gets the value of the aggregate for the given binding /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> protected override string ValueInternal(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = this._expr.Evaluate(context, bindingID); if (temp == null) { throw new RdfQueryException("Cannot do an XPath string-join on a null"); } switch (temp.NodeType) { case NodeType.Literal: case NodeType.Uri: return(temp.AsString()); default: throw new RdfQueryException("Cannot do an XPath string-join on a non-Literal Node"); } }
private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable) { // TODO: Some sort of cast to nullable when necessary switch (targetType.PrimitiveKind) { case EdmPrimitiveTypeKind.Boolean: return(valuedNode.AsBoolean()); case EdmPrimitiveTypeKind.Byte: return((byte)valuedNode.AsInteger()); case EdmPrimitiveTypeKind.DateTime: return(valuedNode.AsDateTime()); case EdmPrimitiveTypeKind.Decimal: return(valuedNode.AsDecimal()); case EdmPrimitiveTypeKind.Double: return(valuedNode.AsDouble()); case EdmPrimitiveTypeKind.Int16: return((Int16)valuedNode.AsInteger()); case EdmPrimitiveTypeKind.Int32: return((Int32)valuedNode.AsInteger()); case EdmPrimitiveTypeKind.Int64: return(valuedNode.AsInteger()); case EdmPrimitiveTypeKind.String: return(valuedNode.AsString()); case EdmPrimitiveTypeKind.DateTimeOffset: return(valuedNode.AsDateTime()); default: throw new NotSupportedException( String.Format("Support for primitive type {0} has not been implemented yet", targetType.PrimitiveKind)); } }
/// <summary> /// Creates a new ARQ String Join function /// </summary> /// <param name="sepExpr">Separator Expression</param> /// <param name="expressions">Expressions to concatentate</param> public StringJoinFunction(ISparqlExpression sepExpr, IEnumerable <ISparqlExpression> expressions) { if (sepExpr is ConstantTerm) { IValuedNode temp = sepExpr.Evaluate(null, 0); if (temp.NodeType == NodeType.Literal) { _separator = temp.AsString(); _fixedSeparator = true; } else { _sep = sepExpr; } } else { _sep = sepExpr; } _exprs.AddRange(expressions); }
/// <summary> /// Returns the value of the Expression as evaluated for a given Binding as a Literal Node /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode result = _expr.Evaluate(context, bindingID); if (result == null) { throw new RdfQueryException("Cannot return the lexical value of an NULL"); } else { switch (result.NodeType) { case NodeType.Literal: case NodeType.Uri: return(new StringNode(null, result.AsString())); default: throw new RdfQueryException("Cannot return the lexical value of Nodes which are not Literal/URI Nodes"); } } }
/// <summary> /// Gets the Timezone of the Argument Expression as evaluated for the given Binding in the given Context /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = this._expr.Evaluate(context, bindingID); if (temp != null) { DateTimeOffset dt = temp.AsDateTime(); //Regex based check to see if the value has a Timezone component //If not then the result is a null if (!Regex.IsMatch(temp.AsString(), "(Z|[+-]\\d{2}:\\d{2})$")) { return(new StringNode(null, string.Empty)); } //Now we have a DateTime we can try and return the Timezone if (dt.Offset.Equals(TimeSpan.Zero)) { //If Zero it was specified as Z (which means UTC so zero offset) return(new StringNode(null, "Z")); } else { //If the Offset is outside the range -14 to 14 this is considered invalid if (dt.Offset.Hours < -14 || dt.Offset.Hours > 14) { return(null); } //Otherwise it has an offset which is a given number of hours (and minutes) return(new StringNode(null, dt.Offset.Hours.ToString("00") + ":" + dt.Offset.Minutes.ToString("00"))); } } else { throw new RdfQueryException("Unable to evaluate a Date Time function on a null argument"); } }
/// <summary> /// Gets the Value of the function as evaluated in the given Context for the given Binding ID /// </summary> /// <param name="context">Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { StringBuilder output = new StringBuilder(); foreach (ISparqlExpression expr in this._exprs) { IValuedNode temp = expr.Evaluate(context, bindingID); if (temp == null) { throw new RdfQueryException("Cannot evaluate the XPath concat() function when an argument evaluates to a Null"); } switch (temp.NodeType) { case NodeType.Literal: output.Append(temp.AsString()); break; default: throw new RdfQueryException("Cannot evaluate the XPath concat() function when an argument is not a Literal Node"); } } return(new StringNode(null, output.ToString(), UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString))); }
/// <summary> /// Configures the Options for the Regular Expression /// </summary> /// <param name="n">Node detailing the Options</param> /// <param name="throwErrors">Whether errors should be thrown or suppressed</param> private void ConfigureOptions(IValuedNode n, bool throwErrors) { //Start by resetting to no options this._options = RegexOptions.None; if (n == null) { if (throwErrors) { throw new RdfQueryException("REGEX Options Expression does not produce an Options string"); } } else { if (n.NodeType == NodeType.Literal) { string ops = n.AsString(); foreach (char c in ops.ToCharArray()) { switch (c) { case 'i': this._options |= RegexOptions.IgnoreCase; break; case 'm': this._options |= RegexOptions.Multiline; break; case 's': this._options |= RegexOptions.Singleline; break; case 'x': this._options |= RegexOptions.IgnorePatternWhitespace; break; default: if (throwErrors) { throw new RdfQueryException("Invalid flag character '" + c + "' in Options string"); } break; } } } else { if (throwErrors) { throw new RdfQueryException("REGEX Options Expression does not produce an Options string"); } } } }
/// <summary> /// Evaluates the expression /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { // Configure Options if (this._optionExpr != null && !this._fixedOptions) { this.ConfigureOptions(this._optionExpr.Evaluate(context, bindingID), true); } // Compile the Regex if necessary if (!this._fixedPattern) { // Regex is not pre-compiled if (this._patternExpr != null) { IValuedNode p = this._patternExpr.Evaluate(context, bindingID); if (p != null) { if (p.NodeType == NodeType.Literal) { this._pattern = p.AsString(); } else { throw new RdfQueryException("Cannot parse a Pattern String from a non-Literal Node"); } } else { throw new RdfQueryException("Not a valid Pattern Expression"); } } else { throw new RdfQueryException("Not a valid Pattern Expression or the fixed Pattern String was invalid"); } } // Execute the Regular Expression IValuedNode textNode = this._textExpr.Evaluate(context, bindingID); if (textNode == null) { throw new RdfQueryException("Cannot evaluate a Regular Expression against a NULL"); } if (textNode.NodeType == NodeType.Literal) { // Execute string text = textNode.AsString(); if (this._regex != null) { return(new BooleanNode(null, this._regex.IsMatch(text))); } else { return(new BooleanNode(null, Regex.IsMatch(text, this._pattern, this._options))); } } else { throw new RdfQueryException("Cannot evaluate a Regular Expression against a non-Literal Node"); } }
/// <summary> /// Returns the value of the Expression as evaluated for a given Binding as a Literal Node. /// </summary> /// <param name="context">Evaluation Context.</param> /// <param name="bindingID">Binding ID.</param> /// <returns></returns> public IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { // Configure Options if (_optionExpr != null) { ConfigureOptions(_optionExpr.Evaluate(context, bindingID), true); } // Compile the Regex if necessary if (!_fixedPattern) { // Regex is not pre-compiled if (_findExpr != null) { IValuedNode p = _findExpr.Evaluate(context, bindingID); if (p != null) { if (p.NodeType == NodeType.Literal) { _find = p.AsString(); } else { throw new RdfQueryException("Cannot parse a Pattern String from a non-Literal Node"); } } else { throw new RdfQueryException("Not a valid Pattern Expression"); } } else { throw new RdfQueryException("Not a valid Pattern Expression or the fixed Pattern String was invalid"); } } // Compute the Replace if necessary if (!_fixedReplace) { if (_replaceExpr != null) { IValuedNode r = _replaceExpr.Evaluate(context, bindingID); if (r != null) { if (r.NodeType == NodeType.Literal) { _replace = r.AsString(); } else { throw new RdfQueryException("Cannot parse a Replace String from a non-Literal Node"); } } else { throw new RdfQueryException("Not a valid Replace Expression"); } } else { throw new RdfQueryException("Not a valid Replace Expression"); } } // Execute the Regular Expression IValuedNode textNode = _textExpr.Evaluate(context, bindingID); if (textNode == null) { throw new RdfQueryException("Cannot evaluate a Regular Expression against a NULL"); } if (textNode.NodeType == NodeType.Literal) { // Execute ILiteralNode lit = (ILiteralNode)textNode; if (lit.DataType != null && !lit.DataType.AbsoluteUri.Equals(XmlSpecsHelper.XmlSchemaDataTypeString)) { throw new RdfQueryException("Text Argument to Replace must be of type xsd:string if a datatype is specified"); } string text = lit.Value; string output = Regex.Replace(text, _find, _replace, _options); if (lit.DataType != null) { return(new StringNode(null, output, lit.DataType)); } else if (!lit.Language.Equals(string.Empty)) { return(new StringNode(null, output, lit.Language)); } else { return(new StringNode(null, output)); } } else { throw new RdfQueryException("Cannot evaluate a Regular Expression against a non-Literal Node"); } }
/// <summary> /// Applies the aggregate over the given bindings /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingIDs">Binding IDs</param> /// <returns></returns> public override IValuedNode Apply(SparqlEvaluationContext context, IEnumerable <int> bindingIDs) { IValuedNode n = base.Apply(context, bindingIDs); return(new StringNode(n.Graph, n.AsString())); }
/// <summary> /// Returns the value of the Expression as evaluated for a given Binding as a Literal Node /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { // Configure Options if (this._optionExpr != null) { this.ConfigureOptions(this._optionExpr.Evaluate(context, bindingID), true); } // Compile the Regex if necessary if (!this._fixedPattern) { // Regex is not pre-compiled if (this._findExpr != null) { IValuedNode p = this._findExpr.Evaluate(context, bindingID); if (p != null) { if (p.NodeType == NodeType.Literal) { this._find = p.AsString(); } else { throw new RdfQueryException("Cannot parse a Pattern String from a non-Literal Node"); } } else { throw new RdfQueryException("Not a valid Pattern Expression"); } } else { throw new RdfQueryException("Not a valid Pattern Expression or the fixed Pattern String was invalid"); } } // Compute the Replace if necessary if (!this._fixedReplace) { if (this._replaceExpr != null) { IValuedNode r = this._replaceExpr.Evaluate(context, bindingID); if (r != null) { if (r.NodeType == NodeType.Literal) { this._replace = r.AsString(); } else { throw new RdfQueryException("Cannot parse a Replace String from a non-Literal Node"); } } else { throw new RdfQueryException("Not a valid Replace Expression"); } } else { throw new RdfQueryException("Not a valid Replace Expression"); } } // Execute the Regular Expression IValuedNode textNode = this._textExpr.Evaluate(context, bindingID); if (textNode == null) { throw new RdfQueryException("Cannot evaluate a Regular Expression against a NULL"); } if (textNode.NodeType == NodeType.Literal) { // Execute string text = textNode.AsString(); string output = Regex.Replace(text, this._find, this._replace, this._options); return(new StringNode(null, output, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString))); } else { throw new RdfQueryException("Cannot evaluate a Regular Expression against a non-Literal Node"); } }
private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable) { // TODO: Some sort of cast to nullable when necessary switch (targetType.PrimitiveKind) { case EdmPrimitiveTypeKind.Boolean: return valuedNode.AsBoolean(); case EdmPrimitiveTypeKind.Byte: return (byte) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.DateTime: return valuedNode.AsDateTime(); case EdmPrimitiveTypeKind.Decimal: return valuedNode.AsDecimal(); case EdmPrimitiveTypeKind.Double: return valuedNode.AsDouble(); case EdmPrimitiveTypeKind.Int16: return (Int16) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.Int32: return (Int32) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.Int64: return valuedNode.AsInteger(); case EdmPrimitiveTypeKind.String: return valuedNode.AsString(); case EdmPrimitiveTypeKind.DateTimeOffset: return valuedNode.AsDateTime(); default: throw new NotSupportedException( String.Format("Support for primitive type {0} has not been implemented yet", targetType.PrimitiveKind)); } }