/// <summary> /// Applies the query to the given store /// </summary> public RDFSelectQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selectResult = new RDFSelectQueryResult(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 RDFSelectQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Get the result table of the current pattern group RDFSelectQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFSelectQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 5: Apply the modifiers of the query to the result table selectResult.SelectResults = RDFSelectQueryEngine.ApplyModifiers(this, queryResultTable); } return(selectResult); } throw new RDFQueryException("Cannot execute SELECT query because given \"store\" parameter is null."); }
/// <summary> /// Applies the query to the given datasource /// </summary> internal RDFSelectQueryResult ApplyToDataSource(RDFDataSource datasource) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selResult = new RDFSelectQueryResult(); 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: Apply the modifiers of the query to the result table selResult.SelectResults = RDFQueryEngine.ApplyModifiers(this, queryResultTable); } return(selResult); }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFSelectQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selectResult = new RDFSelectQueryResult(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 RDFSelectQueryEngine.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 RDFSelectQueryEngine.CombinePatterns(this, patternGroup); //Step 4: Apply the filters of the current pattern group to its result table RDFSelectQueryEngine.ApplyFilters(this, patternGroup); } //Step 5: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 6: Apply the modifiers of the query to the result table selectResult.SelectResults = RDFSelectQueryEngine.ApplyModifiers(this, queryResultTable); } return(selectResult); } throw new RDFQueryException("Cannot execute SELECT query because given \"federation\" parameter is null."); }
/// <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> /// 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) { RDFQueryUtilities.AddColumn(result.SelectResults, varAttr.Value); } else { throw new Exception("one \"variable\" node was found without, or with empty, \"name\" attribute."); } } else { throw new Exception("one \"variable\" node was found without attributes."); } } #endregion } } else { throw new Exception("\"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 Exception("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 Exception("one \"literal\" node was found with attribute different from \"datatype\" or \"xml:lang\"."); } } } else { results.Add(varAttr.Value, bdgNode.InnerText); } } #endregion } else { throw new Exception("one \"binding\" node was found without children."); } } else { throw new Exception("one \"binding\" node was found without, or with empty, \"name\" attribute."); } } else { throw new Exception("one \"binding\" node was found without attributes."); } } #endregion if (!foundUri && !foundLit) { throw new Exception("one \"binding\" node was found without mandatory child \"uri\" or \"literal\"."); } } RDFQueryUtilities.AddRow(result.SelectResults, results); } } #endregion } } else { throw new Exception("\"head\" node was not found, or was after \"results\" node."); } } #endregion } if (!foundHead) { throw new Exception("mandatory \"head\" node was not found"); } if (!foundResults) { throw new Exception("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); } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFSelectQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selectResult = new RDFSelectQueryResult(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 RDFSelectQueryEngine.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 RDFSelectQueryEngine.CombinePatterns(this, patternGroup); //Step 4: Apply the filters of the current pattern group to its result table RDFSelectQueryEngine.ApplyFilters(this, patternGroup); } //Step 5: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList<DataTable>(), false); //Step 6: Apply the modifiers of the query to the result table selectResult.SelectResults = RDFSelectQueryEngine.ApplyModifiers(this, queryResultTable); } return selectResult; } throw new RDFQueryException("Cannot execute SELECT query because given \"federation\" parameter is null."); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFSelectQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selectResult = new RDFSelectQueryResult(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 RDFSelectQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Get the result table of the current pattern group RDFSelectQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFSelectQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList<DataTable>(), false); //Step 5: Apply the modifiers of the query to the result table selectResult.SelectResults = RDFSelectQueryEngine.ApplyModifiers(this, queryResultTable); } return selectResult; } throw new RDFQueryException("Cannot execute SELECT query because given \"store\" parameter is null."); }
/// <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.Ignore; 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) { RDFQueryUtilities.AddColumn(result.SelectResults, varAttr.Value); } else { throw new Exception("one \"variable\" node was found without, or with empty, \"name\" attribute."); } } else { throw new Exception("one \"variable\" node was found without attributes."); } } #endregion } } else { throw new Exception("\"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 Exception("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 Exception("one \"literal\" node was found with attribute different from \"datatype\" or \"xml:lang\"."); } } } else { results.Add(varAttr.Value, bdgNode.InnerText); } } #endregion } else { throw new Exception("one \"binding\" node was found without children."); } } else { throw new Exception("one \"binding\" node was found without, or with empty, \"name\" attribute."); } } else { throw new Exception("one \"binding\" node was found without attributes."); } } #endregion if (!foundUri && !foundLit) { throw new Exception("one \"binding\" node was found without mandatory child \"uri\" or \"literal\"."); } } RDFQueryUtilities.AddRow(result.SelectResults, results); } } #endregion } } else { throw new Exception("\"head\" node was not found, or was after \"results\" node."); } } #endregion } if (!foundHead) { throw new Exception("mandatory \"head\" node was not found"); } if (!foundResults) { throw new Exception("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); } }