private void replaceDeclarations( DeclarationScopeResolver resolver, Pattern pattern, Constraint constraint) { Declaration[] decl = constraint.getRequiredDeclarations(); for (Declaration aDecl : decl) { Declaration resolved = resolver.getDeclaration(null, aDecl.getIdentifier()); 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 if (resolved.getPattern() != pattern) { ((MvelConstraint) constraint).unsetUnification(); } } if (resolved != null && resolved != aDecl && resolved.getPattern() != pattern) { constraint.replaceDeclaration(aDecl, resolved); } else if (resolved == null) { // it is probably an implicit declaration, so find the corresponding pattern Pattern old = aDecl.getPattern(); Pattern current = resolver.findPatternByIndex(old.getIndex()); if (current != null && old != current) { resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current); constraint.replaceDeclaration(aDecl, resolved); } } } }
protected void findConstructor(Declaration[] declarations) throws NoSuchMethodException { int N = this.abducibleArgs.length; cachedConstructor = null; List<Class> availableArgs = N > 0 ? new ArrayList<Class>(N) : Collections.<Class>emptyList(); for (int j = 0; j < N; j++) { // during the initial build (KieBuilder), the declarations are provided on the fly and use for // type checking // when building the KieBase, the internal declarations are set and can be used Declaration decl = declarations != null ? declarations[mapArgToParam(j)] : getDeclaration(abducibleArgs[j]); if (decl != null) { availableArgs.add(decl.getExtractor().getExtractToClass()); } } Class klass = returnType.getClassType(); while (cachedConstructor == null) { try { cachedConstructor = klass.getConstructor(availableArgs.toArray(new Class[availableArgs.size()])); } catch (NoSuchMethodException nsme) { if (klass == Object.class) { throw nsme; } else { klass = klass.getSuperclass(); } } } }
public void testDeclaration() throws IntrospectionException { final FieldExtractor extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); final Pattern pattern = new Pattern(5, new ClassObjectType(Cheese.class)); // Bind the extractor to a decleration // Declarations know the pattern they derive their value from final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern); assertEquals("typeOfCheese", declaration.getIdentifier()); assertSame(String.class, declaration.getExtractor().getExtractToClass()); assertSame(extractor, declaration.getExtractor()); assertEquals(5, declaration.getPattern().getOffset()); }
private void replaceDeclarations(DeclarationScopeResolver resolver, Accumulate accumulate) { Declaration[] decl = accumulate.getRequiredDeclarations(); for (Declaration aDecl : decl) { Declaration resolved = resolver.getDeclaration(null, aDecl.getIdentifier()); if (resolved != null && resolved != aDecl) { accumulate.replaceDeclaration(aDecl, resolved); } else if (resolved == null) { // it is probably an implicit declaration, so find the corresponding pattern Pattern old = aDecl.getPattern(); Pattern current = resolver.findPatternByIndex(old.getIndex()); if (current != null && old != current) { resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current); accumulate.replaceDeclaration(aDecl, resolved); } } } }
/** recurse through the rule condition elements updating the declaration objecs */ private void processElement( final DeclarationScopeResolver resolver, final Stack<RuleConditionElement> contextStack, final RuleConditionElement element) { if (element instanceof Pattern) { Pattern pattern = (Pattern) element; for (RuleConditionElement ruleConditionElement : pattern.getNestedElements()) { processElement(resolver, contextStack, ruleConditionElement); } for (Constraint constraint : pattern.getConstraints()) { if (constraint instanceof Declaration) { continue; } replaceDeclarations(resolver, pattern, constraint); } } else if (element instanceof EvalCondition) { processEvalCondition(resolver, (EvalCondition) element); } else if (element instanceof Accumulate) { for (RuleConditionElement rce : element.getNestedElements()) { processElement(resolver, contextStack, rce); } Accumulate accumulate = (Accumulate) element; replaceDeclarations(resolver, accumulate); accumulate.resetInnerDeclarationCache(); } else if (element instanceof From) { DataProvider provider = ((From) element).getDataProvider(); Declaration[] decl = provider.getRequiredDeclarations(); for (Declaration aDecl : decl) { Declaration resolved = resolver.getDeclaration(null, aDecl.getIdentifier()); if (resolved != null && resolved != aDecl) { provider.replaceDeclaration(aDecl, resolved); } else if (resolved == null) { // it is probably an implicit declaration, so find the corresponding pattern Pattern old = aDecl.getPattern(); Pattern current = resolver.findPatternByIndex(old.getIndex()); if (current != null && old != current) { resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current); provider.replaceDeclaration(aDecl, resolved); } } } } else if (element instanceof QueryElement) { QueryElement qe = (QueryElement) element; Pattern pattern = qe.getResultPattern(); for (Entry<String, Declaration> entry : pattern.getInnerDeclarations().entrySet()) { Declaration resolved = resolver.getDeclaration(null, entry.getValue().getIdentifier()); if (resolved != null && resolved != entry.getValue() && resolved.getPattern() != pattern) { entry.setValue(resolved); } } List<Integer> varIndexes = asList(qe.getVariableIndexes()); for (int i = 0; i < qe.getArguments().length; i++) { if (!(qe.getArguments()[i] instanceof QueryArgument.Declr)) { continue; } Declaration declr = ((QueryArgument.Declr) qe.getArguments()[i]).getDeclaration(); Declaration resolved = resolver.getDeclaration(null, declr.getIdentifier()); if (resolved != declr && resolved.getPattern() != pattern) { qe.getArguments()[i] = new QueryArgument.Declr(resolved); } 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 declr = pattern.addDeclaration(declr.getIdentifier()); // this bit is different, notice its the ArrayElementReader that we wire up to, not the // declaration. ArrayElementReader reader = new ArrayElementReader( new SelfReferenceClassFieldReader(Object[].class), i, resolved.getDeclarationClass()); declr.setReadAccessor(reader); varIndexes.add(i); } } qe.setVariableIndexes(toIntArray(varIndexes)); } else if (element instanceof ConditionalBranch) { processBranch(resolver, (ConditionalBranch) element); } else { contextStack.push(element); for (RuleConditionElement ruleConditionElement : element.getNestedElements()) { processElement(resolver, contextStack, ruleConditionElement); } contextStack.pop(); } }
/** * recurse through the rule condition elements updating the declaration objecs * * @param resolver * @param contextStack * @param element */ private void processElement( final DeclarationScopeResolver resolver, final Stack contextStack, final RuleConditionElement element) { if (element instanceof Pattern) { Pattern pattern = (Pattern) element; for (Iterator it = pattern.getNestedElements().iterator(); it.hasNext(); ) { processElement(resolver, contextStack, (RuleConditionElement) it.next()); } for (Constraint next : pattern.getConstraints()) { 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 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); } } } } } else if (element instanceof EvalCondition) { Declaration[] decl = ((EvalCondition) element).getRequiredDeclarations(); for (Declaration aDecl : decl) { Declaration resolved = resolver.getDeclaration(null, aDecl.getIdentifier()); if (resolved != null && resolved != aDecl) { ((EvalCondition) element).replaceDeclaration(aDecl, resolved); } } } else if (element instanceof Accumulate) { for (RuleConditionElement rce : element.getNestedElements()) { processElement(resolver, contextStack, rce); } ((Accumulate) element).resetInnerDeclarationCache(); } else if (element instanceof From) { DataProvider provider = ((From) element).getDataProvider(); Declaration[] decl = provider.getRequiredDeclarations(); for (Declaration aDecl : decl) { Declaration resolved = resolver.getDeclaration(null, aDecl.getIdentifier()); if (resolved != null && resolved != aDecl) { provider.replaceDeclaration(aDecl, resolved); } else if (resolved == null) { // it is probably an implicit declaration, so find the corresponding pattern Pattern old = aDecl.getPattern(); Pattern current = resolver.findPatternByIndex(old.getIndex()); if (current != null && old != current) { resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current); provider.replaceDeclaration(aDecl, resolved); } } } } else if (element instanceof QueryElement) { QueryElement qe = (QueryElement) element; Pattern pattern = qe.getResultPattern(); for (Entry<String, Declaration> entry : pattern.getInnerDeclarations().entrySet()) { Declaration resolved = resolver.getDeclaration(null, entry.getValue().getIdentifier()); if (resolved != null && resolved != entry.getValue() && resolved.getPattern() != pattern) { entry.setValue(resolved); } } List<Integer> varIndexes = ArrayUtils.asList(qe.getVariableIndexes()); for (int i = 0; i < qe.getDeclIndexes().length; i++) { Declaration declr = (Declaration) qe.getArgTemplate()[qe.getDeclIndexes()[i]]; Declaration resolved = resolver.getDeclaration(null, declr.getIdentifier()); if (resolved != null && resolved != declr && resolved.getPattern() != pattern) { qe.getArgTemplate()[qe.getDeclIndexes()[i]] = resolved; } 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 declr = pattern.addDeclaration(declr.getIdentifier()); // this bit is different, notice its the ArrayElementReader that we wire up to, not the // declaration. ArrayElementReader reader = new ArrayElementReader( new SelfReferenceClassFieldReader(Object[].class, "this"), qe.getDeclIndexes()[i], resolved.getExtractor().getExtractToClass()); declr.setReadAccessor(reader); varIndexes.add(qe.getDeclIndexes()[i]); } } qe.setVariableIndexes(ArrayUtils.toIntArray(varIndexes)); } else { contextStack.push(element); for (RuleConditionElement ruleConditionElement : element.getNestedElements()) { processElement(resolver, contextStack, ruleConditionElement); } contextStack.pop(); } }