% ------------------------------------------------------------------------------------------------------
% Demonstrates the interval-based Event Calculus event algebra
% 
% An event interval [E1,E2] is initated by an event E1 (initator)
% at time T1 (occurence time of E1) and terminated by an event E2 (terminator) at
% time T2 (occurence time of E2). 
%
% The event interval [E1,E2] holds "holdsInterval([E1,E2],[T1,T2]"
% between T1 and T2 if no other terminating event "terminates(E,[E1,E2],T)" or set of 
% terminating events "terminates([E',E'',..,EN],[E1,E2],T)" occurs in between. 
%
% The initiating event E1 might be an terminating event itself, i.e. 
% a event sequence e1 e1 e2 the first occurence of e1 will be terminated by the second 
% occurence of e1, if e1 is defined as a terminating event for [e1,e2]. Accordingly,
% the interval [e1,e2] will hold between the second occurence of e1 and the terminator e2.
% 
% 
% ------------------------------------------------------------------------------------------------------

% include basic predicates
:-eval(consult('ContractLog/math.prova')).
:-eval(consult('ContractLog/list.prova')).
:-eval(consult('ContractLog/list_math.prova')).
:-eval(consult('ContractLog/datetime.prova')).
:-eval(consult('ContractLog/ec.prova')).
:-eval(consult('ContractLog/utils.prova')).


%--------------------------------------------
% Define some event occurences, i.e. a event instance sequence (eis)
%
% EIS = a b c d a a b d a d
% ---------------------------------------------

occurs(a,[datetime(2006,1,1,0,0,0),datetime(2006,1,1,0,0,0)]).
occurs(b,[datetime(2006,1,1,0,0,1),datetime(2006,1,1,0,0,1)]).
occurs(c,[datetime(2006,1,1,0,0,2),datetime(2006,1,1,0,0,2)).
occurs(d,[datetime(2006,1,1,0,0,3),datetime(2006,1,1,0,0,3)]).
occurs(a,[datetime(2006,1,1,0,0,4),datetime(2006,1,1,0,0,4)]).
occurs(a,[datetime(2006,1,1,0,0,5),datetime(2006,1,1,0,0,5)]).
occurs(b,[datetime(2006,1,1,0,0,6),datetime(2006,1,1,0,0,6)]).
occurs(d,[datetime(2006,1,1,0,0,7),datetime(2006,1,1,0,0,7)]).
occurs(a,[datetime(2006,1,1,0,0,8),datetime(2006,1,1,0,0,8)]).
occurs(d,[datetime(2006,1,1,0,0,9),datetime(2006,1,1,0,0,9)]).
occurs(c,[datetime(2006,1,1,0,0,9),datetime(2006,1,1,0,0,9)]).


%--------------------------------------------
% Query some event algebra operators
% ---------------------------------------------

% free queries
terminates([a,b,c,d],[a,b],T).
terminates([a,b,d],[b,d],T).

% Sequence operator

detect(ce1,T):- event(sequence(a,b),T).
detect(ce2,T):- event(sequence(a,b,d),T).
detect(ce3,T):- event(sequence(sequence(a,b),d),T).
detect(ce4,T):- event(sequence(a,sequence(b,c),d),T).

:-solve(detect(ce1,T)).
:-solve(detect(ce2,T)).
:-solve(detect(ce3,T)).
:-solve(detect(ce4,T)).

% Or operator

detect(ce5,T):- event(or(c,d),T).
detect(ce6,T):- event(or(c, sequence(a,b,d)),T).

:-solve(detect(ce5,T)).
:-solve(detect(ce6,T)).


% And operator

detect(ce7,T):- event(and(c,d),T).

:-solve(detect(ce7,T)).

% Simultaneous operator

detect(ce8,T):-event(concurrent(c,d),T).
:-solve(detect(ce8,T)).

% Xor operator

detect(ce9,T):-event(xor(c,e),T).
:-solve(detect(ce9,T)).


% Neg operator
detect(ce10,T):-event(neg([a,b,d],[b,d]),T).
:-solve(detect(ce10,T)).


% Any operator
detect(ce11,T):-event(any(2,neg([a,b,d],[b,d])),T).
:-solve(detect(ce11,T)).

% Aperiodic operator
detect(ce12,T):-event(aperiodic(b,[a,d]),T).
:-solve(detect(ce12,T)).


:-solve(holdsInterval(sequence(a,b),T)).
:-solve(holdsInterval([sequence(a,b),sequence(b,d)],T)).