Question

I have a string of parameters that come in from a client. An example may be :

string param = "(NAME.FULLNAME AND DOB.OPTIONAL) OR (ID AND DOB.REQUIRED) OR (ID AND COUNTRY)"

Now, I have parsed out all the incoming data and have booleans representing each parameter.

Like :

bool name_FullName = true;
bool dob_Optional = false;

etc.

I'm trying to find the best way to evaluate the customer parameter expression to True or False.

I'm thinking just replace the parameters with their true/false bools. Then locate any TRUE AND TRUE and removing them, and replacing TRUE AND FALSE with false. Then evaluate the OR expression i have left.

After typing it this seems like a good way to go. Does anyone have any faster solutions that I'm missing?

Was it helpful?

Solution

Another option is to create a DataTable and use the Select method.

// create data table
var data = new DataTable();
data.Columns.Add("NAME.FULLNAME", typeof(bool));
data.Columns.Add("DOB.OPTIONAL", typeof(bool));
data.Columns.Add("ID", typeof(bool));
data.Columns.Add("DOB.REQUIRED", typeof(bool));
data.Columns.Add("COUNTRY", typeof(bool));

// fill data table
data.Rows.Add(true, false, true, true, false);

// see if it's a match
var isMatch = data.Select("(NAME.FULLNAME AND DOB.OPTIONAL) OR (ID AND DOB.REQUIRED) OR (ID AND COUNTRY)").Any();

OTHER TIPS

As long as your parentheses nesting is always like your example, your proposed solution sounds very easy to implement. If they can be nested, however, then you are talking about making a state machine or at the least implementing matching-parentheses in some manner, which is non-trivial.

One way to handle that would be to replace all TRUE OR FALSE with TRUE and then replacing all (TRUE) with TRUE, which should handle all cases, but that's a lot of replacing. Still, using a string as the state placeholder in your scheme seems alright, though you may consider some small performance gain by instead of using the words TRUE and FALSE with some encoding like 0 and 1 and & and | for and and or.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top