此内容没有您所选择的语言版本。

24.8. Pet Store Example: Evaluate Agenda Group from PetStore.drl


// Free Fish Food sample when we buy a Gold Fish if we haven't already bought 
// Fish Food and don't already have a Fish Food Sample
rule "Free Fish Food Sample"
    agenda-group "evaluate"
    dialect "mvel"
when
    $order : Order()
    not ( $p : Product( name == "Fish Food") && Purchase( product == $p ) )
    not ( $p : Product( name == "Fish Food Sample") && Purchase( product == $p ) )
    exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )
    $fishFoodSample : Product( name == "Fish Food Sample" );
then
    System.out.println( "Adding free Fish Food Sample to cart" );
    purchase = new Purchase($order, $fishFoodSample);
    insert( purchase );
    $order.addItem( purchase ); 
end

// Suggest a tank if we have bought more than 5 gold fish and don't already have one
rule "Suggest Tank"
    agenda-group "evaluate"
    dialect "java"
when
    $order : Order()
    not ( $p : Product( name == "Fish Tank") && Purchase( product == $p ) )
    ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
    $fishTank : Product( name == "Fish Tank" )
then
    requireTank(frame, drools.getWorkingMemory(), $order, $fishTank, $total); 
end
Copy to Clipboard Toggle word wrap
The rule "Free Fish Food Sample" will only fire if:
  • The store does not already have any fish food, and
  • The store does not already have a free fish food sample, and
  • The store does have a Gold Fish in its order.
The rule "Suggest Tank" will only fire if
  • The store does notalready have a Fish Tank in its order, and
  • The store does have more than five Gold Fish Products in its order.
  • If the rule does fire, it calls the requireTank() function . This shows a Dialog to the user, and adding a Tank to the order and Working Memory if confirmed.
  • When calling the requireTank() function the rule passes the global frame variable so that the function has a handle to the Swing GUI.
  • If the rule does fire, it creates a new product (Fish Food Sample), and adds it to the order in Working Memory.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat