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

如何通过Java为Drools XLS决策表设置命中策略?

Configuring Hit Policies for XLS Drools Decision Tables (Drools 7, Eclipse)

Hey there, I’ve worked with Drools 7 and XLS decision tables quite a bit, so let’s walk through how to set up hit policies for your POC. Hit policies control how the engine evaluates and executes matching rules in your decision table—super important for getting the right behavior out of your rules.

Step 1: Set the Hit Policy Directly in the XLS Decision Table

Drools decision tables have a dedicated section in the XLS file to define the hit policy. Here’s how to edit it:

  • Open your XLS decision table in Excel (or Eclipse’s built-in spreadsheet editor, though Excel is easier for formatting).
  • Look for the Hit Policy cell—it’s usually in the top section, right next to the "RuleSet" or "Package" declarations.
  • Replace the default value (often NONE or blank) with your desired hit policy. Common options are:
    • FIRST: Stops after the first matching rule is executed.
    • ALL: Executes all matching rules.
    • COLLECT: Collects all matching rule results into a list.
    • ORDERED: Executes rules in the order they appear in the table.
    • PRIORITY: Executes rules based on a priority column you define.

Pro tip: Stick to uppercase for the hit policy value (while Drools 7 is case-insensitive, this aligns with official docs and avoids accidental typos).

For example, if you want to run every rule that matches your fact, set the hit policy cell to ALL.

Step 2: Load the Decision Table in Java Code

Once your XLS is configured, you need to load it into your Drools KieSession. Here’s a standard snippet tailored for Eclipse/Java:

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;

public class DecisionTableRunner {
    public static void main(String[] args) {
        KieServices kieServices = KieServices.Factory.get();
        KieRepository kieRepository = kieServices.getRepository();
        
        // Load your XLS decision table from the classpath
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newClassPathResource("customer-rules.xls"));
        
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll(); // Compiles rules from the XLS
        
        KieContainer kieContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
        KieSession kieSession = kieContainer.newKieSession();
        
        // Insert your business fact
        Customer customer = new Customer();
        customer.setAge(35);
        customer.setLocation("US");
        kieSession.insert(customer);
        
        // Fire rules—this respects your configured hit policy automatically
        kieSession.fireAllRules();
        
        kieSession.dispose();
    }
}
  • Place your XLS file in src/main/resources (for Maven/Gradle projects) or add it to Eclipse’s build path manually if you’re using a plain Java project.
  • No extra code is needed to enforce the hit policy—the engine reads it directly from the XLS during rule compilation.

Step 3: Verify and Test the Hit Policy

To confirm your hit policy is working:

  • For FIRST: Add two overlapping rules (both match the same fact) and check that only the first rule in the table executes (use System.out.println() in rule actions to track).
  • For COLLECT: Define a global list in the RuleSet section (e.g., global java.util.List ruleResults;), then add ruleResults.add($customer); to each rule’s action. After firing rules, inspect the list to see all matching results.
  • For PRIORITY: Add a "Priority" column to your decision table, assign numeric values (lower = higher priority), and verify rules run in the correct order.

Troubleshooting Common Snags

  • Hit policy not applying: Double-check the hit policy cell is in the correct header section (usually row 3 or 4, depending on your table structure). If unsure, generate a default Drools decision table template via Eclipse’s Drools wizard to reference the correct layout.
  • Eclipse not validating the XLS: Ensure the Drools plugin is installed (Help > Eclipse Marketplace > search for "Drools"). It adds syntax highlighting and validation to catch issues like misspelled hit policies.
  • Rule ordering issues with ORDERED: The engine uses the exact row order in the XLS—reorder rows to adjust execution sequence.

Hope this helps you get your POC up and running smoothly! If you hit any specific roadblocks, feel free to follow up with details.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:55:12