for ( Iterator it = pattern.getConstraints().iterator(); it.hasNext(); ) {
Object next = it.next();
if ( next instanceof Declaration ) {
continue;
}
Constraint constraint = (Constraint) next;
Declaration[] decl = constraint.getRequiredDeclarations();
for ( int i = 0; i < decl.length; i++ ) {
Declaration resolved = resolver.getDeclaration( null,
decl[i].getIdentifier() );
if ( constraint instanceof VariableConstraint && ((VariableConstraint)constraint).getRestriction() instanceof UnificationRestriction ) {
UnificationRestriction restriction = ( UnificationRestriction ) ((VariableConstraint)constraint).getRestriction();
if( ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom( resolved.getPattern().getObjectType() ) ) {
// if the resolved still points to DroolsQuery, we know this is the first unification pattern, so redeclare it as the visible Declaration
Declaration redeclaredDeclr = new Declaration(resolved.getIdentifier(), restriction.getVariableRestriction().getReadAccessor(), pattern, false );
pattern.addDeclaration( redeclaredDeclr );
} else if ( resolved.getPattern() != pattern ) {
// It's a subsequent unification, so it should be able to use the redeclared Declaration as a normal VariableRestriction.
// but only rewrite if the resolved declaration is not for current pattern (this occurs if LogicTransformer is applied twice
// to the same tree that has already been rewritten before.
((VariableConstraint)constraint).setRestriction( restriction.getVariableRestriction() );
}
} else if (constraint instanceof MvelConstraint && ((MvelConstraint)constraint).isUnification()) {
if( ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom( resolved.getPattern().getObjectType() ) ) {
Declaration redeclaredDeclr = new Declaration(resolved.getIdentifier(), ((MvelConstraint)constraint).getFieldExtractor(), pattern, false );
pattern.addDeclaration(redeclaredDeclr);
} else {
((MvelConstraint)constraint).unsetUnification();
}
}
if ( resolved != null && resolved != decl[i] && resolved.getPattern() != pattern ) {
constraint.replaceDeclaration( decl[i],
resolved );
} else if ( resolved == null ) {
// it is probably an implicit declaration, so find the corresponding pattern
Pattern old = decl[i].getPattern();
Pattern current = resolver.findPatternByIndex( old.getIndex() );
if ( current != null && old != current ) {
resolved = new Declaration( decl[i].getIdentifier(),
decl[i].getExtractor(),
current );
constraint.replaceDeclaration( decl[i],
resolved );
}
}
}
}