% ------------------------------------------------------------------------------------------------------
%------------------------------------------------------
% Test the defeasible logic with integrity constraints
%
% Example: 
%
% Module 1
%
% module("module1").
%	
% Facts
%
% status("Adrian",gold).
% status("Adrian",silver).
%
% Defeasible rules
%
% discount5: discount(X,5):= defeasible(status(X,silver)).
% discount10: discount(X,10):= defeasible(status(X,gold)).
%
% Integrity constraints
%
% integrity(xor(discount(X,5),discount(X,10))).
%
% Priorities
%
% overrides("discount10","discount5").
%
%
%
% Module 2
%
% module("module2").
%
% neg(payed("Adrian")).

% overrides("module 2","module 1").

% integrity(xor(discount(X,_),neg(discount(X))).
%
% nodiscount: neg(discount(X)):= defeasible(neg(payed("Adrian")).
%
%----------------------------------------------------------------------

testcase("defeasibleTestCase.prova").


%-----------------------------------------------------------------------
% Imported libraries
%-----------------------------------------------------------------------
:-eval(consult('ContractLog/utils.prova')). % need for not
:-eval(consult('ContractLog/list.prova')).	
:-eval(consult('ContractLog/integrity.prova')).
:-eval(consult('ContractLog/defeasible.prova')).

% ------------------------------------------------------------------------------------------------------
% (Meta) test rules
% ------------------------------------------------------------------------------------------------------



integrity(xor(discount(X,_),neg(discount(X)))).
integrity(xor(discount(X,5),discount(X,10))).


overrides("discount10","nodiscount").
overrides("discount10","discount5").



oid(discount5)::defeasible(discount(X,5)):-
	testIntegrity(discount(X,5)),
	defeasible(status(X,"silver")),	
	not(defeated("discount5",discount(X,5))).
neg(blocked(defeasible(discount(X,5)))):- testIntegrity(discount(X,5)), defeasible(status(X,"silver")).	
body(defeasible(discount(X,5))):- defeasible(status(X,"silver")).

oid(discount10)::defeasible(discount(X,10)):-
	testIntegrity(discount(X,10)),
	defeasible(status(X,"gold")),
	not(defeated("discount10",discount(X,10))).
neg(blocked(defeasible(discount(X,10)))):- testIntegrity(discount(X,10)), defeasible(status(X,"gold")).	
body(defeasible(discount(X,10))) :-	defeasible(status(X,"gold")).

oid(nodiscount)::defeasible(neg(discount(X))):-
	testIntegrity(neg(discount(X))),
	defeasible(neg(payed(X))),
	not(defeated("nodiscount",neg(discount(X)))).	
neg(blocked(defeasible(neg(discount(X))))):-  testIntegrity(neg(discount(X))), defeasible(neg(payed(X))).	
body(defeasible(neg(discount(X)))) :- defeasible(neg(payed(X))).

						


% ------------------------------------------------------------------------------------------------------
% Test facts
% ------------------------------------------------------------------------------------------------------

status("Adrian","gold"). % strict fact
status("Adrian","silver"). % strict fact
neg(payed("Adrian")). % strict fact

% ------------------------------------------------------------------------------------------------------
% Tests
% ------------------------------------------------------------------------------------------------------


testSuccess("Defeasible Test 1","Test 1 succeeded"):-testcase("defeasibleTestCase.prova"), defeasible(discount("Adrian",10)). 
testFailure("Defeasible Test 1","Wrong discount"):-not(testSuccess("Defeasible Test 1",Message)).


runTest("defeasibleTestCase.prova"):-testSuccess("Defeasible Test 1",Message).

