%      Production system KORE/IE (version 12.48)
%
%          (C)1992 Institute for New Generation Computer Technology
%                          (Read COPYRIGHT for detailed information)
%
%      1992.7 Check and refine every programs 
%                             for IFS (ICOT Free Software) release.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% MODIFY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
# modify   --- remake fact with specifed expressions
	USAGE:modify(Element,[Expression_1,...,Expression_n]).
*/
	
%%%%% MODIFY/2 (at Top Level)
modify(Position,Bodies_List) :-
	integer(Position),
	pp_wm,
	ppwm_collect(remove,WMS),
	keysort(WMS,PPWM),
	SEARCH =.. [search|PPWM],
	arg(Position,SEARCH,Element-Ppwm),
	Ppwm =.. [FN|WM],
	structure(F,FN,FF,N,_,_),
	RET =.. [FN,Element|WM],
	!,
	retract(RET),
	remove_cs(Element),
	RCall =.. [FF,-,=,RN,RB,RVL,Inst,RNUS,Element|WM],
	(call(RCall),
	 ass(+,RB,RN,Inst,RNUS,RVL),
	 fail;
	 true),
	modify_check(F,Bodies_List,WM,[KS|New_WM]),
%	statistics(runtime,[Time_Tag,_]),
        tcp_now(timeval(_,Time_Tag)),
	NEW_WM =.. [FN,Time_Tag|New_WM],
%	RET_COMPLEX =.. [FN,_,_|New_WM],
%	wm_assert(NEW_WM,RET_COMPLEX),
	Call =.. [FF,IO,*,Rule,Rule_Base,
			VL,Instantiation,NUS,Time_Tag|New_WM],
	(call(Call),
	 ass(IO,Rule_Base,Rule,Instantiation,NUS,VL),
	 fail;
	 assert(NEW_WM)),
	!.
modify(Element,Bodied_List) :-
	number(Element),
	structure(F,FN,_,Arity,_,_),
	functor(RET,FN,Arity),
	RET =.. [FN,Time_Tag|WM],
	retract(RET),
	remove_cs(Element),
	structure(F,FN,FF,N,_,_),
	RCall =.. [FF,-,=,RRN,RRB,RVL,Inst,RNUS,TT|WM],
	(call(RCall),
	 ass(+,RRB,RRN,Inst,RNUS,RVL),
	 fail;
	 true),
	modify_check(F,Bodies_List,WM,[KS|New_WM]),
%	statistics(runtime,[Time_Tag,_]),
        tcp_now(timeval(_,Time_Tag)),
	NEW_WM =.. [FN,Time_Tag|New_WM],
%	RET_COMPLEX =.. [F,_,_|New_WM],
%	wm_assert(NEW_WM,RET_COMPLEX),
	Call =.. [FF,IO,*,Rule,Rule_Base,
			VL,Instantiation,NUS,Time_Tag|New_WM],
	(call(Call),
	 ass(IO,Rule_Base,Rule,Instantiation,NUS,VL),
	 fail;
	 asserta(NEW_WM)),
	!.
modify(_,_) :-
	!.

%%%%% MODIFY/5 (in Recognize Act Cycle)
modify(F, FF, Arity, Element, Bodies_List) :-
	functor(RET,F,Arity),
	RET =.. [F,Element|WM],
	retract(RET),
	remove_cs(Element),
	RCall =.. [FF,-,=,RN,RB,VL,Inst,NUS,Element|WM],
	(call(RCall),
	 ass(+,RB,RN,Inst,NUS,VL),
	 fail;
	 true),
	modify_convert(Bodies_List,WM,New_WM),
%	statistics(runtime,[Time_Tag,_]),
        tcp_now(timeval(_,Time_Tag)),
	NEW_WM =.. [F,Time_Tag|New_WM],
	Call =.. [FF,IO,*,RN,RB,VL,Inst,NUS,Time_Tag|New_WM],
	(call(Call),
	 ass(IO,RB,RN,Inst,NUS,VL),
	 fail;
	 asserta(NEW_WM)),
	!.
modify(_,_,_,_,_) :-
	!.

%%%%% modify_check
modify_check(F,[Atr = V|Bodies],WM,Latest) :-
	nonvar(V),
	position(F,Atr,Type,_,N),
	!,
	type_check(Type,V),
	NN is N - 2,
	modifing(NN,Atr,WM,V,New),
	!,
	modify_check(F,Bodies,New,Latest).
modify_check(F,[Atr = Pred|Bodies],WM,Latest) :-
	Pred =.. [Func|Atrs],
	NPred =.. [Func,V|Atrs],
	!,
	call(NPred),
	position(F,Atr,Type,_,N),
	!,
	type_check(Type,V),
	NN is  N - 2,
	modifing(NN,Atr,WM,V,New),
	!,
	modify_check(F,Bodies,New,Latest).
modify_check(F,[],LATEST,LATEST) :-
	!.

%%%%% modifing
modifing(1,S,[S = _|R],V,[V|R]) :-
	!.
modifing(1,S,[_|R],V,[V|R]) :-
	!.
modifing(N,S,[H|R],V,[H|T]) :-
	!,
	NN is N - 1,
	modifing(NN,S,R,V,T).
modifing(_,_,_,[],[]) :-
	!.

%%%%% modify_convert
modify_convert(['$$nil'|T],[H|R],[H|D]) :-
	!,
	modify_convert(T,R,D).
modify_convert(['$$status_change'|T],[H|R],[NH|D]) :-
	status_change(H,NH),
	!,
	modify_convert(T,R,D).
modify_convert([H|T],[_|R],[H|D]) :-
	!,
	modify_convert(T,R,D).
modify_convert([],[],[]) :-
	!.

%%%%% wm_assert
wm_assert(_,RET_COMPLEX) :-
	call(RET_COMPLEX),
	!,
	fail.
wm_assert(NEW_WM,_) :-
	assertz(NEW_WM),
	!.
