#! /usr/bin/swipl -q -G1g -L1g -T1g -t main -s
% (home + travail)


% #! /usr/local/bin/swipl -q -G1g -L1g -T1g -t main -s
% (magi)



/* ========================================================================== */ 
/*     COUNT THE AVERAGE NUMBER OF POSSIBILITIES OF AMBIGUOUS EXAMPLES        */
/* ========================================================================== */

main :-
        catch(goal, E, (print_message(error, E), fail)),
        halt.

main :-
        halt(1).

goal :-
   get_arguments(File),
   !,
   count_possibilities(File).

 
get_arguments(File) :-
        current_prolog_flag(argv, Argv),
        append(_, [--|[File]], Argv).


count_possibilities(File) :- 
        writeln('begin...'),     
        call_modules,
        writeln('loading examples...'),
        load_examples_file(File),
        get_examples(LPos, LTabs_pos, LNeg, LTab_neg),
        length(LPos, Npos),
        writeln('counting the possibilities...'),
        count_the_possibilities(pos, LPos, LTabs_pos, Npos, 
                                Average_pos_min, Average_pos_max),
        write('average number MIN of possibilities of the positive examples : '),
        writeln(Average_pos_min),
        write('average number MAX of possibilities of the positive examples : '),
        writeln(Average_pos_max),
        length(LNeg, Nneg),
        count_the_possibilities(neg, LNeg, LTab_neg, Nneg, 
                                Average_neg_min, Average_neg_max),
        write('average number MIN of possibilities of the negative examples : '),
        writeln(Average_neg_min),
        write('average number MAX of possibilities of the negative examples : '),
        writeln(Average_neg_max),
        nl,
        nl,
        writeln('end of the program.').


count_the_possibilities(_Signe, [], [], _Nex, 0.0, 0.0) :-
   !.


count_the_possibilities(Signe, [Ex|LEx], [Tabs_ex|LTabs], Nex, 
                        Average_min, Average_max) :-
   count_possibilities_one_example(Signe, min, Ex, Tabs_ex, N1_min),
   Average_min1 is N1_min*1.0/Nex,
   count_possibilities_one_example(Signe, max, Ex, Tabs_ex, N1_max),
   Average_max1 is N1_max*1.0/Nex,
   count_the_possibilities(Signe, LEx, LTabs, Nex, 
   Average_min2,  Average_max2),
   Average_min is Average_min1 + Average_min2,
   Average_max is Average_max1 + Average_max2.


count_possibilities_one_example(pos, min, (_Id, _Head, min_max(LPossibilities, _Max)), 
                                _, N) :-
   !,
   length(LPossibilities, N).

count_possibilities_one_example(pos, max, (Id, Head, min_max(_Min, Graph)), 
                                     (Id, Tabs_ex), N) :-
   !,
   build_tables_from_graph(graph(Id, Head, Graph), 
                           tabs(Id, Tabs_ex), 
                           tables(Id, Head, LTables)),
   count_possibilities_tables(LTables, N).

count_possibilities_one_example(neg, min, (_Id, _Head, min_max(LPossibilities, _Max)), 
                                _, N) :-
   !,
   length(LPossibilities, N).

count_possibilities_one_example(neg, max, (Id, Head, min_max(_Min, Graph)), 
                                         (Id, Tabs_ex), N) :-
   build_tables_from_graph(graph(Id, Head, Graph), 
                           tabs(Id, Tabs_ex), 
                           tables(Id, Head, LTables)),
   count_possibilities_tables(LTables, N).



count_possibilities_tables([Table], N) :-
   !,
   length(Table, N).

count_possibilities_tables([Table|LTables], N) :-
   length(Table, N1),
   count_possibilities_tables(LTables, N2),
   N is N1 * N2.


call_modules :-

   getenv('LEAR_DIR', Lear),

   atom_concat(Lear, '/manage_examples2.pl', M),   
   use_module(M),

   atom_concat(Lear, '/graph_examples2.pl', G),   
   use_module(G).
              
