9
Background:
I'm working on an application that transforms an input object into one of two output objects based upon a set of drools rules. The output object is not known until runtime and it is created in the first rule to execute.
Here is the rule that creates the output object and an example transformation rule:
rule "Initialization"
dialect "java"
salience 1000
no-loop true
when
t : Trade()
then
if(t.getTran().getInsType().equalsIgnoreCase("EEO") ||
t.getTran().getInsType().equalsIgnoreCase("EEF"))
{
insert(new Option());
}
else
{
insert(new Swap());
}
end
rule "Example Rule"
dialect "java"
when
t : Trade()
opt : Option()
then
opt.setCounterpartyName(t.getTran().getCParty());
end
Here is the code that is calling the rules:
private void test(){
for(File xmlFile : getXmlFilesFromDirectory(XML_DIRECTORY))
{
Trade trade = (Trade)unmarshall(xmlFile, Trade.class);
KnowledgeBase kbase = readKnowledgeBase();
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
List<Command> commands = new ArrayList<Command>();
commands.add(CommandFactory.newInsert(trade, "trade"));
commands.add(CommandFactory.newFireAllRules());
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
logger.close();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception
{
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("security-transformation.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
i