% hypothesisSelection.pl -- original Name

:- use_module(library(lists)).
:- use_module(library(apply_macros)).

:-['testExamples/1.pl'].



/* what variables required? -- end node(base case); start node (stop)
1. score related to each returned hypothesis -- it is contained in the variable of hypothesis: [ClaID-Length-EIList,] -- which Cla is specially for which example can be determined from this information.
The length is a sum of each
The potential example covered is the union of those appeared at the EI-List.
% to improve efficiency, you may want to have seperated variable, and only alter them each time (rather than compute from scratch).

2. anything to be passed between each branch? -- is it OK to use maplist?


3. what to add the Current Node to itis children when it is returned. -- the EI_List is admended according t

4. backtrack when -- by using member after sorting.


% you can make this powerset more efficient, by gathering those for the same example, one of them is enough -- add another hypothesis for example 1 (1-3)

Always choose only one for EI, so only covered EI, no need of TI -- No, when you need to select among TIs for the same EI, than you need to remove

*/

% call hypothesisSelection(startNode,FinalHypothesis)

hypothesisSelection(endNode-EI-TI,[]). % no clauses at all
hypothesisSelection(CurrentNode,[CurrentNode-CurrentCovering|BestCandidate]):-
	expandNode(CurrentNode,ChildNodes),
	maplist(hypothesisSelection,ChildNodes,ReturnedCandidates),
	candidateSelection(ReturnedCandidates,BestCandidate),
	coverageCheck(BestCandidate,CurrentCovering).

%	claNode(CurrentNode,CurrentNodeLength,CurrentNodeEIList).
	

coverageCheck(BestCandidate,CurrentCovering):-
	maplist(extractEIList,BestCandidate,EILists),
	remove_duplicates(EILists,CurrentCovering).
	

extractEIList(ClaID-EIList,EIList).

candidateSelection(ReturnedCandidates,BestCandidate):-
	combineCandidatesAndScore(ReturnedCandidates,AllPossibleCombinations),
	%scoreEachCandidate()-- deal with it when the same EI.
	sort(AllPossibleCombinations,Sorted),
	Sorted = [BestCandidate|Rest]% take care, the fist element is [],
	
	
	

expandNode(CurrentNode,ChildNodes):-
	findall(Child,connect(CurrentNode,Child),ChildNodes).



/*
powersets([],[[]]).
powersets([Ele|List],Powersets):-
	powersets(List,PrePowersets),
	maplist(attachHead(Ele),PrePowersets,ElePrePowersets), % each is appended with Ele
	append(PrePowersets,ElePrePowersets,Powersets).

%attachHead(Ele,List,[Ele|List]).
attachHead(Ele,Pre,New):- append(Ele,Pre,New).

*/

% DescriptionLength-CoveredEI-Clas

combineCandidatesAndScore([],[0-[]-[]]).
combineCandidatesAndScore([OneCandidate|Candidates],CombinedResults):-
	combineCandidatesAndScore(Candidates,PreCombinedResults),
	mapCandidates(combine(OneCandidate),PreCombinedResults,AddedCombinedResults), % each is appended with OneCandidate
	append(PreCombinedResults,AddedCombinedResults,CombinedResults).





t(List,K):-
	%List=[1,2,3,4],
	powersets(List,Powersets),
	length(Powersets,K),
	write(Powersets).

h:-
	hypothesisSelection(startNode,FinalH), % -[1-1,1-2,2-1,2-2,3-1,3-2]
	write(FinalH).
	

