Re: Decision Tables



On Jan 28, 5:20 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:

Another way to go about this (when you don't have closures) is to
encode the actions that match into a stack machine.  One simple
encoding is as function pointers (invented language):

void match_first_name(rM, cM) { push(rM.FirstName == cM.FirstName); }
void match_last_name(rM, cM)  { push(rM.LastName == cM.LastName); }
void match_country(rM, cM)    { push(rM.Country == cM.Country); }

void match_and(rM, cM)        { push(pop() & pop()); }
void match_or(rM, cM)         { push(pop() | pop()); }
void match_not(rM, cM)        { push(!pop()); }

Your complex matches then become short NULL terminated arrays for
these:

complex_match[] = { match_contry, match_not, match_first_name,
                    match_or, NULL };


FWIW heres a C++ variant, which seems to work...


#include <string>
//predicate abstract base class
struct pred{
virtual ~pred(){}
virtual bool operator()() const =0;
virtual pred * clone()const =0;
};

// data class
struct D{
D(std::string const & fn,std::string const & ln, std::string const
& c)
: first_name(fn),last_name(ln),country(c){}
std::string first_name;
std::string last_name;
std::string country;
};

// D member comparison
template <std::string D::* M>
struct Dpred : pred{
D lhs, rhs;
Dpred( D const & lhs_in, D const & rhs_in)
: lhs(lhs_in),rhs(rhs_in){}

bool operator()() const
{
return lhs.*M == rhs.*M;
}
Dpred* clone() const
{
return new Dpred(lhs,rhs);
}
};

// Wrapper not strictly necessary but simple
// all boolean ops act on pred_wrappers
struct pred_wrapper{

pred_wrapper( pred_wrapper const & in)
:p(in.p->clone()){}
~pred_wrapper(){delete p;}

pred_wrapper(pred const & pred_in)
: p(pred_in.clone()){}

pred_wrapper & operator= (pred_wrapper const & in){
if( &in != this){
delete p;
p = in.p->clone();
}
return * this;
}
bool operator()() const
{
return (*p)();
}
private:
pred * p;
};

// comp ops...
inline bool operator ! (pred_wrapper const & in)
{
return ! in();
}
inline bool operator || (pred_wrapper const & lhs, pred_wrapper const
& rhs)
{
return lhs() || rhs();
}
inline bool operator || (pred_wrapper const & lhs, bool rhs)
{
return lhs() || rhs;
}
inline bool operator || (bool lhs,pred_wrapper const & rhs)
{
return lhs || rhs();
}

inline bool operator && (pred_wrapper const & lhs, pred_wrapper const
& rhs)
{
return lhs() && rhs();
}
inline bool operator && (pred_wrapper const & lhs, bool rhs)
{
return lhs() && rhs;
}
inline bool operator && (bool lhs,pred_wrapper const & rhs)
{
return lhs && rhs();
}

#include <iostream>


int main()
{
D d1("John","Smith","England");
D d2("John","Jones","England");

typedef Dpred<&D::first_name> d_first_name;
typedef Dpred<&D::last_name> d_last_name;
typedef Dpred<&D::first_name> d_country;

pred_wrapper first_name((d_first_name(d1,d2)));
pred_wrapper country((d_country(d1,d2)));
pred_wrapper last_name(d_last_name(d1,d2));

std::cout << (first_name && last_name && country) << '\n';
std::cout << (first_name && (last_name || country)) << '\n';
std::cout << (first_name && !last_name && country) << '\n';

}

regards
Andy Little


.



Relevant Pages