% ------------------------------------------------------------------------------------------------------
% Demonstrates the interval-based Event Calculus 
% 
% 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)]).

%--------------------------------------------
% Define some terminating rules for the intervals 
% ---------------------------------------------
terminates([a,b,c,d],[a,d],T).
terminates([a,b,c,d],[a,b],T).


%--------------------------------------------
% Query some intervals
% Note the intervals [a,d] and [a,b] are strictly
% interpreted, i.e. no terminating event is allowed to occur
% in between, whereas [a,c] or [b,d] are not strictly
% interpreted.
% ---------------------------------------------

% free queries on atomic events

:-solve(holdsInterval([a,b],[T1,T2])).
:-solve(holdsInterval([a,d],[T1,T2])).
:-solve(holdsInterval([a,c],[T1,T2])).
:-solve(holdsInterval([b,d],[T1,T2])).


%  bound queries on atomic events

:-solve(holdsInterval([a,b],[datetime(2006,1,1,0,0,5),datetime(2006,1,1,0,0,6)])).
:-solve(holdsInterval([b,d],[datetime(2006,1,1,0,0,1),datetime(2006,1,1,0,0,7)])).

