RDFConstructQueryResult is a container for SPARQL "CONSTRUCT" query results.
Example #1
0
        /// <summary>
        /// Applies the query to the given datasource
        /// </summary>
        internal RDFConstructQueryResult ApplyToDataSource(RDFDataSource datasource)
        {
            this.PatternGroupResultTables.Clear();
            this.PatternResultTables.Clear();

            RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());

            if (this.PatternGroups.Any())
            {
                //Iterate the pattern groups of the query
                var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >();
                foreach (var patternGroup          in this.PatternGroups)
                {
                    //Step 1: Get the intermediate result tables of the current pattern group
                    if (datasource.IsFederation())
                    {
                        #region TrueFederations
                        foreach (var store         in (RDFFederation)datasource)
                        {
                            //Step FED.1: Evaluate the patterns of the current pattern group on the current store
                            RDFQueryEngine.EvaluatePatterns(this, patternGroup, store);

                            //Step FED.2: Federate the patterns of the current pattern group on the current store
                            if (!fedPatternResultTables.ContainsKey(patternGroup))
                            {
                                fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]);
                            }
                            else
                            {
                                fedPatternResultTables[patternGroup].ForEach(fprt =>
                                                                             fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add));
                            }
                        }
                        this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup];
                        #endregion
                    }
                    else
                    {
                        RDFQueryEngine.EvaluatePatterns(this, patternGroup, datasource);
                    }

                    //Step 2: Get the result table of the current pattern group
                    RDFQueryEngine.CombinePatterns(this, patternGroup);

                    //Step 3: Apply the filters of the current pattern group to its result table
                    RDFQueryEngine.ApplyFilters(this, patternGroup);
                }

                //Step 4: Get the result table of the query
                DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false);

                //Step 5: Fill the templates from the result table
                DataTable filledResultTable = RDFQueryEngine.FillTemplates(this, queryResultTable);

                //Step 6: Apply the modifiers of the query to the result table
                constructResult.ConstructResults = RDFQueryEngine.ApplyModifiers(this, filledResultTable);
            }

            return(constructResult);
        }
        /// <summary>
        /// Builds a query result corresponding to the given graph
        /// </summary>
        public static RDFConstructQueryResult FromRDFGraph(RDFGraph graph) {
            RDFConstructQueryResult result = new RDFConstructQueryResult(String.Empty);
            if(graph != null) {

                //Transform the graph into a datatable and assign it to the query result
                result.ConstructResults    = graph.ToDataTable();

            }
            return result;
        }
Example #3
0
        /// <summary>
        /// Applies the query to the given federation
        /// </summary>
        public RDFConstructQueryResult ApplyToFederation(RDFFederation federation)
        {
            if (federation != null)
            {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());
                if (!this.IsEmpty)
                {
                    //Iterate the pattern groups of the query
                    var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >();
                    foreach (RDFPatternGroup patternGroup in this.PatternGroups)
                    {
                        #region TrueFederations
                        foreach (RDFStore store in federation.Stores.Values)
                        {
                            //Step 1: Evaluate the patterns of the current pattern group on the current store
                            RDFConstructQueryEngine.EvaluatePatterns(this, patternGroup, store);

                            //Step 2: Federate the patterns of the current pattern group on the current store
                            if (!fedPatternResultTables.ContainsKey(patternGroup))
                            {
                                fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]);
                            }
                            else
                            {
                                fedPatternResultTables[patternGroup].ForEach(fprt =>
                                                                             fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add));
                            }
                        }
                        this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup];
                        #endregion

                        //Step 3: Get the result table of the current pattern group
                        RDFConstructQueryEngine.CombinePatterns(this, patternGroup);

                        //Step 4: Apply the filters of the current pattern group to its result table
                        RDFConstructQueryEngine.ApplyFilters(this, patternGroup);
                    }

                    //Step 5: Get the result table of the query
                    DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false);

                    //Step 6: Fill the templates from the result table
                    DataTable filledResultTable = RDFConstructQueryEngine.FillTemplates(this, queryResultTable);

                    //Step 7: Apply the modifiers of the query to the result table
                    constructResult.ConstructResults = RDFConstructQueryEngine.ApplyModifiers(this, filledResultTable);
                }

                return(constructResult);
            }
            throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"federation\" parameter is null.");
        }
        /// <summary>
        /// Gets a query result corresponding to the given graph
        /// </summary>
        public static RDFConstructQueryResult FromRDFGraph(RDFGraph graph)
        {
            RDFConstructQueryResult result = new RDFConstructQueryResult(string.Empty);

            if (graph != null)
            {
                //Transform the graph into a datatable and assign it to the query result
                result.ConstructResults = graph.ToDataTable();
            }
            return(result);
        }
Example #5
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();
                    }
                }

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

                RDFQueryEvents.RaiseCONSTRUCTQueryEvaluation(String.Format("Evaluated CONSTRUCT query on SPARQL endpoint '{0}': Found '{1}' results.", sparqlEndpoint, constructResult.ConstructResultsCount));
            }
            return(constructResult);
        }
        /// <summary>
        /// Applies the query to the given SPARQL endpoint
        /// </summary>
        public RDFConstructQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint)
        {
            string constructQueryString             = this.ToString();
            RDFConstructQueryResult constructResult = new RDFConstructQueryResult(constructQueryString);

            if (sparqlEndpoint != null)
            {
                //Establish a connection to the given SPARQL endpoint
                using (WebClient webClient = new WebClient())
                {
                    //Insert reserved "query" parameter
                    webClient.QueryString.Add("query", HttpUtility.UrlEncode(constructQueryString));

                    //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
                    byte[] sparqlResponse = webClient.DownloadData(sparqlEndpoint.BaseAddress);

                    //Parse response from SPARQL endpoint
                    if (sparqlResponse != null)
                    {
                        using (MemoryStream sStream = new MemoryStream(sparqlResponse))
                            constructResult = RDFConstructQueryResult.FromRDFGraph(RDFGraph.FromStream(RDFModelEnums.RDFFormats.Turtle, sStream));
                        constructResult.ConstructResults.TableName = constructQueryString;
                    }
                }

                //Eventually adjust column names (should start with "?")
                int columnsCount = constructResult.ConstructResults.Columns.Count;
                for (int i = 0; i < columnsCount; i++)
                {
                    if (!constructResult.ConstructResults.Columns[i].ColumnName.StartsWith("?"))
                    {
                        constructResult.ConstructResults.Columns[i].ColumnName = string.Concat("?", constructResult.ConstructResults.Columns[i].ColumnName);
                    }
                }
            }
            return(constructResult);
        }
Example #7
0
        /// <summary>
        /// Applies the query to the given store
        /// </summary>
        public RDFConstructQueryResult ApplyToStore(RDFStore store)
        {
            if (store != null)
            {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());
                if (!this.IsEmpty)
                {
                    //Iterate the pattern groups of the query
                    foreach (RDFPatternGroup patternGroup in this.PatternGroups)
                    {
                        //Step 1: Get the intermediate result tables of the current pattern group
                        RDFConstructQueryEngine.EvaluatePatterns(this, patternGroup, store);

                        //Step 2: Get the result table of the current pattern group
                        RDFConstructQueryEngine.CombinePatterns(this, patternGroup);

                        //Step 3: Apply the filters of the current pattern group to its result table
                        RDFConstructQueryEngine.ApplyFilters(this, patternGroup);
                    }

                    //Step 4: Get the result table of the query
                    DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false);

                    //Step 5: Fill the templates from the result table
                    DataTable filledResultTable = RDFConstructQueryEngine.FillTemplates(this, queryResultTable);

                    //Step 6: Apply the modifiers of the query to the result table
                    constructResult.ConstructResults = RDFConstructQueryEngine.ApplyModifiers(this, filledResultTable);
                }

                return(constructResult);
            }
            throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"store\" parameter is null.");
        }
Example #8
0
        /// <summary>
        /// Applies the query to the given store 
        /// </summary>
        public RDFConstructQueryResult ApplyToStore(RDFStore store)
        {
            if (store != null) {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());
                if (this.PatternGroups.Any()) {

                    //Iterate the pattern groups of the query
                    foreach (RDFPatternGroup patternGroup in this.PatternGroups) {

                        //Step 1: Get the intermediate result tables of the current pattern group
                        RDFQueryEngine.EvaluatePatterns(this, patternGroup, store);

                        //Step 2: Get the result table of the current pattern group
                        RDFQueryEngine.CombinePatterns(this, patternGroup);

                        //Step 3: Apply the filters of the current pattern group to its result table
                        RDFQueryEngine.ApplyFilters(this, patternGroup);

                    }

                    //Step 4: Get the result table of the query
                    DataTable queryResultTable          = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false);

                    //Step 5: Fill the templates from the result table
                    DataTable filledResultTable         = RDFQueryEngine.FillTemplates(this, queryResultTable);

                    //Step 6: Apply the modifiers of the query to the result table
                    constructResult.ConstructResults    = RDFQueryEngine.ApplyModifiers(this, filledResultTable);

                }

                return constructResult;
            }
            throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"store\" parameter is null.");
        }
Example #9
0
        /// <summary>
        /// Applies the query to the given federation
        /// </summary>
        public RDFConstructQueryResult ApplyToFederation(RDFFederation federation)
        {
            if (federation != null) {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString());
                if (this.PatternGroups.Any()) {

                    //Iterate the pattern groups of the query
                    var fedPatternResultTables          = new Dictionary<RDFPatternGroup, List<DataTable>>();
                    foreach (RDFPatternGroup patternGroup in this.PatternGroups) {

                        #region TrueFederations
                        foreach (RDFStore store in federation) {

                            //Step 1: Evaluate the patterns of the current pattern group on the current store
                            RDFQueryEngine.EvaluatePatterns(this, patternGroup, store);

                            //Step 2: Federate the patterns of the current pattern group on the current store
                            if (!fedPatternResultTables.ContainsKey(patternGroup)) {
                                 fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]);
                            }
                            else {
                                fedPatternResultTables[patternGroup].ForEach(fprt =>
                                    fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add));
                            }

                        }
                        this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup];
                        #endregion

                        //Step 3: Get the result table of the current pattern group
                        RDFQueryEngine.CombinePatterns(this, patternGroup);

                        //Step 4: Apply the filters of the current pattern group to its result table
                        RDFQueryEngine.ApplyFilters(this, patternGroup);

                    }

                    //Step 5: Get the result table of the query
                    DataTable queryResultTable          = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false);

                    //Step 6: Fill the templates from the result table
                    DataTable filledResultTable         = RDFQueryEngine.FillTemplates(this, queryResultTable);

                    //Step 7: Apply the modifiers of the query to the result table
                    constructResult.ConstructResults    = RDFQueryEngine.ApplyModifiers(this, filledResultTable);

                }

                return constructResult;
            }
            throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"federation\" parameter is null.");
        }