2using System.Collections.Generic;
27 public const string Namespace =
"http://www.w3.org/2005/sparql-results#";
34 private readonly Dictionary<string, ISemanticLiteral> dataTypes =
new Dictionary<string, ISemanticLiteral>();
35 private readonly Uri baseUri;
77 Xml.DocumentElement is
null ||
78 Xml.DocumentElement.LocalName !=
LocalName ||
79 Xml.DocumentElement.NamespaceURI !=
Namespace)
81 throw new ArgumentException(
"Invalid SPARQL Result XML document.", nameof(Xml));
84 this.baseUri = BaseUri;
85 this.BooleanResult =
null;
87 List<string>
Variables =
new List<string>();
88 List<Uri>
Links =
new List<Uri>();
89 List<ISparqlResultRecord>
Records =
new List<ISparqlResultRecord>();
91 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
93 if (!(N is XmlElement E) || E.NamespaceURI !=
Namespace)
99 foreach (XmlNode N2
in E.ChildNodes)
101 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
104 switch (E2.LocalName)
108 if (!
string.IsNullOrEmpty(Name))
114 if (
string.IsNullOrEmpty(HRef))
117 if (this.baseUri is
null)
119 if (Uri.TryCreate(HRef, UriKind.RelativeOrAbsolute, out Uri Link))
124 if (Uri.TryCreate(
this.baseUri, HRef, out Uri Link))
134 this.BooleanResult = b;
138 foreach (XmlNode N2
in E.ChildNodes)
140 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
143 switch (E2.LocalName)
146 Dictionary<string, ISparqlResultItem> Record =
new Dictionary<string, ISparqlResultItem>();
149 foreach (XmlNode N3
in E2.ChildNodes)
151 if (!(N3 is XmlElement E3) || E3.NamespaceURI !=
Namespace)
154 switch (E3.LocalName)
173 this.Links =
Links.ToArray();
174 this.Records =
Records.ToArray();
186 throw new ArgumentException(
"Invalid SPARQL Result JSON document.", nameof(Obj));
188 this.baseUri = BaseUri;
189 this.BooleanResult =
null;
191 List<string>
Variables =
new List<string>();
192 List<Uri>
Links =
new List<Uri>();
193 List<ISparqlResultRecord>
Records =
new List<ISparqlResultRecord>();
195 foreach (KeyValuePair<string, object> P
in Obj)
200 if (!(P.Value is Dictionary<string, object> Head))
203 foreach (KeyValuePair<string, object> P2
in Head)
208 if (P2.Value is Array VariableArray)
210 foreach (
object Item
in VariableArray)
212 if (Item is
string Name)
219 if (P2.Value is Array LinkArray)
221 foreach (
object Item
in LinkArray)
223 if (Item is
string HRef &&
224 !
string.IsNullOrEmpty(HRef))
226 if (this.baseUri is
null)
228 if (Uri.TryCreate(HRef, UriKind.RelativeOrAbsolute, out Uri Link))
233 if (Uri.TryCreate(
this.baseUri, HRef, out Uri Link))
245 if (P.Value is
bool b)
246 this.BooleanResult = b;
250 if (!(P.Value is Dictionary<string, object> Results))
253 foreach (KeyValuePair<string, object> P2
in Results)
255 if (P2.Key !=
"bindings")
258 if (!(P2.Value is Array Bindings))
261 foreach (
object BindingObj
in Bindings)
263 if (!(BindingObj is Dictionary<string, object> Binding))
266 Dictionary<string, ISparqlResultItem> Record =
new Dictionary<string, ISparqlResultItem>();
269 foreach (KeyValuePair<string, object> P3
in Binding)
283 this.Links =
Links.ToArray();
284 this.Records =
Records.ToArray();
289 foreach (XmlNode N
in Xml.ChildNodes)
291 if (!(N is XmlElement E) || E.NamespaceURI !=
Namespace)
297 if (this.baseUri is
null)
299 if (Uri.TryCreate(E.InnerText, UriKind.RelativeOrAbsolute, out Uri UriValue))
300 return new UriNode(UriValue, E.InnerText);
306 if (Uri.TryCreate(
this.baseUri, E.InnerText, out Uri UriValue))
307 return new UriNode(UriValue, E.InnerText);
316 string s = E.InnerText;
317 string DataType =
null;
318 string Language =
null;
320 foreach (XmlAttribute Attr
in E.Attributes)
325 Language = Attr.Value;
329 DataType = Attr.Value;
334 if (!
string.IsNullOrEmpty(DataType))
336 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
341 this.dataTypes[DataType] = LiteralType;
344 return LiteralType.Parse(s, DataType, Language);
346 else if (!
string.IsNullOrEmpty(Language))
356 foreach (XmlNode N2
in E.ChildNodes)
358 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
361 switch (E2.LocalName)
364 Subject = this.ParseXmlValue(E2);
368 Predicate = this.ParseXmlValue(E2);
372 Object = this.ParseXmlValue(E2);
389 if (!(Obj is Dictionary<string, object> Object) ||
390 !(Object.TryGetValue(
"type", out
object TypeObject)) ||
391 !(TypeObject is
string Type))
399 if (!(Object.TryGetValue(
"value", out
object ValueObject)) ||
400 !(ValueObject is
string Value))
405 if (this.baseUri is
null)
407 if (Uri.TryCreate(Value, UriKind.RelativeOrAbsolute, out Uri UriValue))
408 return new UriNode(UriValue, Value);
414 if (Uri.TryCreate(
this.baseUri, Value, out Uri UriValue))
415 return new UriNode(UriValue, Value);
421 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
422 (Value = ValueObject as
string) is
null)
430 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
431 (Value = ValueObject as
string) is
null)
436 if (!Object.TryGetValue(
"datatype", out
object DataTypeObject) ||
437 !(DataTypeObject is
string DataType))
442 if (!Object.TryGetValue(
"xml:lang", out
object LanguageObject) ||
443 !(LanguageObject is
string Language))
448 if (!
string.IsNullOrEmpty(DataType))
450 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
455 this.dataTypes[DataType] = LiteralType;
458 return LiteralType.Parse(Value, DataType, Language);
460 else if (!
string.IsNullOrEmpty(Language))
466 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
467 !(ValueObject is Dictionary<string,object> Triple))
476 foreach (KeyValuePair<string, object> P
in Triple)
481 TripleSubject = this.ParseJsonValue(P.Value);
485 TriplePredicate = this.ParseJsonValue(P.Value);
489 TripleObject = this.ParseJsonValue(P.Value);
494 return new SemanticTriple(TripleSubject, TriplePredicate, TripleObject);
507 this.Variables =
null;
511 this.BooleanResult = Result;
526 this.BooleanResult =
null;
561 return new BooleanMatrix(
new bool[1, 1] { { this.BooleanResult.Value } });
564 int Rows = this.
Records.Length;
568 for (y = 0; y < Rows; y++)
572 for (x = 0; x < Columns; x++)
578 ColumnNames = this.Variables
Helps with parsing of commong data types.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Represents a custom literal.
Represents a string literal.
Implements a semantic triple.
Contains semantic information stored in an RDF document.
Contains a record from the results of a SPARQL query.
Contains an item in a record from the results of a SPARQL query.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
bool? BooleanResult
Any Boolean result returned.
const string LocalName
sparql
bool Pretty
If pretty output is desired.
string[] Variables
Names of variables in result set.
ISparqlResultRecord[] Records
Records in result set.
SparqlResultSet(string Xml, Uri BaseUri)
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/
SparqlResultSet(bool Result)
Contains the results of a SPARQL query.
IMatrix ToMatrix()
Converts the object to a matrix.
IElement ToVector()
Converts the object to a vector.
SparqlResultSet(string[] Variables, Uri[] Links, ISparqlResultRecord[] Records)
Contains the results of a SPARQL query.
SparqlResultSet(XmlDocument Xml)
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/
Uri[] Links
Links to additional metadata about result set.
SparqlResultSet(string Xml)
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/
SparqlResultSet(XmlDocument Xml, Uri BaseUri)
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/
SparqlResultSet(Dictionary< string, object > Obj, Uri BaseUri)
Contains the results of a SPARQL query. https://www.w3.org/TR/sparql12-results-json/
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Static class that dynamically manages types and interfaces available in the runtime environment.
static readonly ObjectValue Null
Null value.
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Interface for semantic nodes.
Interface for semantic literals.
Interface for result records of a SPARQL query.
Basic interface for all types of elements.
Basic interface for matrices.
Interface for objects that can be converted into matrices.
Interface for objects that can be converted into matrices.