Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlResultSetTsvEncoder.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
9
11{
17 {
22 {
23 }
24
28 public string[] ContentTypes => Array.Empty<string>();
29
33 public string[] FileExtensions => Array.Empty<string>();
34
42 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
43 {
44 if (Object is SparqlResultSet &&
46 {
47 Grade = Grade.Excellent;
48 return true;
49 }
50 else if (Object is ObjectMatrix M && M.HasColumnNames &&
52 {
53 Grade = Grade.Ok;
54 return true;
55 }
56 else if (Object is bool &&
58 {
59 Grade = Grade.Barely;
60 return true;
61 }
62 else
63 {
64 Grade = Grade.NotAtAll;
65 return false;
66 }
67 }
68
77 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, 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(false);
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 return Task.FromResult(new ContentResponse(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 ContentResponse(ContentType, Object, Bin));
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:15
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:596
Contains information about a response to a content request.
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:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
Encoder of semantic information from SPARQL queries using TSV. https://www.w3.org/TR/sparql12-results...
string[] ContentTypes
Supported Internet Content Types.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
SparqlResultSetTsvEncoder()
Encoder and Decoder of semantic information from SPARQL queries using TSV.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
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:14
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
Definition: TSV.cs:202
static readonly string[] TsvContentTypes
TSV content types.
Definition: TsvCodec.cs:26
bool HasColumnNames
If the matrix has column names defined.
Interface for reporting progress about an encoding or decoding.
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for matrices.
Definition: IMatrix.cs:7
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.
Grade
Grade enumeration
Definition: Grade.cs:7