Drools 对产品目录中的产品进行验证,以确保选择正确的分类
Drools Validation of Products in a Product Catalog to ensure the right classification is chosen
我有一个 drools 应用程序,它有一个产品目录,产品。客户可以从多个 class 购买任意数量的产品。但是,他们只能从任何给定的 class 中购买一定数量的商品。这是 ProductCatalog class 的结构。它包含根据 class 化的产品列表。
class ProductCatalog {
List<Product> classA =
List<Product> classB =
List<Product> classC =
... getters and setters
}
接下来是产品 class,它提供允许客户订购的产品的详细信息。
class Product {
String classification;
String code;
String name;
String description;
BigDecimal cost;
...getters and setters
}
最后的 class 是采购 class,其中包含客户订购的所有产品。购买是捆绑在一起的,因此在该对象的 classifications 中没有分离产品。有许多规则决定了如何进行购买。用户只能按照这些规则捆绑他们想要购买的产品。这是购买 class.
class Purchase {
Customer details
List<Product> orders =
... getters and setters
}
购买策略类似于有线电视套餐,您必须购买某些类别的某些选项才能获得 HBO 或体育套餐。我试图解决的问题需要提供验证,以确保在用户做出某些选择时只向他们提供正确的包选项。我坚持使用 ProductCatalog 来确定 Purchase 对象中的产品是否在某个 class.
大致是这样想的:
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)
我想先验证他们是否从 classA 中选择了一个产品,然后才能继续进行其他产品 classifications。我不知道如何使用 ProductCatalog 来确定购买中产品的 classification。如果有人能指导我如何完成那部分,我将不胜感激。
如果你想知道 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
希望对您有所帮助,
结束
我有一个 drools 应用程序,它有一个产品目录,产品。客户可以从多个 class 购买任意数量的产品。但是,他们只能从任何给定的 class 中购买一定数量的商品。这是 ProductCatalog class 的结构。它包含根据 class 化的产品列表。
class ProductCatalog {
List<Product> classA =
List<Product> classB =
List<Product> classC =
... getters and setters
}
接下来是产品 class,它提供允许客户订购的产品的详细信息。
class Product {
String classification;
String code;
String name;
String description;
BigDecimal cost;
...getters and setters
}
最后的 class 是采购 class,其中包含客户订购的所有产品。购买是捆绑在一起的,因此在该对象的 classifications 中没有分离产品。有许多规则决定了如何进行购买。用户只能按照这些规则捆绑他们想要购买的产品。这是购买 class.
class Purchase {
Customer details
List<Product> orders =
... getters and setters
}
购买策略类似于有线电视套餐,您必须购买某些类别的某些选项才能获得 HBO 或体育套餐。我试图解决的问题需要提供验证,以确保在用户做出某些选择时只向他们提供正确的包选项。我坚持使用 ProductCatalog 来确定 Purchase 对象中的产品是否在某个 class.
大致是这样想的:
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)
我想先验证他们是否从 classA 中选择了一个产品,然后才能继续进行其他产品 classifications。我不知道如何使用 ProductCatalog 来确定购买中产品的 classification。如果有人能指导我如何完成那部分,我将不胜感激。
如果你想知道 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
希望对您有所帮助,
结束