% fixme: 17.04.16: following 2 predicates were commented out, but active_split_roots_tord/5 is called elsewhere
% Here
active_split_roots_tord( Active, PossRoots, Vars, ResRoots, ResActive ) :-
	list_set_intersection( Vars, PossRoots, Intersection, Difference ),
	active_selects( Intersection, Active, ResRoots ),
	active_selects( Difference, Active, ResActive ).

% list_set_intersection( +List, +Set, -Intersection, -Difference )
% Intersection is the list of elements in List that appear in Set
% and Difference is the lest of elements that dont. Order of elements
% in List is preserved in both Intersection and Difference.
list_set_intersection( [], _Set,  [], [] ).
list_set_intersection( [H|T], Set,  Intersection, Difference ) :-
	( ord_member(H,Set) ->
		Intersection = [H|TIntersection],
		Difference   = TDifference
		;
		Intersection = TIntersection,
		Difference   = [H|TDifference]
	),
	list_set_intersection( T, Set,  TIntersection, TDifference ).

% active_split_roots( +Active, +PossRoots, +Vars, -ResRoots, -ResActive ) :-
% Active is split to ResRoots and ResActive,
% with the intersection of PossRoots and Vars projected to ResRoots and the 
% Rest of Vars, going to ResActive.
% All are assumed/expected to be Sets.
%
active_split_roots( Active, PossRoots, Vars, ResRoots, ResActive ) :-
	ord_intersection( PossRoots, Vars, Intersection, Difference ),
	active_selects( Intersection, Active, ResRoots ),
	% active_selects( Intersection, Active, CandRoots ),
	% active_to_root( CandRoots, ResRoots ),
	active_selects( Difference, Active, ResActive ).

% active_selects( Vars, Active, VarsData ) :-
% VarsData are the pairs corresponding to Vars 
% as they appear in Active. the goal fails if
% some Var's Data pair does not exist in Active.
%
active_selects( [V|Vs], Active, [V-Vdata|VDPairs] ) :-
	id_memberchk_kv( Active, V-Vdata ),
	active_selects( Vs, Active, VDPairs ).
active_selects( [], _Active, [] ).

active_selects_del( [], Active, [], Active ).
active_selects_del(  [V|Vs], Active, [V-Vdata|VDPairs], NewAct ) :-
	id_select( V-Vdata, Active, MidActive ),
	% universal (to be)
	!,
	% select( V-Vdata, Active, MidActive ), sicstus
	% select( Active, V-Vdata, MidActive ),  % swi
	active_selects_del( Vs, MidActive, VDPairs, NewAct ).

active_additions( [H|VDPairs], Active, NewActive ) :-
	ord_add_element( Active, H, MidActive ),
	active_additions( VDPairs, MidActive, NewActive ).
active_additions( [], Active, Active ).

/* 
%   2002da05, i should also comment out any singly parented children clauses.
% active_satisfied( Vars, Active, RefVar, Val, Probed, NewActive, Satisfied ) :-
% Vars from Active are checked whether they Need, RefVar,
% (they all should do, btw). The constraint Needed is enforced, 
% and removed, from its Needed data bit. Vars with empty Needed are moved,
% to Satisfied, accomapanied by their Pfd. (Probed is used for Pin/5
% declarations. Vars with non empty Needed are carried on to NewActive.
active_satisfied( [], Actv, _Var, _Val, _Probed, Prb, Actv, [], Prb ).
active_satisfied( [V|Vs], Actv, RefVar, Val, Probed, AccPrb, ResActv, ResSat, Prb ) :-
	dbg( sat_var(V) ),
	aux_ord_del_element( Actv, V-Vdata, Nth, MidActv ),
	!,
	data_choices( [nds(Needs),dmn(Fd),mtd(Mtd)], Vdata ),
	dbg( needs(Needs) ),
	data_needs_satisfy( RefVar, Val, V, Mtd, Fd, Needs, MidFd, NewNeeds, SatPrb ),
	dbg( data_needs_satisfy( RefVar, Val, V, Mtd, Fd, Needs, MidFd, NewNeeds, SatPrb ) ),
	rationals_multiplication( AccPrb, SatPrb, NxPrb ),
	dbg( sat(rationals_multiplication( AccPrb, SatPrb, NxPrb)) ),
	dbg( newneeds(NewNeeds) ),
	( data_needs_is_empty(NewNeeds) -> 
		NewActv = MidActv,
		data_choose( rqr(ReqBy), Vdata ),
		method_pfd_constract( Mtd, MidFd, Probed, NewFd, Prs ),
		ResSat = [V-[NewFd,Prs,ReqBy]|TSat]
		;
		data_update( nds(NewNeeds), Vdata, MidVData ),
		data_update( dmn(MidFd), MidVData, NewVData ),
		aux_add_element_on_nth( MidActv, Nth, V-NewVData, NewActv ),
		ResSat = TSat
	),
	active_satisfied( Vs, NewActv, RefVar, Val, Probed, NxPrb, ResActv, TSat, Prb ).
active_satisfied( [_V|Vs], Actv, RefVar, Val, Probed, AccPrb, ResActv, ResSat, Prb ) :-
	% if V was not here, then forget you 've seen it.
	active_satisfied( Vs, Actv, RefVar, Val, Probed, AccPrb, ResActv, ResSat, Prb ).
*/

% 
active_to_root( [], [] ).
active_to_root( [Var-ActStr|ASs], [Var-ActStr|RSs] ) :-
% active_to_root( [Var-ActStr|ASs], [Var-RtStr|RSs] ) :-
	% ActStr = [Method,PrvFd,_Needs,_ReqBy],
	% ( fd_var( Var ) -> 
		% PrvFd = FullFd,
		% fd_set( Var, Set ),
		% fdset_to_list( Set, List ),
		% fdlist_to_pfd_domain( List, FullFd, Fd )
		% ;
		% ( integer( Var ) ->
			% PrvFd = FullFd,
			% nth( Var, FullFd, FdElem ),
			% Fd = [FdElem]
			% ;
			% Fd = PrvFd
		% )
	% ),
	% data_choices( [mtd(Method),dmn_or_v(Fd)], ActStr ),
	% method_apply( Method, Fd, Prbs ),
	% kv_compose( Fd, Prbs, Static ),
	% RtStr = (Static,ReqBy),
	active_to_root( ASs, RSs ).