Subdividing a Sequence: Exercise

Set a subet of events to occur within a specific amount of time independent of the overall timing.

When you define a pattern-matching query, you may need a subset of the events to occur within a specific amount of time independent of the overall timing. For example, perhaps front running is defined in your organization as a broker order being placed no more than two seconds before a valid order for the same stock, which must occur no more than 10 seconds after the customer order arrives.

  1. Replace the Query statement in the Queries tab with the following text (or copy the text from PatternMatchingCCL.txt between the lines "-- Pattern Matching Code Block 3" and "-- Pattern Matching Code Block 4"):
    /* Match a pattern indicating front running:
         Row with customer order
         (from either of two streams)
         Row with placed order for broker
         Row with placed order for customer
         (last two within 2 secs) */
    
    INSERT INTO StreamFraudAlerts
    SELECT "Front Running", Broker.OrderId, Broker.Symbol, Broker.Customer
    From StreamPhoneOrders Phone, StreamWebOrders Web, 
    	StreamPlacedOrders Broker, StreamPlacedOrders Cust
    MATCHING [10 SECONDS: Phone || Web, [2 SECONDS: Broker, Cust]]
    ON Phone.Broker = Web.Broker = Broker.Customer = Cust.Broker 
    	AND Phone.Symbol = Web.Symbol = Broker.Symbol = Cust.Symbol
    WHERE Phone.OrderId = Cust.OrderId 
    	OR Web.OrderId = Cust.OrderId;
    

    The only change to this Query statement from the previous activity is in the MATCHING clause. The entire pattern must still happen within 10 seconds, but now the row arriving on Cust must also arrive within two seconds of the row arriving on Broker.

  2. Start the project and let it run to completion.
  3. Examine the StreamFraudAlerts stream viewer:


    SubFAStream

    Now only a single sequence of rows matches the pattern and produces an output row. Look through the other viewers to ensure that you can identify the rows that matched the pattern.

  4. Stop the project.

Exercise

Complete the following exercise to explore additional pattern matching capabilities. See Solutions for a possible solution to this exercise.