function pot = mk_pot_from_tabular(bnode, bnet)
% MK_POT_FROM_TABULAR Make a potential representing the conditional distrib. of a tabular node.
% pot = mk_pot_from_tabular(bnode, bnet)

fam = [bnode.parents bnode.self];
obs_fam = intersect(fam, bnet.observed_nodes);
vals = bnet.values(obs_fam);

switch bnet.jtree.pot_type
  case {'d', 'm'}
    ns = bnet.node_sizes;
    ns(bnet.observed_nodes) = 1;
    if bnet.jtree.pot_type == 'd'
      pot = mk_table(fam, ns(fam));
    else
      pot = mk_max_table(fam, ns(fam));
    end
    map = find_equiv_posns(obs_fam, fam);
    index = mk_multi_index(length(fam), map, vals);
    pot.T = myreshape(bnode.CPT(index{:}), ns(fam));
  case 'g',
    assert(subset(fam, bnet.observed_nodes));
    ns = bnet.node_sizes;
    ns(bnet.observed_nodes) = 0;
    pot = mk_gpot(fam, ns(fam), 'c');
    pot.g = log(prob_tabular(bnode, bnet));
  case 'cg',
    ns = bnet.node_sizes;
    ns(bnet.observed_nodes) = 1;
    type = 'm'; % so we can add the CPT to logp
    pot = mk_cgpot(fam, [], ns, type);
    map = find_equiv_posns(obs_fam, fam);
    index = mk_multi_index(length(fam), map, vals);
    CPT = bnode.CPT(index{:});
    for i=1:pot.dsize
      pot.pot{i}.logp = log(CPT(i));
    end
    %pot.T = myreshape(bnode.CPT(index{:}), ns(fam));
  otherwise,
    error(['bad type ' bnet.jtree.pot_type]);
end

