% ------------------------------------------------------------------------------------------------------
%  Copyright (C) 1999-2007 <a href="mailto:adrian.paschke@gmx.de">Adrian Paschke</a>
% 
% The Contractlog library is part of the Rule Based Service Level Agreement (RBSLA)
%  project, available at 
% <a href="https://sourceforge.net/projects/rbsla">SourceForge RBSLA</a>
% 
% More information <a href="http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA Project Site</a>
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
%  version 2 of the License, or (at your option) any later version.
% 
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
%
% ------------------------------------------------------------------------------------------------------


% ------------------------------------------------------------------------------------------------------
% ECA Runtime
% 
% Integrates the eca runtime into prova 
%
% @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
% @version 0.2 <1 Nov 2006>
% @since 0.1
% ------------------------------------------------------------------------------------------------------

moduleOID("./ContractLog/ecaruntime.prova").


%-------------------------------------------------------- 
%ECA runtime settings
%--------------------------------------------------------

daemonInterval(1). % default daemon loop interval = 1 second.
timer(Boolean.TRUE). % default print timer on console (hide timer = false)
console(Boolean.TRUE). % default show console output (hide console = false)



%------------------------------------
% Start the ECA runtime without multi threading.
% All (T)ECA rules in the given <Prova File> are evaluated
% in a loop every interval time. 
%
% The interval can be set by setInterval(<Interval in seconds>).
%-------------------------------------
startECARuntime(File):-
	rbsla.RBSLAStart.getRBSLAflag(),
	rbsla.RBSLAStart.setRBSLAflag(Boolean.FALSE),
	Wrapper = rbsla.wrapper.ProvaWrapper(File),
	console(C),
	Wrapper.showPrintWriter(C),
	%RS = Wrapper.consult("eca(TIME, EVENT, CONDITION, ACTION, POSTCONDITION, ELSE)"),
	KB = rbsla.eca.ActiveKnowledgeBase(Wrapper),
	Daemon = rbsla.eca.Daemon(),
	Daemon.setActiveKnowledgeBase(KB),
	daemonInterval(I),
	Daemon.setInterval(I),
	timer(F),
	Daemon.showTimer(F),
	Daemon.run().


%------------------------------------
% Start the ECA runtime with multi threading.
% For every (T)ECA rule evaluation an own thread is used
% from a internal thread pool.
% All ECA rules in the given <File> are evaluated
% in a loop every interval time.
%
% The interval can be set by setInterval(<Interval in seconds>).
%-------------------------------------
startECARuntimeMultiThreading(File):-	
	rbsla.RBSLAStart.getRBSLAflag(),
	rbsla.RBSLAStart.setRBSLAflag(Boolean.FALSE),
	Wrapper = rbsla.wrapper.ProvaWrapper(File),
	console(C),
	Wrapper.showPrintWriter(C),
	%RS = Wrapper.consult("eca(TIME, EVENT, CONDITION, ACTION, POSTCONDITION, ELSE)"),
	KB = rbsla.eca.ActiveKnowledgeBase(Wrapper),
	Daemon = rbsla.eca.DaemonMultithreading(),
	Daemon.setActiveKnowledgeBase(KB),
	daemonInterval(I),
	Daemon.setInterval(I),
	timer(F),
	Daemon.showTimer(F),
	Daemon.run().

		
%------------------------------------
% set a new interval for the evaluation of ECA rules.
%-------------------------------------
setInterval(I):-
	retractall(daemonInterval(_)),
	assert(daemonInterval(I)).

	
%------------------------------------
% show the timer on the console
%-------------------------------------
showTimer():-
	retractall(timer(_)),
	assert(timer(Boolean.TRUE)).


%------------------------------------
% hide the timer
%-------------------------------------	
hideTimer():-
	retractall(timer(_)),
	assert(timer(Boolean.FALSE)).


%------------------------------------
% show the console output
%-------------------------------------
showConsole():-
	retractall(console(_)),
	assert(console(Boolean.TRUE)).

%------------------------------------
% hide the console
%-------------------------------------	
hideConsole():-
	retractall(console(_)),
	assert(console(Boolean.FALSE)).


		
%---------------------------------------
% Inference rule for eca rule syntax
%---------------------------------------
eca(_,EVENT,CONDITION,ACTION,_,_) :-
	eca(EVENT,CONDITION,ACTION).

eca(_,_,CONDITION,ACTION,_,_) :-
	eca(CONDITION,ACTION).	
	
eca(TIME,EVENT,CONDITION,ACTION,_,_) :-
	eca(TIME,EVENT,CONDITION,ACTION).
	
	



