Search in sources :

Example 51 with TypeDeclaration

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

the class PatternBuilder method getTypeDeclaration.

private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
    String packageName = ClassUtils.getPackage(userProvidedClass);
    KnowledgeBuilderImpl kbuilder = context.getKnowledgeBuilder();
    PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
    TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
    if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
        // check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
        InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
        typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
    }
    if (typeDeclaration == null) {
        typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
    }
    return typeDeclaration;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 52 with TypeDeclaration

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

the class PatternBuilder method buildCcdDescr.

protected Constraint buildCcdDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr d, ConstraintConnectiveDescr ccd, MVELDumper.MVELDumperContext mvelCtx) {
    d.copyLocation(patternDescr);
    mvelCtx.clear();
    String expr = context.getCompilerFactory().getExpressionProcessor().dump(d, ccd, mvelCtx);
    Map<String, OperatorDescr> aliases = mvelCtx.getAliases();
    // create bindings
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    for (BindingDescr bind : mvelCtx.getBindings()) {
        buildRuleBindings(context, patternDescr, pattern, bind, typeDeclaration);
    }
    if (expr.length() == 0) {
        return null;
    }
    // check if it is an atomic expression
    Constraint constraint = processAtomicExpression(context, pattern, d, expr, aliases);
    // otherwise check if it is a simple expression
    return constraint != null ? constraint : buildExpression(context, pattern, d, expr, aliases);
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) 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) OperatorDescr(org.drools.compiler.lang.descr.OperatorDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 53 with TypeDeclaration

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

the class PatternBuilder method processConstraintsAndBinds.

/**
 * Process all constraints and bindings on this pattern
 */
protected void processConstraintsAndBinds(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern) {
    MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext().setRuleContext(context);
    for (BaseDescr b : patternDescr.getDescrs()) {
        String expression;
        boolean isPositional = false;
        if (b instanceof BindingDescr) {
            BindingDescr bind = (BindingDescr) b;
            expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
        } else if (b instanceof ExprConstraintDescr) {
            ExprConstraintDescr descr = (ExprConstraintDescr) b;
            expression = descr.getExpression();
            isPositional = descr.getType() == ExprConstraintDescr.Type.POSITIONAL;
        } else {
            expression = b.getText();
        }
        ConstraintConnectiveDescr result = parseExpression(context, patternDescr, b, expression);
        if (result == null) {
            return;
        }
        isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
        if (isPositional) {
            processPositional(context, patternDescr, pattern, (ExprConstraintDescr) b);
        } else {
            // need to build the actual constraint
            List<Constraint> constraints = build(context, patternDescr, pattern, result, mvelCtx);
            pattern.addConstraints(constraints);
        }
    }
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
        for (String field : context.getRuleDescr().lookAheadFieldsOfIdentifier(patternDescr)) {
            addFieldToPatternWatchlist(pattern, typeDeclaration, field);
        }
    }
    combineConstraints(context, pattern, mvelCtx);
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) 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) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) MVELDumper(org.drools.compiler.lang.MVELDumper) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 54 with TypeDeclaration

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

the class PatternBuilder method processPositional.

protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
    if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
        Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
        TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
        if (tDecl == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
            return;
        }
        ClassDefinition clsDef = tDecl.getTypeClassDef();
        if (clsDef == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        FieldDefinition field = clsDef.getField(descr.getPosition());
        if (field == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        String expr = descr.getExpression();
        boolean isSimpleIdentifier = isIdentifier(expr);
        if (isSimpleIdentifier) {
            // create a binding
            BindingDescr binder = new BindingDescr();
            binder.setUnification(true);
            binder.setExpression(field.getName());
            binder.setVariable(descr.getExpression());
            buildRuleBindings(context, patternDescr, pattern, binder);
        } else {
            // create a constraint
            build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
        }
    }
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 55 with TypeDeclaration

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

the class PatternBuilder method getSettableProperties.

protected List<String> getSettableProperties(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
    ObjectType patternType = pattern.getObjectType();
    if (!(patternType instanceof ClassObjectType)) {
        return null;
    }
    Class<?> patternClass = patternType.getClassType();
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (!typeDeclaration.isPropertyReactive()) {
        registerDescrBuildError(context, patternDescr, "Wrong usage of @" + Watch.class.getSimpleName() + " annotation on class " + patternClass.getName() + " that is not annotated as @PropertyReactive");
    }
    typeDeclaration.setTypeClass(patternClass);
    return typeDeclaration.getAccessibleProperties();
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Watch(org.kie.api.definition.rule.Watch) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

TypeDeclaration (org.drools.core.rule.TypeDeclaration)72 Test (org.junit.Test)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)10 ClassDefinition (org.drools.core.factmodel.ClassDefinition)9 ClassObjectType (org.drools.core.base.ClassObjectType)7 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 ObjectType (org.drools.core.spi.ObjectType)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3