• 0

[C++] .Net Need a little advice on decision or expression trees


Question

Hi All,

This is a bit of a vague question and I apologise in advance for it. However, that's why I need help as I'm not sure where to start looking.

Here's the problem. I need to be able to construct a somewhat simple query. This will start off as a string and look something like this:

(A && B) || (C && D)

This will be defined in a configuration file. What I then need to do is pull out those "variables" (The A, B, C and D) and display them to the user as a list of tickboxes. They'll never see the config file, but said file will just be a text document (With other bits and pieces mixed in). That bit I can do easily enough.

Where I'm not sure how to proceed is taking the results of those tickboxes and plugging them back into the query, then "running" the query. What exactly is this query? Is there a library in .net for this kind of thing?

So for example, in the above query the end user will see 4 tick boxes. If they tick A, B and C but leave D blank, then the query turns into this:

(true && true) || (true && false)

Which should, ultimately, return true. However at the moment it's just a string and I'm not sure what the best way would be to turn the above string into something that can be processed. I could write some code and kludge it together myself, but I get the feeling that this isn't a new issue and there's probably either a library of some sort that can already do this, or something built into .net itself, or a "defined way" to process this kind of thing. It's almost as though I'm trying to write a really tiny scripting language.

Can someone point me in the right direction? I'm not really sure what I'm googling for, but "decision trees" and "expression trees" keeps cropping up, but as near as I can tell that's something to do with LINQ and I'm not even sure what that is.

The catch is, this needs to be available in C++/Cli - not C# or VB.net.

Once again, I apologise for the vague question but I appreciate any help or pointers someone can give.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

The last post here has a really dirty solution that relies solely on string replacements (it's C# but should be trivial to convert).

It's not the best solution, but a solution nonetheless. And one that's really easy to follow if you don't want/have the time to write an expression parser :p

Link to comment
Share on other sites

  • 0

Apologies for the double post, but I promised an update.

The expression is valid C#, so it's possible to compile and run it dynamically using the CSharpCodeProvider (example) - it doesn't matter if your project is C++/CLI as this is just a .NET API. If Roslyn ever ships this should get much easier.

This solution worked really well. The example was easy to follow and I got it up and running really quick. Saved me a lot of work. Thanks!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.