function [p,qtable] = BR_query(Q,vtable,vprobs)
%

p = [];
qtable = [];

if isempty(Q)
    return
end

len_Q = length(Q);
for c = 1:len_Q
    F(c).f = [];
end
p = zeros(len_Q,1);
len_qtable = 0;

for c = 1:len_Q
    clause = Q(c).clauses;
    switch length(clause)
        case 1 % [L1]
            L1 = clause(1);
            if L1<0
                p(c) = 1 - vprobs(abs(L1));
            else
                p(c) = vprobs(abs(L1));
            end
        case 2 % [L1,L2]  p(c) = p(L1) + p(L2) - p(L1|L2)*p(L2)
            L1 = clause(1);
            L2 = clause(2);
            index = find((vtable(:,1)==L1)&(vtable(:,2)==L2)...
                &(vtable(:,3)==0));
            if isempty(index)
                bit1 = L1>0;
                bit2 = L2>0;
                parity_index2 = bit1*2 + bit2;
                len_qtable = len_qtable + 1;
                qtable(len_qtable,:) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                qtable(len_qtable,:) = zeros(1,6);
                qtable(len_qtable,1) = L1;
                qtable(len_qtable,2) = L2;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,5) = parity_index2;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',...
                    num2str(len_qtable-1),') + ',...
                    num2str(vprobs(abs(L1))),' + ',...
                    num2str(vprobs(abs(L2))),' - x(',...
                    num2str(len_qtable),')*',num2str(vprobs(abs(L2))),';'];
            else
                p(c) = vprobs(abs(L1)) + vprobs(abs(L2))...
                    - vprobs(index)*vprobs(abs(L2));
            end
        case 3
            L1 = clause(1);
            L2 = clause(2);
            L3 = clause(3);
            L1s = num2str(vprobs(abs(L1)));
            L2s = num2str(vprobs(abs(L2)));
            L3s = num2str(vprobs(abs(L3)));
            qindex23 = [];
            vindex23 = find((vtable(:,1)==L2)&(vtable(:,2)==L3)...
                &(vtable(:,3)==0));
            if isempty(vindex23)
                qindex23 = find((qtable(:,1)==L2)&(qtable(:,2)==L3)...
                    &(qtable(:,3)==0));
            end
            vindex123 = find((vtable(:,1)==L1)&(vtable(:,2)==L2)...
                &(vtable(:,3)==L3));
            if isempty(vindex23)&isempty(qindex23)&isempty(vindex123)
                % set up 3 variables: p(c), P(L2|L3), P(L1|L2vL3)
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                bit1 = L2>0;
                bit2 = L3>0;
                parity_index2 = 2*bit1 + bit2;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,1) = L2;
                qtable(len_qtable,2) = L3;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,5) = parity_index2;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,1) = L1;
                qtable(len_qtable,2) = L2;
                qtable(len_qtable,3) = L3;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',num2str(len_qtable-2),...
                    ' + ',L1s,' + ',L2s,' + ',L3s,' + ',...
                    'x(',num2str(len_qtable-1),')*',L3s,...
                    ' - x(',num2str(len_qtable),')*',L2s,...
                    ' - x(',num2str(len_qtable),')*',L3s,...
                    ' + x(',num2str(len_qtable),')*x(',...
                    num2str(len_qtable-1),'*',L3s,';'];
            elseif isempty(vindex23)&isempty(qindex23)&~isempty(vindex123)
                % set up 2 variables: p(c), P(L2|L3)
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                bit1 = L2>0;
                bit2 = L3>0;
                parity_index2 = 2*bit1 + bit2;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,1) = L2;
                qtable(len_qtable,2) = L3;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,5) = parity_index2;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',num2str(len_qtable-1),...
                    ' + ',L1s,' + ',L2s,' + ',L3s,' + ',...
                    'x(',num2str(len_qtable),')*',L3s,...
                    ' - ',num2str(vprobs(vindex123)),'*',L2s,...
                    ' - ',num2str(vprobs(vindex123)),'*',L3s,...
                    ' + ',num2str(vprobs(vindex123)),'*x(',...
                    num2str(len_qtable),')*',L3s,';'];
            elseif isempty(vindex23)&~isempty(qindex23)&isempty(vindex123)
                % set up 2 new vars: p(c), P(L1|L2vL3) and 1 old: P(L2|L3)
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1) = L1;
                qtable(len_qtable,2) = L2;
                qtable(len_qtable,3) = L3;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',num2str(len_qtable-1),...
                    ' + ',L1s,' + ',L2s,' + ',L3s,' + ',...
                    'x(',num2str(qtable(qindex23,6)),')*',L3s,...
                    ' - x(',num2str(len_qtable),')*',L2s,...
                    ' - x(',num2str(len_qtable),')*',L3s,...
                    ' + x(',num2str(len_qtable),')*x(',...
                    num2str(qtable(qindex23,6)),')*',L3s,';'];
            elseif isempty(vindex23)&~isempty(qindex23)&~isempty(vindex123)
                % set up 1 var: p(c)
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',num2str(len_qtable),...
                    ' + ',L1s,' + ',L2s,' + ',L3s,' + ',...
                    'x(',num2str(qtable(qindex23,6)),')*',L3s,...
                    ' - ',num2str(vprobs(vindex123)),'*',L2s,...
                    ' - ',num2str(vprobs(vindex123)),'*',L3s,...
                    ' + ',num2str(vprobs(vindex123)),'*',...
                    'x(',num2str(qtable(qindex23,6)),')*',L3s,';'];
            elseif ~isempty(vindex23)&isempty(qindex23)&isempty(vindex123)
                % set up 2 vars: p(c), P(L1|L2vL3)
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                len_qtable = len_qtable + 1;
                qtable(len_qtable,1:6) = zeros(1,6);
                qtable(len_qtable,1) = L1;
                qtable(len_qtable,2) = L2;
                qtable(len_qtable,3) = L3;
                qtable(len_qtable,4) = c;
                qtable(len_qtable,6) = len_qtable;
                F(c).f = ['F(',num2str(c),') = -x(',num2str(len_qtable-1),...
                    ' + ',L1s,' + ',L2s,' + ',L3s,' + ',...
                    num2str(vprobs(vindex23)),'*',L3s,...
                    ' - x(',num2str(len_qtable),')*',L2s,...
                    ' - x(',num2str(len_qtable),')*',L3s,...
                    ' + x(',num2str(len_qtable),')*',...
                    num2str(vprobs(vindex23)),'*',L3s,';'];
            elseif ~isempty(vindex23)&isempty(qindex23)&~isempty(vindex123)
                % set up 1 var: p(c)
                p_L1 = vprobs(abs(L1));
                p_L2 = vprobs(abs(L2));
                p_L3 = vprobs(abs(L3));
                p_L2_L3 = vprobs(vindex23);
                p_L1_L2_L3 = vprobs(vindex123);
                p(c) = p_L1 + p_L2 + p_L3 + p_L2_L3*p_L3...
                    - p_L1_L2_L3*p_L2 - p_L1_L2_L3*p_L3 ...
                    + p_L1_L2_L3*p_L2_L3*p_L3;
            end
    end
end
tch = 0;
