Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphPattern.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
8
10{
15 {
16 private readonly ISparqlPattern pattern;
17 private ScriptNode graph;
18 private bool graphIsVariableRef;
19 private string graphVariableRef;
20
26 public GraphPattern(ScriptNode Graph, ISparqlPattern Pattern)
27 {
28 this.graph = Graph;
29 this.pattern = Pattern;
30
31 this.CheckGraphVariableReference();
32 }
33
34 private void CheckGraphVariableReference()
35 {
36 if (this.graph is VariableReference Ref)
37 {
38 this.graphIsVariableRef = true;
39 this.graphVariableRef = Ref.VariableName;
40 }
41 else
42 {
43 this.graphIsVariableRef = false;
44 this.graphVariableRef = null;
45 }
46 }
47
51 public bool IsEmpty => false;
52
61 public async Task<IEnumerable<Possibility>> Search(ISemanticCube Cube,
62 Variables Variables, IEnumerable<Possibility> ExistingMatches, SparqlQuery Query)
63 {
65 IElement Graph;
66
67 if (ExistingMatches is null)
68 {
69 if (this.graphIsVariableRef &&
70 !Variables.ContainsVariable(this.graphVariableRef))
71 {
72 LinkedList<Possibility> Result = new LinkedList<Possibility>();
73
74 foreach (UriNode GraphName in Query.NamedGraphNames)
75 {
76 Cube = await Query.GetNamedGraph(GraphName, Variables);
77
78 if (!(Cube is null))
79 {
80 Possibility P = new Possibility(this.graphVariableRef, GraphName);
81
82 if (ObjectProperties is null)
84 else
85 ObjectProperties.Object = P;
86
87 IEnumerable<Possibility> PartResult = await this.pattern.Search(
88 Cube, ObjectProperties, new Possibility[] { P }, Query);
89
90 if (!(PartResult is null))
91 {
92 foreach (Possibility P2 in PartResult)
93 Result.AddLast(P2);
94 }
95 }
96 }
97
98 return Result;
99 }
100 else
101 {
102 Graph = await this.graph.EvaluateAsync(Variables);
103 Cube = await Query.GetNamedGraph(Graph.AssociatedObjectValue, Variables);
104 return await this.pattern.Search(Cube, Variables, ExistingMatches, Query);
105 }
106 }
107 else
108 {
109 LinkedList<Possibility> Result = new LinkedList<Possibility>();
110
111 foreach (Possibility P in ExistingMatches)
112 {
113 if (ObjectProperties is null)
115 else
116 ObjectProperties.Object = P;
117
118 if (!this.graphIsVariableRef ||
119 ObjectProperties.ContainsVariable(this.graphVariableRef))
120 {
121 Graph = await this.graph.EvaluateAsync(ObjectProperties);
122 Cube = await Query.GetNamedGraph(Graph.AssociatedObjectValue, Variables);
123
124 if (!(Cube is null))
125 {
126 IEnumerable<Possibility> PartResult = await this.pattern.Search(
127 Cube, ObjectProperties, new Possibility[] { P }, Query);
128
129 if (!(PartResult is null))
130 {
131 foreach (Possibility P2 in PartResult)
132 Result.AddLast(P2);
133 }
134 }
135 }
136 else
137 {
138 foreach (UriNode GraphName in Query.NamedGraphNames)
139 {
140 Cube = await Query.GetNamedGraph(GraphName, Variables);
141
142 if (!(Cube is null))
143 {
144 IEnumerable<Possibility> PartResult = await this.pattern.Search(
145 Cube, ObjectProperties, new Possibility[]
146 {
147 new Possibility(this.graphVariableRef, GraphName, P)
148 }, Query);
149
150 if (!(PartResult is null))
151 {
152 foreach (Possibility P2 in PartResult)
153 Result.AddLast(P2);
154 }
155 }
156 }
157 }
158 }
159
160 return Result;
161 }
162 }
163
168 public void SetParent(ScriptNode Parent)
169 {
170 this.graph.SetParent(Parent);
171 this.pattern.SetParent(Parent);
172 }
173
181 public bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
182 {
183 if (Order == SearchMethod.DepthFirst)
184 {
185 this.graph.ForAllChildNodes(Callback, State, Order);
186 this.pattern.ForAllChildNodes(Callback, State, Order);
187 }
188
189 this.ForAll(Callback, State, Order);
190
191 if (Order == SearchMethod.BreadthFirst)
192 {
193 this.graph.ForAllChildNodes(Callback, State, Order);
194 this.pattern.ForAllChildNodes(Callback, State, Order);
195 }
196
197 return true;
198 }
199
207 public bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
208 {
209 if (!Callback(this.graph, out ScriptNode NewNode, State))
210 return false;
211
212 if (!(NewNode is null))
213 {
214 this.graph = NewNode;
215 this.CheckGraphVariableReference();
216 }
217
218 if (!this.pattern.ForAll(Callback, State, Order))
219 return false;
220
221 return true;
222 }
223
225 public override bool Equals(object obj)
226 {
227 return obj is GraphPattern Typed &&
228 this.graph.Equals(Typed.graph) &&
229 this.pattern.Equals(Typed.pattern);
230 }
231
233 public override int GetHashCode()
234 {
235 int Result = typeof(GraphPattern).GetHashCode();
236 Result ^= Result << 5 ^ this.graph.GetHashCode();
237 Result ^= Result << 5 ^ this.pattern.GetHashCode();
238 return Result;
239 }
240 }
241}
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.
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:15
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
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:26
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:61
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
UriNode[] NamedGraphNames
Names of named graphs, may be null.
Definition: SparqlQuery.cs:143
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:76
Interface for semantic cubes.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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