%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CHF Guide 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Include the Physician Advisor System
#include('heart_failure_rules.pl').
%% Include the Guide interface
#include('heart_failure_guide.pred.pl').

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Treatments Information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Reason: What are the reason for a treatment

%Digoxin
%% "Digoxin can be beneficial in patients with HFrEF, unless
%% contraindicated, to decrease hospitalizations for HF."

reason(digoxin) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

%Beta_blocker
reason(beta_blockers) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

%Anticoagulation    
reason(anticoagulation) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef),
    evidence(cardioembolic_stroke_risk_factor),
    diagnosis(atrial_fibrillation).

%Ace_inhibitors
reason(ace_inhibitors) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

%Diuretics
reason(diuretics) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

%aldosterone_antagonist
reason(aldosterone_antagonist) :-
    evidence(accf_stage_c),
    evidence(lvef_equal_or_less_than_35_precent),
    evidence(nyha_class_4),
    evidence(normal_kidney_function),
    evidence(potassium_less_than_5_0).

%Hydralazine and Isosorbide Dinitrate
reason(hydralazine) :-
    evidence(accf_stage_c),
    evidence(nyha_class_4),
    evidence(african_american),
    not discarded(ace_inhibitors),
    not discarded(beta_blockers).

reason(isosorbide_dinitrate) :-
    evidence(accf_stage_c),
    evidence(nyha_class_4),
    evidence(african_american),
    not discarded(ace_inhibitors),
    not discarded(beta_blockers).


% Nonpharmacological Interventions
reason(specific_education) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

reason(exercise_training) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).
        
reason(sodium_restriction) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).

reason(continuous_positive_airway_pressure) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef),
    evidence(sleepApnea).

reason(cardiac_rehabilitation) :-
    evidence(accf_stage_c),
    diagnosis(hf_with_reduced_ef).
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. contraindication: When is a danger in taking a treatment

%Diagoxin
contraindication(digoxin) :-
    evidence(atrioventricular_block).

%Ace_inhibitors
contraindication(ace_inhibitors):-
    history(angioedema).
contraindication(ace_inhibitors):-
    evidence(pregnancy).

%Anticoagulation
%% "Anticoagulation is not recommended in patients with chronic HFrEF
%% without AF, a prior thromboembolic event, or a cardioembolic
%% source."
contraindication(anticoagulation) :-
    diagnosis(hf_with_reduced_ef),
    not evidence(cardioembolic_source),
    not diagnosis(atrial_fibrillation),
    not history(thromboembolism).

contraindication(exercise_training) :-
    evidence(can_not_improve_functional_status).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. second_line: What is the second choise if a treatment is not
%% available

%Arbs
%% "ARBs are recommended in patients with HFrEF with current or prior
%% symptoms who are ACE inhibitor intolerant, unless contraindicated,
%% to reduce morbidity and mortality."

second_line(ace_inhibitors, arbs).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 5. concomitat: What treatment is automatically chosen if is not
%% prohibited

%Concomitat
%% "Diuretics should generally be combined with an ACE inhibitor, beta
%% blocker, and aldosterone antagonist.  Few patients with HF will be
%% able to maintain target weight without the use of diuretics."

concomitant(ace_inhibitors, diuretics).
concomitant(beta_blockers, diuretics).
concomitant(aldosterone_antagonist, diuretics).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 6. indispensables: What treatment must be chosen

%Indispensables
%% "In patients with a current or recent history of fluid retention,
%% beta blockers should not be prescribed without diuretics"
indispensable(beta_blockers, diuretics) :-
    history(fluid_retention).

indispensable(hydralazine, isosorbide_dinitrate).
indispensable(hydralazine, beta_blockers).
indispensable(hydralazine, ace_inhibitors).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 7. incompatibility: Which treatment can not be chosen together

%Incompatibilities
%% "Routine combined use of an ACE inhibitor, ARB, and aldosterone
%% antagonist is potentially harmful for patients with HFrEF."

incompatibility(ace_inhibitors,arbs).
incompatibility(aldosterone_antagonist,arbs).
incompatibility(ace_inhibitors,aldosterone_antagonist).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% AUXILIAR PREDICATES INSIDE GUIDE

% A1 evidence inference from patient information
evidence(cardioembolic_stroke_risk_factor):-
    history(hypertension).
evidence(cardioembolic_stroke_risk_factor):-
    diagnosis(diabetes).
evidence(cardioembolic_stroke_risk_factor):-
    history(stroke).
evidence(cardioembolic_stroke_risk_factor):-
    history(ischemic_attack).
evidence(cardioembolic_stroke_risk_factor):-
    evidence(over_75_years_old).

evidence(over_75_years_old):-
    measurement(age, Data), Data > 75.

% A2 diagnosis inference from patient information
diagnosis(hf_with_reduced_ef) :-
    measurement(lvef, Data), Data < 0.4.
evidence(creatinine_equal_or_less_than_2) :-
    measurement(creatinine, Data), Data =< 2.
evidence(glomerular_filtration_rate_greater_than_30) :-
    measurement(glomerular_filtration_rate, Data), Data > 30.

evidence(normal_kidney_function) :-
    evidence(creatinine_equal_or_less_than_2).
evidence(normal_kidney_function) :-
    evidence(glomerular_filtration_rate_greater_than_30).

evidence(potassium_less_than_5_0) :-
    measurement(potassium, Data), Data < 5.

evidence(lvef_equal_or_less_than_35_precent) :-
    measurement(lvef, Data), Data =< 0.35.


