%      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.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% CONFLICT RESOLUTION %%%%%%%% modified 3/11 %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%% conflict_resolution
conflict_resolution([Select,Position,less],
			[[RULE,TIME_TAG,S_TIME_TAG,N_OF_UNDEF_SLOTS,VLIST]|CS],
			[Rule,Time_Tag,S_Time_Tag,N_of_Undef_Slots,VList],
			Result,
		[[Rule,Time_Tag,S_Time_Tag,N_of_Undef_Slots,VList]|New_CS]):-
	N_OF_UNDEF_SLOTS > N_of_Undef_Slots,
	!,
	conflict_resolution([Select,Position,less],CS,
			   [RULE,TIME_TAG,S_TIME_TAG,N_OF_UNDEF_SLOTS,VLIST],
			    Result,New_CS).
conflict_resolution(S,[[R0,TT0,STT,NUS0,VL0]|CS],
			[R1,TT1,STT,NUS1,VL1],
			R,
			[[R1,TT1,STT,NUS1,VL1]|New_CS]) :-
	NUS0 < NUS1,
	!,
	conflict_resolution(S,CS,[R0,TT0,STT,NUS0,VL0],R,New_CS).
conflict_resolution(lex,[[R0,TT0,STT0,NUS0,VL0]|CS],
				[R1,TT1,STT1,NUS1,VL1],
				R,
				[[R1,TT1,STT1,NUS1,VL1]|New_CS]) :-
	STT0 @> STT1,
	!,
	conflict_resolution(lex,CS,[R0,TT0,STT0,NUS0,VL0],R,New_CS).
conflict_resolution(mea,[[R0,TT0,STT0,NUS0,VL0]|CS],
				[R1,TT1,STT1,NUS1,VL1],
				R,
				[[R1,TT1,STT1,NUS1,VL1]|New_CS]) :-
	STT0 @> STT1,
	!,
	conflict_resolution(lex,CS,[R0,TT0,STT0,NUS0,VL0],R,New_CS).
conflict_resolution([oldest|S],[[R0,TT0,STT0,NUS0,VL0]|CS],
				[R1,TT1,STT1,NUS1,VL1],
				R,
				[[R1,TT1,STT1,NUS1,VL1]|New_CS]) :-
	STT0 @> STT1,
	!,
	conflict_resolution([oldest|S],CS,[R0,TT0,STT0,NUS0,VL0],R,New_CS).
conclict_resolution([latest|S],[[R0,TT0,STT0,NUS0,VL0]|CS],
		       [R1,TT1,STT1,NUS1,VL1],
		       R,
		       [[R1,TT1,STT1,NUS1,VL1]|New_CS]) :-
	STT0 @> STT1,
	!,
	conflict_resolution([latest|S],CS,[R0,TT0,STT0,NUS0,VL0],R,New_CS).
conflict_resolution(S,[H|CS],One,Result,[H|New_CS]) :-
	!,
	conflict_resolution(S,CS,One,Result,New_CS).
conflict_resolution(_,[],H,H,[]) :-
	!.

%%%%% ie_sort
ie_sort(lex,Instantiation,INSTANTIATION) :-
	speedy_sort(d,Instantiation,INSTANTIATION),
	!.
ie_sort([latest],Instantiation,INSTANTIATION) :-
	speedy_sort(d,Instantiation,INSTANTIATION),
	!.
ie_sort([oldest],Inst,INST) :-
	speedy_sort(a,Inst,INST),
	!.
ie_sort([latest,position,_],Inst,INST) :-
	speedy_sort(d,Inst,INST),
	!.
ie_sort(mea,[First|Instantiation],[First|INSTANTIATION]) :-
	speedy_sort(d,Instantiation,INSTANTIATION),
	!.
ie_sort([latest,1,_],[F|Inst],[F|INST]) :-
	speedy_sort(d,Inst,INST),
	!.
ie_sort([oldest,position,_],Inst,INST) :-
	speedy_sort(a,Inst,INST),
	!.
ie_sort([oldest,1,_],[F|Inst],[F|INST]) :-
	speedy_sort(a,Inst,INST),
	!.
ie_sort([latest,N,_],Inst,[NE|SORT]) :-
	inst_separate(Inst,N,NE,Sort),
	speedy_sort(d,Sort,SORT),
	% inst_connect(SORT,N,NE,INST),
	!.
ie_sort([oldest,N,_],Inst,[NE|SORT]) :-
	inst_separate(Inst,N,NE,Sort),
	speedy_sort(a,Sort,SORT),
	% inst_connect(SORT,N,NE,INST),
	!.

%%%%% inst_separate
inst_separate([H],_,H,[]) :-
	!.
inst_separate([H|T],1,H,T) :-
	!.
inst_separate([H|R],N,E,[H|T]) :-
	NN is N - 1,
	!,
	inst_separate(R,NN,E,T).

%%%%% inst_connect
inst_connect(R,1,E,[E|R]) :-
	!.
inst_connect([H|R],N,E,[H|T]) :-
	NN is N - 1,
	!,
	inst_connect(R,NN,E,T).
inst_connect([],_,E,[E]) :-
	!.
