
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This is the code for the Classifier component of ASE-Progol.

% The code consults the following three files:-

% 1/ A file containing hypoth(Loop_no, H, Compression) facts. H
% takes the form [Head, Body]. Head is the head of the hypothesis and
% Body is the body of the hypothesis surrounded by parenthesis. If a
% hypothesis is a fact then Body takes the value (true); this can be
% the case when Hypothesis Generator Component performs abduction.

% 2/ A file containing trial(E) facts.
% 3/ A file containing the static knowledge.

% The code outputs matrix_cell(H, Compression, E, Cell_value) facts to a file.

% Cell values are binary. 1 indicates that the static knowledge,
% the given hypothesis and the given trial are consistent.
% Otherwise the cell value is 0.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- set(r,10000)?
:- set(h,10000)?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

classify(Loop_no, Static_know_file, Trials_file, Hypotheses_file, File_out) :- 
	consult(Static_know_file),
	consult(Trials_file),
	consult(Hypotheses_file),
	tell(File_out),
	classify_2(Loop_no),
	told.

classify_2(Loop_no) :- 
	hypoth(Loop_no, H, _),
	H = [Head, Body],
	asserta((Head :- Body)),
	classify_exs(Loop_no, H),
	once(clause(Head,B,N)),
	retract(Head,N),
	fail.

classify_2(_).

% Could not implement the retraction as retract((Head :- Body)). 

% The once predicate must be used because the Head of a hypothesis may
% contain a variable; this prevents the combination of the retract and
% failure driven loop acting like an abolish predicate.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

classify_exs(Loop_no, H) :- 
	trial(E),
	consistent(Loop_no, H, E),
	fail.

classify_exs(_,_).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

consistent(Loop_no, H, E) :-
	E,
	!,
	write_matrix_cell(Loop_no, H, E, 1).

consistent(Loop_no, H, E):-
	write_matrix_cell(Loop_no, H, E, 0).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
once(P):- P, !.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

write_matrix_cell(Loop_no, H, E, Cell_value):-
	write('matrix_cell('),
	write(H),
	write(', '),
	hypoth(Loop_no, H, Compression),
	write(Compression),
	write(', '),
	write(E),
	write(', '),
	write(Cell_value),
	write(').'),
	nl.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
