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

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

% ----------------------------------------------------------------
% Defeasible Logic for Defeasible Rules
% @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
% @version 0.1 <1 Feb 2005>
% @since 0.1
% @dependencies list.prova, utils.prova, integrity.prova
% ----------------------------------------------------------------
 


% cache defeasible (sub-)goals
% moved to cache.prova


% strict knowldege is also defeasible derivable
defeasible([P|Args]) :- bound(P), derive([P|Args]).

% a defeasible rule is blocked if it is not neg blocked
blocked(defeasible([P|Args])):-bound(P), not(neg(blocked(defeasible([P|Args])))).

% negative information by default overrides positive information
% overrides(neg([P|Args]), [P|Args]):-bound(P).
	
% derive integrity constraints from overrides definitions
%integrity(xor(Oppo1,Oppo2)):-
%	overrides(R1,R2),
%	oid(R1,Oppo1),
%	oid(R2,Oppo2). 	

%----------------------------------------------------------------------------
%
%	Defeasible Updates and Transactions
%
%----------------------------------------------------------------------------

%TODO Defeasible Updates and Transactions
% e.g. based on String concatenation build defeasible meta program
	
%----------------------------------------------------------------------------
%
%	Defeasible Logic with Integrity Constraints / Rules
%
%----------------------------------------------------------------------------

%TODO: Write java.utils.List version which is faster

% --------------------------------------------------------------------------- 
% defeated(OID, Literal)
%
% Test whether the Literal defeasible is defeated.
% The literal might be the head of a defeasible rule. 
%
% @param OID the oid of the defeasible rule
% @param Literal A defeasible literal (rule head) of the form p(x1..xn) possibly negated with neg
%----------------------------------------------------------------------------

%TODO: Cached version

defeated(OID, Literal):-
	% test XOR (mutex) integrity constraints
	integrity([xor|Mutex]), % all mutex integrities
	delete(Literal,Mutex,NMutex), % list with opposers
	member(Opposer,NMutex), % all opposers
	neg(blocked(defeasible(Opposer))), % opposer not blocked	
	not(neg(overruled(OID,Literal,Opposer))),  
	!.

neg(overruled(OID,Literal,Opposer)):-
	overrides(Literal, Opposer),!.
neg(overruled(OID,Literal,Opposer)):-
	oid(OpposerID,Opposer),
	overrides(OID,OpposerID),!.
neg(overruled(OID,Literal,Opposer)):-	
	oid(ModuleID,Literal),	% get module id
	oid(OpposerModuleID,Opposer),	% get opposer module id
	overrides(ModuleID,OpposerModuleID), % defeated
	!.
