Jump to content



Photo

  • Please log in to reply
4 replies to this topic

#1 Kushan

Kushan

    Sold his soul for some megahurtz

  • Joined: 10-August 03
  • Location: The End of the Universe

Posted 07 May 2013 - 11:09

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.


#2 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 07 May 2013 - 11:45

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

#3 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 07 May 2013 - 13:06

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.

#4 OP Kushan

Kushan

    Sold his soul for some megahurtz

  • Joined: 10-August 03
  • Location: The End of the Universe

Posted 08 May 2013 - 16:27

Thanks, guys! Both of those look like viable solutions. I'll report back with my findings later =)

#5 OP Kushan

Kushan

    Sold his soul for some megahurtz

  • Joined: 10-August 03
  • Location: The End of the Universe

Posted 28 May 2013 - 16:09

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!