-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathPropertyDefinition.cs
More file actions
221 lines (190 loc) · 5.14 KB
/
PropertyDefinition.cs
File metadata and controls
221 lines (190 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Persistence.Attributes;
using Waher.Persistence.Serialization;
using Waher.Runtime.Inventory;
namespace Waher.Persistence.FullTextSearch
{
/// <summary>
/// Type of indexed property.
/// </summary>
public enum PropertyType
{
/// <summary>
/// A label (field or property) containing the value to be tokenized.
/// </summary>
Label,
/// <summary>
/// Reference to external evaluator, returning an object that is tokenized.
/// </summary>
External
}
/// <summary>
/// Defines an indexable property.
/// </summary>
[TypeName(TypeNameSerialization.None)]
public class PropertyDefinition
{
private readonly Dictionary<string, KeyValuePair<PropertyInfo, FieldInfo>> memberInfo = new Dictionary<string, KeyValuePair<PropertyInfo, FieldInfo>>();
private IPropertyEvaluator evaluator;
private bool initialized;
/// <summary>
/// Defines an indexable property.
/// </summary>
public PropertyDefinition()
{
}
/// <summary>
/// Defines an indexable property.
/// </summary>
/// <param name="Label">Property of Field name.</param>
public PropertyDefinition(string Label)
{
this.ExternalSource = null;
this.Definition = Label;
this.Type = PropertyType.Label;
}
/// <summary>
/// Defines an indexable property.
/// </summary>
/// <param name="ExternalSource">External Source</param>
/// <param name="Definition">Definition</param>
public PropertyDefinition(string ExternalSource, string Definition)
{
this.ExternalSource = ExternalSource;
this.Definition = Definition;
this.Type = PropertyType.External;
}
/// <summary>
/// Creates an array of property definitions from an array of property names.
/// </summary>
/// <param name="PropertyNames">Property names</param>
/// <returns>Array of property definitions</returns>
public static PropertyDefinition[] ToArray(string[] PropertyNames)
{
int i, c = PropertyNames.Length;
PropertyDefinition[] Result = new PropertyDefinition[c];
for (i = 0; i < c; i++)
Result[i] = new PropertyDefinition(PropertyNames[i]);
return Result;
}
/// <summary>
/// Definition, as a string.
/// </summary>
public string Definition { get; set; }
/// <summary>
/// External source.
/// </summary>
[DefaultValueStringEmpty]
public string ExternalSource { get; set; }
/// <summary>
/// Type of property definition
/// </summary>
public PropertyType Type { get; set; }
/// <summary>
/// Gets the object to index.
/// </summary>
/// <param name="Instance">Object instance being indexed.</param>
/// <returns>Property value to index.</returns>
public async Task<object> GetValue(object Instance)
{
if (Instance is null)
return null;
Type T;
switch (this.Type)
{
case PropertyType.Label:
KeyValuePair<PropertyInfo, FieldInfo> P;
T = Instance.GetType();
lock (this.memberInfo)
{
if (!this.memberInfo.TryGetValue(T.FullName, out P))
{
P = new KeyValuePair<PropertyInfo, FieldInfo>(
T.GetRuntimeProperty(this.Definition),
T.GetRuntimeField(this.Definition));
this.memberInfo[T.FullName] = P;
}
}
if (!(P.Key is null))
{
if (P.Key.DeclaringType.IsAssignableFrom(Instance.GetType()))
return P.Key.GetValue(Instance);
}
else if (!(P.Value is null))
{
if (P.Value.DeclaringType.IsAssignableFrom(Instance.GetType()))
return P.Value.GetValue(Instance);
}
break;
case PropertyType.External:
if (!this.initialized)
{
this.initialized = true;
T = Types.GetType(this.ExternalSource);
if (!(T is null))
{
this.evaluator = Types.Create(true, T) as IPropertyEvaluator;
if (!(this.evaluator is null))
await this.evaluator.Prepare(this.Definition);
}
}
if (!(this.evaluator is null))
{
try
{
return await this.evaluator.Evaluate(Instance);
}
catch (Exception ex)
{
Log.Exception(ex);
return null;
}
}
break;
}
return null;
}
/// <summary>
/// Gets the object to index.
/// </summary>
/// <param name="Instance">Object instance being indexed.</param>
/// <returns>Property value to index.</returns>
public async Task<object> GetValue(GenericObject Instance)
{
if (!this.initialized)
{
this.initialized = true;
switch (this.Type)
{
case PropertyType.Label:
break;
case PropertyType.External:
Type T = Types.GetType(this.ExternalSource);
if (!(T is null))
{
this.evaluator = Types.Create(true, T) as IPropertyEvaluator;
if (!(this.evaluator is null))
await this.evaluator.Prepare(this.Definition);
}
break;
}
}
switch (this.Type)
{
case PropertyType.Label:
if (Instance.TryGetFieldValue(this.Definition, out object Value))
return Value;
break;
case PropertyType.External:
if (!(this.evaluator is null))
return await this.evaluator.Evaluate(Instance);
break;
}
return null;
}
}
}