-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSparqlResultSetTsvEncoder.cs
More file actions
174 lines (154 loc) · 5.03 KB
/
SparqlResultSetTsvEncoder.cs
File metadata and controls
174 lines (154 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Text;
using System.Threading.Tasks;
using Waher.Content.Semantic.Model;
using Waher.Content.Text;
using Waher.Runtime.Inventory;
using Waher.Script.Abstraction.Elements;
using Waher.Script.Objects.Matrices;
namespace Waher.Content.Semantic
{
/// <summary>
/// Encoder of semantic information from SPARQL queries using TSV.
/// https://www.w3.org/TR/sparql12-results-csv-tsv/
/// </summary>
public class SparqlResultSetTsvEncoder : IContentEncoder
{
/// <summary>
/// Encoder and Decoder of semantic information from SPARQL queries using TSV.
/// </summary>
public SparqlResultSetTsvEncoder()
{
}
/// <summary>
/// Supported Internet Content Types.
/// </summary>
public string[] ContentTypes => Array.Empty<string>();
/// <summary>
/// Supported file extensions.
/// </summary>
public string[] FileExtensions => Array.Empty<string>();
/// <summary>
/// If the encoder encodes a specific object.
/// </summary>
/// <param name="Object">Object to encode.</param>
/// <param name="Grade">How well the encoder supports the given object.</param>
/// <param name="AcceptedContentTypes">Accepted content types.</param>
/// <returns>If the encoder encodes the given object.</returns>
public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
{
if (Object is SparqlResultSet &&
InternetContent.IsAccepted(TsvCodec.TsvContentTypes, AcceptedContentTypes))
{
Grade = Grade.Excellent;
return true;
}
else if (Object is ObjectMatrix M && M.HasColumnNames &&
InternetContent.IsAccepted(TsvCodec.TsvContentTypes, AcceptedContentTypes))
{
Grade = Grade.Ok;
return true;
}
else if (Object is bool &&
InternetContent.IsAccepted(TsvCodec.TsvContentTypes, AcceptedContentTypes))
{
Grade = Grade.Barely;
return true;
}
else
{
Grade = Grade.NotAtAll;
return false;
}
}
/// <summary>
/// Encodes an object
/// </summary>
/// <param name="Object">Object to encode</param>
/// <param name="Encoding">Encoding</param>
/// <param name="Progress">Optional progress reporting of encoding/decoding. Can be null.</param>
/// <param name="AcceptedContentTypes">Accepted content types.</param>
/// <returns>Encoded object.</returns>
public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
{
string Text;
if (Encoding is null)
Encoding = Encoding.UTF8;
if (Object is SparqlResultSet Result)
{
if (Result.BooleanResult.HasValue)
{
string[][] Records = new string[1][];
Records[0] = new string[] { CommonTypes.Encode(Result.BooleanResult.Value) };
Text = TSV.Encode(Records);
}
else
{
IMatrix M = Result.ToMatrix(false);
if (M is ObjectMatrix OM && !(OM.ColumnNames is null))
{
int i, c = OM.ColumnNames.Length;
for (i = 0; i < c; i++)
OM.ColumnNames[i] = "?" + OM.ColumnNames[i];
}
Text = TSV.Encode(M);
}
}
else if (Object is ObjectMatrix M)
Text = TSV.Encode(M, ElementToString, false);
else if (Object is bool b)
{
string[][] Records = new string[1][];
Records[0] = new string[] { CommonTypes.Encode(b) };
Text = TSV.Encode(Records);
}
else
return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object))));
byte[] Bin = Encoding.GetBytes(Text);
string ContentType = TsvCodec.TsvContentTypes[0] + "; charset=" + Encoding.WebName;
return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
}
private static string ElementToString(IElement E)
{
object Obj = E.AssociatedObjectValue;
if (Obj is SemanticTriple Triple)
{
StringBuilder sb = new StringBuilder();
sb.Append("<<");
sb.Append(ElementToString(Triple.Subject));
sb.Append(' ');
sb.Append(ElementToString(Triple.Predicate));
sb.Append(' ');
sb.Append(ElementToString(Triple.Object));
sb.Append(">>");
return sb.ToString();
}
else if (Obj is ISemanticElement)
return Obj.ToString();
else
return JSON.Encode(Obj?.ToString() ?? string.Empty);
}
/// <summary>
/// Tries to get the content type of content of a given file extension.
/// </summary>
/// <param name="FileExtension">File Extension</param>
/// <param name="ContentType">Content Type, if recognized.</param>
/// <returns>If File Extension was recognized and Content Type found.</returns>
public bool TryGetContentType(string FileExtension, out string ContentType)
{
ContentType = null;
return false;
}
/// <summary>
/// Tries to get the file extension of content of a given content type.
/// </summary>
/// <param name="ContentType">Content Type</param>
/// <param name="FileExtension">File Extension, if recognized.</param>
/// <returns>If Content Type was recognized and File Extension found.</returns>
public bool TryGetFileExtension(string ContentType, out string FileExtension)
{
FileExtension = null;
return false;
}
}
}