1 | #Region "Microsoft.VisualBasic::0d8adb0fe9174ca398087afbc12e82d2, Microsoft.VisualBasic.Core\ComponentModel\DataSource\Repository\ILocalSearchHandle.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Interface ILocalSearchHandle |
35 | ' |
36 | ' Function: Match, Matches |
37 | ' |
38 | ' Module SearchFramework |
39 | ' |
40 | ' Function: MultipleQuery, Query |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Imports System.Runtime.CompilerServices |
48 | |
49 | Namespace ComponentModel.DataSourceModel.Repository |
50 | |
51 | Public Interface ILocalSearchHandle |
52 | |
53 | ''' <summary> |
54 | ''' |
55 | ''' </summary> |
56 | ''' <param name="Keyword"></param> |
57 | ''' <param name="CaseSensitive">是否大小写敏感,默认不敏感</param> |
58 | ''' <returns></returns> |
59 | Function Matches(Keyword As String, Optional CaseSensitive As CompareMethod = CompareMethod.Text) As ILocalSearchHandle() |
60 | ''' <summary> |
61 | ''' |
62 | ''' </summary> |
63 | ''' <param name="Keyword"></param> |
64 | ''' <param name="CaseSensitive">是否大小写敏感,默认不敏感</param> |
65 | ''' <returns></returns> |
66 | Function Match(Keyword As String, Optional CaseSensitive As CompareMethod = CompareMethod.Text) As Boolean |
67 | End Interface |
68 | |
69 | ''' <summary> |
70 | ''' Extensions for data query |
71 | ''' </summary> |
72 | Public Module SearchFramework |
73 | |
74 | <Extension> |
75 | Public Iterator Function MultipleQuery(Of T, Term)(source As IEnumerable(Of T), |
76 | query As IEnumerable(Of NamedValue(Of Term())), |
77 | assert As Func(Of T, Term, Boolean)) As IEnumerable(Of NamedValue(Of Map(Of Term, T))) |
78 | |
79 | Dim terms As NamedValue(Of Term())() = query.ToArray |
80 | |
81 | For Each entity As T In source |
82 | For Each block In query |
83 | For Each match As Term In block.Value |
84 | If assert(entity, match) Then |
85 | Yield New NamedValue(Of Map(Of Term, T)) With { |
86 | .Name = block.Name, |
87 | .Value = New Map(Of Term, T) With { |
88 | .Key = match, |
89 | .Maps = entity |
90 | } |
91 | } |
92 | End If |
93 | Next |
94 | Next |
95 | Next |
96 | End Function |
97 | |
98 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
99 | <Extension> |
100 | Public Function Query(Of T, Term)(source As IEnumerable(Of T), queries As IEnumerable(Of Term), assert As Func(Of T, Term, Boolean)) As IEnumerable(Of Map(Of Term, T)) |
101 | Return source.MultipleQuery({New NamedValue(Of Term())("null", queries.ToArray)}, assert).Values |
102 | End Function |
103 | End Module |
104 | End Namespace |