
filterIsolated(OrderedGroupedCandidateHs):-
	maplist(eachExCandidate,OrderedGroupedCandidateHs).

eachExCandidate(EI-TIs):-
	maplist(hTranslate_mappedToClaID1(EI),TIs).

hTranslate_mappedToClaID1(EI,TI):-
	candidateH0(EI,TI,OneH),
	hTranslate_mappedToClaID(EI,TI,OneH).


gen_output_readIn_candiateH(PosEIs):-
	tell('candidateH.pl'), %tell('grammar_candidateH.pl'),	
	genAll(PosEIs,AllTs,TotalNum),%genAll_EIsigned(PosEIs,AllTs,TotalNum),
	write('%Total is '), write(TotalNum),nl,
	%print_list(AllTs),nl,
	told,
	consult('candidateH.pl').	%consult('grammar_candidateH.pl'),

indentifySharedHs(PosEIs,T_EITIs):-	%,OrderedGroups
	rb_new(Tree0),
	reverse(PosEIs,PosEIsReversedOrder),
	insertTheories(PosEIsReversedOrder,Tree0,Tree), % insertTheories(AllTs,Tree0,Tree) *** be careful about the function you are using
	%scoreAll(AllTs,Tree,ScoredEIsTs).
	rb_visit(Tree,T_EITIs), %print_list(T_EITIs),

	maplist(outputSharedInfo,T_EITIs),
	findall(EINum-DL-{RID,Change,Time}-EITIs_any,(sharedControlReation([rs-{RID,enzymeLimiting,Change,Time}],EITIs_any,EINum,DL)),AllShared0),
	sort(AllShared0,AllShared),
	print_list(AllShared).




% this also include generating candidates
indentifySharedHs(PosEIs,T_EITIs,Grouped):-	%,OrderedGroups
	rb_new(Tree0),
	reverse(PosEIs,PosEIsReversedOrder),
	insertTheories(PosEIsReversedOrder,Tree0,Tree), % insertTheories(AllTs,Tree0,Tree) *** be careful about the function you are using
	%scoreAll(AllTs,Tree,ScoredEIsTs).
	rb_visit(Tree,T_EITIs), %print_list(T_EITIs),

	maplist(outputSharedInfo,T_EITIs), %listing,
	findall(EITIs_any,sharedControlReation(ClaID,EITIs_any),AllShared0),
	flatten(AllShared0,AllShared1),sort(AllShared1,AllShared), % this not only remove duplicate, but also facilitate the grouping later
	ti_GroupedByEI(AllShared,Grouped).
	%order_by_candidatesNum(Grouped,OrderedGroups),
	%print_list(OrderedGroups). 


order_by_candidatesNum(Grouped,OrderedGroups):-
    maplist(assignNum,Grouped,NumberedGroups),
    sort(NumberedGroups,OrderedGroups0),
    maplist(removeNum,OrderedGroups0,OrderedGroups,Nums),
    write(Nums),
    multiplyList(Nums,TotalSearchSpace),write(TotalSearchSpace),nl.

assignNum(EI-TIs,Num-EI-TIs):-
    length(TIs,Num).
removeNum(Num-EI-TIs,EI-TIs,Num).

outputSharedInfo([rs-{RID,enzymeLimiting,Change,Time}]-EITIs):-    
	exExtraction(EITIs,EIs),
     	length(EIs,EINum),EINum>1,!,
	ti_GroupedByEI(EITIs,EIGrouped),maplist(shortestLength,EIGrouped,Lengths),sumList(Lengths,DL),
	asserta(sharedControlReation([rs-{RID,enzymeLimiting,Change,Time}],EITIs,EINum,DL)).
outputSharedInfo(ClaID-EITIs).

shortestLength(EI-TIs,MinLength):-
	maplist(hScore(EI),TIs,Scores),
	min(Scores,1000,MinLength).
	

/*ti_GroupedByEI([],[]).
ti_GroupedByEI([EI-TI|Record],EIs):- 
	ti_GroupedByEI(Record,PreEIs),
	(append(Pre,[EI-TIs|Rest],PreEIs) ->
		EIs=[EI-[TI|TIs][EI-TIs|Rest],PreEIs);
		EIs=[EI-[TI]|PreEIs]
	).*/
% since you have sorted before grouping, so
ti_GroupedByEI([],[]).
ti_GroupedByEI([EI-TI|Record],EIs):- 
	ti_GroupedByEI(Record,PreEIs),
	(PreEIs=[EI-TIs|Rest] ->
		EIs=[EI-[TI|TIs]|Rest];
		EIs=[EI-[TI]|PreEIs]
	).

exExtraction([],[]).
exExtraction([EI-TI|Record],EIs):- 
	exExtraction(Record,PreEIs),
	(PreEIs=[EI|Rest] ->
		EIs=PreEIs;
		EIs=[EI|PreEIs]
	).
