Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NodeGraph.cs
1using System;
3using System.Threading.Tasks;
11
13{
17 public class NodeGraph : IDynamicGraph
18 {
19 private readonly INode node;
20
25 public NodeGraph(INode Node)
26 {
27 this.node = Node;
28 }
29
37 {
38 UriNode NodeGraphUriNode = new UriNode(DataSourceGraph.GetNodeUri(this.node));
39
40 Result.Add(NodeGraphUriNode, Rdf.type, IoTConcentrator.Node);
41
42 Result.Add(NodeGraphUriNode, IoTConcentrator.typeName,
43 await this.node.GetTypeNameAsync(Language));
44
45 Result.Add(NodeGraphUriNode, IoTConcentrator.sourceId, this.node.SourceId);
46 Result.Add(NodeGraphUriNode, IoTConcentrator.nodeId, this.node.NodeId);
47 Result.Add(NodeGraphUriNode, IoTConcentrator.logId, this.node.LogId);
48 Result.Add(NodeGraphUriNode, IoTConcentrator.localId, this.node.LocalId);
49 Result.Add(NodeGraphUriNode, RdfSchema.label, this.node.LocalId);
50
51 if (!string.IsNullOrEmpty(this.node.Partition))
52 Result.Add(NodeGraphUriNode, IoTConcentrator.partition, this.node.Partition);
53
54 if (!(this.node.Parent is null))
55 Result.Add(NodeGraphUriNode, IoTConcentrator.parentNode, DataSourceGraph.GetNodeUri(this.node.Parent));
56
57 Result.Add(NodeGraphUriNode, DublinCoreTerms.updated, this.node.LastChanged);
58 Result.Add(NodeGraphUriNode, IoTConcentrator.hasChildSources, this.node.HasChildren);
59 Result.Add(NodeGraphUriNode, IoTConcentrator.hasCommands, this.node.HasCommands);
60 Result.Add(NodeGraphUriNode, IoTConcentrator.isControllable, this.node.IsControllable);
61 Result.Add(NodeGraphUriNode, IoTConcentrator.isReadable, this.node.IsReadable);
62 Result.Add(NodeGraphUriNode, IoTConcentrator.state, this.node.State.ToString(),
64
65 IEnumerable<DisplayableParameters.Parameter> Parameters = await this.node.GetDisplayableParametersAsync(Language, Caller);
67
68 if (!(Parameters is null))
69 {
70 foreach (DisplayableParameters.Parameter P in Parameters)
71 {
72 BlankNode DisplayableParameter = new BlankNode("n" + Guid.NewGuid().ToString());
73 Items.Add(DisplayableParameter);
74
75 Result.Add(DisplayableParameter, RdfSchema.label, P.Name);
76 Result.Add(DisplayableParameter, IoTConcentrator.parameterId, P.Id);
77
78 Result.Add(DisplayableParameter, IoTSensorData.value,
79 SemanticElements.Encapsulate(P.UntypedValue));
80 }
81
82 Result.AddContainer(NodeGraphUriNode, Rdf.Seq, IoTConcentrator.dispParam, Items);
83 Items.Clear();
84 }
85
86 IEnumerable<DisplayableParameters.Message> NodeMessages = await this.node.GetMessagesAsync(Caller);
87 if (!(NodeMessages is null))
88 {
89 foreach (DisplayableParameters.Message M in NodeMessages)
90 {
91 BlankNode Message = new BlankNode("n" + Guid.NewGuid().ToString());
92 Items.Add(Message);
93
94 Result.Add(Message, IoTConcentrator.body, M.Body);
95 Result.Add(Message, IoTSensorData.timestamp, M.Timestamp);
96
97 if (!string.IsNullOrEmpty(M.EventId))
98 Result.Add(Message, IoTConcentrator.eventId, M.EventId);
99
100 Result.Add(Message, Rdf.type, M.Type.ToString(), IoTConcentrator.messageType);
101 }
102
103 Result.AddContainer(NodeGraphUriNode, Rdf.Seq, IoTConcentrator.messages, Items);
104 Items.Clear();
105 }
106
107 if (this.node.HasChildren)
108 {
109 foreach (INode ChildNode in await this.node.ChildNodes)
110 Items.Add(new UriNode(DataSourceGraph.GetNodeUri(ChildNode)));
111
112 Result.AddContainer(NodeGraphUriNode, Rdf.Bag, IoTConcentrator.childNodes, Items);
113 Items.Clear();
114 }
115
116 if (this.node.HasCommands)
117 {
118 IEnumerable<ICommand> NodeCommands = await this.node.Commands;
119
120 if (!(NodeCommands is null))
121 {
122 foreach (ICommand Command in NodeCommands)
123 {
124 if (!await Command.CanExecuteAsync(Caller))
125 continue;
126
127 UriNode CommandUriNode = new UriNode(DataSourceGraph.GetCommandUri(this.node, Command));
128
129 Result.Add(CommandUriNode, Rdf.type, IoTConcentrator.Command);
130 Result.Add(CommandUriNode, RdfSchema.label,
131 await Command.GetNameAsync(Language), Language.Code);
132 Result.Add(CommandUriNode, IoTConcentrator.commandId, Command.CommandID);
133 Result.Add(CommandUriNode, IoTConcentrator.sortCategory, Command.SortCategory);
134 Result.Add(CommandUriNode, IoTConcentrator.sortKey, Command.SortKey);
135 Result.Add(CommandUriNode, IoTConcentrator.commandType,
136 Command.Type.ToString(), IoTConcentrator.commandType);
137
138 AddOptionalStringLiteral(Result, CommandUriNode,
140 await Command.GetSuccessStringAsync(Language));
141
142 AddOptionalStringLiteral(Result, CommandUriNode,
144 await Command.GetFailureStringAsync(Language));
145
146 AddOptionalStringLiteral(Result, CommandUriNode,
148 await Command.GetConfirmationStringAsync(Language));
149
150 Items.Add(CommandUriNode);
151 }
152 }
153
154 Result.AddContainer(NodeGraphUriNode, Rdf.Seq, IoTConcentrator.commands, Items);
155 Items.Clear();
156 }
157
158 AddOperation(Result, Items, IoTConcentrator.edit, DataSourceGraph.GetOperationUri(this.node, "edit", "parameters"),
159 await Language.GetStringAsync(typeof(DataSourceGraph), 1, "Editable parameters for the node."));
160
161 if (this.node.IsReadable && this.node is ISensor)
162 {
163 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "all"),
164 await Language.GetStringAsync(typeof(DataSourceGraph), 2, "Read all available sensor data from node."));
165
166 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "momentary"),
167 await Language.GetStringAsync(typeof(DataSourceGraph), 3, "Read momentary sensor data from node."));
168
169 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "identity"),
170 await Language.GetStringAsync(typeof(DataSourceGraph), 4, "Read identity sensor data from node."));
171
172 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "status"),
173 await Language.GetStringAsync(typeof(DataSourceGraph), 5, "Read status sensor data from node."));
174
175 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "computed"),
176 await Language.GetStringAsync(typeof(DataSourceGraph), 6, "Read computed sensor data from node."));
177
178 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "peak"),
179 await Language.GetStringAsync(typeof(DataSourceGraph), 7, "Read peak sensor data from node."));
180
181 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "historical"),
182 await Language.GetStringAsync(typeof(DataSourceGraph), 8, "Read historical sensor data from node."));
183
184 AddOperation(Result, Items, IoTConcentrator.read, DataSourceGraph.GetOperationUri(this.node, "read", "nonHistorical"),
185 await Language.GetStringAsync(typeof(DataSourceGraph), 9, "Read all non-historical sensor data from node."));
186 }
187
188 if (this.node.IsControllable && this.node is IActuator Actuator)
189 {
190 foreach (ControlParameter P in await Actuator.GetControlParameters())
191 {
192 AddOperation(Result, Items, IoTConcentrator.control, DataSourceGraph.GetOperationUri(this.node, "control", P.Name),
193 string.IsNullOrEmpty(P.Description) ? await Language.GetStringAsync(typeof(DataSourceGraph), 10, "Control parameter.") : P.Description);
194 }
195 }
196
197 Result.AddContainer(NodeGraphUriNode, Rdf.Seq, IoTConcentrator.operations, Items);
198 }
199
200 private static void AddOptionalStringLiteral(InMemorySemanticCube Result,
201 ISemanticElement Subject, Uri PredicateUri, string Value)
202 {
203 if (!string.IsNullOrEmpty(Value))
204 Result.Add(Subject, PredicateUri, Value);
205 }
206
207 private static void AddOperation(InMemorySemanticCube Result,
208 ChunkedList<ISemanticElement> Operations, Uri Predicate, Uri Graph, string Label)
209 {
210 BlankNode Operation = new BlankNode("n" + Guid.NewGuid().ToString());
211 Operations.Add(Operation);
212
213 Result.Add(Operation, Predicate, Graph);
214 Result.Add(Operation, RdfSchema.label, Label);
215 }
216
217 }
218}
override void Add(ISemanticTriple Triple)
Adds a triple to the cube.
void AddContainer(ISemanticElement Subject, Uri Type, Uri Predicate, params object[] Items)
Adds a linked list of items to the graph.
Represents a blank node
Definition: BlankNode.cs:7
static ISemanticElement Encapsulate(object Value)
Encapsulates an object as a semantic element.
static readonly Uri updated
http://purl.org/dc/terms/updated
static readonly Uri Bag
URI representing rdf:Bag, an unordered container.
Definition: Rdf.cs:96
static readonly Uri Seq
URI representing rdf:Seq, an ordered container.
Definition: Rdf.cs:91
static readonly Uri type
URI representing rdf:type
Definition: Rdf.cs:51
static readonly Uri label
http://www.w3.org/2000/01/rdf-schema#label
Definition: RdfSchema.cs:51
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Clear()
Clears the collection.
Definition: ChunkedList.cs:306
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
string Code
Language code.
Definition: Language.cs:49
Abstract base class for control parameters.
string Description
Description for parameter.
Tokens available in request.
Definition: RequestOrigin.cs:9
static readonly Uri body
urn:nf:iot:concentrator:1.0:body
static readonly Uri failure
urn:nf:iot:concentrator:1.0:failure
static readonly Uri commandType
urn:nf:iot:concentrator:1.0:commandType
static readonly Uri messageType
urn:nf:iot:concentrator:1.0:messageType
static readonly Uri read
urn:nf:iot:concentrator:1.0:read
static readonly Uri isControllable
urn:nf:iot:concentrator:1.0:isControllable
static readonly Uri commands
urn:nf:iot:concentrator:1.0:commands
static readonly Uri Command
urn:nf:iot:concentrator:1.0:Command
static readonly Uri logId
urn:nf:iot:concentrator:1.0:logId
static readonly Uri commandId
urn:nf:iot:concentrator:1.0:commandId
static readonly Uri hasCommands
urn:nf:iot:concentrator:1.0:hasCommands
static readonly Uri localId
urn:nf:iot:concentrator:1.0:localId
static readonly Uri messages
urn:nf:iot:concentrator:1.0:messages
static readonly Uri edit
urn:nf:iot:concentrator:1.0:edit
static readonly Uri sourceId
urn:nf:iot:concentrator:1.0:sourceId
static readonly Uri state
urn:nf:iot:concentrator:1.0:state
static readonly Uri Node
urn:nf:iot:concentrator:1.0:Node
static readonly Uri parameterId
urn:nf:iot:concentrator:1.0:parameterId
static readonly Uri childNodes
urn:nf:iot:concentrator:1.0:childNodes
static readonly Uri typeName
urn:nf:iot:concentrator:1.0:typeName
static readonly Uri sortKey
urn:nf:iot:concentrator:1.0:sortKey
static readonly Uri control
urn:nf:iot:concentrator:1.0:control
static readonly Uri nodeState
urn:nf:iot:concentrator:1.0:nodeState
static readonly Uri success
urn:nf:iot:concentrator:1.0:success
static readonly Uri hasChildSources
urn:nf:iot:concentrator:1.0:hasChildSources
static readonly Uri isReadable
urn:nf:iot:concentrator:1.0:isReadable
static readonly Uri operations
urn:nf:iot:concentrator:1.0:operations
static readonly Uri sortCategory
urn:nf:iot:concentrator:1.0:sortCategory
static readonly Uri eventId
urn:nf:iot:concentrator:1.0:eventId
static readonly Uri dispParam
urn:nf:iot:concentrator:1.0:dispParam
static readonly Uri confirmation
urn:nf:iot:concentrator:1.0:confirmation
static readonly Uri partition
urn:nf:iot:concentrator:1.0:partition
static readonly Uri nodeId
urn:nf:iot:concentrator:1.0:nodeId
static readonly Uri parentNode
urn:nf:iot:concentrator:1.0:parentNode
static readonly Uri timestamp
urn:nf:iot:sd:1.0:timestamp
static readonly Uri value
urn:nf:iot:concentrator:1.0:value
Makes harmonized data sources and nodes available as semantic information.
static Uri GetCommandUri(INode Node, ICommand Command)
Gets the Graph URI for a node command.
static Uri GetNodeUri(INode Node)
Gets the Graph URI for a node.
static Uri GetOperationUri(INode Node, string Category, string Action)
Gets the Graph URI for a node operation.
Dynamic graph of a node in a data source on the Neuron.
Definition: NodeGraph.cs:18
NodeGraph(INode Node)
Dynamic graph of a node in a data source on the Neuron.
Definition: NodeGraph.cs:25
async Task GenerateGraph(InMemorySemanticCube Result, Language Language, RequestOrigin Caller)
Generates the semantic graph.
Definition: NodeGraph.cs:36
Interface for semantic nodes.
Interface for actuator nodes.
Definition: IActuator.cs:10
Interface for commands.
Definition: ICommand.cs:32
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: INode.cs:140
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for dynamic graphs.