Drools in FIX Edge Java
1. Intro
Drools is an extension on the current Business Layer, not its replacement. It is present in standard package, embedded in FEJ and can be used without some additional components.
FEJ API can be called from Drools rules. External Drools server cannot be used.
2. Drools Engine documentation
The following property is used to configure if Drools Engine is enabled or disabled:
drools.engine.enabled |
Possible values: true | false. Default value: false.
This property is specified in fixedge.properties file in FIXEdge Java.
2.1.1. Methods to work with stateless sessions
org.kie.api.runtime.StatelessKieSession createStateless(String sessionName) throws com.epam.fej.routing.drools.DroolsException
Creates new stateless session and returns it.
Parameters:
sessionName - the name of the session
Returns created stateless Kie Session.
Throws DroolsException if Drools Engine is not initialized.
org.drools.core.runtime.impl.ExecutionResults runStateless(String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Executes command for stateless session.
Parameters:
sessionName - the name of the session
values - command arguments
Returns execution result.
Throws DroolsException if Drools Engine is not initialized.
2.1.2. Methods to work with stateful sessions
2.1.2.1. Methods to create session
org.kie.api.runtime.KieSession createStateful(String id, String sessionName) throws com.epam.fej.routing.drools.DroolsException
Creates new stateful session and adds it to sessions cache. SessionName is used to create new session if a session is not found by id.
Please pay attention: When new Drools Config comes from FIXICC H2, the session will be disposed. A user should get session again.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
Returns created Kie Session.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession getOrCreateStateful(String id, String sessionName) throws com.epam.fej.routing.drools.DroolsException
Returns existing stateful session if it exists in sessions cache. Otherwise creates new stateful session, adds it to the sessions cache and returns. SessionName is used to create new session if a session is not found by id.
Please pay attention: When new Drools Config comes from FIXICC H2, the session will be disposed. A user should get session again.
Parameters:
id - the ID of the session in sessions cachet
sessionName - the name of the session
Returns Kie Session from the sessions cache.
Throws DroolsException if Drools Engine is not initialized.
2.1.2.2. Methods to run session
org.kie.api.runtime.KieSession runStateful(String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session and fires all matches of the agenda for this session. SessionName is used as session id.
Parameters:
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession runStatefulAndClean(String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session, fires all matches of the agenda for this session and retracts the facts. SessionName is used as session id.
Parameters:
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession runStateful(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session and fires all matches of the agenda for this session. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession runStatefulAndClean(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session, fires all matches of the agenda for this session and retracts the facts. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireAll(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session and fires all matches of the agenda for this session. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireAllUntilHalt(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session and keeps firing Matches until a halt is called for this session. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireAllAndClean(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session, fires all matches of the agenda for this session and retracts the facts. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireAllUntilHaltAndClean(String id, String sessionName, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session, keeps firing Matches until a halt is called for this session and retracts the facts. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
values - the facts to be inserted
Returns Kie Session the matches have been fired for.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireRules(String id, String sessionName, Consumer<org.kie.api.runtime.KieSession> method, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session and performs consumer operation on the session. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
method - operation performed on the session
values - the facts to be inserted
Returns Kie Session the operation has been performed on.
Throws DroolsException if Drools Engine is not initialized.
org.kie.api.runtime.KieSession fireRulesAndClean(String id, String sessionName, Consumer<org.kie.api.runtime.KieSession> method, Object...values) throws com.epam.fej.routing.drools.DroolsException
Inserts new facts into the stateful session, performs consumer operation on the session and retracts the facts. SessionName is used to create new session if a session is not found by id.
Parameters:
id - the ID of the session in sessions cache
sessionName - the name of the session
method - operation performed on the session
values - the facts to be inserted
Returns Kie Session the operation has been performed on.
Throws DroolsException if Drools Engine is not initialized.
void fireAllRules(org.kie.api.runtime.KieSession kieSession)
Fire all Matches of the agenda for a stateful session.
Parameters:
kieSession - stateful session
void fireUntilHalt(org.kie.api.runtime.KieSession kieSession)
Fire all Matches for a stateful session until a halt is called for this session.
Parameters:
kieSession - stateful session
2.1.2.3. Methods to dispose sessions
void dispose(String id) throws com.epam.fej.routing.drools.DroolsException
Releases all the current session resources, setting up the session for garbage collection.
Parameters:
id - the ID of the session in sessions cache
Throws DroolsException if Drools Engine is not initialized.
void disposeSessions()
Disposes all Kie stateful sessions.
2.1.3. Other methods
void reset()
Initializes Kie Container and Kie Container Sessions Pool.
void clear()
Disposes Kie Sessions and Kie Container, clear Kie Sessions cache, shutdowns Kie Container Sessions Pool.
org.kie.api.runtime.KieContainer getContatiner()
Returns Kie Container.
3. Drools Config Manager Listener documentation
If you need to create your own listener, you should implement DroolsConfigManagerListener interface and override it's methods. After this, the listener should be registered in fej-routing.xml.
Example:
<bean id="droolsCfgListener" class="com.epam.fej.routing.drools.cfg.NoOpDroolsConfigManagerListener"/> |
3.1.1. Listener methods
void onConfigChanged(com.epam.fej.routing.drools.cfg.DroolsConfig droolsConfig, Path pathInCache)
Apply some actions if Drools configuration (kmodule.xml, Drools assets) has been changed.
Parameters:
droolsConfig - Drools config (kmodule.xml, Drools assets)
pathInCache - path where Drools configuration is saved in cache
void onKModuleChanged(String kmodule)
Apply some actions if kmodule.xml has been changed.
Parameters:
kmodule - the content of kmodule.xml
void onAssetAdded(com.epam.fej.routing.drools.cfg.RuleSetConfig ruleSetConfig, Path pathInCache)
Apply some actions if Drools asset has been added.
Parameters:
ruleSetConfig - added Drools asset
pathInCache - path where Drools asset is saved in cache
void onAssetChanged(com.epam.fej.routing.drools.cfg.RuleSetConfig ruleSetConfig, Path pathInCache)
Apply some actions if Drools asset has been changed.
Parameters:
ruleSetConfig - changed Drools asset
pathInCache - path where Drools asset is saved in cache
void onAssetDeleted(com.epam.fej.routing.drools.cfg.RuleSetConfig ruleSetConfig, Path pathInCache)
Apply some actions if Drools asset has been deleted.
Parameters:
ruleSetConfig - deleted Drools asset
pathInCache - path where Drools asset is saved in cache
4. Examples
4.1 This example describes how to work with Drools in FEJ with FIXICC-H2
Add the following business rules configuration:
import com.epam.fej.routing.drools.DroolsEngine
import static dsl.CommonRulesDsl.rulesDSL
DroolsEngine dre = drools as DroolsEngine
rulesDSL(routingContext as RoutingContext) {
messageRules {
messageRule("Route FIX message by decision table in kbase1") {
action {
custom {
ctx ->
dre.runStatefulAndClean("ksession1", ctx)
routingContext.getDestinationById(ctx.messageEvent.getHeader("destination")).send(ctx.messageEvent)
}
context exit
}
}
}
} |
Then configure Drools. To do it, in FIXICC-H2 select "Edit kmodule.xml" menu item in Server View in Configuration menu and add the following content:
Then add new Drools component. Select "Drools components" menu item in Server View in Configuration menu, click "Add" popup button, choose "Decision table" and upload the following file:
Spreadsheet
Enter Package value, check Enabled checkbox and click OK button:
Then add FIX sessions. There are 3 acceptor sessions in our example: session1 (target1-sender1), session2 (target2-sender2) and session3 (target3-session3):
After this, connect to these sessions. Create initiator sessions session1 (sender1-target1), session2 (sender2-target2) and session3 (sender3-target3).
To test Drools, send some message to session1. As a result, session3 will receive this message.
4.2 This example describes mixed DSL/Drools calls.
Add the following business rules configuration:
Routing- Other blocks besides messageRules can be called
Rule1- General Routing based on message type
Rule2- Custom Drools call