我已经在这里停留了很长一段时间,并且似乎将问题归结为使用正则表达式的不正确的 NEsper 行为。我写了一个简单的项目来重现这个问题,它可以从github获得。
简而言之,NEsper 允许我通过一组规则(类似 SQL)来泵送消息(事件)。如果事件与规则匹配,NEsper 会触发警报。在我的应用程序中,我需要使用正则表达式,这似乎不起作用。
问题
我尝试了两种创建语句 createPattern
的方法,createEPL
它们没有触发匹配事件,但是 .NET Regex 类匹配正则表达式和输入。如果不是正则表达式 ("\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b") 我传递一个匹配值 (" 127.0.0.5") 到语句,事件成功触发。
输入 127.0.0.5 ==规则失败== 每 (Id123=TestDummy(值正则表达式 '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')) // 我希望它通过 ==规则通过== 每 (Id123=TestDummy(Value regexp '127.0.0.5'))
问题
谁能帮我提供一个 NEsper 正则表达式匹配的示例?或者也许指出我在代码中的愚蠢错误。
代码
这是我的 NEsper 演示包装类
public class NesperAdapter
{
public MatchEventSubscrtiber Subscriber { get; set; }
internal EPServiceProvider Engine { get; private set; }
public NesperAdapter()
{
//This call internally depend on log4net,
//will throw an error if log4net cannot be loaded
EPServiceProviderManager.PurgeDefaultProvider();
//config
var configuration = new Configuration();
configuration.AddEventType("TestDummy", typeof(TestDummy).FullName);
configuration.EngineDefaults.Threading.IsInternalTimerEnabled = false;
configuration.EngineDefaults.Logging.IsEnableExecutionDebug = false;
configuration.EngineDefaults.Logging.IsEnableTimerDebug = false;
//engine
Engine = EPServiceProviderManager.GetDefaultProvider(configuration);
Engine.EPRuntime.SendEvent(new TimerControlEvent(TimerControlEvent.ClockTypeEnum.CLOCK_EXTERNAL));
Engine.Initialize();
Engine.EPRuntime.UnmatchedEvent += OnUnmatchedEvent;
}
public void AddStatementFromRegExp(string regExp)
{
const string pattern = "any (Id123=TestDummy(Value regexp '{0}'))";
string formattedPattern = String.Format(pattern, regExp);
EPStatement statement = Engine.EPAdministrator.CreatePattern(formattedPattern);
//this is subscription
Subscriber = new MatchEventSubscrtiber();
statement.Subscriber = Subscriber;
}
internal void OnUnmatchedEvent(object sender, UnmatchedEventArgs e)
{
Console.WriteLine(@"Unmatched event");
Console.WriteLine(e.Event);
}
public void SendEvent(object someEvent)
{
Engine.EPRuntime.SendEvent(someEvent);
}
}
然后订阅者和一个 DummyType
public class MatchEventSubscrtiber
{
public bool HasEventFired { get; set; }
public MatchEventSubscrtiber()
{
HasEventFired = false;
}
public void Update(IDictionary<string, object> rows)
{
Console.WriteLine("Match event fired");
Console.WriteLine(rows);
HasEventFired = true;
}
}
public class TestDummy
{
public string Value { get; set; }
}
和 NUnit 测试。如果有评论 nesper.AddStatementFromRegExp(regexp); 行和取消注释//nesper.AddStatementFromRegExp(input);
行然后测试通过。但是我需要一个正则表达式。
//Match any IP address
[TestFixture(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", "127.0.0.5")]
public class WhenValidRegexpPassedAndRuleCreatedAndPropagated
{
private NesperAdapter nesper;
//Setup
public WhenValidRegexpPassedAndRuleCreatedAndPropagated(string regexp, string input)
{
//check it is valid regexp in .NET
var r = new Regex(regexp);
var match = r.Match(input);
Assert.IsTrue(match.Success, "Regexp validation failed in .NET");
//create and start engine
nesper = new NesperAdapter();
//Add a rule, this fails with a correct regexp and a matching input
//PROBLEM IS HERE
nesper.AddStatementFromRegExp(regexp);
//PROBLEM IS HERE
//This works, but it is just input self-matching
//nesper.AddStatementFromRegExp(input);
var oneEvent = new TestDummy
{
Value = input
};
nesper.SendEvent(oneEvent);
}
[Test]
public void ThenNesperFiresMatchEvent()
{
//wait till nesper process the event
Thread.Sleep(100);
//Check if subscriber has received the event
Assert.IsTrue(nesper.Subscriber.HasEventFired,
"Event didn't fire");
}
}