• 规则引擎以及blaze 规则库的集成初探之三——Blaze规则引擎和SRL


    原文地址:http://jefferson.iteye.com/blog/68604

    在上面介绍利用JSR94的api使用的章节中,我们使用的具体引擎的实现是一个商业产品,如果想了解Drools的使用,这里有一份入门的参考文章:http://blog.csdn.net/tigerflower/archive/2006/09/06/1185922.aspx。由于关于blaze的文章很少,这里根据经验,简单介绍一下它的实现。

    blaze是由blazeSoft公司提供的产品,由于是商业产品,因此专门IDE非常强大,现在流行的版本是Blaze Advisor5.5和Blaze Advisor6.x.。blaze的开发模式有编程式开发的好处,也有声明式开发的好处。在开发的IDE,你看到的是一般传统的编程式编程的IDE一样,保存后的原代码是xml格式的文件。看过DRool 的规则语言,它有一些固定的xml标签,有不同的含义,你必须掌握,而在blaze的编程里面,你就像写java代码一样。

    在介绍blaze的建立和开发之前,现简单介绍一下规则语言的语法。规则语言,名称SRL (全称为Standard Rule Language),据自称,是一个类English的语言,也就是让业务人员也看得懂的语言。我们看一个例子:theAccount's balance is between $00.00 and $500.00 。在java中的表示是theAccount.balance>=00 and theAccount.balance<=500,当然,在srl里面也支持像前面那句在java中的类似用法。看另外一个例子,是一个类的声明:
    a Vehicle is an object;其实,它的语法规则很简单,也易学,学习代价非常小。在使用中我们可以发现,它一样是一个面向对象的语言,提供了原子类型、基本的类库和内置的方法等等,与java的兼容好,java代码的嵌入方便。
    现在来看看具体的用法:
    类的声明,当然包括属性:

    a Vehicle is an object with {
    a speed : a real,
    a size : a string,
    a unit_price : a real }.

    在java中的使用是:
    class Vehicle
    {
     public double speed;
     public String size;
     public double unit_price;
    }
    过过瘾,举一个声明一个对象实例的例子:
    就刚才Vehicl类为例:
    a vehicle1 is some Vehicle initially a vehicle1 initially{
    set  speed to 10.0,size  to 20,unit_price to $100000.00}
    或者更加简洁的:
    myCar is a Vehicle initially{
    speed=10.0,size="2m" ,unit_price=$100000.00}
    非常灵活,而且你不必担心语法的松散带来许多错误,IDE可以识别所以的语法错误。

    对象数组:
    Accounts is a fixed array of 4 Account
    initially { it[0] = checkingAccount. }
    这里声明了一个对象数组,名为Accounts,数组的类型是Account,大小为4,其中第一项指向的对象引用是checkingAccount.
    同样我们看另外一个例子:
    itinerary is an array of city
    initially { it.append(city2),
    it.append(city3),
    it.append(city4).
    }.
    利用了array的内置方法append,it代表当前对象,相当于java中的this关键字。

    声明数组的其他例子,数组下标可以是string类型,也可以是整数类型等等,随意定义,这像java类库的Map及其子类,可谓功能强大。
    airports is an association from
    string to string initially
    { it["New York"] = "JFK",
    it["San Francisco"] = "SFO",
    it["Los Angeles"] = "LAX".
    }.
    这段话什么意思呢?解释一下前半部分。
    airports是对象名,也就是对象数组名,an association from是关键字,from后面的string是数组的key的类型,to后面的string是制定这个数组是属于string类型的数组。
    为了增加感性认识,再举一个例子:
    Products is an association from
    integer to Product initially {
    it[094] = someProduct. }

    对象属性的声明:
    a speed : a real,
    a size : a string,
    a unit_price : a real
    ....
    如果属性是数组,则为an address : some array of string。

    对象的初始化,上面的介绍中,大家也大概清楚了,且看代码段:
    initially { temp = 98.6, firstName = "Bill" }
    initially { it.setTemp(98.6),
    firstName = "Bill" }
    initially { Deposit(newAccount, amount) }.
    第二个代码段,调用了类内部的方法。第三个代码段,调用了Deposit方法。

    Enumeration,声明为:a[n] enumerationName is one of { itemName,
    itemName[, itemName ]... } .
    例如:a color is one of { red, green, blue }.
    有必要说说Enumeration的作用。
    上面定义了一个Enumeration,名为color,那么在application中定义了一个属性,为color,如下:
    an application is an object with {
    a customer : a string,
    a color,
    an amount: an integer }
    initially { amount = 0, color = red }.
    那么,这个application的对象的属性color将有三个标准值: red, green, blue,你可以初始化指定为red。挺像一个有值约束的自定义类型。

    Pattern的声明:
    且看例子:
    theCustomer is any Customer.
    theAccount is any Account in accountArray.
    seniorMale is any person such that
    (it.age > 65 and it.gender is male).
    我们知道,一个类可以有多个instance,我们可以制定一个对象,它的值限制在某个对象或者对象数组的某个或者某几个对象实例,那么可以定义一个patten。这就是典型的条件匹配,简化成一种类型。
    上面的例子,theCustomer指定是任何一个Customer的对象,theAccount指定为accountArray数组中的任何一个Account对象,seniorMale指定为年龄大于65并且为女性的person对象。
    这种模式匹配,就像我们用到的正则表达式一样,用起来威力无穷矣:
    说一个例子:
    假定有一个类Account,在某个ruleset中,拥有n个Account的对象:a1,a2,...,an,,那么你可以声明一个patten如下:
    accountPattern is any Account.
    在rules中的条件匹配,你可以这样使用:
    if accountPattern.balance > 0
    then accountPattern.positiveBalance = true
    那么它的作用相当于:
    if a1.balance > 0
    then a1.positiveBalance = true
    if a2.balance > 0
    then a2.positiveBalance = true
    ...
    if an.balance > 0
    then an.positiveBalance = true


    最后看看原始类型的声明:
    i is an integer.
    startDate is a date initially '1/31/99'.
    Today is a date.
    Today = calendar().currentDate().
    price is a money initially $1684.37.
    再看看具体的变量声明为Enumeration的话,应为:sample is a color initially blue.
    color为上述的a color is one of { red, green, blue }.

    常用的也就这些,象event阿,ruleset的定义自看文档,在GUI中不用srl去定义。

    现在说说,rule也就是规则匹配吗,主要用到的是条件匹配句式。且看:
    if theForm’s name starts with "req" then...
    if theForm’s name ends with "req" then...
    if theApplicant’s lastName is blank then...
    else ……
    上述的就是if-then-else句式,没什么特别。其中theForm’s name,theForm对象的属性 name是string类型,因此对string类型的操作 从句子中可以看到。
    if 语句中的boolean操作有:not | or | and
    比较操作符有:is | = | <> | < | > | <= | >=
    对数字的操作有:+ | - | * | / | div | mod 例如:
    19 / 5 = 3.8
    19 div 5 = 3
    19 mod 5 = 4

    其他流程控制语句:
    case语句:
    select countryofOrigin of product
    case China : apply ChinaTariffRules
    case Japan : apply JapanTariffRules
    case Thailand : apply ThailandTariffRules
    otherwise : print("No tariffs apply.").

    迭代for语句:
    for each customer such that (it.balance > 10000)
    do { print(it.name " qualifies for promotion.").
    apply PromotionRuleset. }
    for each Invoice in customer.invoices
    such that it.balance
    do { sum += it.amount. }

    While_Do语句:
    while (portfolioValue = unavailable)
    do { portfolioValue = promptReal("What
    is the value of your portfolio?") }

    Until do语句:
    until (productsRemaining = 0)
    do { apply CheckPriceRuleset(product).
    productsRemaining -= 1. }


    异常的捕捉和处理:
    try Statement_Block
    catch a[n] exceptionClassName with Statement_Block
    [finally Statement_Block]
    例如:
    try { customer.addAccount(payment) }
    catch an accountException with
    { print("Account not created because " it).}

    其实,都跟java相似,没多少区别。

    (blaze的规则引擎的介绍继续……)

  • 相关阅读:
    多线程(一)初步使用
    数据迁移:MSSQL脚本文件过大,客户端没有足够的内存继续执行程序
    统计数据,数据库就只有8,9,10的,而前端需要返回连续的记录
    Windows10禁用update
    C#模拟HTTP POST 请求
    C#中Equals和= =(等于号)的比较(转)
    .net framework4与其client profile版本的区别
    centos7 安装mysql
    JAVA中使用ASN.1
    使用gradle建立java application
  • 原文地址:https://www.cnblogs.com/xiayahui/p/5045273.html
Copyright © 2020-2023  润新知