static private AddRow ( |
||
table | ||
bindings | String>.Dictionary | |
return | void |
/// <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> /// Fills the templates of the given query with data from the given result table /// </summary> internal static DataTable FillTemplates(RDFConstructQuery constructQuery, DataTable resultTable) { //Create the structure of the result datatable DataTable result = new DataTable("CONSTRUCT_RESULTS"); result.Columns.Add("SUBJECT", Type.GetType("System.String")); result.Columns.Add("PREDICATE", Type.GetType("System.String")); result.Columns.Add("OBJECT", Type.GetType("System.String")); result.AcceptChanges(); //Initialize working variables Dictionary <String, String> constructRow = new Dictionary <String, String>(); constructRow.Add("SUBJECT", null); constructRow.Add("PREDICATE", null); constructRow.Add("OBJECT", null); //Iterate on the templates foreach (RDFPattern tp in constructQuery.Templates.Where(tp => tp.Variables.Count == 0 || tp.Variables.TrueForAll(v => resultTable.Columns.Contains(v.ToString())))) { #region GROUND TEMPLATE //Check if the template is ground, so represents an explicit triple to be added as-is if (tp.Variables.Count == 0) { constructRow["SUBJECT"] = tp.Subject.ToString(); constructRow["PREDICATE"] = tp.Predicate.ToString(); constructRow["OBJECT"] = tp.Object.ToString(); RDFQueryUtilities.AddRow(result, constructRow); continue; } #endregion #region NON-GROUND TEMPLATE //Iterate the result datatable's rows to construct the triples of the current template IEnumerator rowsEnum = resultTable.Rows.GetEnumerator(); while (rowsEnum.MoveNext()) { #region SUBJECT //Subject of the template is a variable if (tp.Subject is RDFVariable) { //Check if the template must be skipped, in order to not produce illegal triples //Row contains an unbound value in position of the variable corresponding to the template subject if (((DataRow)rowsEnum.Current).IsNull(tp.Subject.ToString())) { continue; } RDFPatternMember subj = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)rowsEnum.Current)[tp.Subject.ToString()].ToString()); //Row contains a literal in position of the variable corresponding to the template subject if (subj is RDFLiteral) { continue; } //Row contains a resource in position of the variable corresponding to the template subject constructRow["SUBJECT"] = subj.ToString(); } //Subject of the template is a resource else { constructRow["SUBJECT"] = tp.Subject.ToString(); } #endregion #region PREDICATE //Predicate of the template is a variable if (tp.Predicate is RDFVariable) { //Check if the template must be skipped, in order to not produce illegal triples //Row contains an unbound value in position of the variable corresponding to the template predicate if (((DataRow)rowsEnum.Current).IsNull(tp.Predicate.ToString())) { continue; } RDFPatternMember pred = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)rowsEnum.Current)[tp.Predicate.ToString()].ToString()); //Row contains a blank resource or a literal in position of the variable corresponding to the template predicate if ((pred is RDFResource && ((RDFResource)pred).IsBlank) || pred is RDFLiteral) { continue; } //Row contains a non-blank resource in position of the variable corresponding to the template predicate constructRow["PREDICATE"] = pred.ToString(); } //Predicate of the template is a resource else { constructRow["PREDICATE"] = tp.Predicate.ToString(); } #endregion #region OBJECT //Object of the template is a variable if (tp.Object is RDFVariable) { //Check if the template must be skipped, in order to not produce illegal triples //Row contains an unbound value in position of the variable corresponding to the template object if (((DataRow)rowsEnum.Current).IsNull(tp.Object.ToString())) { continue; } RDFPatternMember obj = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)rowsEnum.Current)[tp.Object.ToString()].ToString()); //Row contains a resource or a literal in position of the variable corresponding to the template object constructRow["OBJECT"] = obj.ToString(); } //Object of the template is a resource or a literal else { constructRow["OBJECT"] = tp.Object.ToString(); } #endregion //Insert the triple into the final table RDFQueryUtilities.AddRow(result, constructRow); } #endregion } return(result); }