Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SubQueryPattern.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
16 private SparqlQuery subQuery;
17
23 {
24 this.subQuery = SubQuery;
25 }
26
30 public bool IsEmpty => false;
31
40 public async Task<IEnumerable<Possibility>> Search(ISemanticCube Cube,
41 Variables Variables, IEnumerable<Possibility> ExistingMatches, SparqlQuery Query)
42 {
43 IElement E = await this.subQuery.EvaluateAsync(Variables, ExistingMatches);
44 if (!(E.AssociatedObjectValue is SparqlResultSet ResultSet))
45 throw new ScriptRuntimeException("Subquery did not return a SPARQL Result Set.", this.subQuery);
46
47 if (ResultSet.BooleanResult.HasValue)
48 throw new ScriptRuntimeException("Subquery only returned a Boolean value.", this.subQuery);
49
50 LinkedList<Possibility> Result = new LinkedList<Possibility>();
51
52 foreach (ISparqlResultRecord Record in ResultSet.Records)
53 {
54 Possibility P = null;
55
56 foreach (ISparqlResultItem Item in Record)
57 {
58 if (P is null)
59 P = new Possibility(Item.Name, Item.Value);
60 else
61 P = new Possibility(Item.Name, Item.Value, P);
62 }
63
64 Result.AddLast(P);
65 }
66
67 return Result;
68 }
69
74 public void SetParent(ScriptNode Parent)
75 {
76 this.subQuery.SetParent(Parent);
77 }
78
86 public bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
87 {
88 if (Order == SearchMethod.DepthFirst)
89 this.subQuery.ForAllChildNodes(Callback, State, Order);
90
91 this.ForAll(Callback, State, Order);
92
93 if (Order == SearchMethod.BreadthFirst)
94 this.subQuery.ForAllChildNodes(Callback, State, Order);
95
96 return true;
97 }
98
106 public bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
107 {
108 if (!Callback(this.subQuery, out ScriptNode NewNode, State))
109 return false;
110
111 if (!(NewNode is null))
112 {
113 if (!(NewNode is SparqlQuery SubQuery))
114 throw new Exception("Expected sub-query node.");
115
116 this.subQuery = SubQuery;
117 }
118
119 return true;
120 }
121
123 public override bool Equals(object obj)
124 {
125 return obj is SubQueryPattern Typed &&
126 this.subQuery.Equals(Typed.subQuery);
127 }
128
130 public override int GetHashCode()
131 {
132 int Result = typeof(SubQueryPattern).GetHashCode();
133 Result ^= Result << 5 ^ this.subQuery.GetHashCode();
134 return Result;
135 }
136
137 }
138}
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
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.
async Task< IEnumerable< Possibility > > Search(ISemanticCube Cube, Variables Variables, IEnumerable< Possibility > ExistingMatches, SparqlQuery Query)
Searches for the pattern on information in a semantic cube.
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: SparqlQuery.cs:671
override Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: SparqlQuery.cs:161
Collection of variables.
Definition: Variables.cs:25
Interface for semantic cubes.
Interface for items in a record from the results of a SPARQL query.
string Name
Name of item in record.
ISemanticElement Value
Value of item in record.
Interface for result records of a SPARQL query.
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