forked from mengyangliu/AddressMatch
/
MatchMachine.cs
211 lines (175 loc) · 6.2 KB
/
MatchMachine.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace AddressMatch
{
public delegate bool MatchRule(State state, GraphNode node);
public class MatchMachine
{
private MatchRule LocalMatchRule;
private AddrSet _addrset;
private bool defualtRule(State state, GraphNode nextNode)
{
if (nextNode.NodeLEVEL == LEVEL.Uncertainty || state.MinStateLEVEL == LEVEL.Uncertainty)
{
return true;
}
if (nextNode.NodeLEVEL > state.MinStateLEVEL)
{
return true;
}
else
{
return false;
}
}
public MatchMachine(AddrSet addrset)
{
if (addrset == null)
{
throw new Exception("AddrSet is not initialized");
}
if (AddrSet.AddrGraph == null)
{
throw new Exception("Graph is not initialized");
}
_addrset = addrset;
init();
}
private void init()
{
LocalMatchRule = new MatchRule(defualtRule);
//MatchStack = new Stack<State>();
}
/// <summary>
/// Backward Match
/// </summary>
/// <param name="s">address string[]</param>
/// <returns>result</returns>
public MatchResult Match(String[] s)
{
MatchHelper.Assert(s.Count() == 0,
@" input string[]'s length is 0 ");
Stack<State> MatchStack = new Stack<State>();
MatchResult result = new MatchResult();
s.Reverse();
//Store the first Match
State firstState = new State();
ReaderWriterLockSlim rwlock = _addrset.GetRWlock();
rwlock.EnterReadLock();
for (int i = 0; i < s.Count();i++)
{
State correntState = _addrset.FindNodeInHashTable(s[i]);
if (i ==0)
{
firstState = correntState;
}
if (correntState.NodeCount == 0)
{
result.ResultState = MatchResultState.NOTFOUND;
goto RESULT;
}
//MatchStack.
if (MatchStack.Count > 0)
{
FilterState(correntState, MatchStack.Peek());
if (correntState.NodeCount == 0)
{
result.ResultState = MatchResultState.NOTMATCHED;
goto RESULT;
}
}
MatchStack.Push(correntState);
}
if (MatchStack.Count == 0)
{
result.ResultState = MatchResultState.NOTFOUND;
goto RESULT;
}
if (MatchStack.Peek().NodeCount > 1)
{
result.ResultState = MatchResultState.MULTIMATCHED;
goto RESULT;
}
List<GraphNode> resList;
State TopState = MatchStack.Pop();
do
{
State nextState = MatchStack.Pop();
resList =
_addrset.ForwardSearchNode(delegate(GraphNode node)
{
return node.Name == nextState.Name;
},
TopState.NodeList);
//if (resList.Count > 1)
//{
// result.ResultState = MatchResultState.MULTIMATCHED;
// return result;
//}
} while (MatchStack.Count > 0);
rwlock.ExitReadLock();
if (resList == null || resList.Count == 0)
{
result.ResultState = MatchResultState.NOTMATCHED;
goto RESULT;
}
if (resList.Count > 1)
{
result.ResultState = MatchResultState.MULTIMATCHED;
goto RESULT;
}
result.Result = resList.First();
result.ResultState = MatchResultState.SUCCESS;
RESULT:
rwlock.ExitReadLock();
return result;
}
// TODO Uncompleted
public MatchResult FuzzyMatch()
{
return new MatchResult();
}
private State FilterState(State correntState,State preState)
{
correntState.NodeList.RemoveAll(delegate(GraphNode node)
{
return !LocalMatchRule(preState, node);
});
if (correntState.NodeList.Count() == 0)
{
correntState.MaxStateLEVEL = LEVEL.Uncertainty;
correntState.MinStateLEVEL = LEVEL.Uncertainty;
correntState.NodeCount = 0;
correntState.NodeList = null;
}
else
{
//--------------------TODO not effective
LEVEL min = correntState.NodeList.First().NodeLEVEL;
LEVEL max = correntState.NodeList.First().NodeLEVEL;
foreach (GraphNode node in correntState.NodeList)
{
min = min < node.NodeLEVEL ? min : node.NodeLEVEL;
max = max > node.NodeLEVEL ? max : node.NodeLEVEL;
}
correntState.MaxStateLEVEL = max;
correntState.MinStateLEVEL = min;
correntState.NodeCount = correntState.NodeList.Count();
}
return correntState;
}
#region -----------------------GET or SET-------------------------
/// <summary>
/// custom Match Rule
/// </summary>
/// <param name="rule">User-defined rule</param>
public void SetMatchRule(MatchRule rule)
{
LocalMatchRule = rule;
}
#endregion
}
}