public Globals getGlobals() {
return this.sessionGlobals;
}
public <T> T execute(Command<T> command) {
StatefulKnowledgeSession ksession = newWorkingMemory();
FixedKnowledgeCommandContext context = new FixedKnowledgeCommandContext( new ContextImpl( "ksession",
null ),
null,
null,
ksession,
null );
try {
((StatefulKnowledgeSessionImpl) ksession).session.startBatchExecution( new ExecutionResultImpl() );
Object o = ((GenericCommand) command).execute( context );
// did the user take control of fireAllRules, if not we will auto execute
boolean autoFireAllRules = true;
if ( command instanceof FireAllRulesCommand ) {
autoFireAllRules = false;
} else if ( command instanceof BatchExecutionCommandImpl ) {
for ( Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands() ) {
if ( nestedCmd instanceof FireAllRulesCommand ) {
autoFireAllRules = false;
break;
}
}
}
if ( autoFireAllRules ) {
ksession.fireAllRules( );
}
if ( command instanceof BatchExecutionCommandImpl) {
ExecutionResults result = ((StatefulKnowledgeSessionImpl) ksession).session.getExecutionResult();
return (T) result;
} else {
return (T) o;
}
} finally {
((StatefulKnowledgeSessionImpl) ksession).session.endBatchExecution();
ksession.dispose();
}
}