1

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?

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Make sure you catch the parenthesis, too - unless it's always (AND..AND..AND)OR(AND..AND...)OR... – Scott Mermelstein Jul 29 '13 at 21:08
  • Have you looked at using one of the boolean parsers on codeplex? http://stackoverflow.com/q/5029699/2270839 – Kevin Jul 29 '13 at 21:11
  • 1
    A couple things to note about solutions: 1) Make sure to take into account Boolean order of precedence, but as long as you deal with parenthesis appropriately that probably won't be an issue, 2) It sounds like you're not going to implement short-circuiting, and that's probably a good thing, 3) if your interest is in clarity of your code this is probably fine; if you're interested in improving the speed (and capabilities) of this, though, you might want to look into Tokenization. With a very small potential argument set, it could be really easy to just use an array of token id's instead. – TASagent Jul 29 '13 at 21:12

2 Answers2

5

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();
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
1

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.

welegan
  • 3,013
  • 3
  • 15
  • 20