% ------------------------------------------------------------------------------------------------------
% SLA Contract
% 
% ------------------------------------------------------------------------------------------------------

module("sla_sample.prova").

:-eval(consult('ContractLog/math.prova')).
:-eval(consult('ContractLog/list.prova')).
:-eval(consult('ContractLog/list_math.prova')).
:-eval(consult('ContractLog/datetime.prova')).
:-eval(consult('ContractLog/timeinterval.prova')).
:-eval(consult('ContractLog/ec.prova')).
:-eval(consult('examples/function_tests/ec_db/db.prova')).
:-eval(consult('ContractLog/utils.prova')).

% ------------------------------------------------------------------------------------------------------
% Contract
% ------------------------------------------------------------------------------------------------------

% +++++++++++++++ open database +++++++++++++++++++++++++++

location(database,"rbsla","jdbc:mysql://localhost/rbsla","root","").
table("events").

% ++++++++++++++++++ set database timelimit ++++++++++++++++

happens(pingSucceeded,datetime(2005,1,1,0,0,0)).
timelimit(datetime(2005,1,1,0,0,0)). % start time limit


% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%% ++++++++++++++++++++ Test Webserver ++++++++++++++++++++++++++++
testWebserver():-
rbsla.utils.WebsiteAvailable.testWebserver("http://ibis.in.tum.de/"),
!.
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



% +++++++++++++++  collect data for availability ++++++++++++++++++++++



% +++++++++++++++  ECA Rule: test homepage every 10 seconds ++++++++++++++

%ECA 1
eca(everyTenSec(),pingFailed(),alive(),assertFailed(),_,_).

%TIME
everyTenSec() :-
	println(["Test Time"]),
	sysTime(DT),
	interval(timespan(0,0,0,10), DT).

%EVENT
pingFailed() :-
	println(["Ping service"]),
	testWebserver(),!,fail().
pingFailed().

%CONDITION
alive():-
sysTime(DT),
println(["Ping failed: ",DT]),
notholdsAt(serverDown,DT).

%ACTION
assertFailed() :-	
	sysTime(DT),
	println(["Write into database: ",DT]),
	timepoint_construct(TP, DT, "pingFailed"),
	save_timepoint(TP).




%ECA 2
eca(everyTenSec(),pingSucceeded(),down(),assertSucceeded()).

%TIME
%Reuse TIMER from ECA rule 1

%EVENT
pingSucceeded() :-
	sysTime(DT),
	testWebserver(), 
	println(["Ping succeeded:",DT]).


%CONDITION
down():-
sysTime(DT),
holdsAt(serverDown,DT).

%ACTION
assertSucceeded() :-
	sysTime(DT),
	println(["Write into database: ",DT]),
	timepoint_construct(TP, DT, "pingSucceeded"),
	save_timepoint(TP).





% +++++++++++++++  Event Calculus rules  ++++++++++++++++++++++

initiallyTrue(serverDown).
initiates(pingFailed,serverDown,T).
terminates(pingSucceeded,serverDown,T).




	
% ++++++++++++++++++ Report ++++++++++++++++++++++++++++

%% +++++++++++++++ send report each week ++++++++++++++++++++++	

% ECA Rule: Send Report each week
eca(eventintervalEachMinute(DT),_,_,actionReportSystem(DT)).

eventintervalEachMinute(DT) :-
	sysTime(DT),
	interval(timespan(0,0,0,30), DT).

actionReportSystem(DT) :-
	mvi(serverDown,DownTime)	
	println(["-------------------------------------"]),
	println(["Report:"]),
	println(["Down Time:",DownTime]),
	println(["-------------------------------------"]).

