List<Declaration> requiredDeclarations,
InternalReadAccessor arrayReader,
Pattern pattern,
BindingDescr bind,
ConstraintConnectiveDescr result ) {
Declaration declr = context.getDeclarationResolver().getDeclaration( context.getRule(),
bind.getVariable() );
if ( declr != null ) {
// check right maps to a slot, otherwise we can't reverse this and should error
int pos = getPos( bind.getExpression(),
params );
if ( pos >= 0 ) {
// slot exist, reverse and continue
String slot = bind.getExpression();
String var = bind.getVariable();
bind.setVariable( slot );
bind.setExpression( var );
} else {
// else error, we cannot find the slot to unify against
}
}
// left does not already exist, is it a slot?
int pos = getPos( bind.getVariable(),
params );
if ( pos >= 0 ) {
// it's an input on a slot, is the input using bindings?
declr = context.getDeclarationResolver().getDeclaration( context.getRule(),
bind.getExpression() );
if ( declr != null ) {
arguments.set( pos,
declr );
declrIndexes.add( pos );
requiredDeclarations.add( declr );
} else {
// it must be a literal/expression
// it's an expression and thus an input
DrlExprParser parser = new DrlExprParser();
ConstraintConnectiveDescr bresult = parser.parse( bind.getExpression() );
if ( parser.hasErrors() ) {
for ( DroolsParserException error : parser.getErrors() ) {
context.addError( new DescrBuildError( context.getParentDescr(),
descr,
null,
"Unable to parser pattern expression:\n" + error.getMessage() ) );
}
return;
}
MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext();
String expr = context.getCompilerFactory().getExpressionProcessor().dump( bresult,
mvelCtx );
try {
MVELDialectRuntimeData data = ( MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
ParserConfiguration conf = data.getParserConfiguration();
conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() );
arguments.set( pos,
MVEL.executeExpression( MVEL.compileExpression( expr, new ParserContext( conf ) ) ) );
} catch ( Exception e ) {
context.addError( new DescrBuildError( context.getParentDescr(),
descr,
null,
"Unable to compile expression:\n" + expr ) );
}
}
} else {
// this is creating a new output binding
// we know it doesn't exist, as we already checked for left == var
declr = pattern.addDeclaration( bind.getVariable() );
pos = getPos( bind.getExpression(),
params );
if ( pos < 0 ) {
// error this must be a binding on a slot
context.addError( new DescrBuildError( context.getParentDescr(),
descr,
null,
"named argument does not exist:\n" + bind.getExpression() ) );
return;
}
// this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
ArrayElementReader reader = new ArrayElementReader( arrayReader,
pos,
params[pos].getExtractor().getExtractToClass() );
// Should the reader be registered like the others? Probably yes...
// PatternBuilder.registerReadAccessor( );
declr.setReadAccessor( reader );
varIndexes.add( pos );
arguments.set( pos,
Variable.v );
}