/// <summary> /// Gets the row value for the aggregator as number /// </summary> internal Double GetRowValueAsNumber(DataRow tableRow) { try { if (!tableRow.IsNull(this.AggregatorVariable.VariableName)) { RDFPatternMember rowAggregatorValue = RDFQueryUtilities.ParseRDFPatternMember(tableRow[this.AggregatorVariable.VariableName].ToString()); //PlainLiteral: accepted only if numeric and non-languaged if (rowAggregatorValue is RDFPlainLiteral) { if (String.IsNullOrEmpty(((RDFPlainLiteral)rowAggregatorValue).Language)) { if (Double.TryParse(rowAggregatorValue.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out Double doubleValue)) { return(doubleValue); } } } //TypedLiteral: accepted only if numeric else if (rowAggregatorValue is RDFTypedLiteral) { if (((RDFTypedLiteral)rowAggregatorValue).HasDecimalDatatype()) { return(Double.Parse(((RDFTypedLiteral)rowAggregatorValue).Value, NumberStyles.Float, CultureInfo.InvariantCulture)); } } } return(Double.NaN); } catch (Exception ex) { RDFQueryEvents.RaiseSELECTQueryEvaluation(String.Format("Exception intercepted during evaluation of RDFAggregator '{0}' in method GetRowValueAsNumber: '{1}'", this, ex.Message)); return(Double.NaN); } }
/// <summary> /// Applies the query to the given SPARQL endpoint /// </summary> public RDFSelectQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint) { RDFSelectQueryResult selResult = new RDFSelectQueryResult(); if (sparqlEndpoint != null) { RDFQueryEvents.RaiseSELECTQueryEvaluation(String.Format("Evaluating SELECT query on SPARQL endpoint '{0}'...", sparqlEndpoint)); //Establish a connection to the given SPARQL endpoint using (WebClient webClient = new WebClient()) { //Insert reserved "query" parameter webClient.QueryString.Add("query", HttpUtility.UrlEncode(this.ToString())); //Insert user-provided parameters webClient.QueryString.Add(sparqlEndpoint.QueryParams); //Insert request headers webClient.Headers.Add(HttpRequestHeader.Accept, "application/sparql-results+xml"); //Send querystring to SPARQL endpoint var sparqlResponse = webClient.DownloadData(sparqlEndpoint.BaseAddress); //Parse response from SPARQL endpoint if (sparqlResponse != null) { using (var sStream = new MemoryStream(sparqlResponse)) { selResult = RDFSelectQueryResult.FromSparqlXmlResult(sStream); } selResult.SelectResults.TableName = this.ToString(); } } //Eventually adjust column names (should start with "?") Int32 columnsCount = selResult.SelectResults.Columns.Count; for (Int32 i = 0; i < columnsCount; i++) { if (!selResult.SelectResults.Columns[i].ColumnName.StartsWith("?")) { selResult.SelectResults.Columns[i].ColumnName = "?" + selResult.SelectResults.Columns[i].ColumnName; } } RDFQueryEvents.RaiseSELECTQueryEvaluation(String.Format("Evaluated SELECT query on SPARQL endpoint '{0}': Found '{1}' results.", sparqlEndpoint, selResult.SelectResultsCount)); } return(selResult); }
/// <summary> /// Gets the row value for the aggregator as string /// </summary> internal String GetRowValueAsString(DataRow tableRow) { try { if (!tableRow.IsNull(this.AggregatorVariable.VariableName)) { return(tableRow[this.AggregatorVariable.VariableName].ToString()); } return(String.Empty); } catch (Exception ex) { RDFQueryEvents.RaiseSELECTQueryEvaluation(String.Format("Exception intercepted during evaluation of RDFAggregator '{0}' in method GetRowValueAsString: '{1}'", this, ex.Message)); return(String.Empty); } }