RDFAskResult is a container for SPARQL "ASK" query results.
Exemplo n.º 1
0
        /// <summary>
        /// Applies the query to the given store
        /// </summary>
        public RDFAskQueryResult ApplyToStore(RDFStore store)
        {
            if (store != null)
            {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFAskQueryResult askResult = new RDFAskQueryResult();
                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
                        RDFAskQueryEngine.EvaluatePatterns(this, patternGroup, store);

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

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

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

                    //Step 5: Transform the result into a boolean response
                    askResult.AskResult = (queryResultTable.Rows.Count > 0);
                }

                return(askResult);
            }
            throw new RDFQueryException("Cannot execute ASK query because given \"store\" parameter is null.");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Applies the query to the given SPARQL endpoint
        /// </summary>
        public RDFAskQueryResult ApplyToSPARQLEndpoint(RDFSPARQLEndpoint sparqlEndpoint)
        {
            RDFAskQueryResult askResult = new RDFAskQueryResult();

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

                    //Parse response from SPARQL endpoint
                    if (sparqlResponse != null)
                    {
                        using (var sStream = new MemoryStream(sparqlResponse))
                        {
                            askResult = RDFAskQueryResult.FromSparqlXmlResult(sStream);
                        }
                    }
                }
            }
            return(askResult);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Applies the query to the given datasource
        /// </summary>
        internal RDFAskQueryResult ApplyToDataSource(RDFDataSource datasource)
        {
            this.PatternGroupResultTables.Clear();
            this.PatternResultTables.Clear();

            RDFAskQueryResult askResult = new RDFAskQueryResult();

            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
                var queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false);

                //Step 5: Transform the result into a boolean response
                askResult.AskResult = (queryResultTable.Rows.Count > 0);
            }

            return(askResult);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies the query to the given federation
        /// </summary>
        public RDFAskQueryResult ApplyToFederation(RDFFederation federation)
        {
            if (federation != null)
            {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFAskQueryResult askResult = new RDFAskQueryResult();
                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
                            RDFAskQueryEngine.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
                        RDFAskQueryEngine.CombinePatterns(this, patternGroup);

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

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

                    //Step 6: Transform the result into a boolean response
                    askResult.AskResult = (queryResultTable.Rows.Count > 0);
                }

                return(askResult);
            }
            throw new RDFQueryException("Cannot execute ASK query because given \"federation\" parameter is null.");
        }
Exemplo n.º 5
0
        /// <summary>
        /// Applies the query to the given federation
        /// </summary>
        public RDFAskQueryResult ApplyToFederation(RDFFederation federation)
        {
            if (federation != null) {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFAskQueryResult askResult    = new RDFAskQueryResult();
                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: Transform the result into a boolean response
                    askResult.AskResult        = (queryResultTable.Rows.Count > 0);

                }

                return askResult;
            }
            throw new RDFQueryException("Cannot execute ASK query because given \"federation\" parameter is null.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads the given "SPARQL Query Results XML Format" stream into an ASK query result
        /// </summary>
        public static RDFAskQueryResult FromSparqlXmlResult(Stream inputStream)
        {
            try {
                #region deserialize
                RDFAskQueryResult result = new RDFAskQueryResult();
                using (StreamReader streamReader = new StreamReader(inputStream, Encoding.UTF8)) {
                    using (XmlTextReader xmlReader = new XmlTextReader(streamReader)) {
                        xmlReader.DtdProcessing = DtdProcessing.Parse;
                        xmlReader.Normalization = false;

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

                        #region parse
                        Boolean foundHead    = false;
                        Boolean foundBoolean = 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;
                            }
                            #endregion

                            #region BOOLEAN
                            else if (node.Name.ToUpperInvariant().Equals("BOOLEAN", StringComparison.Ordinal))
                            {
                                foundBoolean = true;
                                if (foundHead)
                                {
                                    Boolean bRes = false;
                                    if (Boolean.TryParse(node.InnerText, out bRes))
                                    {
                                        result.AskResult = bRes;
                                    }
                                    else
                                    {
                                        throw new Exception("\"boolean\" node contained data not corresponding to a valid Boolean.");
                                    }
                                }
                                else
                                {
                                    throw new Exception("\"head\" node was not found, or was after \"boolean\" node.");
                                }
                            }
                            #endregion
                        }

                        if (!foundHead)
                        {
                            throw new Exception("mandatory \"head\" node was not found");
                        }
                        if (!foundBoolean)
                        {
                            throw new Exception("mandatory \"boolean\" 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);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Reads the given "SPARQL Query Results XML Format" stream into an ASK query result
        /// </summary>
        public static RDFAskQueryResult FromSparqlXmlResult(Stream inputStream)
        {
            try {

                #region deserialize
                RDFAskQueryResult result          = new RDFAskQueryResult();
                using(StreamReader streamReader   = new StreamReader(inputStream, Encoding.UTF8)) {
                    using(XmlTextReader xmlReader = new XmlTextReader(streamReader)) {
                        xmlReader.DtdProcessing   = DtdProcessing.Ignore;
                        xmlReader.Normalization   = false;

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

                        #region parse
                        Boolean foundHead         = false;
                        Boolean foundBoolean      = 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;
                            }
                            #endregion

                            #region BOOLEAN
                            else if (node.Name.ToUpperInvariant().Equals("BOOLEAN", StringComparison.Ordinal)) {
                                foundBoolean      = true;
                                if (foundHead) {
                                    Boolean bRes  = false;
                                    if (Boolean.TryParse(node.InnerText, out bRes)) {
                                        result.AskResult = bRes;
                                    }
                                    else {
                                        throw new Exception("\"boolean\" node contained data not corresponding to a valid Boolean.");
                                    }
                                }
                                else {
                                    throw new Exception("\"head\" node was not found, or was after \"boolean\" node.");
                                }
                            }
                            #endregion

                        }

                        if (!foundHead) {
                             throw new Exception("mandatory \"head\" node was not found");
                        }
                        if (!foundBoolean) {
                             throw new Exception("mandatory \"boolean\" 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);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Applies the query to the given store 
        /// </summary>
        public RDFAskQueryResult ApplyToStore(RDFStore store) {
            if (store != null) {
                this.PatternGroupResultTables.Clear();
                this.PatternResultTables.Clear();

                RDFAskQueryResult askResult    = new RDFAskQueryResult();
                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
                        RDFAskQueryEngine.EvaluatePatterns(this, patternGroup, store);

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

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

                    }

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

                    //Step 5: Transform the result into a boolean response 
                    askResult.AskResult        = (queryResultTable.Rows.Count > 0);

                }

                return askResult;
            }
            throw new RDFQueryException("Cannot execute ASK query because given \"store\" parameter is null.");
        }