function [KB,cc_probs,ap] = BR_gen_test_KB_ind(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;

KB = [];
cc_probs = [];
ap = [];

m = randi(max_sentences);
max_sentence_len = min(n,max_sentence_len);
OK = 0;
count = 0;
thm1(1).clauses = [1];
thm2(1).clauses = [-1];
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
    B1 = CS4300_Ask(KB,thm1);
    B2 = CS4300_Ask(KB,thm2);
    if ~isempty(KB)&(B1*B2==1)
%            &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);
cc_probs = BR_ap2ccp(ap);
for s = 1:num_sentences
    KB(s).prob = BR_prob_or_atom_probs(KB(s).clauses,ap);
end
