/* **************************************************************
	Query module 
**************************************************************** */

:- op(1180,fx,[trans]).

trans(X) :- 
	cleanup, !,
	parse_body(X,L,query),
	write_new_rules,
	assert((query :- L)), !,
	inserting(tr), call(L), deleting(tr).



% remove some useless rules from database
%
cleanup :-
	retract((query :- _)),
	cleanup.
cleanup :-
	cleanup_temp,		% remove temperory rules and predicates
	keyedlist(KList),
	clean_keyedlist(KList). % clean up tuples which are deleted


% remove temperory rules and predicates
cleanup_temp :-
	retract(templist(TList)),
	retract(looplist(LList)),
	clean_templist(TList),	% remove temperory predicates
	clean_looplist(LList),	% remove temperory rules
	assert(looplist([])),
	assert(templist([])).	


% remove all tuples in the database according to the input list
clean_templist([]).
clean_templist([Temp|Rest]) :-
	bulk_del(Temp),
	clean_templist(Rest).


% remove all rules in the database with the head in the input list
clean_looplist([]).
clean_looplist([Pred|Rest]) :-
	remove_all(Pred),
	clean_looplist(Rest).

remove_all(Pred) :-
	copy_term(Pred,Copy),
	retract((Copy :- _)),
	remove_all(Pred).
remove_all(_).

% look for tuples which are deleted and remove the tags from the database
clean_keyedlist([]).
clean_keyedlist([(Name,NArgs,NKeys)|List]) :-
	name(Name,NList),
	index_list(NArgs,IdList),
	append(IdList,NList,IdNList),
	append("i_key_",IdNList,IList),
	append("d_key_",IdNList,DList),
	name(IName,IList),
	name(DName,DList),
	createpreds(NKeys,Args),
	IPred =.. [IName|Args],
	DPred =.. [DName|Args],
	clean_pred(IPred,DPred),
	clean_keyedlist(List).

% remove predicates with both insert and delete tags
clean_pred(IPred,DPred) :-
	getlist(IPred,DPred,List), !,
	retractall(DPred),
	deletelist(List)
	;
	true.
	
