Ejemplo n.º 1
0
 /// <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);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Applies the query to the given SPARQL endpoint
        /// </summary>
        public RDFDescribeQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint)
        {
            RDFDescribeQueryResult describeResult = new RDFDescribeQueryResult(this.ToString());

            if (sparqlEndpoint != null)
            {
                RDFQueryEvents.RaiseDESCRIBEQueryEvaluation(String.Format("Evaluating DESCRIBE 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/turtle");
                    webClient.Headers.Add(HttpRequestHeader.Accept, "text/turtle");

                    //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))
                        {
                            describeResult = RDFDescribeQueryResult.FromRDFGraph(RDFGraph.FromStream(RDFModelEnums.RDFFormats.Turtle, sStream));
                        }
                        describeResult.DescribeResults.TableName = this.ToString();
                    }
                }

                //Eventually adjust column names (should start with "?")
                Int32 columnsCount = describeResult.DescribeResults.Columns.Count;
                for (Int32 i = 0; i < columnsCount; i++)
                {
                    if (!describeResult.DescribeResults.Columns[i].ColumnName.StartsWith("?"))
                    {
                        describeResult.DescribeResults.Columns[i].ColumnName = "?" + describeResult.DescribeResults.Columns[i].ColumnName;
                    }
                }

                RDFQueryEvents.RaiseDESCRIBEQueryEvaluation(String.Format("Evaluated DESCRIBE query on SPARQL endpoint '{0}': Found '{1}' results.", sparqlEndpoint, describeResult.DescribeResultsCount));
            }
            return(describeResult);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
 /// <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);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds the given alternative steps to the path. If only one is given, it is considered sequence.
        /// </summary>
        public RDFPropertyPath AddAlternativeSteps(List <RDFPropertyPathStep> alternativeSteps)
        {
            if (alternativeSteps != null && alternativeSteps.Any())
            {
                #region Depth Guard
                if (this.Steps.Count == 0 ||
                    alternativeSteps.Count == 1 ||
                    this.Steps.LastOrDefault()?.StepFlavor == RDFQueryEnums.RDFPropertyPathStepFlavors.Sequence)
                {
                    this.Depth++;
                }
                #endregion

                #region Steps Update
                if (alternativeSteps.Count == 1)
                {
                    this.Steps.Add(alternativeSteps[0].SetOrdinal(this.Steps.Count)
                                   .SetFlavor(RDFQueryEnums.RDFPropertyPathStepFlavors.Sequence));
                }
                else
                {
                    alternativeSteps.ForEach(alternativeStep =>
                    {
                        this.Steps.Add(alternativeStep.SetOrdinal(this.Steps.Count)
                                       .SetFlavor(RDFQueryEnums.RDFPropertyPathStepFlavors.Alternative));
                    });
                }
                #endregion

                #region Evaluability Guard
                if (this.Start is RDFVariable || this.End is RDFVariable || this.Depth > 1)
                {
                    this.IsEvaluable = true;
                    RDFQueryEvents.RaiseGENERICQueryEvaluation("AddAlternativeSteps: non-ground property path detected, evaluability granted or confirmed.");
                }
                else
                {
                    RDFQueryEvents.RaiseGENERICQueryEvaluation("AddAlternativeSteps: ground property path detected, evaluability not granted.");
                }
                #endregion
            }
            return(this);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Applies the query to the given SPARQL endpoint
        /// </summary>
        public RDFConstructQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint)
        {
            RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());

            if (sparqlEndpoint != null)
            {
                RDFQueryEvents.RaiseCONSTRUCTQueryEvaluation(String.Format("Evaluating CONSTRUCT 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/turtle");
                    webClient.Headers.Add(HttpRequestHeader.Accept, "text/turtle");

                    //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))
                        {
                            constructResult = RDFConstructQueryResult.FromRDFGraph(RDFGraph.FromStream(RDFModelEnums.RDFFormats.Turtle, sStream));
                        }
                        constructResult.ConstructResults.TableName = this.ToString();
                    }
                }

                RDFQueryEvents.RaiseCONSTRUCTQueryEvaluation(String.Format("Evaluated CONSTRUCT query on SPARQL endpoint '{0}': Found '{1}' results.", sparqlEndpoint, constructResult.ConstructResultsCount));
            }
            return(constructResult);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Applies the query to the given SPARQL endpoint
        /// </summary>
        public RDFAskQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint)
        {
            RDFAskQueryResult askResult = new RDFAskQueryResult();

            if (sparqlEndpoint != null)
            {
                RDFQueryEvents.RaiseASKQueryEvaluation(String.Format("Evaluating ASK 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))
                        {
                            askResult = RDFAskQueryResult.FromSparqlXmlResult(sStream);
                        }
                    }
                }

                RDFQueryEvents.RaiseASKQueryEvaluation(String.Format("Evaluated ASK query on SPARQL endpoint '{0}': Result is '{1}'.", sparqlEndpoint, askResult.AskResult));
            }
            return(askResult);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Adds the given sequence step to the path
        /// </summary>
        public RDFPropertyPath AddSequenceStep(RDFPropertyPathStep sequenceStep)
        {
            if (sequenceStep != null)
            {
                #region Steps Update
                this.Depth++;
                this.Steps.Add(sequenceStep.SetOrdinal(this.Steps.Count)
                               .SetFlavor(RDFQueryEnums.RDFPropertyPathStepFlavors.Sequence));
                #endregion

                #region Evaluability Guard
                if (this.Start is RDFVariable || this.End is RDFVariable || this.Depth > 1)
                {
                    this.IsEvaluable = true;
                    RDFQueryEvents.RaiseGENERICQueryEvaluation("AddSequenceStep: non-ground property path detected, evaluability granted or confirmed.");
                }
                else
                {
                    RDFQueryEvents.RaiseGENERICQueryEvaluation("AddSequenceStep: ground property path detected, evaluability not granted.");
                }
                #endregion
            }
            return(this);
        }