FreeOZ论坛

标题: R++: Rule based programming in C++ [打印本页]

作者: coredump    时间: 8-5-2009 13:46
标题: R++: Rule based programming in C++

                               
登录/注册后可看大图

R++ , 来自Bell实验室的C++扩展, 如上图所示R++在C++ OOP的基础上增加了一个Rules维度,使得可以在C++的class中定义规则和基于规则的动作。如果你对Java很熟悉的话,应该听说过一个Drool的软件包,其用XML的方式时间了Java的商业规则引擎,而R++就是采用了C++的方式实现了Drool所做的事情。

附件为R++用户手册和R++的一个简单例子, 同样的例子如果用Drool来表达的话应该像下面这个样子:
  1.   <rule name="update area">  
  2.      <parameter identifier="rectangle">  
  3.        <class>Rectangle>  
  4.      parameter>  
  5.       
  6.      <java:condition>  
  7.        rectangle.height > 0 && rectangle.width > 0
  8.      java:condition>  

  9.      <java:consequence>  
  10.        stockOffer.area = rectangle.height * rectangle.width;
  11.      java:consequence>  
  12.    rule>  
复制代码


R++写出来的是这样:
Rectangle.rh
  1. class Rectangle {
  2. private:
  3.     monitored int height, width;
  4.     int area;
  5. public:
  6.     Rectangle(int h, int w) : height(h), width(w) {}
  7.     add_to_height(int delta) { set_height(height + delta); }
  8.     rule update_area;
  9.     friend ostream& operator<<(ostream&, Rectangle * const);
  10. };
复制代码


Rectangle.rC

  1. #include <iostream.h>
  2. $include "Rectangle.rh"

  3. rule Rectangle::update_area {
  4.     height > 0 &&
  5.     width > 0
  6.        =>
  7.     area = height * width;
  8. }

  9. ostream& operator<< (ostream& s, Rectangle * const p) {
  10.     return s << "Rectangle " << p->height << " * "
  11.                  << p->width << " = "
  12.                   << p->area;
  13. }
复制代码

作者: coredump    时间: 8-5-2009 13:57
基于规则的编程(RBP) 可以应用在以下场景中:




基于规则的编程的优点:

作者: coredump    时间: 8-5-2009 14:12
PS:比较成熟的商业版的C++规则引擎我知道的事ILOG Rules for C++ : http://www.ilog.cn/products/rules/index.php, 这个规则引擎同时也可以用于Java。
作者: ubuntuhk    时间: 8-5-2009 14:19
挺有用的,用R++做状态机确实是个不错的选择




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2