Search in sources :

Example 1 with ReturnValueRestrictionDescr

use of org.drools.compiler.lang.descr.ReturnValueRestrictionDescr in project drools by kiegroup.

the class PatternBuilder method getDeclarationsForReturnValue.

private Declaration[] getDeclarationsForReturnValue(RuleBuildContext context, RelationalExprDescr relDescr, String operator, String value2) {
    Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
    ReturnValueRestrictionDescr returnValueRestrictionDescr = new ReturnValueRestrictionDescr(operator, relDescr, value2);
    AnalysisResult analysis = context.getDialect().analyzeExpression(context, returnValueRestrictionDescr, returnValueRestrictionDescr.getContent(), new BoundIdentifiers(pattern, context, null, pattern.getObjectType().getClassType()));
    if (analysis == null) {
        // something bad happened
        return null;
    }
    final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    final List<Declaration> tupleDeclarations = new ArrayList<Declaration>();
    final List<Declaration> factDeclarations = new ArrayList<Declaration>();
    for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
        Declaration decl = context.getDeclarationResolver().getDeclaration(id);
        if (decl.getPattern() == pattern) {
            factDeclarations.add(decl);
        } else {
            tupleDeclarations.add(decl);
        }
    }
    createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), usedIdentifiers, factDeclarations);
    final Declaration[] previousDeclarations = tupleDeclarations.toArray(new Declaration[tupleDeclarations.size()]);
    final Declaration[] localDeclarations = factDeclarations.toArray(new Declaration[factDeclarations.size()]);
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    final String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] requiredDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length];
    System.arraycopy(previousDeclarations, 0, requiredDeclarations, 0, previousDeclarations.length);
    System.arraycopy(localDeclarations, 0, requiredDeclarations, previousDeclarations.length, localDeclarations.length);
    Declaration[] declarations = new Declaration[requiredDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration requiredDeclaration : requiredDeclarations) {
        declarations[i++] = requiredDeclaration;
    }
    for (String requiredGlobal : requiredGlobals) {
        declarations[i++] = context.getDeclarationResolver().getDeclaration(requiredGlobal);
    }
    return declarations;
}
Also used : Pattern(org.drools.core.rule.Pattern) ReturnValueRestrictionDescr(org.drools.compiler.lang.descr.ReturnValueRestrictionDescr) ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Aggregations

ArrayList (java.util.ArrayList)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 ReturnValueRestrictionDescr (org.drools.compiler.lang.descr.ReturnValueRestrictionDescr)1 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)1 Declaration (org.drools.core.rule.Declaration)1 Pattern (org.drools.core.rule.Pattern)1 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1 NegConstraint (org.drools.core.rule.constraint.NegConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 Constraint (org.drools.core.spi.Constraint)1