简体   繁体   English

表达评估设计问题

[英]Expression evaluation design questions

I a modeling a system to evaluate expressions. 我建立了一个评估表达式的系统。 Now the operands in these expressions can be of one of several types including some primitive .NET types. 现在,这些表达式中的操作数可以是几种类型中的一种,包括一些原始的.NET类型。 When defining my Expression class, I want some degree of type-safety and therefore don't want to use 'object' for the operand object type, so I am considering defining an abstract Operand base class with nothing in it and creating a subclass for each type of Operand. 在定义我的Expression类时,我想要一定程度的类型安全性,因此不希望对操作数对象类型使用'object',所以我正在考虑定义一个抽象的Operand基类,其中没有任何内容并创建一个子类每种类型的操作数。 What do you think of this? 你觉得这怎么样?

Also, only some types of operands make sense with others. 此外,只有某些类型的操作数与其他操作符有意义。 And finally, only some operators make sense with particular operands. 最后,只有一些运算符对特定的操作数有意义。 I can't really think of a way to implement these rules at compile-time so I'm thinking I'll have to do these checks at runtime. 我无法想到在编译时实现这些规则的方法,所以我想我必须在运行时进行这些检查。

Any ideas on how I might be able to do this better? 关于我如何能够更好地做到这一点的任何想法?

I'm not sure if C based languages have this, however Java has several packages that would really make sense for this. 我不确定基于C的语言是否具有此功能,但是Java有几个真正有用的软件包。

The JavaCC or java compiler compiler allows you to define a language (your expressions for example) and them build the corresponding java classes. JavaCC或Java编译器编译器允许您定义一种语言(例如您的表达式),并构建相应的Java类。 A somewhat more user friendly if not more experimental and academic package is DemeterJ - this allows you to very easily specify the expression language and comes with a library for defining visitors and strategies to operate over the generated class structure. DemeterJ是一个更加用户友好的(如果不是更多的实验和学术包) - 这使您可以非常轻松地指定表达式语言,并附带一个库,用于定义访问者和操作生成的类结构的策略。 If you could afford to switch to Java I might try that. 如果你能负担得起切换到Java,我可能会试试。 Other wise I'd look for a C# clone of one of these technologies. 另外,我会寻找其中一种技术的C#克隆。

Another thing to consider if you go down this route is that once you've generated your class structure within some reasonable approximation of the end result, you can subclass all of the generated classes and build all of your application specific login into the subclasses. 如果你沿着这条路走下去,另一件需要考虑的事情是,一旦你在最终结果的合理近似内生成了你的类结构,你就可以将所有生成的类子类化,并将所有特定于应用程序的登录构建到子类中。 That way if you really need to regenerate a new model for the expression language your logic will be relatively independent of your class hierarchy. 这样,如果您确实需要为表达式语言重新生成新模型,那么您的逻辑将相对独立于您的类层次结构。

Update: Actually it looks as though some of this stuff is ported to .NET technology though I havent used it so I'm not sure what shape it may be in: 更新:实际上看起来有些东西被移植到.NET技术虽然我没有使用它所以我不确定它可能是什么形状:

http://www.ccs.neu.edu/home/lieber/inside-impl.html http://www.ccs.neu.edu/home/lieber/inside-impl.html

good luck! 祝好运!

I've recently built a dynamic expression evaluator. 我最近构建了一个动态表达式求值程序。 What I found to be effective was to create, as you suggested, a BaseOperand with meaningful derived classes (NumericOperand, StringOperand, DateOperand, etc) Depending on your implementation, generics may make sense as well (Operand). 我发现有效的是按照你的建议创建一个带有有意义的派生类的BaseOperand(NumericOperand,StringOperand,DateOperand等)。根据你的实现,泛型也可能有意义(操作数)。

Through the implementation of the Visitor pattern, you can perform any kind of validation you like. 通过访问者模式的实现,您可以执行任何类型的验证。

I had a very specific need to roll my own solution, but there are many options already available for processing expressions. 我有一个非常具体的需求来推出我自己的解决方案,但是有许多选项可用于处理表达式。 You may want to take a look at some of these for inspiration or to avoid reinventing the wheel. 您可能需要查看其中一些以获取灵感或避免重新发明轮子。

How about Expression in 3.5? 在3.5中Expression怎么样? I recently wrote an expression parser/compiler using this. 我最近用这个写了一个表达式解析器/编译器。

I found a good approach to handle the types of objects with EXPRESSIONOASIS framework. 我找到了一种使用EXPRESSIONOASIS框架处理对象类型的好方法。 They are using custom data structure to carry the types of the objects. 他们使用自定义数据结构来承载对象的类型。 So after parsing the operand with regular expressions and given expressions, they decide the type and store this type as property of a generic class which can be used any time for getting the type. 因此,在使用正则表达式和给定表达式解析操作数之后,它们决定类型并将此类型存储为泛型类的属性,可以在任何时候使用它来获取类型。

http://code.google.com/p/expressionoasis/ http://code.google.com/p/expressionoasis/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM