You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求提供OptaPlanner及Drools Accumulate Functions的官方文档链接

Hey there! I totally get how frustrating it is to hunt down specific function docs when you're working with OptaPlanner and Drools—those accumulate functions are super useful but can be tricky to track down if you're not looking in the right spots. Let's break this down for you:

Accumulate Functions in OptaPlanner/Drools: Breakdown & Context

Below is a breakdown of the functions you're asking about, along with where to find formal details in official documentation (no external links needed—you can navigate directly to these sections on the OptaPlanner and Drools sites):

Core Functions You're Searching For

sum()

  • This built-in function calculates the sum of a numeric field across a collection of planning entities or facts.
  • Practical example in a Drools rule (used within OptaPlanner constraints):
    rule "Calculate Total Resource Load"
      when
        $resource: Resource()
        $totalLoad: Number() from accumulate(
          Assignment(resource == $resource, $load: loadAmount),
          sum($load)
        )
      then
        // Use $totalLoad to enforce load-balancing constraints
        if ($totalLoad.intValue() > $resource.getMaxCapacity()) {
          scoreHolder.addHardConstraintMatch(kcontext, -1);
        }
    end
    
  • Formal details are in the Drools Rule Language Guide under the "Built-in Accumulate Functions" section, and OptaPlanner's documentation ties this to planning-specific use cases like resource capacity checks.

accumulate (Base Construct)

  • accumulate is the overarching structure that enables aggregation over sets of facts. It works with built-in functions like sum() or can be extended for custom logic.
  • Two common syntax styles:
    1. Simple syntax for built-in functions:
      $totalOrderValue: Number() from accumulate(
        Order(customer == $customer, $value: orderValue),
        sum($value)
      )
      
    2. Advanced syntax for custom accumulators:
      $customAggregate: CustomResult() from accumulate(
        Task($duration: duration),
        init(TaskAccumulator acc = new TaskAccumulator();),
        action(acc.addDuration($duration);),
        reverse(acc.removeDuration($duration);),
        result(acc.getTotalAndAverage();)
      )
      
  • Deep dives are available in the Drools documentation's "Rule Consequences & Aggregations" section.

list()

  • This accumulator collects matching facts into a List for further processing in your rules.
  • Example use case for planning:
    rule "Collect Understaffed Shifts"
      when
        $understaffedShifts: List() from accumulate(
          Shift(staffCount < requiredStaff),
          list()
        )
      then
        // Process the list to reallocate employees
        $understaffedShifts.forEach(shift -> 
          scoreHolder.addMediumConstraintMatch(kcontext, -1 * (shift.getRequiredStaff() - shift.getStaffCount()))
        );
    end
    
  • This function is grouped with other built-in accumulators in the same Drools "Accumulate Functions" section mentioned earlier.

Pro Tips for Finding More Context

  • OptaPlanner: Navigate to the "Integrating with Drools Rule Engine" chapter—this includes real-world examples of using accumulate functions to define planning constraints.
  • Drools: Head to the "Drools Rule Language (DRL)" guide, specifically the "Accumulate" subsections. All built-in functions (including the ones you're after) are documented here with syntax and use cases.
  • If formal docs feel too abstract, check OptaPlanner's sample projects (like employee rostering or vehicle routing)—they use these functions in practical constraint rules that mirror real planning problems.

内容的提问来源于stack exchange,提问作者P_C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:31:04