% ------------------------------------------------------------------------------------------------------
%  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
%
% ------------------------------------------------------------------------------------------------------

% ------------------------------------------------------------------------------------------------------
% Update
% 
% Adds new ID-based update predicates to dynamically update and add new knowldege (rules and facts) to a knowldege base
% including transactional updates.
%
% Can be used e.g. in combination with testcases and integrity constraints or ECA rules.
%
% @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
% @version 0.2 <1 Nov 2006>
% @since 0.1
% ------------------------------------------------------------------------------------------------------

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


% ------------------------------------------------------------------------ 
% transaction(add(...)) % add knowldege, internally tests all test cases and integrity constraints
% transaction(add(file) add from file, ID = filename + test all test cases and integrity constraints
% transaction(remove(ID)) remove knowldege with ID + test all test cases and integrity constraints
%
% transaction(add(Update),Test) % with Test
% transaction(add(Filename), Test) % with Test
% transaction(remove(ID),Test) remove knowldege with ID and Test
%
% Transactional Update. If tests or updates fail --> rollback
%
% @param ID transaction id
% @param Update knowledge updates, e.g. "r(1):-f(1). f(1)."
% @param Test test case
% @param File file name of update script
% ------------------------------------------------------------------------ 	

% update transaction with specified test case
	
transaction([H|T],Test):-
	derive([H|T]),
	test(Test),
	commit(ID),						
	!.
transaction([H|T],Test):- % transaction failed
	rollback([H|T]), % rollback
	!,
	fail().		
	
transaction([H|T]):-
	derive([H|T]),
	testIntegrity(), % test all integrity constraints
	test(), % run all test cases
	commit(ID),						
	!.
transaction([H|T]):- % transaction failed
	rollback([H|T]), % rollback
	!,
	fail().	
	



% ------------------------------------------------------------------------ 
% rollback(ID)
%
% Rollback a transaction
%
% @param ID = ID of transaction
% ------------------------------------------------------------------------ 	
rollback(["add"|T]):- head(H,T), remove(H).	
rollback(["remove"|T]):- head(H,T), add(H).

% ------------------------------------------------------------------------ 
% commit(ID)
%
% Commit a transaction
%
% @param ID = ID of transaction
% ------------------------------------------------------------------------ 	
commit(ID).




	
% ------------------------------------------------------------------------ 
% add(ID, Input)
% add(Input)
%
% Add new knowledge to the knowldege base
%
% @param Input
%	File = a prova file (ID of new knowledge is filename)
%	"ID","Knowledge" = ID of new knowledge; Knowldege = new Knowledge
%
% Example:
%	add("./examples/test/test.prova").
%   add(id,"r(1):-f(1). f(1)."). % add rule "r(1):-f(1)." and fact "f(1)."
%   add(id,"r(X):-f(X)."). % add rule "r(X):-f(X)." 
%   add(id,"r(_0):-f(_0), g(_0). f(_0). g(_1)",[1,2]). % replace _0 and _2 with "1" and "2"
% ------------------------------------------------------------------------ 	
add(ID,java.lang.String.Input):-	add([ID,java.lang.String.Input,[]]).
add(ID,java.lang.String.Input,Bindings):-
	add([ID,java.lang.String.Input,Bindings]).
add(Input):- bound(Input), consult(Input).

%addModuleID([ID,Input,Bindings]):- not(module(ID)),print([]), !, assert(module(ID)).
%addModuleID(ID):- not(module(ID)),print([]), !, assert(module(ID)).
%addModuleID(ID).
		


% ------------------------------------------------------------------------ 
% remove(ID)
%
% Remove knowldege from the knowledge base
%
% @param ID
% ------------------------------------------------------------------------ 	
remove(ID):-bound(ID), retractall(ID). %, retractall(module(ID)).



