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

Drools技术问询:能否将决策表/Excel规则集存入数据库并调用执行?

Hey there! Let's tackle your Drools questions one by one:

1. 通用技术疑问:Drools是否支持将决策表保存至数据库,并像Excel规则文件一样调用使用?

Absolutely! Drools doesn't tie you to a specific storage location for decision tables. Whether your table lives as an Excel file on disk, a binary blob in a database, or even a cloud storage object, the core logic stays the same: as long as you can get the decision table content into Drools' parser, it will work just like a local Excel file.

Drools operates by parsing the content of the decision table (regardless of where it's stored) into executable rules. So storing it in a database is totally feasible—you just need to handle retrieving the content and feeding it to Drools' APIs.

2. 个人场景需求:可行的实现方案

Since you already have Excel-based rule sets, here's a practical, step-by-step plan to store them in a database and load them into Drools seamlessly:

Step 1: Design your database storage

Create a table to manage your decision tables with these key fields:

  • rule_set_id: Unique identifier for each rule set
  • rule_name: Human-readable name for easy reference
  • excel_content: A BLOB (binary large object) field to store the raw Excel file bytes (works for both .xls and .xlsx formats)
  • version: Version number (critical for tracking updates and avoiding stale rule loads)
  • created_at/updated_at: Timestamp fields to track when rules are added or modified

Step 2: Retrieve Excel content from the database

Use your preferred database tooling (JDBC, MyBatis, Hibernate, etc.) to fetch the excel_content BLOB for your target rule set. Convert the BLOB into a byte array or input stream—this is what you'll pass to Drools.

Step 3: Load the Excel content into Drools

Use Drools' Kie API to parse the retrieved Excel content and initialize your rule session. Here's a simplified code example:

// Get the core Drools service instance
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();

// Assume this method fetches the Excel bytes from your database
byte[] excelRuleBytes = fetchExcelFromDatabase("your_target_rule_set_id");

// Add the Excel content to the KieFileSystem with a virtual resource path
kfs.write(
    "src/main/resources/rules/db-loaded-rule-table.xls",
    kieServices.getResources().newByteArrayResource(excelRuleBytes)
);

// Build the rule set
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
// Check for build errors
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
    throw new RuntimeException("Rule build failed: " + kieBuilder.getResults());
}

// Initialize the rule container and session
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieSession kieSession = kieContainer.newKieSession();

// Execute rules as you normally would
kieSession.insert(yourBusinessFactObject);
kieSession.fireAllRules();
kieSession.dispose();
  • Versioning: Use the version field to only reload rules when the version changes—this avoids unnecessary database calls and rule rebuilds.
  • Caching: Implement a cache (like Guava Cache or Caffeine) to store initialized KieContainers for frequently used rule sets. Invalidate the cache whenever a rule set's version is updated.
  • Alternative storage: If you prefer text-based storage for easier debugging, convert the Excel decision table to Drools DRL format first, then store the DRL text in a VARCHAR/TEXT field instead of a BLOB. This works just as well, though you'll lose direct Excel editing capability unless you re-convert when updating rules.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:51:22