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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
19 : base()
20 {
21 this.Optional = false;
22 this.Required = true;
23 this.OrderCategory = int.MaxValue;
24 this.OrderComplexity = int.MaxValue;
25 this.Ignore = true;
26
27 foreach (Keyword Keyword in Keywords)
28 {
29 if (Keyword.Ignore)
30 continue;
31
32 this.Ignore = false;
33 this.Optional |= Keyword.Optional;
34 this.Required &= Keyword.Required;
35 this.OrderCategory = Math.Min(this.OrderCategory, Keyword.OrderCategory);
36 this.OrderComplexity = Math.Min(this.OrderComplexity, Keyword.OrderCategory);
37 }
38
39 this.Keywords = Keywords;
40
41 }
42
46 public Keyword[] Keywords { get; }
47
51 public override bool Optional { get; }
52
56 public override bool Required { get; }
57
61 public override int OrderCategory { get; }
62
66 public override int OrderComplexity { get; }
67
71 public override bool Ignore { get; }
72
74 public override bool Equals(object obj)
75 {
76 int i, c;
77
78 if (!(obj is SequenceOfKeywords Sequence) ||
79 (c = this.Keywords.Length) != Sequence.Keywords.Length)
80 {
81 return false;
82 }
83
84 for (i = 0; i < c; i++)
85 {
86 if (!this.Keywords[i].Equals(Sequence.Keywords[i]))
87 return false;
88 }
89
90 return true;
91 }
92
94 public override string ToString()
95 {
96 StringBuilder sb = new StringBuilder();
97 bool First = true;
98
99 sb.Append('"');
100
101 foreach (Keyword Keyword in this.Keywords)
102 {
103 if (Keyword.Ignore)
104 continue;
105
106 if (First)
107 First = false;
108 else
109 sb.Append(' ');
110
111 sb.Append(Keyword.ToString());
112 }
113
114 sb.Append('"');
115
116 return sb.ToString();
117 }
118
124 public override async Task<IEnumerable<KeyValuePair<string, TokenReferences>>> GetTokenReferences(SearchProcess Process)
125 {
126 Dictionary<ulong, ObjectReference> ExpectedDocIndex = null;
127 IEnumerable<KeyValuePair<string, TokenReferences>> Records;
128 ObjectReference Ref;
129 ulong Key;
130 int i, c;
131
132 foreach (Keyword Keyword in this.Keywords)
133 {
134 if (Keyword.Ignore)
135 continue;
136
137 Records = await Keyword.GetTokenReferences(Process);
138
139 if (ExpectedDocIndex is null)
140 {
141 ExpectedDocIndex = new Dictionary<ulong, ObjectReference>();
142
143 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
144 {
145 c = Rec.Value.ObjectReferences.Length;
146 for (i = 0; i < c; i++)
147 {
148 Ref = await Process.TryGetObjectReference(Rec.Value.ObjectReferences[i], true);
149 if (Ref is null)
150 continue;
151
152 if (Ref.TryGetCount(Rec.Key, out TokenCount TokenCount))
153 {
154 foreach (uint DocIndex in TokenCount.DocIndex)
155 {
156 Key = Ref.Index;
157 Key <<= 32;
158 Key |= DocIndex;
159
160 ExpectedDocIndex[Key + 1] = Ref;
161 }
162 }
163 }
164 }
165 }
166 else
167 {
168 Dictionary<ulong, ObjectReference> ExpectedDocIndex2 = new Dictionary<ulong, ObjectReference>();
169
170 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
171 {
172 c = Rec.Value.ObjectReferences.Length;
173 for (i = 0; i < c; i++)
174 {
175 Ref = await Process.TryGetObjectReference(Rec.Value.ObjectReferences[i], false);
176 if (Ref is null)
177 continue;
178
179 if (Ref.TryGetCount(Rec.Key, out TokenCount TokenCount))
180 {
181 foreach (uint DocIndex in TokenCount.DocIndex)
182 {
183 Key = Ref.Index;
184 Key <<= 32;
185 Key |= DocIndex;
186
187 if (ExpectedDocIndex.ContainsKey(Key))
188 ExpectedDocIndex2[Key + 1] = Ref;
189 }
190 }
191 }
192 }
193
194 ExpectedDocIndex = ExpectedDocIndex2;
195 }
196
197 if (ExpectedDocIndex.Count == 0)
198 return new KeyValuePair<string, TokenReferences>[0];
199 }
200
201 if (ExpectedDocIndex is null)
202 return new KeyValuePair<string, TokenReferences>[0];
203
204 c = ExpectedDocIndex.Count;
205 ulong[] ObjectReferences = new ulong[c];
206 uint[] Counts = new uint[c];
207 DateTime[] Timestamps = new DateTime[c];
208
209 i = 0;
210 foreach (ObjectReference Ref2 in ExpectedDocIndex.Values)
211 {
212 ObjectReferences[i] = Ref2.Index;
213 Counts[i] = 1;
214 Timestamps[i] = Ref2.Indexed;
215
216 i++;
217 }
218
219 return new KeyValuePair<string, TokenReferences>[]
220 {
221 new KeyValuePair<string, TokenReferences>(this.ToString(),
222 new TokenReferences()
223 {
224 LastBlock = 0,
225 ObjectReferences = ObjectReferences,
226 Timestamps = Timestamps,
227 Counts = Counts
228 })
229 };
230 }
231
237 public override async Task<bool> Process(SearchProcess Process)
238 {
239 IEnumerable<KeyValuePair<string, TokenReferences>> Records = await this.GetTokenReferences(Process);
240
241 foreach (KeyValuePair<string, TokenReferences> Rec in Records)
242 {
243 string Token = Rec.Key;
244 TokenReferences References = Rec.Value;
245
246 int j, d = References.ObjectReferences.Length;
247
248 for (j = 0; j < d; j++)
249 {
250 ulong ObjectReference = References.ObjectReferences[j];
251
252 if (Process.IsRestricted)
253 Process.Found[ObjectReference] = true;
254
255 if (!Process.ReferencesByObject.TryGetValue(ObjectReference, out MatchInformation ByObjectReference))
256 {
257 if (Process.IsRestricted)
258 continue;
259
260 ByObjectReference = new MatchInformation();
261 Process.ReferencesByObject[ObjectReference] = ByObjectReference;
262 }
263
264 ByObjectReference.AddTokenReference(new TokenReference()
265 {
266 Count = References.Counts[j],
267 LastBlock = References.LastBlock,
269 Timestamp = References.Timestamps[j],
270 Token = Token
271 });
272 }
273 }
274
275 if (Process.IsRestricted)
276 {
277 LinkedList<ulong> ToRemove = null;
278
279 foreach (ulong ObjectReference in Process.ReferencesByObject.Keys)
280 {
281 if (!Process.Found.ContainsKey(ObjectReference))
282 {
283 if (ToRemove is null)
284 ToRemove = new LinkedList<ulong>();
285
286 ToRemove.AddLast(ObjectReference);
287 }
288 }
289
290 if (!(ToRemove is null))
291 {
292 foreach (ulong ObjectReference in ToRemove)
293 Process.ReferencesByObject.Remove(ObjectReference);
294 }
295 }
296
297 if (Process.ReferencesByObject.Count == 0)
298 return false;
299
300 Process.IncRestricted();
301
302 return true;
303 }
304
305 }
306}
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.