Drools技术问询:能否将决策表/Excel规则集存入数据库并调用执行?
Hey there! Let's tackle your Drools questions one by one:
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.
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 setrule_name: Human-readable name for easy referenceexcel_content: A BLOB (binary large object) field to store the raw Excel file bytes (works for both.xlsand.xlsxformats)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();
Step 4: Add optimizations (optional but recommended)
- Versioning: Use the
versionfield 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

