% ------------------------------------------------------------------------------------------------------
% Demonstrates the ECA Runtime API with out multi threading
% This might lead to delays in the ECA rule execution, for example if events cecks take some time,
% e.g. event needs complex calculations or internet calls, user interactions etc.
%
% Note: Use ecaruntimeMultiThreading in this cases !!!!!
%
%If the ECA rulebase consists of many ECA rules increase the timer loop interval "setInterval(<seconds>)"
% 
% Uses the \ContractLog\ecaruntime.prova implementation of the ContractLog framework
% ------------------------------------------------------------------------------------------------------

:-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/ecaruntime.prova')).

%-----------------------------------------------------------------------
% Define ECA rules
%-----------------------------------------------------------------------




% +++++++++++++++  ECA Rule1: Print a notification every 10 seconds ++++++++++++++
% 
%ECA 1

eca(everyTenSec(),_,_,printNotification(),_,_). % ECA rule definition

%TIME
everyTenSec() :-
	sysTime(DT),
	interval(timespan(0,0,0,10), DT).

%EVENT empty

%CONDITION emtpy

%ACTION
printNotification() :-	
	println(["ECA rule 1 fired"]).
	
	
	
	
	
% +++++++++++++++  ECA Rule2: is true for two seconds and than false for two seconds ++++++++++++++
%
%ECA 2 eca(T,E,C,A)
 

eca(eventintervalTwoSeconds(),_,_,actionChangeFlag()).

trueFalseRotating().

%TIME
eventintervalTwoSeconds() :-
	sysTime(DT),
	interval(timespan(0,0,0,2), DT).
	
%EVENT empty

%CONDITION emtpy

%ACTION
actionChangeFlag():-
	trueFalseRotating(),
		println(["ECA rule 2: Set status to inactive"]),      
        retractall(trueFalseRotating()),
        !.
actionChangeFlag():-  
		println(["ECA rule 2: Set status to active"]),      
        assert(trueFalseRotating()).
        


% +++++++++++++++  ECA Rule3: Check rule 3 every 15 seconds and fire if event is true ++++++++++++++
% 
%ECA 3

eca(everyFifteenSec(),checkStatus(),_,printStatus()). % ECA rule definition

%TIME
everyFifteenSec() :-
	sysTime(DT),
	interval(timespan(0,0,0,15), DT).

%EVENT empty
checkStatus() :-
	trueFalseRotating().

%CONDITION emtpy

%ACTION
printStatus() :-	
	println(["ECA rule 3 fired - Status active (from ECA rule 2)"]).        
	
	

%-----------------------------------------------------------------------
% Start the ECA runtime
%-----------------------------------------------------------------------

start():-
	showTimer(), % show the timer on console
	showConsole(), % print console output
	setInterval(1), % set the timer loop interval to 1 second
	startECARuntime(".\examples\function_tests\eca_runtime\ecaruntime.prova").
	
:- solve(start()).
	