Sequence Pattern Matching using StreamInsight 2.1

Answered Sequence Pattern Matching using StreamInsight 2.1

  • Friday, November 16, 2012 6:02 PM
     
      Has Code

    I have a continuous stream of events coming from a tcp source

    Event1 new { RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "A" , typ = "Wo",IsRelated=""},

    Event2 new { RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "B" , typ = "Wo",IsRelated=""},

    Event3 new { RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "C" , typ = "Wo",IsRelated=""},

    Event4 new { RaisedTime = DateTime.Parse("2012-09-01 01:16:00"), UserName = "X" , typ = "Yo",IsRelated=""},

    Event5 new { RaisedTime = DateTime.Parse("2012-09-01 01:16:00"), UserName = "Z" , typ = "Yo",IsRelated=""},

    I have a function lets call it IsRelated(string userName1, string userName2)

    what I want is if I get a event with typ = "WO" , followed by a event of type = "YO" in next 5 minutes

    where UserName of the two events are related to each other (which can be got by calling functions IsRelated)

    set the value of IsRelated = "Earlier UserName"

    so here in this case (say UserName=A and UserName=X are related so I want event 4 to have value of IsRelated="A"

    Any suggestions are welcomed



All Replies

  • Sunday, November 18, 2012 2:42 AM
     
     Answered Has Code

    Simple enough.

    Take your event stream and select only the "Wo" events. Alter the Event Duration to your timeout (5 minutes, in your example).

    Join back to the original stream, selecting only "Yo" events and join on UserName == IsRelated.

    Here's a mockup in LinqPad:

    void Main()
    {
    	var events = new EventClass[]{ 
    	new EventClass() { RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "A" , EventType = "Wo",IsRelated=""},
    	new EventClass(){ RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "B" , EventType = "Wo",IsRelated=""},
    	new EventClass(){ RaisedTime = DateTime.Parse("2012-09-01 01:15:00"), UserName = "C" , EventType = "Wo",IsRelated=""},
    	new EventClass(){ RaisedTime = DateTime.Parse("2012-09-01 01:16:00"), UserName = "X" , EventType = "Yo",IsRelated="A"},
    	new EventClass(){ RaisedTime = DateTime.Parse("2012-09-01 01:16:00"), UserName = "Z" , EventType = "Yo",IsRelated=""}
    	};
    	
    	var stream = Application.DefineEnumerable(() => events)
    		.ToPointStreamable(e => PointEvent<EventClass>.CreateInsert(e.RaisedTime, e), AdvanceTimeSettings.IncreasingStartTime); 
    	
    
    	
    	var woEvents = stream.Where(e => e.EventType=="Wo").AlterEventDuration(e => TimeSpan.FromMinutes(5));
    	
    	//Any matching events in the following 5 minutes? 
    	var results = from f in woEvents 
    				  from s in stream.Where(e => e.EventType == "Yo") 
    				  where f.UserName == s.IsRelated 
    				  select new {
    				  	First = f, 
    					Next = s
    				  };
    				  
    		results.ToPointEnumerable().Dump(); 
    	
    }
    public class EventClass{
    	public DateTime RaisedTime{get;set;}
    	public string UserName { get; set; }
    	public string EventType { get; set; }
    	public string IsRelated { get; set; }
    }


    DevBiker (aka J Sawyer)
    Microsoft MVP - Sql Server (StreamInsight)


    Ruminations of J.net


    If I answered your question, please mark as answer.
    If my post was helpful, please mark as helpful.

    • Proposed As Answer by TXPower125 Sunday, November 18, 2012 4:11 PM
    • Marked As Answer by Iric WenModerator Monday, November 26, 2012 2:58 AM
    •