Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SequenceOfKeywords.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
9{
14 {
20 : base()
21 {
22 this.Optional = false;
23 this.Required = true;
24 this.OrderCategory = int.MaxValue;
25 this.OrderComplexity = int.MaxValue;
26 this.Ignore = true;
27
28 foreach (Keyword Keyword in Keywords)
29 {
30 if (Keyword.Ignore)
31 continue;
32
33 this.Ignore = false;
34 this.Optional |= Keyword.Optional;
35 this.Required &= Keyword.Required;
36 this.OrderCategory = Math.Min(this.OrderCategory, Keyword.OrderCategory);
37 this.OrderComplexity = Math.Min(this.OrderComplexity, Keyword.OrderCategory);
38 }
39
40 this.Keywords = Keywords;
41
42 }
43
47 public Keyword[] Keywords { get; }
48
52 public override bool Optional { get; }
53
57 public override bool Required { get; }
58
62 public override int OrderCategory { get; }
63
67 public override int OrderComplexity { get; }
68
72 public override bool Ignore { get; }
73
75 public override bool Equals(object obj)
76 {
77 int i, c;
78
79 if (!(obj is SequenceOfKeywords Sequence) ||
80 (c = this.Keywords.Length) != Sequence.Keywords.Length)
81 {
82 return false;
83 }
84
85 for (i = 0; i < c; i++)
86 {
87 if (!this.Keywords[i].Equals(Sequence.Keywords[i]))
88 return false;
89 }
90
91 return true;
92 }
93
95 public override string ToString()
96 {
97 StringBuilder sb = new StringBuilder();
98 bool First = true;
99
100 sb.Append('"');
101
102 foreach (Keyword Keyword in this.Keywords)
103 {
104 if (Keyword.Ignore)
105 continue;
106
107 if (First)
108 First = false;
109 else
110 sb.Append(' ');
111
112 sb.Append(Keyword.ToString());
113 }
114
115 sb.Append('"');
116
117 return sb.ToString();
118 }
119
125 public override async Task<IEnumerable<KeyValuePair<string, TokenReferences>>> GetTokenReferences(SearchProcess Process)
126 {
127 Dictionary<ulong, ObjectReference> ExpectedDocIndex = null;
128 IEnumerable<KeyValuePair<string, TokenReferences>> Records;
129 ObjectReference Ref;
130 ulong Key;
131 int i, c;
132
133 foreach (Keyword Keyword in this.Keywords)
134 {
135 if (Keyword.Ignore)
136 continue;
137
138 Records = await Keyword.GetTokenReferences(Process);
139
140 if (ExpectedDocIndex is null)
141 {
142 ExpectedDocIndex = new Dictionary<ulong, ObjectReference>();
143
144 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
145 {
146 c = Rec.Value.ObjectReferences.Length;
147 for (i = 0; i < c; i++)
148 {
149 Ref = await Process.TryGetObjectReference(Rec.Value.ObjectReferences[i], true);
150 if (Ref is null)
151 continue;
152
153 if (Ref.TryGetCount(Rec.Key, out TokenCount TokenCount))
154 {
155 foreach (uint DocIndex in TokenCount.DocIndex)
156 {
157 Key = Ref.Index;
158 Key <<= 32;
159 Key |= DocIndex;
160
161 ExpectedDocIndex[Key + 1] = Ref;
162 }
163 }
164 }
165 }
166 }
167 else
168 {
169 Dictionary<ulong, ObjectReference> ExpectedDocIndex2 = new Dictionary<ulong, ObjectReference>();
170
171 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
172 {
173 c = Rec.Value.ObjectReferences.Length;
174 for (i = 0; i < c; i++)
175 {
176 Ref = await Process.TryGetObjectReference(Rec.Value.ObjectReferences[i], false);
177 if (Ref is null)
178 continue;
179
180 if (Ref.TryGetCount(Rec.Key, out TokenCount TokenCount))
181 {
182 foreach (uint DocIndex in TokenCount.DocIndex)
183 {
184 Key = Ref.Index;
185 Key <<= 32;
186 Key |= DocIndex;
187
188 if (ExpectedDocIndex.ContainsKey(Key))
189 ExpectedDocIndex2[Key + 1] = Ref;
190 }
191 }
192 }
193 }
194
195 ExpectedDocIndex = ExpectedDocIndex2;
196 }
197
198 if (ExpectedDocIndex.Count == 0)
199 return Array.Empty<KeyValuePair<string, TokenReferences>>();
200 }
201
202 if (ExpectedDocIndex is null)
203 return Array.Empty<KeyValuePair<string, TokenReferences>>();
204
205 c = ExpectedDocIndex.Count;
206 ulong[] ObjectReferences = new ulong[c];
207 uint[] Counts = new uint[c];
208 DateTime[] Timestamps = new DateTime[c];
209
210 i = 0;
211 foreach (ObjectReference Ref2 in ExpectedDocIndex.Values)
212 {
213 ObjectReferences[i] = Ref2.Index;
214 Counts[i] = 1;
215 Timestamps[i] = Ref2.Indexed;
216
217 i++;
218 }
219
220 return new KeyValuePair<string, TokenReferences>[]
221 {
222 new KeyValuePair<string, TokenReferences>(this.ToString(),
223 new TokenReferences()
224 {
225 LastBlock = 0,
226 ObjectReferences = ObjectReferences,
227 Timestamps = Timestamps,
228 Counts = Counts
229 })
230 };
231 }
232
238 public override async Task<bool> Process(SearchProcess Process)
239 {
240 IEnumerable<KeyValuePair<string, TokenReferences>> Records = await this.GetTokenReferences(Process);
241
242 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
243 {
244 string Token = Rec.Key;
245 TokenReferences References = Rec.Value;
246
247 int j, d = References.ObjectReferences.Length;
248
249 for (j = 0; j < d; j++)
250 {
251 ulong ObjectReference = References.ObjectReferences[j];
252
253 if (Process.IsRestricted)
254 Process.Found[ObjectReference] = true;
255
256 if (!Process.ReferencesByObject.TryGetValue(ObjectReference, out MatchInformation ByObjectReference))
257 {
258 if (Process.IsRestricted)
259 continue;
260
261 ByObjectReference = new MatchInformation();
262 Process.ReferencesByObject[ObjectReference] = ByObjectReference;
263 }
264
265 ByObjectReference.AddTokenReference(new TokenReference()
266 {
267 Count = References.Counts[j],
268 LastBlock = References.LastBlock,
270 Timestamp = References.Timestamps[j],
271 Token = Token
272 });
273 }
274 }
275
276 if (Process.IsRestricted)
277 {
278 ChunkedList<ulong> ToRemove = null;
279
280 foreach (ulong ObjectReference in Process.ReferencesByObject.Keys)
281 {
282 if (!Process.Found.ContainsKey(ObjectReference))
283 {
284 if (ToRemove is null)
285 ToRemove = new ChunkedList<ulong>();
286
287 ToRemove.Add(ObjectReference);
288 }
289 }
290
291 if (!(ToRemove is null))
292 {
293 foreach (ulong ObjectReference in ToRemove)
294 Process.ReferencesByObject.Remove(ObjectReference);
295 }
296 }
297
298 if (Process.ReferencesByObject.Count == 0)
299 return false;
300
301 Process.IncRestricted();
302
303 return true;
304 }
305
306 }
307}
Abstract base class for keywords.
Definition: Keyword.cs:11
virtual bool Ignore
If keyword should be ignored.
Definition: Keyword.cs:47
virtual bool Required
If keyword is required
Definition: Keyword.cs:27
virtual int OrderCategory
Order category of keyword
Definition: Keyword.cs:37
virtual bool Optional
If keyword is optional
Definition: Keyword.cs:22
abstract Task< IEnumerable< KeyValuePair< string, TokenReferences > > > GetTokenReferences(SearchProcess Process)
Gets available token references.
Contains information about a search process.
SequenceOfKeywords(params Keyword[] Keywords)
Represents a sequence of keywords.
override async Task< bool > Process(SearchProcess Process)
Processes the keyword in a search process.
override async Task< IEnumerable< KeyValuePair< string, TokenReferences > > > GetTokenReferences(SearchProcess Process)
Gets available token references.
override bool Ignore
If keyword should be ignored.
override int OrderComplexity
Order complexity (within category) of keyword
Contains a reference to an indexed object.
DateTime Indexed
When object was indexed.
ulong Index
Reference number to use in full-text-index.
Contains matching information about a document in a search.
Represents a token and a corresponding occurrence count.
Definition: TokenCount.cs:12
uint[] DocIndex
Index inside document of each occurrence.
Definition: TokenCount.cs:40
Contains a sequence of object references that include the token in its indexed text properties.
DateTime[] Timestamps
Timestamps when corresponding object refernces were indexed.
uint[] Counts
Token counts for respective object reference.
ulong[] ObjectReferences
References to objects containing the token.
uint LastBlock
Index to last block in index representing the same token.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54