final RuleDescr ruleDescr = context.getRuleDescr();
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations( context.getRule() );
BoundIdentifiers bindings = new BoundIdentifiers(context.getDeclarationResolver().getDeclarationClasses( decls ),
context.getPackageBuilder().getGlobals(),
null,
KnowledgeHelper.class );
String consequenceStr = ( "default".equals( consequenceName ) ) ? (String) ruleDescr.getConsequence() : (String) ruleDescr.getNamedConsequences().get( consequenceName );
consequenceStr = consequenceStr + "\n";
JavaAnalysisResult analysis = ( JavaAnalysisResult) context.getDialect().analyzeBlock( context,
ruleDescr,
consequenceStr,
bindings );
if ( analysis == null ) {
// not possible to get the analysis results
return;
}
// This is a list of all the non container blocks, which initially are in tree form.
List<JavaBlockDescr> blocks = new ArrayList<JavaBlockDescr>();
buildBlockDescrs( blocks, analysis.getBlockDescrs() );
// @TODO <!--(mdp) commented this out until MVEL supports generics.
// // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
// setContainerBlockInputs(context,
// descrs,
// analysis.getBlockDescrs(),
// consequenceStr,
// bindings,
// new HashMap(),
// 0 );
// @TODO (mdp) commented this out until MVEL supports generics.-->
// this will fix modify, retract, insert, update, entrypoints and channels
String fixedConsequence = this.fixBlockDescr( context,
consequenceStr,
blocks,
bindings,
decls );
if ( fixedConsequence == null ) {
// not possible to rewrite the modify blocks
return;
}
fixedConsequence = ((JavaDialect) context.getDialect()).getKnowledgeHelperFixer().fix( fixedConsequence );
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
String[] declrStr = new String[declarations.length];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet() ) {
declrStr[j] = str;
declarations[j++] = decls.get( str );
}
Arrays.sort( declarations, SortDeclarations.instance );
for ( int i = 0; i < declrStr.length; i++) {
declrStr[i] = declarations[i].getIdentifier();
}
context.getRule().setRequiredDeclarations( declrStr );
final Map<String, Object> map = createVariableContext( className,
fixedConsequence,
context,
declarations,
null,
usedIdentifiers.getGlobals(),
(JavaAnalysisResult) analysis );
map.put( "consequenceName", consequenceName );
//final int[] indexes = new int[declarations.length];