Exemple #1
0
        /// <summary>
        /// Gets the datatable representing the SPARQL values
        /// </summary>
        internal DataTable GetDataTable()
        {
            DataTable result = new DataTable();

            result.ExtendedProperties.Add("IsOptional", false);
            result.ExtendedProperties.Add("JoinAsUnion", false);

            //Create the columns of the SPARQL values
            this.Bindings.ToList()
            .ForEach(b => RDFQueryEngine.AddColumn(result, b.Key));
            result.AcceptChanges();

            //Create the rows of the SPARQL values
            result.BeginLoadData();
            for (int i = 0; i < this.MaxBindingsLength; i++)
            {
                Dictionary <String, String> bindings = new Dictionary <String, String>();
                this.Bindings.ToList()
                .ForEach(b =>
                {
                    RDFPatternMember bindingValue = b.Value.ElementAtOrDefault(i);
                    bindings.Add(b.Key, bindingValue?.ToString());
                    if (bindingValue == null)
                    {
                        result.ExtendedProperties["IsOptional"] = true;
                    }
                });
                RDFQueryEngine.AddRow(result, bindings);
            }
            result.EndLoadData();

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Executes the projection producing result's table
        /// </summary>
        internal override DataTable ExecuteProjection(List <RDFVariable> partitionVariables)
        {
            DataTable projFuncTable = new DataTable();

            //Initialization
            partitionVariables.ForEach(pv =>
                                       RDFQueryEngine.AddColumn(projFuncTable, pv.VariableName));
            RDFQueryEngine.AddColumn(projFuncTable, this.ProjectionVariable.VariableName);

            //Finalization
            foreach (string partitionKey in this.AggregatorContext.ExecutionRegistry.Keys)
            {
                //Get aggregator value
                double aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <double>(partitionKey, 0d);
                //Get aggregator counter
                double aggregatorCounter = this.AggregatorContext.GetPartitionKeyExecutionCounter(partitionKey);
                //In case of non-numeric values, consider partition failed
                double finalAggregatorValue = double.NaN;
                if (!aggregatorValue.Equals(double.NaN))
                {
                    finalAggregatorValue = aggregatorValue / aggregatorCounter;
                }
                //Update aggregator context (sum, count)
                this.AggregatorContext.UpdatePartitionKeyExecutionResult <double>(partitionKey, finalAggregatorValue);
                //Update result's table
                this.UpdateProjectionTable(partitionKey, projFuncTable);
            }

            return(projFuncTable);
        }
Exemple #3
0
        /// <summary>
        /// Executes the projection producing result's table
        /// </summary>
        internal override DataTable ExecuteProjection(List <RDFVariable> partitionVariables)
        {
            DataTable projFuncTable = new DataTable();

            //Initialization
            partitionVariables.ForEach(pv =>
                                       RDFQueryEngine.AddColumn(projFuncTable, pv.VariableName));
            RDFQueryEngine.AddColumn(projFuncTable, this.ProjectionVariable.VariableName);

            //Finalization
            foreach (string partitionKey in this.AggregatorContext.ExecutionRegistry.Keys)
            {
                //Update result's table
                this.UpdateProjectionTable(partitionKey, projFuncTable);
            }

            return(projFuncTable);
        }
Exemple #4
0
        /// <summary>
        /// Gets the datatable representing the SPARQL values
        /// </summary>
        internal DataTable GetDataTable()
        {
            DataTable result = new DataTable();

            //Create the columns of the SPARQL values
            this.Bindings.ToList()
            .ForEach(b => RDFQueryEngine.AddColumn(result, b.Key));
            result.AcceptChanges();

            //Create the rows of the SPARQL values
            result.BeginLoadData();
            for (int i = 0; i < this.MaxBindingsLength; i++)
            {
                Dictionary <String, String> bindings = new Dictionary <String, String>();
                this.Bindings.ToList()
                .ForEach(b => bindings.Add(b.Key, b.Value.ElementAtOrDefault(i)?.ToString()));
                RDFQueryEngine.AddRow(result, bindings);
            }
            result.EndLoadData();

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Reads the given "SPARQL Query Results XML Format" stream into a SELECT query result
        /// </summary>
        public static RDFSelectQueryResult FromSparqlXmlResult(Stream inputStream)
        {
            try
            {
                #region deserialize
                RDFSelectQueryResult result = new RDFSelectQueryResult();
                using (StreamReader streamReader = new StreamReader(inputStream, Encoding.UTF8))
                {
                    using (XmlTextReader xmlReader = new XmlTextReader(streamReader))
                    {
                        xmlReader.DtdProcessing = DtdProcessing.Parse;
                        xmlReader.Normalization = false;

                        #region document
                        XmlDocument srxDoc = new XmlDocument();
                        srxDoc.Load(xmlReader);
                        #endregion

                        #region results
                        Boolean foundHead    = false;
                        Boolean foundResults = false;
                        var     nodesEnum    = srxDoc.DocumentElement.ChildNodes.GetEnumerator();
                        while (nodesEnum != null && nodesEnum.MoveNext())
                        {
                            XmlNode node = (XmlNode)nodesEnum.Current;

                            #region HEAD
                            if (node.Name.ToUpperInvariant().Equals("HEAD", StringComparison.Ordinal))
                            {
                                foundHead = true;
                                if (node.HasChildNodes)
                                {
                                    var variablesEnum = node.ChildNodes.GetEnumerator();
                                    while (variablesEnum != null && variablesEnum.MoveNext())
                                    {
                                        #region VARIABLE
                                        XmlNode varNode = (XmlNode)variablesEnum.Current;
                                        if (varNode.Name.ToUpperInvariant().Equals("VARIABLE", StringComparison.Ordinal))
                                        {
                                            if (varNode.Attributes.Count > 0)
                                            {
                                                XmlAttribute varAttr = varNode.Attributes["name"];
                                                if (varAttr != null && varAttr.Value != String.Empty)
                                                {
                                                    RDFQueryEngine.AddColumn(result.SelectResults, varAttr.Value);
                                                }
                                                else
                                                {
                                                    throw new RDFModelException("one \"variable\" node was found without, or with empty, \"name\" attribute.");
                                                }
                                            }
                                            else
                                            {
                                                throw new RDFModelException("one \"variable\" node was found without attributes.");
                                            }
                                        }
                                        #endregion
                                    }
                                }
                                else
                                {
                                    throw new RDFModelException("\"head\" node was found without children.");
                                }
                            }
                            #endregion

                            #region RESULTS
                            else if (node.Name.ToUpperInvariant().Equals("RESULTS", StringComparison.Ordinal))
                            {
                                foundResults = true;
                                if (foundHead)
                                {
                                    var resultsEnum = node.ChildNodes.GetEnumerator();
                                    while (resultsEnum != null && resultsEnum.MoveNext())
                                    {
                                        XmlNode resNode = (XmlNode)resultsEnum.Current;

                                        #region RESULT
                                        if (resNode.Name.ToUpperInvariant().Equals("RESULT", StringComparison.Ordinal))
                                        {
                                            if (resNode.HasChildNodes)
                                            {
                                                var results = new Dictionary <String, String>();
                                                var bdgEnum = resNode.ChildNodes.GetEnumerator();
                                                while (bdgEnum != null && bdgEnum.MoveNext())
                                                {
                                                    XmlNode bdgNode  = (XmlNode)bdgEnum.Current;
                                                    Boolean foundUri = false;
                                                    Boolean foundLit = false;

                                                    #region BINDING
                                                    if (bdgNode.Name.ToUpperInvariant().Equals("BINDING", StringComparison.Ordinal))
                                                    {
                                                        if (bdgNode.Attributes != null && bdgNode.Attributes.Count > 0)
                                                        {
                                                            XmlAttribute varAttr = bdgNode.Attributes["name"];
                                                            if (varAttr != null && varAttr.Value != String.Empty)
                                                            {
                                                                if (bdgNode.HasChildNodes)
                                                                {
                                                                    #region URI / BNODE
                                                                    if (bdgNode.FirstChild.Name.ToUpperInvariant().Equals("URI", StringComparison.Ordinal) ||
                                                                        bdgNode.FirstChild.Name.ToUpperInvariant().Equals("BNODE", StringComparison.Ordinal))
                                                                    {
                                                                        foundUri = true;
                                                                        if (RDFModelUtilities.GetUriFromString(bdgNode.InnerText) != null)
                                                                        {
                                                                            results.Add(varAttr.Value, bdgNode.InnerText);
                                                                        }
                                                                        else
                                                                        {
                                                                            throw new RDFModelException("one \"uri\" node contained data not corresponding to a valid Uri.");
                                                                        }
                                                                    }
                                                                    #endregion

                                                                    #region LITERAL
                                                                    else if (bdgNode.FirstChild.Name.ToUpperInvariant().Equals("LITERAL", StringComparison.Ordinal))
                                                                    {
                                                                        foundLit = true;
                                                                        if (bdgNode.FirstChild.Attributes != null && bdgNode.FirstChild.Attributes.Count > 0)
                                                                        {
                                                                            XmlAttribute litAttr = bdgNode.FirstChild.Attributes["datatype"];
                                                                            if (litAttr != null && litAttr.Value != String.Empty)
                                                                            {
                                                                                results.Add(varAttr.Value, bdgNode.FirstChild.InnerText + "^^" + litAttr.Value);
                                                                            }
                                                                            else
                                                                            {
                                                                                litAttr = bdgNode.FirstChild.Attributes[RDFVocabulary.XML.PREFIX + ":lang"];
                                                                                if (litAttr != null && litAttr.Value != String.Empty)
                                                                                {
                                                                                    results.Add(varAttr.Value, bdgNode.FirstChild.InnerText + "@" + litAttr.Value);
                                                                                }
                                                                                else
                                                                                {
                                                                                    throw new RDFModelException("one \"literal\" node was found with attribute different from \"datatype\" or \"xml:lang\".");
                                                                                }
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            results.Add(varAttr.Value, bdgNode.InnerText);
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    throw new RDFQueryException("one \"binding\" node was found without children.");
                                                                }
                                                            }
                                                            else
                                                            {
                                                                throw new RDFQueryException("one \"binding\" node was found without, or with empty, \"name\" attribute.");
                                                            }
                                                        }
                                                        else
                                                        {
                                                            throw new RDFQueryException("one \"binding\" node was found without attributes.");
                                                        }
                                                    }
                                                    #endregion

                                                    if (!foundUri && !foundLit)
                                                    {
                                                        throw new RDFQueryException("one \"binding\" node was found without mandatory child \"uri\" or \"literal\".");
                                                    }
                                                }
                                                RDFQueryEngine.AddRow(result.SelectResults, results);
                                            }
                                        }
                                        #endregion
                                    }
                                }
                                else
                                {
                                    throw new RDFModelException("\"head\" node was not found, or was after \"results\" node.");
                                }
                            }
                            #endregion
                        }

                        if (!foundHead)
                        {
                            throw new RDFStoreException("mandatory \"head\" node was not found");
                        }
                        if (!foundResults)
                        {
                            throw new RDFStoreException("mandatory \"results\" node was not found");
                        }
                        #endregion
                    }
                }
                return(result);

                #endregion
            }
            catch (Exception ex)
            {
                throw new RDFQueryException("Cannot read given \"SPARQL Query Results XML Format\" source because: " + ex.Message, ex);
            }
        }