【问题标题】:Drools Validation of Products in a Product Catalog to ensure the right classification is chosenDrools 对产品目录中的产品进行验证,以确保选择正确的分类
【发布时间】:2019-07-30 03:23:46
【问题描述】:

我有一个drools 应用程序,它有一个产品目录,products。客户可以从多个类别购买任意数量的产品。但是,他们只能从任何给定的班级购买一定数量的商品。这是 ProductCatalog 类的结构。它包含根据其分类的产品列表。

class ProductCatalog {

  List<Product> classA =
  List<Product> classB =
  List<Product> classC =

  ... getters and setters
}

接下来是 Product 类,它提供了允许客户订购的产品的详细信息。

  class Product {
   String classification;
   String code;
   String name;
   String description;
   BigDecimal cost;
   ...getters and setters
  }

最后一个类是包含客户订购的所有产品的采购类。购买是捆绑在一起的,因此该对象中的分类中的产品没有分离。有许多规则决定了如何进行购买。用户只能按照这些规则捆绑他们希望购买的产品。这是购买类。

  class Purchase {
    Customer details 
    List<Product> orders =
   ... getters and setters
  }

购买策略类似于有线电视套餐,您必须购买某些类别的某些选项才能获得 HBO 或体育套餐。我试图解决的问题需要提供验证,以确保在用户做出某些选择时只向用户提供正确的包选项。我坚持使用 ProductCatalog 来确定 Purchase 对象中的产品是否属于某个类。

这是我的大致想法:

  rule Determine if a purchase is in classA

  when 
     $catalog: ProductCatalog ( )
     $purchases: Purchase()
     $prod: Product (  classification == $catalog..., code='XFEEEO222'....,  'PDX12224') from $purchases.orders

then
    insert($prod)

我想先验证他们是否选择了 A 类产品,然后才能继续进行其他产品分类。我不知道如何使用 ProductCatalog 从购买的产品中确定分类。如果有人能指导我如何完成那部分,我将不胜感激。

【问题讨论】:

    标签: drools rule-engine


    【解决方案1】:

    如果您想知道Purchase 是否包含至少1 个Product 类A,那么您可以尝试以下方式:

     rule Determine if a purchase is in classA
    
      when 
         $catalog: ProductCatalog ( )
         $purchases: Purchase()
         exists Product (this in $catalog.classA) from $purchases.orders
    
    then
    
    end
    

    请注意,在前面的规则中,您不能引用导致激活的 Product。如果您需要在 RHS 中引用 Product/s,那么您可以尝试以下操作:

     rule Determine if a purchase is in classA
    
      when 
         $catalog: ProductCatalog ( )
         $purchases: Purchase()
         $aProducts: List (size > 0) from collect (
            Product(this in $catalog.classA) from $purchases.orders
         )
    then
    
    end
    

    希望对你有帮助,

    结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 2011-02-05
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2021-07-30
      相关资源
      最近更新 更多