0

I am faced with a task of allowing the user to define the expressions using the compiled classes with RTTI enabled. Let me put it in a simple way.

TAnimal = class(TPersistent)
private
  fWeight : Double;
  fHeight : Double;
  fName : string;
published
  property Weight : Double read fWeight write fWeight;
  property Height : Double read fHeight write fHeight;
  property Name : string read fName write fName;
end;

And i have a routine which will evaluate the animal with the supplied expression

function EvaluateAnimal(const animal : TAnimal; expression : string) : Double;
begin
  //Result :=  Turn expression to evaluation and give the result
end;

The user expression is (TAnimal.Weight * TAnimal.Height)/(TAnimal.Weight + TAnimal.Height)

Now, I can get the TAnimal using the RTTI Context and get the value of the animal's Height and Weight. However, how can i evaluate the expression what the user has supplied??

Is there any mechanism which i can use to prepare the user expression when my application starts and at runtime, just send the instance of animal to retrieve the value. The user is free to change the expression at anytime and the application has to evaluate the expression.

I am using Delphi XE3.

4

1 回答 1

2

您可以使用实时绑定来评估表达式。这是一个简单的例子:

program BindingsDemo;
{$APPTYPE CONSOLE}

uses
  System.Rtti,
  System.Bindings.Expression,
  System.Bindings.EvalProtocol,
  System.Bindings.Helper;

type
  TFoo = class
    Val1: Integer;
    Val2: Integer;
    Result: TValue;
  end;

procedure Main;
var
  Foo: TFoo;
  scope: IScope;
  expr: TBindingExpression;
begin
  Foo := TFoo.Create;
  Foo.Val1 := 42;
  Foo.Val2 := 666;
  scope := TBindings.CreateAssociationScope([Associate(Foo, 'Foo')]);
  expr := TBindings.CreateUnmanagedBinding(
    [Scope],
    'Foo.Val1 + Foo.Val2',
    [Scope],
    'Foo.Result',
    nil
  );
  expr.Evaluate;
  Assert(Foo.Result.AsInteger=708);
  Writeln(Foo.Result.ToString);
end;

begin
  Main;
  Readln;
end.

请注意,我故意省略了释放对象的代码,因此该代码泄漏。我选择这样做,以便我们可以专注于表达式评估方面。

于 2013-05-20T14:24:09.100 回答