%:- auto_table.

:- export
	make_frame/1,
	get_text/3.

:- import build/2 from build.
%:- import pretty_print/1 from pretty_print.
:- import str_cat/3 from string.
:- import member2/2 from listutil.
%:- import append/3 from lists.

make_frame([]).

make_frame([H|T]) :-
	make_frame(H),
	make_frame(T).

make_frame(files(FileList)) :-
	make_frame(FileList).

make_frame(FileName) :-
	atom(FileName),
	build(FileName, graph(EdgeList)),
	Thing =.. [txt, _, _, _],
%	member2(Thing, EdgeList),
%	write(Thing), nl,
%	write(EdgeList), nl,
%	EdgeList = [Head|Tail], write(Head), nl, assert(Head),
	findall(Thing, member2(Thing, EdgeList), Text),
%	write(Text), nl,
	claim_graph(Text),
	setof(ann(D,E,F), member2(ann(D,E,F), EdgeList), Ann),
	claim_facts(FileName, Ann),
	abolish(txt, 3),
	prune(FileName).

prune(FileName) :-
	Fact =.. [FileName, _, _],
	setof(Fact, call(Fact), Facts),
	abolish(FileName, 2),
	claim(Facts).

claim([]).

claim([H|T]) :-
	assert(H),
	claim(T).

claim_graph([]).

claim_graph([H|T]) :-
%	write(H), nl,
	assert(H), !,
	claim_graph(T).

claim_facts(_, []).

claim_facts(FileName, [ann(A,B,C)|T]) :-
	get_text(A,B,Text),
	Claim =.. [FileName, C, Text],
%	write(Claim), nl,
	assert(Claim), !,
	claim_facts(FileName, T).

get_text(Start, Start, []).

get_text(Start, End, Text) :-
	Query =.. [txt, Start, Middle, Prefix],
	call(Query),
	get_text(Middle, End, Rest),
	(Rest = [], Prefix = Text;
	str_cat(Prefix, Rest, Text)), !.

