Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlResultSetTsvCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
10
12{
18 {
23 {
24 }
25
29 public string[] ContentTypes => new string[0];
30
34 public string[] FileExtensions => new string[0];
35
43 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
44 {
45 if (Object is SparqlResultSet &&
47 {
48 Grade = Grade.Excellent;
49 return true;
50 }
51 else if (Object is ObjectMatrix M && M.HasColumnNames &&
53 {
54 Grade = Grade.Ok;
55 return true;
56 }
57 else if (Object is bool &&
59 {
60 Grade = Grade.Barely;
61 return true;
62 }
63 else
64 {
65 Grade = Grade.NotAtAll;
66 return false;
67 }
68 }
69
77 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
78 {
79 string Text;
80
81 if (Encoding is null)
82 Encoding = Encoding.UTF8;
83
84 if (Object is SparqlResultSet Result)
85 {
86 if (Result.BooleanResult.HasValue)
87 {
88 string[][] Records = new string[1][];
89 Records[0] = new string[] { CommonTypes.Encode(Result.BooleanResult.Value) };
90
91 Text = TSV.Encode(Records);
92 }
93 else
94 {
95 IMatrix M = Result.ToMatrix();
96
97 if (M is ObjectMatrix OM && !(OM.ColumnNames is null))
98 {
99 int i, c = OM.ColumnNames.Length;
100
101 for (i = 0; i < c; i++)
102 OM.ColumnNames[i] = "?" + OM.ColumnNames[i];
103 }
104
105 Text = TSV.Encode(M);
106 }
107 }
108 else if (Object is ObjectMatrix M)
109 Text = TSV.Encode(M, ElementToString, false);
110 else if (Object is bool b)
111 {
112 string[][] Records = new string[1][];
113 Records[0] = new string[] { CommonTypes.Encode(b) };
114
115 Text = TSV.Encode(Records);
116 }
117 else
118 throw new ArgumentException("Unable to encode object.", nameof(Object));
119
120 byte[] Bin = Encoding.GetBytes(Text);
121 string ContentType = TsvCodec.TsvContentTypes[0] + "; charset=" + Encoding.WebName;
122
123 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
124 }
125
126 private static string ElementToString(IElement E)
127 {
128 object Obj = E.AssociatedObjectValue;
129
130 if (Obj is SemanticTriple Triple)
131 {
132 StringBuilder sb = new StringBuilder();
133
134 sb.Append("<<");
135 sb.Append(ElementToString(Triple.Subject));
136 sb.Append(' ');
137 sb.Append(ElementToString(Triple.Predicate));
138 sb.Append(' ');
139 sb.Append(ElementToString(Triple.Object));
140 sb.Append(">>");
141
142 return sb.ToString();
143 }
144 else if (Obj is ISemanticElement)
145 return Obj.ToString();
146 else
147 return JSON.Encode(Obj?.ToString() ?? string.Empty);
148 }
149
156 public bool TryGetContentType(string FileExtension, out string ContentType)
157 {
158 ContentType = null;
159 return false;
160 }
161
168 public bool TryGetFileExtension(string ContentType, out string FileExtension)
169 {
170 FileExtension = null;
171 return false;
172 }
173 }
174}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
Encoder and Decoder of semantic information from SPARQL queries using TSV. https://www....
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
string[] ContentTypes
Supported Internet Content Types.
SparqlResultSetTsvCodec()
Encoder and Decoder of semantic information from SPARQL queries using TSV.
string[] FileExtensions
Supported file extensions.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Helps with common TSV-related tasks. (TSV=TAB Separated Values)
Definition: TSV.cs:13
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
Definition: TSV.cs:201
static readonly string[] TsvContentTypes
TSV content types.
Definition: TsvCodec.cs:25
bool HasColumnNames
If the matrix has column names defined.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Interface for semantic nodes.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for matrices.
Definition: IMatrix.cs:9
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.
Grade
Grade enumeration
Definition: Grade.cs:7