:- module(autobuild_naive_dependencies, 
    [
    find_naive_dependencies/0,
    potential_dependency/2
    ]).

/** <module> Find Naive Dependencies : Automatic model building

@author Jochem Liem

 * Automatic model building algorithm
 * Inspired by Hylke Buisman's algorithm

*/


%% find_naive_dependencies is det
%  Finds all the naive dependencies
find_naive_dependencies :-
    get_all_quantity_names(QuantityNames),
    pairs(QuantityNames, QuantityPairs),
    forall(
	member(QuantityPair, QuantityPairs),
	(
	    potential_dependency(QuantityPair, PotentialDependencyRelations),
	    % Check all the potential dependencies
	    forall(
		member(PotentialDependency, PotentialDependencyRelations),
		(
		    check_relation_consistency([PotentialDependency]) ->
		    add_naive_dependency(PotentialDependency),
		    format('Found naive dependency: ~w\n', [PotentialDependency])
		;
		    true
		)
	    )
	)
    ).

potential_dependency([A,B], DependencyRelations) :-
    get_naive_dependency_types(NaiveDependencyTypes),
    findall(DependencyRelation,
	(
	    member(NaiveDependencyType, NaiveDependencyTypes),
	    DependencyRelation =.. [NaiveDependencyType, B, A]
	),
	DependencyRelations
    ).

