Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlResultSetImageEncoder.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
8
10{
15 {
20 {
21 }
22
26 public string[] ContentTypes => Array.Empty<string>();
27
31 public string[] FileExtensions => Array.Empty<string>();
32
40 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
41 {
42 if (Object is SparqlResultSet &&
43 (InternetContent.IsAccepted(ImageCodec.ContentTypeSvg, AcceptedContentTypes) ||
44 InternetContent.IsAccepted(ImageCodec.ContentTypePng, AcceptedContentTypes) ||
46 {
47 Grade = Grade.Ok;
48 return true;
49 }
50 else
51 {
52 Grade = Grade.NotAtAll;
53 return false;
54 }
55 }
56
65 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
66 {
67 if (!(Object is SparqlResultSet Result))
68 return new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object)));
69
70 if ((Result.Variables?.Length ?? 0) != 3)
71 return new ContentResponse(new ArgumentException("Graphs can only be generated for semantic-triple result sets.", nameof(Object)));
72
74 string SubjectVariable = Result.Variables[0];
75 string PredicateVariable = Result.Variables[1];
76 string ObjectVariable = Result.Variables[2];
77
78 foreach (ISparqlResultRecord Record in Result.Records)
79 {
80 ISemanticElement Subject = Record[SubjectVariable];
81 ISemanticElement Predicate = Record[PredicateVariable];
82 ISemanticElement Object2 = Record[ObjectVariable];
83
84 if (Subject is null || Predicate is null || Object2 is null)
85 return new ContentResponse(new ArgumentException("Graphs can only be generated for semantic-triple result sets.", nameof(Object)));
86
87 Cube.Add(Subject, Predicate, Object2);
88 }
89
90 string Graph = GraphVizUtilities.GenerateGraph(Cube, string.Empty);
91 string ContentType;
92 byte[] Bin;
93
94 if (InternetContent.IsAccepted(ImageCodec.ContentTypeSvg, AcceptedContentTypes))
95 {
96 Bin = await GraphVizUtilities.DotToImage(Graph, ResultType.Svg);
97 ContentType = ImageCodec.ContentTypeSvg;
98 }
99 else if (InternetContent.IsAccepted(ImageCodec.ContentTypePng, AcceptedContentTypes))
100 {
101 Bin = await GraphVizUtilities.DotToImage(Graph, ResultType.Png);
102 ContentType = ImageCodec.ContentTypePng;
103 }
104 else if (InternetContent.IsAccepted(GraphVizCodec.DefaultContentType, AcceptedContentTypes))
105 {
106 if (Encoding is null)
107 {
108 ContentType = GraphVizCodec.DefaultContentType + "; charset=utf-8";
109 Encoding = Encoding.UTF8;
110 }
111 else
112 ContentType = GraphVizCodec.DefaultContentType + "; charset=" + Encoding.WebName;
113
114 Bin = Encoding.GetBytes(Graph);
115 }
116 else
117 return new ContentResponse(new ArgumentException("Requestd Content-Type not acceptable.", nameof(AcceptedContentTypes)));
118
119 return new ContentResponse(ContentType, Object, Bin);
120 }
121
128 public bool TryGetContentType(string FileExtension, out string ContentType)
129 {
130 ContentType = null;
131 return false;
132 }
133
140 public bool TryGetFileExtension(string ContentType, out string FileExtension)
141 {
142 FileExtension = null;
143 return false;
144 }
145 }
146}
Contains information about a response to a content request.
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypePng
image/png
Definition: ImageCodec.cs:30
const string ContentTypeSvg
image/svg+xml
Definition: ImageCodec.cs:45
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.
const string DefaultContentType
text/vnd.graphviz
static Task< byte[]> DotToImage(string GraphText, ResultType ResultType)
Converts a GraphViz dot graph description to an image.
static string GenerateGraph(ISemanticModel Graph, string Title)
Generates a GraphViz dot graph from a semantic graph.
Encoder of semantic information from SPARQL queries as images.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
async 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.
SparqlResultSetImageEncoder()
Encoder and Decoder of semantic information from SPARQL queries using HTML.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
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.
Interface for result records of a SPARQL query.
ResultType
Image types supported by GraphViz
Definition: GraphViz.cs:35
Grade
Grade enumeration
Definition: Grade.cs:7