• Drools学习笔记3—Conditions / LHS—字段约束连接&字段约束操作符


    字段约束连接

    • 用于字段约束
    • 对象内部多个约束连接,采用“&&”(and)、“||”(or)和“,”(and)
    • 执行顺序:“&&”(and)、“||”(or)和“,”

    字段约束操作符

    • >、>=、<、<=、= =、!=
    • contains:包含 A contains B, A中包含B
    • not contains:与contains相反
    • memberOf:单个对象属于某个集合,this表示当前对象
    • not memberOf:与memberof相反
    • matches:正则表达式,匹配
    • not matches:正则表达式,不匹配

    contains 、 not contains

    package com.sample
     
    import  com.bean.Customer;
    import  com.bean.Account;
     
     rule "contains"
        when
           $account : Account();
           $customer : Customer(name=="七夜雪" && accounts contains $account);
        then
            System.out.println( "contains test success"  );
    end
    
    
     rule "not contains"
        when
           $account : Account();
           $customer : Customer(name=="七夜雪" && accounts not contains $account);
        then
            System.out.println( "not contains test success"  );
    end
      /**
       * 字段约束符,contains
       * @throws Exception
       */
      @Test
      public void testContainsRule() throws Exception {
        KnowledgeBase kbase = readKnowledgeBase("Contains.drl");
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
        Customer customer = new Customer();
        customer.setName("七夜雪");
        Account account1 = new Account();
        account1.setNum(100);
        Account account2 = new Account();
        account2.setNum(100);
        customer.getAccounts().add(account1);   //not contains
        //customer.getAccounts().add(account2);  // contains
        ksession.insert(customer);
        ksession.insert(account2);
        ksession.fireAllRules();
        logger.close();
      }

     

    memberOf 、not memberOf 

    package com.sample
     
    import  com.bean.Customer;
    import  com.bean.Account;
     
     rule "Memberof"
        when
           $customer : Customer();
           //当前的account是$customer.getAccounts()的一个成员
            $account : Account(this memberOf  $customer.getAccounts()); 
        then
            System.out.println( "memberOf test success and account is " + $account.getName() );
    end
    
    
     rule "not Memberof"
        when
           $customer : Customer();
           //当前的account不是$customer.getAccounts()的一个成员
            $account : Account(this not memberOf  $customer.getAccounts()); 
        then
            System.out.println( "not memberOf test success and account is " + $account.getName() );
    end
      /**
       * 字段约束符,memberOf
       * @throws Exception
       */
      @Test
      public void testMemberOfRule() throws Exception {
        KnowledgeBase kbase = readKnowledgeBase("Members.drl");
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
        Customer customer = new Customer();
        Account account1 = new Account();
        account1.setName("碧落");
        Account account2 = new Account();
        account2.setName("黄泉");
        customer.getAccounts().add(account1);   
        ksession.insert(customer);
        ksession.insert(account1);
        ksession.insert(account2);
        ksession.fireAllRules();
        logger.close();
      }

    matches 、 not matches

    package com.sample
     
    import  com.bean.Customer;
    import  com.bean.Account;
     
     rule "Matchs"
        when
           $customer : Customer(name matches "qiye*");
        then
            System.out.println( "Matchs test success and customer is " + $customer.getName() );
    end
    
    
     rule "not Matchs"
        when
           $customer : Customer(name not matches "qiye*");
        then
            System.out.println( "not Matchs test success and customer is " + $customer.getName() );
    end
      /**
       * 字段约束符,memberOf
       * @throws Exception
       */
      @Test
      public void testMatcherRule() throws Exception {
        KnowledgeBase kbase = readKnowledgeBase("Matchs.drl");
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        Customer customer = new Customer();
        customer.setName("qiyexue");
        Customer customer1 = new Customer();
        customer1.setName("biluo");
        ksession.insert(customer);
        ksession.insert(customer1);
        ksession.fireAllRules();
      }

    fact对象代码:http://www.cnblogs.com/qiyexue/p/7822670.html

     

  • 相关阅读:
    多表头GridView
    Updater Application Block自动更新实施方案[源代码]
    IE和Firefox的Javascript兼容性总结
    JavaScript IE加速
    GridView汇总
    Oracle 中取当前日期的上个月最后天和第一天
    Atitit,通过pid获取进程文件路径 java php  c#.net版本大总结
    Atitit. 数据库catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php  c#.Net
    Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
    Atitit.一些公司的开源项目 重大知名开源项目attilax总结
  • 原文地址:https://www.cnblogs.com/qiyexue/p/7822850.html
Copyright © 2020-2023  润新知