Search in sources :

Example 56 with Declaration

use of org.drools.core.rule.Declaration 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)

Example 57 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class PatternBuilder method isDateType.

private boolean isDateType(RuleBuildContext context, Pattern pattern, String leftValue) {
    Declaration declaration = pattern.getDeclarations().get(leftValue);
    if (declaration != null && declaration.getExtractor() != null) {
        return declaration.getValueType() == ValueType.DATE_TYPE;
    }
    if (pattern.getObjectType() instanceof FactTemplateObjectType) {
        return ((FactTemplateObjectType) pattern.getObjectType()).getFactTemplate().getFieldTemplate(leftValue).getValueType() == ValueType.DATE_TYPE;
    }
    Class<?> clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
    Class<?> fieldType = context.getPkg().getClassFieldAccessorStore().getFieldType(clazz, leftValue);
    return fieldType != null && ValueType.isDateType(fieldType);
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 58 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class PatternBuilder method getUsedDeclarations.

public static Declaration[][] getUsedDeclarations(RuleBuildContext context, Pattern pattern, AnalysisResult analysis) {
    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(), analysis.getBoundIdentifiers(), factDeclarations);
    Declaration[][] usedDeclarations = new Declaration[2][];
    usedDeclarations[0] = tupleDeclarations.toArray(new Declaration[tupleDeclarations.size()]);
    usedDeclarations[1] = factDeclarations.toArray(new Declaration[factDeclarations.size()]);
    return usedDeclarations;
}
Also used : ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 59 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class PatternBuilder method createDeclarationForOperator.

private static Declaration createDeclarationForOperator(RuleBuildContext context, Pattern pattern, String expr) {
    Declaration declaration;
    int dotPos = expr.indexOf('.');
    if (dotPos < 0) {
        if (!isIdentifier(expr)) {
            return null;
        }
        declaration = context.getDeclarationResolver().getDeclaration(expr);
        if (declaration == null) {
            if ("this".equals(expr)) {
                declaration = createDeclarationObject(context, "this", pattern);
            } else {
                declaration = new Declaration("this", pattern);
                context.getPkg().getClassFieldAccessorStore().getReader(pattern.getObjectType().getClassName(), expr, declaration);
            }
        }
    } else {
        String part1 = expr.substring(0, dotPos).trim();
        String part2 = expr.substring(dotPos + 1).trim();
        if ("this".equals(part1)) {
            declaration = createDeclarationObject(context, part2, (Pattern) context.getDeclarationResolver().peekBuildStack());
        } else {
            declaration = context.getDeclarationResolver().getDeclaration(part1);
            // if a declaration exists, then it may be a variable direct property access
            if (declaration != null) {
                declaration = createDeclarationObject(context, part2, declaration.getPattern());
            }
        }
    }
    return declaration;
}
Also used : Pattern(org.drools.core.rule.Pattern) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) 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)

Example 60 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class PatternBuilder method buildEval.

@SuppressWarnings("unchecked")
protected Constraint buildEval(final RuleBuildContext context, final Pattern pattern, final PredicateDescr predicateDescr, final Map<String, OperatorDescr> aliases, final String expr, final boolean isEvalExpression) {
    AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
    if (analysis == null) {
        // something bad happened
        return null;
    }
    Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
    Declaration[] previousDeclarations = usedDeclarations[0];
    Declaration[] localDeclarations = usedDeclarations[1];
    BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    boolean isJavaEval = isEvalExpression && context.getDialect() instanceof JavaDialect;
    if (isJavaEval) {
        final PredicateConstraint predicateConstraint = new PredicateConstraint(null, previousDeclarations, localDeclarations);
        final PredicateBuilder builder = context.getDialect().getPredicateBuilder();
        builder.build(context, usedIdentifiers, previousDeclarations, localDeclarations, predicateConstraint, predicateDescr, analysis);
        return predicateConstraint;
    }
    MVELCompilationUnit compilationUnit = getConstraintBuilder(context).buildCompilationUnit(context, previousDeclarations, localDeclarations, predicateDescr, analysis);
    String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] mvelDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration d : previousDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (Declaration d : localDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (String global : requiredGlobals) {
        mvelDeclarations[i++] = context.getDeclarationResolver().getDeclaration(global);
    }
    boolean isDynamic = !pattern.getObjectType().getClassType().isArray() && !context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType().getClassType()).isTypesafe();
    return getConstraintBuilder(context).buildMvelConstraint(context.getPkg().getName(), expr, mvelDeclarations, getOperators(usedIdentifiers.getOperators()), compilationUnit, isDynamic);
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) 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) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) PredicateConstraint(org.drools.core.rule.PredicateConstraint)

Aggregations

Declaration (org.drools.core.rule.Declaration)115 Pattern (org.drools.core.rule.Pattern)42 ClassObjectType (org.drools.core.base.ClassObjectType)29 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)27 TypeDeclaration (org.drools.core.rule.TypeDeclaration)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)17 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)17 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)16 Test (org.junit.Test)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)14 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)12 WorkingMemory (org.drools.core.WorkingMemory)11 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)11 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 Consequence (org.drools.core.spi.Consequence)10 Constraint (org.drools.core.spi.Constraint)10 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9