Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Emanuele Della Valle
    @emanueledellavalle

    EPL

    EPL is also adopted by Oracle CEP
    Emanuele Della Valle
    @emanueledellavalle

    schema of the events for the running example

    create schema TemperatureSensorEvent (
    sensor string,
    temperature double
    );
    
    create schema SmokeSensorEvent (
    sensor string,
    smoke boolean
    );
    
    create schema FireEvent (
    sensor string,
    smoke boolean,
    temperature double
    );

    some event that we use to simulate the real data stream

    TemperatureSensorEvent={sensor='S1', temperature=30}
    
    SmokeSensorEvent={sensor='S1', smoke=false}
    
    t=t.plus(1 seconds)
    
    TemperatureSensorEvent={sensor='S1', temperature=40}
    
    SmokeSensorEvent={sensor='S1', smoke=true}
    
    t=t.plus(1 seconds)
    
    TemperatureSensorEvent={sensor='S1', temperature=55}
    
    SmokeSensorEvent={sensor='S1', smoke=true}
    
    t=t.plus(1 seconds)
    
    TemperatureSensorEvent={sensor='S1', temperature=55}
    
    SmokeSensorEvent={sensor='S1', smoke=true}
    
    t=t.plus(1 seconds)
    TemperatureSensorEvent={sensor='S1', temperature=55}
    
    SmokeSensorEvent={sensor='S1', smoke=true}
    
    t=t.plus(4 seconds)
    t=t.plus(4 seconds)
    Emanuele Della Valle
    @emanueledellavalle

    queries

    Q0

    //sensors that measure a temperature above 50 C
    
    @Name('Q0') 
    select * 
    from TemperatureSensorEvent
    where temperature > 50;
    Emanuele Della Valle
    @emanueledellavalle

    Q1

    sensors that observe smoke (smoke either true or false)
    @Name('Q1') 
    select * 
    from SmokeSensorEvent
    sensors that observe smoke (smoke=true)
    @Name('Q1') 
    select * 
    from SmokeSensorEvent
    where smoke = true;

    Q2

    the average temperature observed by the sensors
    @Name('Q2') 
    select avg(temperature) 
    from TemperatureSensorEvent;
    Emanuele Della Valle
    @emanueledellavalle

    Q3

    the average temperature observed by the sensors in the last 4 seconds
    
    @Name('Q3') 
    select avg(temperature) 
    from TemperatureSensorEvent.win:time(4 seconds);
    Emanuele Della Valle
    @emanueledellavalle

    Q4

    the average temperature observed by the sensors in the last 4 seconds updating the window after 4 seconds
    @Name('Q4') 
    select avg(temperature) 
    from TemperatureSensorEvent.win:time_batch(4 sec);

    Q5

    the average temperature observed by the sensors in the last 5 events
    @Name('Q5') 
    select avg(temperature) 
    from TemperatureSensorEvent.win:length(5);
    Emanuele Della Valle
    @emanueledellavalle

    Q6

    the average temperature observed by the sensors in the last 5 events updating the window after 5 events
    @Name('Q6') 
    select avg(temperature) 
    from TemperatureSensorEvent.win:length_batch(5);
    Emanuele Della Valle
    @emanueledellavalle

    Q7

    the average temperature observed by the sensors in the last 4 seconds computed every 2 seconds
    @Name('Q7') 
    select avg(temperature) 
    from TemperatureSensorEvent.win:time(4 seconds)
    output snapshot every 2 seconds;
    Emanuele Della Valle
    @emanueledellavalle

    Q8

    (almost) the query of the running example
    @Name('Q8')  
    select a.sensor
    from pattern [
        every (
            a = SmokeSensorEvent(smoke=true) 
            -> 
            TemperatureSensorEvent(temperature > 50, sensor=a.sensor)
            where timer:within(2 sec)
            )
        ];
    Emanuele Della Valle
    @emanueledellavalle

    Q9

    @Name('Q9')
    the query of the running example
    insert into FireEvent 
    select a.sensor as sensor, 
           a.smoke as smoke, 
           b.temperature as temperature
    from pattern [
         every ( 
             a = SmokeSensorEvent(smoke=true) 
             -> 
             b = TemperatureSensorEvent(temperature>5, sensor=a.sensor) 
             where timer:within(2 sec) 
             ) 
         ] ;
    and the query that counts the number of fires in 10 minutes
    @Name('Fire')
    select count(*)
    from FireEvent.win:time(10 sec);
    Emanuele Della Valle
    @emanueledellavalle

    Exercise 1 stock tick event

    Assume a stock tick event

    StockTick(String name, int price)

    with the fields name and price representing the name of an company and the associated price for a stock tick.

    Q1.1

    Write a query which computes the average prices over the last 30 seconds
    select avg(price)
    from StockTick.win:time(30 sec)

    Q1.2

    Write a query which alerts on each IBM stock tick with a price greater then 100 followed by an IBM stock tick lower than 100 within 60 seconds
    Emanuele Della Valle
    @emanueledellavalle
    insert into alertIBM
    select *
    from pattern [
         every ( 
            StockTick(name="IBM",price>100) 
            -> 
           (StockTick(name="IBM",price<100) where timer:within(60 seconds))
        ];

    Q1.3

    Write a query that returns the average price per name for the last 100 stock ticks
    select name, avg(price) as averagePrice
    from StockTick.win:length(100)
    group by name
    Emanuele Della Valle
    @emanueledellavalle

    to play with the every clause in pattern

    create schema A (
    n int
    );
    
    create schema B (
    n int
    );
    
    create schema C (
    n int
    );
    
    @Name('Q1')
    select a.n as A, b.n as B
    from pattern [
         every  a=A  -> (b=B where timer:within(3 sec))
         ] ;
    for the data
    A={n=1}
    t=t.plus(1 seconds)
    C={n=1}
    t=t.plus(2 seconds)
    B={n=1}
    t=t.plus(1 seconds)
    B={n=2}
    t=t.plus(1 seconds)
    A={n=2}
    t=t.plus(1 seconds)
    A={n=3}
    t=t.plus(1 seconds)
    B={n=3}
    t=t.plus(1 seconds)
    C={n=2}
    t=t.plus(1 seconds)
    t=t.plus(1 seconds)
    t=t.plus(1 seconds)