:- use_module(library(clpfd)).
:- use_module('//rules2cp/interpreter/prolog/r2cp.pl').
:- init_interpreter.


t1(_A_) :- 
	rcp_var(from(t1,0,1), T1),
	rcp_rec([start=T1,duration=2], _A_).

t1_search(_A_) :- 
	rcp_var(from(t1,0,1), T1),
	rcp_rec([start=T1,duration=2], _A_).

t2(_A_) :- 
	rcp_var(from(t2,0,1), T2),
	rcp_rec([start=T2,duration=5], _A_).

t2_search(_A_) :- 
	rcp_var(from(t2,0,1), T2),
	rcp_rec([start=T2,duration=5], _A_).

t3(_A_) :- 
	rcp_var(from(t3,0,1), T3),
	rcp_rec([start=T3,duration=4], _A_).

t3_search(_A_) :- 
	rcp_var(from(t3,0,1), T3),
	rcp_rec([start=T3,duration=4], _A_).

t4(_A_) :- 
	rcp_var(from(t4,0,1), T4),
	rcp_rec([start=T4,duration=3], _A_).

t4_search(_A_) :- 
	rcp_var(from(t4,0,1), T4),
	rcp_rec([start=T4,duration=3], _A_).

t5(_A_) :- 
	rcp_var(from(t5,0,1), T5),
	rcp_rec([start=T5,duration=1], _A_).

t5_search(_A_) :- 
	rcp_var(from(t5,0,1), T5),
	rcp_rec([start=T5,duration=1], _A_).

cost(_B_) :- 
	t5(_A_),
	rcp_att(start, _A_, _B_).

cost_search(_B_) :- 
	t5_search(_A_),
	rcp_att(start, _A_, _B_).

prec(T1, T2, _E_) :- 
	rcp_att(start, T1, _A_),
	rcp_att(duration, T1, _B_),
	fd_aeq(_A_+_B_, _C_),
	rcp_att(start, T2, _D_),
	fd_leq(_C_, _D_, _E_).

prec_search(T1, T2, _E_) :- 
	rcp_att(start, T1, _A_),
	rcp_att(duration, T1, _B_),
	fd_aeq(_A_+_B_, _C_),
	rcp_att(start, T2, _D_),
	fd_leq(_C_, _D_),
	fd_aeq(_E_, 1).

disj(T1, T2, _C_) :- 
	prec(T1, T2, _A_),
	prec(T2, T1, _B_),
	fd_or(_A_, _B_, _C_).

disj_search(T1, T2, _C_) :- 
	(
	prec_search(T1, T2, _A_),
	rcp_fol_call(_A_)
	;
	prec_search(T2, T1, _B_),
	rcp_fol_call(_B_)
	),
	fd_aeq(_C_, 1).

precedences(_W_) :- 
	t1(_A_),
	t2(_B_),
	prec(_A_, _B_, _C_),
	t2(_D_),
	t5(_E_),
	prec(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_),
	t1(_H_),
	t3(_I_),
	prec(_H_, _I_, _J_),
	fd_and(_G_, _J_, _K_),
	t3(_L_),
	t5(_M_),
	prec(_L_, _M_, _N_),
	fd_and(_K_, _N_, _O_),
	t1(_P_),
	t3(_Q_),
	prec(_P_, _Q_, _R_),
	fd_and(_O_, _R_, _S_),
	t3(_T_),
	t5(_U_),
	prec(_T_, _U_, _V_),
	fd_and(_S_, _V_, _W_).

precedences_search(_S_) :- 
	t1_search(_A_),
	t2_search(_B_),
	prec_search(_A_, _B_, _C_),
	rcp_fol_call(_C_),
	t2_search(_D_),
	t5_search(_E_),
	prec_search(_D_, _E_, _F_),
	rcp_fol_call(_F_),
	t1_search(_G_),
	t3_search(_H_),
	prec_search(_G_, _H_, _I_),
	rcp_fol_call(_I_),
	t3_search(_J_),
	t5_search(_K_),
	prec_search(_J_, _K_, _L_),
	rcp_fol_call(_L_),
	t1_search(_M_),
	t3_search(_N_),
	prec_search(_M_, _N_, _O_),
	rcp_fol_call(_O_),
	t3_search(_P_),
	t5_search(_Q_),
	prec_search(_P_, _Q_, _R_),
	rcp_fol_call(_R_),
	fd_aeq(_S_, 1).

disjunctives(_K_) :- 
	t2(_A_),
	t4(_B_),
	disj(_A_, _B_, _C_),
	t3(_D_),
	t4(_E_),
	disj(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_),
	t2(_H_),
	t3(_I_),
	disj(_H_, _I_, _J_),
	fd_and(_G_, _J_, _K_).

disjunctives_search(_J_) :- 
	t2_search(_A_),
	t4_search(_B_),
	disj_search(_A_, _B_, _C_),
	rcp_fol_call(_C_),
	t3_search(_D_),
	t4_search(_E_),
	disj_search(_D_, _E_, _F_),
	rcp_fol_call(_F_),
	t2_search(_G_),
	t3_search(_H_),
	disj_search(_G_, _H_, _I_),
	rcp_fol_call(_I_),
	fd_aeq(_J_, 1).

solve([T1,T2,T4,T3,T5]) :- 
	rcp_var(from(t1,0,1), T1),
	rcp_var(from(t5,0,1), T5),
	rcp_var(from(t2,0,1), T2),
	rcp_var(from(t4,0,1), T4),
	rcp_var(from(t3,0,1), T3),
	domain([T1,T2,T3,T4,T5], 0, 20),
	T1+2#=<T2,
	T2+5#=<T5,
	T1+2#=<T3,
	T3+4#=<T5,
	T1+2#=<T3,
	T3+4#=<T5,
	minimize((((T2+5#=<T3;T3+4#=<T2),(T2+5#=<T4;T4+3#=<T2),(T3+4#=<T4;T4+3#=<T3)),labeling([up],[T5])), T5).
