Search in sources :

Example 1 with Constraint

use of org.drools.model.Constraint in project drools by kiegroup.

the class KiePackagesBuilder method addQueryPattern.

private void addQueryPattern(Query query, QueryImpl queryImpl, RuleContext ctx) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset is 0 by default
    0, ClassObjectType.DroolsQuery_ObjectType, null);
    InternalReadAccessor extractor = new LambdaReadAccessor(DroolsQuery.class, q -> ((DroolsQuery) q).getName());
    QueryNameConstraint constraint = new QueryNameConstraint(extractor, query.getName());
    pattern.addConstraint(constraint);
    queryImpl.getLhs().addChild(pattern);
    Variable<?>[] args = query.getArguments();
    Declaration[] declarations = new Declaration[args.length];
    for (int i = 0; i < args.length; i++) {
        int index = i;
        LambdaReadAccessor accessor = new LambdaReadAccessor(index, args[index].getType(), obj -> ((DroolsQuery) obj).getElements()[index]);
        declarations[i] = new Declaration(args[i].getName(), accessor, pattern, true);
        pattern.addDeclaration(declarations[i]);
        ctx.addQueryDeclaration(args[i], declarations[i]);
    }
    queryImpl.setParameters(declarations);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 2 with Constraint

use of org.drools.model.Constraint in project drools by kiegroup.

the class KiePackagesBuilder method addConstraintsToPattern.

private void addConstraintsToPattern(RuleContext ctx, Pattern pattern, org.drools.model.Pattern modelPattern, Constraint constraint) {
    if (constraint.getType() == Constraint.Type.SINGLE) {
        SingleConstraint singleConstraint = (SingleConstraint) constraint;
        boolean isEqual = singleConstraint.getIndex() != null && singleConstraint.getIndex().getConstraintType() == Index.ConstraintType.EQUAL;
        if (singleConstraint.getVariables().length > 0) {
            Variable[] vars = singleConstraint.getVariables();
            Declaration[] declarations = new Declaration[vars.length];
            Declaration unificationDeclaration = null;
            for (int i = 0; i < vars.length; i++) {
                declarations[i] = ctx.getDeclaration(vars[i]);
                if (isEqual && declarations[i].getPattern().getObjectType().equals(ClassObjectType.DroolsQuery_ObjectType)) {
                    unificationDeclaration = declarations[i];
                } else if (pattern.getSource() instanceof MultiAccumulate) {
                    Declaration accDeclaration = pattern.getDeclarations().get(declarations[i].getBindingName());
                    if (accDeclaration != null) {
                        declarations[i].setReadAccessor(accDeclaration.getExtractor());
                    }
                }
            }
            ConstraintEvaluator constraintEvaluator = singleConstraint.isTemporal() ? new TemporalConstraintEvaluator(declarations, pattern, singleConstraint) : new ConstraintEvaluator(declarations, pattern, singleConstraint);
            org.drools.core.spi.Constraint droolsConstraint = unificationDeclaration != null ? new UnificationConstraint(unificationDeclaration, constraintEvaluator) : new LambdaConstraint(constraintEvaluator);
            pattern.addConstraint(droolsConstraint);
        }
    } else if (modelPattern.getConstraint().getType() == Constraint.Type.AND) {
        for (Constraint child : constraint.getChildren()) {
            addConstraintsToPattern(ctx, pattern, modelPattern, child);
        }
    }
}
Also used : PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) Constraint(org.drools.model.Constraint) MultiAccumulate(org.drools.core.rule.MultiAccumulate) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) SingleConstraint(org.drools.model.SingleConstraint) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

Declaration (org.drools.core.rule.Declaration)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 WindowDeclaration (org.drools.core.rule.WindowDeclaration)2 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)2 Constraint (org.drools.model.Constraint)2 EntryPoint (org.drools.model.EntryPoint)2 FlowDSL.entryPoint (org.drools.model.FlowDSL.entryPoint)2 PrototypeVariable (org.drools.model.PrototypeVariable)2 SingleConstraint (org.drools.model.SingleConstraint)2 Variable (org.drools.model.Variable)2 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)2 UnificationConstraint (org.drools.modelcompiler.constraints.UnificationConstraint)2 TypeDeclarationUtil.createTypeDeclaration (org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration)2 DroolsQuery (org.drools.core.base.DroolsQuery)1 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 Pattern (org.drools.core.rule.Pattern)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 AccumulatePattern (org.drools.model.AccumulatePattern)1 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)1 ConstraintEvaluator (org.drools.modelcompiler.constraints.ConstraintEvaluator)1