Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphStoreFileSource.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
9using Waher.Things;
10
12{
17 {
18 private readonly GraphReference reference;
19
24 {
25 this.reference = Reference;
26 }
27
33 public Grade Supports(Uri _)
34 {
35 return Grade.NotAtAll; // Explicitly selected by processor.
36 }
37
46 public Task<ISemanticCube> LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound,
47 RequestOrigin Caller)
48 {
49 // TODO: Check access privileges
50
51 return this.LoadGraph(Source, NullIfNotFound);
52 }
53
60 public async Task<ISemanticCube> LoadGraph(Uri Source, bool NullIfNotFound)
61 {
62 string[] Files = Directory.GetFiles(this.reference.Folder, "*.*", SearchOption.TopDirectoryOnly);
63 ISemanticCube Result = null;
64 InMemorySemanticCube Union = null;
65
66 foreach (string FileName in Files)
67 {
68 string Extension = Path.GetExtension(FileName);
69 string ContentType = InternetContent.GetContentType(Extension);
70 if (string.IsNullOrEmpty(ContentType) || ContentType == BinaryCodec.DefaultContentType)
71 continue;
72
73 byte[] Bin = await Runtime.IO.Files.ReadAllBytesAsync(FileName);
74 ContentResponse Content = await InternetContent.DecodeAsync(ContentType, Bin, Source);
75 Content.AssertOk();
76
77 if (Union is null && !(Result is null))
78 {
79 Union = new InMemorySemanticCube();
80 await Union.Add(Result);
81 }
82
83 if (Union is null)
84 {
85 Result = Content.Decoded as ISemanticCube;
86 if (Result is null)
87 {
88 if (Content.Decoded is ISemanticModel Model)
89 Result = await InMemorySemanticCube.Create(Model);
90 else
91 continue;
92 }
93 }
94 else
95 {
96 if (Content.Decoded is ISemanticModel Model)
97 await Union.Add(Model);
98 }
99 }
100
101 return Union ?? Result ?? (NullIfNotFound ? null : new InMemorySemanticCube());
102 }
103
104 }
105}
const string DefaultContentType
text/plain
Definition: BinaryCodec.cs:24
Contains information about a response to a content request.
object Decoded
Decoded object.
void AssertOk()
Asserts response is OK.
Static class managing encoding and decoding of internet content.
static string GetContentType(string FileExtension)
Gets the content type of an item, given its file extension. It uses the TryGetContentType to see if a...
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Contains a reference to a graph in the graph store.
Graph source in the local graph store, based on files.
async Task< ISemanticCube > LoadGraph(Uri Source, bool NullIfNotFound)
Loads the graph
Grade Supports(Uri _)
How well a source with a given URI can be loaded by the class.
GraphStoreFileSource(GraphReference Reference)
Graph source in the local graph store, based on files.
Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for semantic cubes.
Interface for semantic models.
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7