28 public const string Namespace =
"http://www.w3.org/2005/sparql-results#";
35 private readonly Dictionary<string, ISemanticLiteral> dataTypes =
new Dictionary<string, ISemanticLiteral>();
36 private readonly Uri baseUri;
37 private Dictionary<string, string> shortBlankNodeLabels =
null;
38 private Dictionary<string, string> shortUris =
null;
39 private Dictionary<string, SemanticLiteral> literals =
null;
81 Xml.DocumentElement is
null ||
82 Xml.DocumentElement.LocalName !=
LocalName ||
83 Xml.DocumentElement.NamespaceURI !=
Namespace)
85 throw new ArgumentException(
"Invalid SPARQL Result XML document.", nameof(Xml));
88 this.baseUri = BaseUri;
89 this.BooleanResult =
null;
95 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
97 if (!(N is XmlElement E) || E.NamespaceURI !=
Namespace)
103 foreach (XmlNode N2
in E.ChildNodes)
105 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
108 switch (E2.LocalName)
112 if (!
string.IsNullOrEmpty(Name))
118 if (
string.IsNullOrEmpty(HRef))
121 if (this.baseUri is
null)
123 if (Uri.TryCreate(HRef, UriKind.RelativeOrAbsolute, out Uri Link))
128 if (Uri.TryCreate(
this.baseUri, HRef, out Uri Link))
138 this.BooleanResult = b;
142 foreach (XmlNode N2
in E.ChildNodes)
144 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
147 switch (E2.LocalName)
150 Dictionary<string, ISparqlResultItem> Record =
new Dictionary<string, ISparqlResultItem>();
153 foreach (XmlNode N3
in E2.ChildNodes)
155 if (!(N3 is XmlElement E3) || E3.NamespaceURI !=
Namespace)
158 switch (E3.LocalName)
177 this.Links =
Links.ToArray();
178 this.Records =
Records.ToArray();
190 throw new ArgumentException(
"Invalid SPARQL Result JSON document.", nameof(Obj));
192 this.baseUri = BaseUri;
193 this.BooleanResult =
null;
199 foreach (KeyValuePair<string, object> P
in Obj)
204 if (!(P.Value is Dictionary<string, object> Head))
207 foreach (KeyValuePair<string, object> P2
in Head)
212 if (P2.Value is Array VariableArray)
214 foreach (
object Item
in VariableArray)
216 if (Item is
string Name)
223 if (P2.Value is Array LinkArray)
225 foreach (
object Item
in LinkArray)
227 if (Item is
string HRef &&
228 !
string.IsNullOrEmpty(HRef))
230 if (this.baseUri is
null)
232 if (Uri.TryCreate(HRef, UriKind.RelativeOrAbsolute, out Uri Link))
237 if (Uri.TryCreate(
this.baseUri, HRef, out Uri Link))
249 if (P.Value is
bool b)
250 this.BooleanResult = b;
254 if (!(P.Value is Dictionary<string, object> Results))
257 foreach (KeyValuePair<string, object> P2
in Results)
259 if (P2.Key !=
"bindings")
262 if (!(P2.Value is Array Bindings))
265 foreach (
object BindingObj
in Bindings)
267 if (!(BindingObj is Dictionary<string, object> Binding))
270 Dictionary<string, ISparqlResultItem> Record =
new Dictionary<string, ISparqlResultItem>();
273 foreach (KeyValuePair<string, object> P3
in Binding)
287 this.Links =
Links.ToArray();
288 this.Records =
Records.ToArray();
293 foreach (XmlNode N
in Xml.ChildNodes)
295 if (!(N is XmlElement E) || E.NamespaceURI !=
Namespace)
301 if (this.baseUri is
null)
303 if (Uri.TryCreate(E.InnerText, UriKind.RelativeOrAbsolute, out Uri UriValue))
304 return new UriNode(UriValue, E.InnerText);
310 if (Uri.TryCreate(
this.baseUri, E.InnerText, out Uri UriValue))
311 return new UriNode(UriValue, E.InnerText);
320 string s = E.InnerText;
321 string DataType =
null;
322 string Language =
null;
324 foreach (XmlAttribute Attr
in E.Attributes)
329 Language = Attr.Value;
333 DataType = Attr.Value;
338 if (!
string.IsNullOrEmpty(DataType))
340 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
345 this.dataTypes[DataType] = LiteralType;
348 return LiteralType.Parse(s, DataType, Language);
350 else if (!
string.IsNullOrEmpty(Language))
360 foreach (XmlNode N2
in E.ChildNodes)
362 if (!(N2 is XmlElement E2) || E2.NamespaceURI !=
Namespace)
365 switch (E2.LocalName)
368 Subject = this.ParseXmlValue(E2);
372 Predicate = this.ParseXmlValue(E2);
376 Object = this.ParseXmlValue(E2);
393 if (!(Obj is Dictionary<string, object> Object) ||
394 !(Object.TryGetValue(
"type", out
object TypeObject)) ||
395 !(TypeObject is
string Type))
403 if (!(Object.TryGetValue(
"value", out
object ValueObject)) ||
404 !(ValueObject is
string Value))
409 if (this.baseUri is
null)
411 if (Uri.TryCreate(Value, UriKind.RelativeOrAbsolute, out Uri UriValue))
412 return new UriNode(UriValue, Value);
418 if (Uri.TryCreate(
this.baseUri, Value, out Uri UriValue))
419 return new UriNode(UriValue, Value);
425 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
426 (Value = ValueObject as
string) is
null)
434 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
435 (Value = ValueObject as
string) is
null)
440 if (!Object.TryGetValue(
"datatype", out
object DataTypeObject) ||
441 !(DataTypeObject is
string DataType))
446 if (!Object.TryGetValue(
"xml:lang", out
object LanguageObject) ||
447 !(LanguageObject is
string Language))
452 if (!
string.IsNullOrEmpty(DataType))
454 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
459 this.dataTypes[DataType] = LiteralType;
462 return LiteralType.Parse(Value, DataType, Language);
464 else if (!
string.IsNullOrEmpty(Language))
470 if (!(Object.TryGetValue(
"value", out ValueObject)) ||
471 !(ValueObject is Dictionary<string, object> Triple))
480 foreach (KeyValuePair<string, object> P
in Triple)
485 TripleSubject = this.ParseJsonValue(P.Value);
489 TriplePredicate = this.ParseJsonValue(P.Value);
493 TripleObject = this.ParseJsonValue(P.Value);
498 return new SemanticTriple(TripleSubject, TriplePredicate, TripleObject);
511 this.Variables =
null;
515 this.BooleanResult = Result;
530 this.BooleanResult =
null;
575 return new BooleanMatrix(
new bool[1, 1] { { this.BooleanResult.Value } });
578 int Rows = this.
Records.Length;
582 for (y = 0; y < Rows; y++)
586 for (x = 0; x < Columns; x++)
604 ColumnNames = this.Variables
626 if (this.shortBlankNodeLabels is
null)
627 this.shortBlankNodeLabels =
new Dictionary<string, string>();
629 if (!this.shortBlankNodeLabels.TryGetValue(s, out
string Short))
631 Short =
"_:bn" + (this.shortBlankNodeLabels.Count + 1).
ToString();
632 this.shortBlankNodeLabels[s] = Short;
655 if (this.shortUris is
null)
656 this.shortUris =
new Dictionary<string, string>();
658 if (!this.shortUris.TryGetValue(Uri, out
string Short))
661 if (Ontology is
null)
662 this.shortUris[Uri] = Short = Uri;
665 Short = Ontology.OntologyPrefix +
":" +
668 this.shortUris[Uri] = Short;
682 if (this.literals is
null)
683 this.literals =
new Dictionary<string, SemanticLiteral>();
685 int i = this.literals.Count + 1;
686 string Label =
"L" + i.ToString();
688 this.literals[Label] = Literal;
Helps with parsing of commong data types.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
string NodeId
Blank node Node ID.
Represents a custom literal.
Represents a string literal.
Abstract base class for semantic literal values.
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/ ...
IMatrix ToMatrix(bool ShortLinks)
Converts the object to a matrix.
string GetShortBlankNodeLabel(BlankNode Node)
Gets a short blank node label for the given blank node.
bool? BooleanResult
Any Boolean result returned.
const string LocalName
sparql
string GetShortUri(UriNode Node)
Gets a short URI label for the given uri node.
bool Pretty
If pretty output is desired.
string GetShortUri(string Uri)
Gets a short URI label for the given uri node.
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.
string GetLiteralLabel(SemanticLiteral Literal)
Gets a label unique for a literal.
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.
A chunked list is a linked list of chunks of objects of type T .
Static class that dynamically manages types and interfaces available in the runtime environment.
Base class for all types of elements.
static readonly ObjectValue Null
Null value.
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Interface for semantic ontologies.
string OntologyNamespace
Ontology namespace.
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.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.