% Demonstrate the classic discount rule:
%    "Give a 25% discount on items the customer hasn't bought before."
% Called from ws.prova.test.ProvaRunner2.java

orders_history("cust_A","item_A").
orders_history("cust_A","item_C").

rcvMsg(XID,Protocol,From,process,order(Order)) :-
	println(["Original order: ",Order]),
 	Customer=Order.getCustomer(),
 	Items=Order.getItems(),
	assign_discount(Order,Customer,Items).

assign_discount(Order,Customer,Items) :-
	element(Item,Items),
	not(orders_history(Customer,Item)),
	println(["Item not ordered before: ",Item]),
	Order.setDiscount(Item,0.25).

rcvMsg(XID,Protocol,From,submit,order(Order)) :-
	println(["Updated order: ",Order]),
 	Customer=Order.getCustomer(),
 	Items=Order.getItems(),
	record_history(Customer,Items).

record_history(Customer,Items) :-
	element(Item,Items),
	retractall(orders_history(Customer,Item)),
	assert(orders_history(Customer,Item)).

not([X|Args]) :-
	derive([X|Args]),
	!,
	fail().
not([X|Args]).
