Search in sources :

Example 1 with RuleConditionElement

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

the class KiePackagesBuilder method addPatternForVariable.

private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset will be set by ReteooBuilder
    0, getObjectType(patternVariable), patternVariable.getName(), true);
    if (patternVariable instanceof org.drools.model.Declaration) {
        org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
        if (decl.getSource() != null) {
            if (decl.getSource() instanceof EntryPoint) {
                pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
            } else if (decl.getSource() instanceof WindowReference) {
                WindowReference<?> window = (WindowReference) decl.getSource();
                if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
                    createWindowReference(ctx, window);
                }
                pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
            } else if (decl.getSource() instanceof From) {
                From<?> from = (From) decl.getSource();
                DataProvider provider = new LambdaDataProvider(ctx.getDeclaration(from.getVariable()), from.getProvider(), from.isReactive());
                org.drools.core.rule.From fromSource = new org.drools.core.rule.From(provider);
                fromSource.setResultPattern(pattern);
                pattern.setSource(fromSource);
            } else if (decl.getSource() instanceof UnitData) {
                UnitData unitData = (UnitData) decl.getSource();
                pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
            } else {
                throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
            }
        } else {
            Accumulate accSource = ctx.getAccumulateSource(patternVariable);
            if (accSource != null) {
                for (RuleConditionElement element : group.getChildren()) {
                    if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
                        if (accSource instanceof MultiAccumulate) {
                            ((Pattern) element).getConstraints().forEach(pattern::addConstraint);
                            ((Pattern) element).getDeclarations().values().forEach(d -> {
                                pattern.addDeclaration(d);
                                d.setPattern(pattern);
                            });
                        }
                        group.getChildren().remove(element);
                        break;
                    }
                }
                pattern.setSource(accSource);
            }
        }
        if (decl.getWindow() != null) {
            pattern.addBehavior(createWindow(decl.getWindow()));
        }
    }
    ctx.registerPattern(patternVariable, pattern);
    return pattern;
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) MultiAccumulate(org.drools.core.rule.MultiAccumulate) EntryPoint(org.drools.model.EntryPoint) From(org.drools.model.From) RuleConditionElement(org.drools.core.rule.RuleConditionElement) WindowReference(org.drools.model.WindowReference) UnitData(org.drools.model.UnitData) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) DataProvider(org.drools.core.spi.DataProvider) EntryPointId(org.drools.core.rule.EntryPointId) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) 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)

Example 2 with RuleConditionElement

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

the class JavaAccumulateBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final AccumulateDescr accumDescr = (AccumulateDescr) descr;
    if (!accumDescr.hasValidInput()) {
        return null;
    }
    // build source
    BaseDescr input = accumDescr.getInput();
    if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
        input = ((AndDescr) input).getDescrs().get(0);
    }
    final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
    final RuleConditionElement source = builder.build(context, input);
    if (source == null) {
        return null;
    }
    final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
    Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
    Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
    Accumulate accumulate;
    if (accumDescr.isExternalFunction()) {
        // if it uses 1+ external function, build methods for them
        accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    } else {
        // if it uses inline code, build the class for it
        accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    }
    return accumulate;
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) Declaration(org.drools.core.rule.Declaration) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate)

Example 3 with RuleConditionElement

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

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with RuleConditionElement

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

the class KiePackagesBuilder method buildAccumulate.

private RuleConditionElement buildAccumulate(RuleContext ctx, GroupElement group, AccumulatePattern accumulatePattern) {
    Pattern pattern = null;
    boolean isGroupBy = accumulatePattern instanceof GroupByPattern;
    if (accumulatePattern.getAccumulateFunctions() != null) {
        if (!isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1) {
            // non groupby with single accumulates can be optimized to directly return the result, rather than place in an array of 1
            pattern = ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult());
        } else if (accumulatePattern.getAccumulateFunctions().length > 0 && ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult()) != null) {
            // Illegal executable model. Cannot have groupby or multi accumulate mapped to a single result object.
            throw new RuntimeException("Only single accumulate functions, with no group by can optimize the result pattern to be the function return value");
        }
    }
    boolean existingPattern = pattern != null;
    if (!existingPattern) {
        ObjectType type = !isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1 ? new ClassObjectType(accumulatePattern.getAccumulateFunctions()[0].getResult().getType()) : // groupby or multi function accumulate
        JAVA_CLASS_ARRAY_TYPE;
        pattern = new Pattern(ctx.getNextPatternIndex(), type);
    }
    PatternImpl<?> sourcePattern = (PatternImpl<?>) accumulatePattern.getPattern();
    Set<String> usedVariableName = new LinkedHashSet<>();
    if (sourcePattern != null) {
        for (Variable v : sourcePattern.getInputVariables()) {
            usedVariableName.add(v.getName());
        }
    }
    RuleConditionElement source;
    if (accumulatePattern.isQuerySource()) {
        source = buildQueryPattern(ctx, ((QueryCallPattern) accumulatePattern.getCondition()));
    } else if (accumulatePattern.isCompositePatterns()) {
        CompositePatterns compositePatterns = (CompositePatterns) accumulatePattern.getCondition();
        GroupElement allSubConditions = new GroupElement(conditionToGroupElementType(compositePatterns.getType()));
        for (Condition c : compositePatterns.getSubConditions()) {
            recursivelyAddConditions(ctx, group, allSubConditions, c);
        }
        source = allSubConditions.getChildren().size() == 1 ? allSubConditions.getChildren().get(0) : allSubConditions;
    } else {
        source = buildPattern(ctx, group, accumulatePattern);
    }
    Collection<Binding> bindings = new ArrayList<>();
    if (sourcePattern != null) {
        bindings.addAll(sourcePattern.getBindings());
        bindings.add(new SelfPatternBiding<>(sourcePattern.getPatternVariable()));
    } else {
        // No pattern is associated. It likely uses inner bindings
        addInnerBindings(bindings, accumulatePattern.getAccumulateFunctions(), accumulatePattern.getCondition());
    }
    pattern.setSource(buildAccumulate(ctx, accumulatePattern, source, pattern, usedVariableName, bindings));
    return existingPattern ? null : pattern;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EvalCondition(org.drools.core.rule.EvalCondition) Condition(org.drools.model.Condition) Binding(org.drools.model.Binding) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) ClassObjectType(org.drools.core.base.ClassObjectType) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) RuleConditionElement(org.drools.core.rule.RuleConditionElement) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) CompositePatterns(org.drools.model.patterns.CompositePatterns) GroupByPattern(org.drools.model.GroupByPattern) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl)

Example 5 with RuleConditionElement

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

the class GroupElementBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
    final GroupElement ge = this.newGroupElementFor(descr);
    context.getDeclarationResolver().pushOnBuildStack(ge);
    if (prefixPattern != null) {
        ge.addChild(prefixPattern);
    }
    // iterate over child descriptors
    for (final BaseDescr child : cedescr.getDescrs()) {
        // gets child to build
        // child.setResource(..) does not seem to be necessary (since builderImpls have already set the resource for all children)
        // but leaving it in here to be save
        child.setResource(context.getRuleDescr().getResource());
        child.setNamespace(context.getRuleDescr().getNamespace());
        // gets corresponding builder
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(child.getClass());
        if (builder != null) {
            final RuleConditionElement element = builder.build(context, child);
            // builder will return null. Ex: ClassNotFound for the pattern type
            if (element != null) {
                ge.addChild(element);
            }
        } else {
            throw new RuntimeException("BUG: no builder found for descriptor class " + child.getClass());
        }
    }
    context.getDeclarationResolver().popBuildStack();
    return ge;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) BaseDescr(org.drools.drl.ast.descr.BaseDescr) RuleConditionElement(org.drools.core.rule.RuleConditionElement) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Aggregations

RuleConditionElement (org.drools.core.rule.RuleConditionElement)15 GroupElement (org.drools.core.rule.GroupElement)9 Declaration (org.drools.core.rule.Declaration)8 Accumulate (org.drools.core.rule.Accumulate)7 MultiAccumulate (org.drools.core.rule.MultiAccumulate)6 Pattern (org.drools.core.rule.Pattern)6 SingleAccumulate (org.drools.core.rule.SingleAccumulate)6 ArrayList (java.util.ArrayList)5 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 AccumulatePattern (org.drools.model.AccumulatePattern)4 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)4 Accumulator (org.drools.core.spi.Accumulator)3 Constraint (org.drools.core.spi.Constraint)3 GroupByPattern (org.drools.model.GroupByPattern)3 CompositePatterns (org.drools.model.patterns.CompositePatterns)3 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2