Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphPattern.cs
2using System.Threading.Tasks;
9
11{
16 {
17 private readonly ISparqlPattern pattern;
18 private ScriptNode graph;
19 private bool graphIsVariableRef;
20 private string graphVariableRef;
21
27 public GraphPattern(ScriptNode Graph, ISparqlPattern Pattern)
28 {
29 this.graph = Graph;
30 this.pattern = Pattern;
31
32 this.CheckGraphVariableReference();
33 }
34
35 private void CheckGraphVariableReference()
36 {
37 if (this.graph is VariableReference Ref)
38 {
39 this.graphIsVariableRef = true;
40 this.graphVariableRef = Ref.VariableName;
41 }
42 else
43 {
44 this.graphIsVariableRef = false;
45 this.graphVariableRef = null;
46 }
47 }
48
52 public bool IsEmpty => false;
53
62 public async Task<IEnumerable<Possibility>> Search(ISemanticCube Cube,
63 Variables Variables, IEnumerable<Possibility> ExistingMatches, SparqlQuery Query)
64 {
66 UriNode Graph;
67
68 if (ExistingMatches is null)
69 {
70 if (this.graphIsVariableRef &&
71 !Variables.ContainsVariable(this.graphVariableRef))
72 {
73 await Query.LoadUnloadedNamedGraphs(Variables);
74
76
77 foreach (UriNode GraphName in Query.NamedGraphNames)
78 {
79 Cube = await Query.GetNamedGraph(GraphName, Variables);
80
81 if (!(Cube is null))
82 {
83 Possibility P = new Possibility(this.graphVariableRef, GraphName);
84
85 if (ObjectProperties is null)
87 else
88 ObjectProperties.Object = P;
89
90 IEnumerable<Possibility> PartResult = await this.pattern.Search(
91 Cube, ObjectProperties, new Possibility[] { P }, Query);
92
93 if (!(PartResult is null))
94 {
95 foreach (Possibility P2 in PartResult)
96 Result.Add(P2);
97 }
98 }
99 }
100
101 return Result;
102 }
103 else
104 {
105 Graph = Query.GetGraphName((await this.graph.EvaluateAsync(Variables)).AssociatedObjectValue);
106 Cube = await Query.GetNamedGraph(Graph.AssociatedObjectValue, Variables);
107 return await this.pattern.Search(Cube, Variables, ExistingMatches, Query);
108 }
109 }
110 else
111 {
113 Dictionary<UriNode, bool> ReferencedGraphs = new Dictionary<UriNode, bool>();
114
115 foreach (Possibility P in ExistingMatches)
116 {
117 if (ObjectProperties is null)
119 else
120 ObjectProperties.Object = P;
121
122 if (!this.graphIsVariableRef ||
123 ObjectProperties.ContainsVariable(this.graphVariableRef))
124 {
125 Graph = Query.GetGraphName(await this.graph.EvaluateAsync(ObjectProperties));
126 if (Graph is null)
127 continue;
128 }
129 else
130 {
131 ToProcess = null;
132 ReferencedGraphs = null;
133 break;
134 }
135
136 ToProcess.Add(new KeyValuePair<Possibility, UriNode>(P, Graph));
137 ReferencedGraphs[Graph] = true;
138 }
139
140 if (!(ReferencedGraphs is null))
141 {
142 UriNode[] Referenced = new UriNode[ReferencedGraphs.Count];
143 ReferencedGraphs.Keys.CopyTo(Referenced, 0);
144
145 await Query.LoadUnloadedNamedGraphs(Variables, Referenced);
146 }
147 else
148 await Query.LoadUnloadedNamedGraphs(Variables);
149
151
152 if (!(ToProcess is null))
153 {
154 foreach (KeyValuePair<Possibility, UriNode> P in ToProcess)
155 {
156 if (ObjectProperties is null)
158 else
159 ObjectProperties.Object = P.Key;
160
161 Cube = await Query.GetNamedGraph(P.Value, Variables);
162
163 if (!(Cube is null))
164 {
165 IEnumerable<Possibility> PartResult = await this.pattern.Search(
166 Cube, ObjectProperties, new Possibility[] { P.Key }, Query);
167
168 if (!(PartResult is null))
169 Result.AddRange(PartResult);
170 }
171 }
172 }
173 else
174 {
175 foreach (Possibility P in ExistingMatches)
176 {
177 if (ObjectProperties is null)
179 else
180 ObjectProperties.Object = P;
181
182 if (!this.graphIsVariableRef ||
183 ObjectProperties.ContainsVariable(this.graphVariableRef))
184 {
185 Graph = Query.GetGraphName(await this.graph.EvaluateAsync(ObjectProperties));
186 if (Graph is null)
187 continue;
188
189 Cube = await Query.GetNamedGraph(Graph, Variables);
190
191 if (!(Cube is null))
192 {
193 IEnumerable<Possibility> PartResult = await this.pattern.Search(
194 Cube, ObjectProperties, new Possibility[] { P }, Query);
195
196 if (!(PartResult is null))
197 Result.AddRange(PartResult);
198 }
199 }
200 else
201 {
202 foreach (UriNode GraphName in Query.NamedGraphNames)
203 {
204 Cube = await Query.GetNamedGraph(GraphName, Variables);
205
206 if (!(Cube is null))
207 {
208 IEnumerable<Possibility> PartResult = await this.pattern.Search(
209 Cube, ObjectProperties, new Possibility[]
210 {
211 new Possibility(this.graphVariableRef, GraphName, P)
212 }, Query);
213
214 if (!(PartResult is null))
215 Result.AddRange(PartResult);
216 }
217 }
218 }
219 }
220 }
221
222 return Result;
223 }
224 }
225
230 public void SetParent(ScriptNode Parent)
231 {
232 this.graph.SetParent(Parent);
233 this.pattern.SetParent(Parent);
234 }
235
243 public bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
244 {
245 if (Order == SearchMethod.DepthFirst)
246 {
247 this.graph.ForAllChildNodes(Callback, State, Order);
248 this.pattern.ForAllChildNodes(Callback, State, Order);
249 }
250
251 this.ForAll(Callback, State, Order);
252
253 if (Order == SearchMethod.BreadthFirst)
254 {
255 this.graph.ForAllChildNodes(Callback, State, Order);
256 this.pattern.ForAllChildNodes(Callback, State, Order);
257 }
258
259 return true;
260 }
261
269 public bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
270 {
271 if (!Callback(this.graph, out ScriptNode NewNode, State))
272 return false;
273
274 if (!(NewNode is null))
275 {
276 this.graph = NewNode;
277 this.CheckGraphVariableReference();
278 }
279
280 if (!this.pattern.ForAll(Callback, State, Order))
281 return false;
282
283 return true;
284 }
285
287 public override bool Equals(object obj)
288 {
289 return obj is GraphPattern Typed &&
290 this.graph.Equals(Typed.graph) &&
291 this.pattern.Equals(Typed.pattern);
292 }
293
295 public override int GetHashCode()
296 {
297 int Result = typeof(GraphPattern).GetHashCode();
298 Result ^= Result << 5 ^ this.graph.GetHashCode();
299 Result ^= Result << 5 ^ this.pattern.GetHashCode();
300 return Result;
301 }
302 }
303}
override object AssociatedObjectValue
Associated object value.
Definition: UriNode.cs:72
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
override bool Equals(object obj)
Definition: ScriptNode.cs:258
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed or created.
Definition: ScriptNode.cs:132
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Represents a variable reference.
A pattern referencing a named source.
Definition: GraphPattern.cs:16
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed or created.
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
GraphPattern(ScriptNode Graph, ISparqlPattern Pattern)
A pattern referencing a named source.
Definition: GraphPattern.cs:27
bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
async Task< IEnumerable< Possibility > > Search(ISemanticCube Cube, Variables Variables, IEnumerable< Possibility > ExistingMatches, SparqlQuery Query)
Searches for the pattern on information in a semantic cube.
Definition: GraphPattern.cs:62
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
UriNode[] NamedGraphNames
Names of named graphs, may be null.
Definition: SparqlQuery.cs:144
override bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
Collection of variables.
Definition: Variables.cs:25
virtual bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
Definition: Variables.cs:80
Interface for semantic cubes.
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38