%      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.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% MAKE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
# make     --- add new fact into working memory
        USAGE:make(Fact).
        Fact ::= term
*/

%%%%% MAKE (At the Top Level)
make(Fact)  :-
	Fact =.. [F|Atrs],
%	statistics(runtime,[Time_Tag,_]),
        tcp_now(timeval(_,Time_Tag)),
	structure(F,FN,FF,_,[_|ANL],[_|ATL]),
	reforming_make(F,Atrs,ANL,ATL,Reformed,Restriction),
	cut_true_from_restrictions(Restriction,RESTRICTION),
	call(RESTRICTION),
	FACT =.. [FN,Time_Tag|Reformed],
	Call =.. [FF,IO,*,Rule_Name,Rule_Base,VL,
				Instantiation,NUS,Time_Tag|Reformed],
	(call(Call),
	 ass(IO,Rule_Base,Rule_Name,Instantiation,NUS,VL),
	 fail;
	 asserta(FACT)),
	!.

%%%% MAKE (in Recognize Act Cyclele)
make(F,FF,Atrs):-
%	statistics(runtime,[Time_Tag,_]),
        tcp_now(timeval(_,Time_Tag)),
        FACT =.. [F,Time_Tag|Atrs],
        Call =.. [FF,IO,*,Rule,Rule_Base,VL,
                        Instantiation,NUS,Time_Tag|Atrs],
        (call(Call),
         ass(IO,Rule_Base,Rule,Instantiation,NUS,VL),
         fail;
         asserta(FACT)),
        !.
make(_,_,_) :-
        !.

%%%%% ass
ass(+,Rule_Base,Rule,Time_Tags,Number_of_Undef_Slots,VL) :-
        retract(cs(Rule_Base,O)),
        strategy_rec(Rule_Base,_,Strategy),
        ie_sort(Strategy,Time_Tags,TIME_TAGS),
        assert(
             cs(Rule_Base,
                [[Rule,Time_Tags,TIME_TAGS,Number_of_Undef_Slots,VL]|O])),
        !.
ass(+,RB,R,TiTa,NUS,VL) :-
        strategy_rec(RB,_,S),
        ie_sort(S,TiTa,TITA),
        assert(cs(RB,[[R,TiTa,TITA,NUS,VL]])),!.
ass(-,RB,R,TiTa,_,_) :-
        retract(cs(RB,O)),
        ass_cut_member([R,TiTa,_,_,_],O,NEWO),
        assert(cs(RB,NEWO)),
        !.

%%%%% ass_cut_member
ass_cut_member(H,[H|T],T) :-
        !.
ass_cut_member(H,[X|T],[X|R]) :-
        !,
        ass_cut_member(H,T,R).
ass_cut_member(_,[],[]) :-
        !.

%%%%% reforming_make
reforming_make(F,Atrs,[Atr],[Type],[Value],[Restriction]) :-
	!,
	cut_make(Atr = Value,Atrs,ATRS,Type,F,Restriction),
	!.
reforming_make(F,Atrs,[Atr|ANL],[Type|ATL],
			[Value|Reform],[Restriction|RESTRICTION]):-
	!,
	cut_make(Atr = Value,Atrs,ATRS,Type,F,Restriction),
	!,
	reforming_make(F,ATRS,ANL,ATL,Reform,RESTRICTION).
reforming_make(_,_,[],[],[],Restriction):-
	!.

%%%%% cut_make
cut_make(A = V,[A = RV|T],T,Type,_,Restriction):-
	!,
	type_check_and_call(Type,RV,V,Restriction),
	!.
cut_make(X,[H|T],[H|R],Type,F,Restriction):-
	!,
	cut_make(X,T,R,Type,F,Restriction).
cut_make(A = O,[],[],Type,F,true) :-
	position(F,A,Type,O,_),
	!.
