Drools spring hibernate load rules from database
Drools is a business rule management system (rules engine). there are multiple ways to define and process rules.
1. define rules in drl file and this .drl file can be saved in resources.
2. define template in .drt file (in code resources) and load rules from excel file.
3. define template in .drt file and load rules from database.
option 3 is little tricky in hibernate where it takes input only in java.sql.ResultSet format. Implementation for option 3 is as below.
sample drt file
assume you have columns in database table (eg- rule) id,rule_type,agenda_group,activation_group,salience,when_logic,then_logic
implementation
If you have doubts or need any clarification, please let me know in comments.
1. define rules in drl file and this .drl file can be saved in resources.
2. define template in .drt file (in code resources) and load rules from excel file.
3. define template in .drt file and load rules from database.
option 3 is little tricky in hibernate where it takes input only in java.sql.ResultSet format. Implementation for option 3 is as below.
sample drt file
template header id rule_type agenda_group activation_group salience when_logic then_logic package com.sample; import java.util.Arrays; import java.util.ArrayList; import java.lang.Long; template "sample_rule" rule "sample_@{id}_@{rule_type}" agenda-group "@{agenda_group}" activation-group "@{activation_group}" salience @{salience} when @{when_logic} then @{then_logic} end end template
assume you have columns in database table (eg- rule) id,rule_type,agenda_group,activation_group,salience,when_logic,then_logic
implementation
@Autowired private EntityManagerFactory entityManagerFactory; private ResultSet getResultSet() { SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); Session session = sessionFactory.openSession(); String query = "SELECT id,rule_type,agenda_group,activation_group,salience,when_logic,then_logic from rule"; return session.doReturningWork(connection -> { PreparedStatement statement = connection.prepareStatement(query); return statement.executeQuery(); }); } private KieSession getKieSession() { ResultSetGenerator resultSetGenerator = new ResultSetGenerator(); InputStream inputStream = null; try { inputStream = drt.getInputStream(); // convert sample.drt in resource to input stream } catch (IOException e) { e.printStackTrace(); } String drl = resultSetGenerator.compile(resultSet, inputStream); log.info(drl); kieFileSystem.write("src/main/resources/sample.drl", kieServices.getResources().newReaderResource(new StringReader(drl))); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); KieModule kieModule = kieBuilder.getKieModule(); kieContainer = kieServices.newKieContainer(kieModule.getReleaseId()); return kieHelperService.getStatefulSession(); // now this kieSession is ready to insert facts and fire rules }
If you have doubts or need any clarification, please let me know in comments.
Hi,
ReplyDeleteThank you for providing us this code, i tried to implement the same solution but the code given is not complete, i wonder if is it possible to give us the whole demo.
Thanks in advance.
did you implement this?
Delete