function [KB,cc_probs,ap] = BR_gen_test_KB_ind1(n,max_sentences,...
    max_sentence_len)
% BR_gen_test_KB_ind - create a random KB with correct probabilities
%   where variables are independent
% On input:
%     n (int): number of atoms
%     max_sentences (int): maximum number of sentences
%     max_sentence_length (int): maximum length of any sentence
% On output:
%     KB (KB data structure): KB
%     cc_probs (1x2^n vector): complete conjunction probabilities
%     ap (1xn vector): atom probabilities
% Call:
%     [KB2,cc_probs2] = BR_gen_test_KB_ind(3,6,3);
% Author:
%     T. Henderson
%     UU
%     Fall 2017
%

MAX_FAILS = 100;

m = randi(max_sentences);
cc_probs = [];
max_sentence_len = min(n,max_sentence_len);
OK = 0;
count = 0;
while OK==0
    OK = 1;
    KB = [];
    num_sentences = 0;
    while num_sentences<m
        clause_length = randi(max_sentence_len);
        clause = randperm(n);
        clause = clause(1:clause_length);
        lit_sign = rand(1,clause_length)<0.5;
        lit_sign = 3.^lit_sign - 2;
        clause = clause.*lit_sign;
        clause = sort(unique(clause));
        KB = BR_KB_insert_clause(KB,clause);
        num_sentences = length(KB);
    end
    if ~isempty(KB)...
            &isempty(BR_RTP(KB,[1],[1:n]))&isempty(BR_RTP(KB,[-1],[1:n]))
        count = count + 1;
        if count>MAX_FAILS
            return
        end
        display('Failed');
        OK = 0;
    end
end
display('Success');
ap = rand(1,n);
num_cc = 2^n;
cc_probs = zeros(1,num_cc);
for e = 0:num_cc-1
    bits = BR_int2bits(e,n);
    p = 1;
    for v = 1:n
        if bits(v)==0
            p = p*(1-ap(v));
        else
            p = p*ap(v);
        end
    end
    cc_probs(e+1) = p;
end
for s = 1:num_sentences
    KB(s).prob = BR_cc2clauseprob(KB(s).clauses,cc_probs);
end
