% =============================================================================
% Automated modeling in process-based qualitative reasoning
%  ** Main module
% 
% April - July 2007
% 
% Hylke Buisman   - hbuisman@science.uva.nl
% =============================================================================

%==============================================================================
% SETTINGS
%==============================================================================

% The path to the GARP3 start-up file
% garp_pl/1
% garp_pl(-GARPPath)

%garp_pl('N:/garp3-v1.3.8/startup').
%garp_pl('F:/garp3-v1.3.8/startup').
% garp_pl('/home/hbuisman/garp3-v1.3.8/startup').
garp_pl('/home/jliem/PrologWorkspace/Latest/startup').
%==============================================================================
% INITIALIZE
%==============================================================================

% Load all required files
%:- [kb, input, output].
%:- [naive_model_generation, dependency_interactions, condition_handling].

% Set all dynamic predicates
:- dynamic garp_running/1, model_dependency/2, dependency_bag/3.
:- dynamic unequal_somewhere/1, cluster_member/3, model_counter/1.

% Initialization of predicates
garp_running(false).

%==============================================================================
% MAIN
%==============================================================================

/*
:- 	writeln('> '),
	writeln('> '),
	writeln('> First set garp_pl/1 in run.pl to the GARP3 startup file'),
	writeln('> Then call: \'?- go.\' to start GARP3 and get more info.'),
	writeln('> '),
	writeln('> ').
*/

% Run GARP3
% go/0
% go
go :- 
    %garp_pl(Path),
    %consult(Path),
    retractall(garp_running(_)),
    retractall(model_dependency(_,_)),
    retractall(dependency_bag(_,_,_)),
    assert(garp_running(true)),	
    
    write_usage.

	
% Print the usage explanation
% write_usage/0
% write_usage
write_usage :-
	nl,
	nl,
	writeln('% ========================== START OF EXPLANATION =========================='),
	writeln('% To be able to run the automated modeling, execute the following steps:'),
	writeln('% 1. Open a model in GARP3'),
	writeln('% 2. Run a simulation'),
	writeln('% 3. In PROLOG, call: ?- identify(M).'),
	writeln('% 4. The output model can be output to GARP3 with: ?- output_model(M).'),
	writeln('%'),
	writeln('% Naive dependencies can also be generated separately'),
	writeln('% by calling: ?- find_naive_dependencies.'),
	writeln('%'),
	writeln('% A model can then be generatead by calling: ?- generate_naive_model(M).'),
	writeln('% =========================== END OF EXPLANATION ==========================='),
	nl,	
	nl.
	
% Run the algorithm on the data generated by the GARP3 simulation
% to identify the underlying system
% identify/1
% identify(-Model)
identify([]) :-
	\+ garp_running(true),
	writeln('>> Please run a simulation after GARP3 is loaded'),
	go.
identify([]) :-
	\+ get_state_values(_,_),
	writeln('>> Please run a simulation first').
identify(NaiveModel) :-
	writeln('Generating naive dependencies'),
	find_naive_dependencies,
	
	writeln('Generating naive model'), !, 
	generate_naive_model(NaiveModel).
	
	
% Returns the Nth model produced by the algorithm
% nth_model/2
% nth_model(+N, -Model)
nth_model(N, Model) :-
	retractall(model_counter(_)),
	assert(model_counter(1)), !,
	generate_naive_model(Model),

	model_counter(M),
	NM is M + 1,
	retractall(model_counter(_)),
	assert(model_counter(NM)),
	M >= N. % Retrieve the Nth model using backtracking
	
	
% Returns all model
% generate_all_models/1
% generate_all_models(-Model)
generate_all_models(AllModels) :-
	% Find the set of naive dependencies
	find_naive_dependencies,
	!,
	% Return all possible models
	findall(Model,
		generate_naive_model(Model),
		AllModels).
	
	

	

