Search code examples
espernesper

Why wont this context work?


I want the same statement to select between the times specified in the two contexts below. However I get no output. How can I get output when B is active and C is active using nested contexts in a single statement?

create context A
    context B start (0, 12, *, *, *) end (0, 18, *, *, *),
    context C start (0, 20, *, *, *) end (0, 23, *, *, *);

Statement:

context A select * from MyEvent;

Solution

  • This is because nested context are not an OR relationship. For OR use a pattern like at the end of this example.

    Assume event types AStart, AEnd, BStart, BEnd and C.

    create context CtxSampleNestedContext
      context SpanA start AStart end AEnd,
      context SpanB start BStart end BEnd;
    
    context CtxSampleNestedContext select count(*) from C;
    

    After creating the EPL statements above, the engine starts looking for an AStart event only and does not yet look for AEnd, BStart, BEnd or C events.

    Assume that an AStart event arrives next:

    • The engine stops looking for an AStart event.
    • The engine starts looking for an AEnd event, since that would mean the end of the current SpanA lifecycle.
    • The engine starts looking for a BStart event, in order to detect the beginning of a SpanB lifecycle.

    In the scenario, assume that a BStart event arrives. This is, logically, the beginning of the SpanB lifecycle:

    • The engine stops looking for further BStart events.
    • The engine starts looking for a BEnd event, since that would mean the end of the current SpanB lifecycle.
    • The engine keeps looking for an AEnd event, since that would mean the end of the current SpanA lifecycle.
    • The engine starts looking for C events and now starts counting each C that arrives.

    In the scenario, assume that a BEnd event arrives. This is, logically, the end of the SpanB lifecycle:

    • The engine stops looking for a BEnd event.
    • The engine stops looking for C events and stops counting each.
    • The engine starts looking for a BStart event, since that would mean the beginning of another SpanB lifecycle.

    In the scenario, assume that an AEnd event arrives. This is, logically, the end of the SpanA lifecycle:

    • The engine stops looking for an AEnd event.
    • The engine stops looking for a BStart event.
    • The engine starts looking for an AStart event, since that would mean the beginning of another SpanA lifecycle.

    In the scenario describe above, after the AEnd arrives, the engine is back to the same state as the engine had after the statements were created originally.

    If your use case calls for a logical OR relationships like for example so (not equivalent to above):

    create context CtxSampleNestedContext 
      start pattern[every a=AStart or every a=BStart] as mypattern 
      end pattern[every AEnd or every BEnd]