% ------------------------------------------------------------------------------------------------------
% Test the Prova Rule Implementation
% ------------------------------------------------------------------------------------------------------

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






% ------------------------------------------------------------------------ 
%
% Some test events / test data


initiallyTrue(serverIsDown).

% small test set

happens(serverAwakes, datetime(2006,1,1,0,0,0)).
happens(serverGoesDown, datetime(2006,1,1,13,20,0)).

% test set 12 events
test_set12():-
assert(happens(serverAwakes, datetime(2006,1,1,13,20,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,20,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,20,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,20,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,20,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,20,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,20,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,20,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,20,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,21,0))).

% test set 22 events
test_set22():-
test_set12(),
assert(happens(serverAwakes, datetime(2006,1,1,13,21,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,21,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,21,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,21,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,21,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,21,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,21,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,21,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,21,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,22,0))).

% test set 32 events
test_set32():-
test_set22(),
assert(happens(serverAwakes, datetime(2006,1,1,13,22,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,22,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,22,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,22,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,22,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,22,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,22,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,22,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,22,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,23,0))).

% test set 42 events
test_set42():-
test_set32(),
assert(happens(serverAwakes, datetime(2006,1,1,13,23,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,23,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,23,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,23,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,23,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,23,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,23,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,23,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,23,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,24,0))).


% test set 52 events
test_set52():-
test_set42(),
assert(happens(serverAwakes, datetime(2006,1,1,13,24,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,24,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,24,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,24,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,24,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,24,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,24,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,24,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,24,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,25,0))).

% test set 62 events
test_set62():-
test_set52(),
assert(happens(serverAwakes, datetime(2006,1,1,13,25,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,25,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,25,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,25,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,25,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,25,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,25,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,25,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,25,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,26,0))).


% test set 72 events
test_set72():-
test_set62(),
assert(happens(serverAwakes, datetime(2006,1,1,13,26,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,26,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,26,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,26,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,26,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,26,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,26,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,26,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,26,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,27,0))).

% test set 82 events
test_set82():-
test_set72(),
assert(happens(serverAwakes, datetime(2006,1,1,13,27,1))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,27,2))),
assert(happens(serverAwakes, datetime(2006,1,1,13,27,3))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,27,4))),
assert(happens(serverAwakes, datetime(2006,1,1,13,27,5))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,27,6))),
assert(happens(serverAwakes, datetime(2006,1,1,13,27,7))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,27,8))),
assert(happens(serverAwakes, datetime(2006,1,1,13,27,9))),
assert(happens(serverGoesDown, datetime(2006,1,1,13,28,0))).





% ------------------------------------------------------------------------ 
%
% Some test rules

initiates(serverGoesDown, serverIsDown, T).
terminates(serverAwakes, serverIsDown, T).


% ------------------------------------------------------------------------ 
%
% Tests


%Test 1: holdsAt-Test

testHoldsAt() :-
println(["Test 1: holdsAt"]),
println(["-----------------------"]),
C1=rbsla.utils.WebsiteAvailable.getDate(),
T1=C1.getTimeInMillis(),

holdsAt(serverIsDown,datetime(2006,1,1,13,35,0)),

C2=rbsla.utils.WebsiteAvailable.getDate(),
T2=C2.getTimeInMillis(),
math_sub(T2,T1,DIF),
println(["Duration:",DIF]),
println(["-----------------------"]).




%Test 1.1: not holdsAt-Test

testnotHoldsAt() :-
println(["Test 1.1: not holdsAt"]),
println(["-----------------------"]),
C1=rbsla.utils.WebsiteAvailable.getDate(),
T1=C1.getTimeInMillis(),

neg(holdsAt(serverIsDown,datetime(2006,1,1,13,28,0))),

C2=rbsla.utils.WebsiteAvailable.getDate(),
T2=C2.getTimeInMillis(),
math_sub(T2,T1,DIF),
println(["Duration:",DIF]),
println(["-----------------------"]).




%Test 2: valueAt-Test




trajectory(serverIsDown, Before, serverDownDuration, After, X):-
	math_sub(After,Before,X).
	
antitrajectories(serverIsDown, Before, serverDownDuration, Tdelta, X):-
	valueAt(serverDownDuration, Before, X).


testValueAt(Name) :-
println(["Test 2: ",Name]),
println(["-----------------------"]),
C1=rbsla.utils.WebsiteAvailable.getDate(),
T1=C1.getTimeInMillis(),

valueAt(serverDownDuration,datetime(2006,1,1,13,35,10),T),

println(["Server Downtime: ",T]),
C2=rbsla.utils.WebsiteAvailable.getDate(),
T2=C2.getTimeInMillis(),
math_sub(T2,T1,DIF),
println(["Duration:",DIF]),
println(["-----------------------"]).


:- solve(test_set82()).  % insert test set


:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt
:- solve(testHoldsAt()). % normal holdsAt


:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt
:- solve(testnotHoldsAt()). % normal notholdsAt

:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
:- solve(testValueAt("valueAt")). %normal valueAt
