[Qt-interest] Implementing a dynamic expression at runtime

Donal donaloconnor at gmail.com
Thu Mar 5 16:23:16 CET 2009


Thanks, that looks quite interesting too.

However I have messed with QtScript there for a while and it seems that it
will have no problem doing the trick for me.

I done just a little proof of concept:

 QScriptEngine engine;

 int fooVal = 0;

float fooBarVal = 4.3;

 char fooName[] = "foo";

char fooBarName[] = "foobar";

 QScriptValue val(&engine, fooVal);

engine.globalObject().setProperty(fooName, val);

 QScriptValue val2(&engine, fooBarVal);

engine.globalObject().setProperty(fooBarName, val2);

 qDebug() << "foo times two is:" << engine.evaluate("(foo<4) &&
(foobar<4.4)").toNumber();


And it does the trick perfect! :) - outputs a 1 for true and 0 for false.

All I have to do now is store the name of the var as "0102" and do a lookup
in my QList of sensors for a match and use it to get the result. I will also
look at integrating directly with the QObjects as this probably be a nicer
way to do it.

Cheers for the suggestions guys.


Regards,
Donal

On Thu, Mar 5, 2009 at 2:13 PM, Alan M. Carroll <amc at network-geographics.com
> wrote:

> You might find the Spirit Parser Generator interesting for this project.
>
> http://spirit.sourceforge.net/
>
> Writing an expression evaluator should be simple give the limited domain.
> Note that you can name your QObjects and then use findChild() to locate a
> specific object by name.
>
> At 07:15 AM 3/5/2009, Donal wrote:
> >Hi There,
> >
> >I want to enable a user to choose rules dynamically, for example if the
> Coolant Temperature and Engine RPM
> >are monitored in real time (4 samples a second), I want to allow the user
> to say produce an alert
> >when for example, "If coolantTemp < 40 AND engineRPM > 4500" alert the
> user with a dialog message.
> >
> >So I am wondering which is the best way to implement such dynamic logic.
> As far as I know C++ doesn't allow you to evaluate
> >dynamic code at run time.
> >I have looked at QtScript and it seems to be able to interact with objects
> of QObject derived type.
> >
> >My idea is to construct a string such as "If 010C < 40 AND 010D > 60",
> where the 01n are the identifiers for the sensors.
> >Then parse this string and construct some qscript logic from it to
> evaluate to true or false.
> >Reason I'd like to store as QString is to allow the storing of rules to a
> file.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090305/69959a78/attachment.html 


More information about the Qt-interest-old mailing list