Search in sources :

Example 1 with BehaviorDescr

use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.

the class PatternBuilder method processBehaviors.

private void processBehaviors(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
    for (BehaviorDescr behaviorDescr : patternDescr.getBehaviors()) {
        if (pattern.getObjectType().isEvent()) {
            Behavior window = createWindow(behaviorDescr);
            if (window != null) {
                pattern.addBehavior(window);
                context.setNeedStreamMode();
            } else {
                registerDescrBuildError(context, patternDescr, "Unknown window type '" + behaviorDescr.getSubType() + "'");
            }
        } else {
            // Some behaviors can only be assigned to patterns declared as events
            registerDescrBuildError(context, patternDescr, "A Sliding Window can only be assigned to types declared with @role( event ). The type '" + pattern.getObjectType() + "' in '" + context.getRule().getName() + "' is not declared as an Event.");
        }
    }
}
Also used : BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) Behavior(org.drools.core.rule.Behavior)

Example 2 with BehaviorDescr

use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testTimeWindowBehavior.

@Test
public void testTimeWindowBehavior() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
    typeDeclDescr.addAnnotation("role", "event");
    packageDescr.addTypeDeclaration(typeDeclDescr);
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
    final BehaviorDescr windowDescr = new BehaviorDescr("window");
    windowDescr.setSubType("time");
    windowDescr.setParameters(Collections.singletonList("60000"));
    patternDescr.addBehavior(windowDescr);
    lhs.addDescr(patternDescr);
    ruleDescr.setConsequence("System.out.println( $tick );");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
    final RuleImpl rule = pkg.getRule("rule-1");
    assertNotNull(rule);
    final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
    assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
    final Behavior window = pattern.getBehaviors().get(0);
    assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
    assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.mvel.compiler.StockTick) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 3 with BehaviorDescr

use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.

the class RuleParserTest method testSlidingWindow.

@Test
public void testSlidingWindow() throws Exception {
    final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("symbol==\"ACME\"", fcd.getExpression());
    List<BehaviorDescr> behaviors = pattern.getBehaviors();
    assertNotNull(behaviors);
    assertEquals(1, behaviors.size());
    BehaviorDescr descr = behaviors.get(0);
    assertEquals("window", descr.getType());
    assertEquals("length", descr.getSubType());
    assertEquals("10", descr.getParameters().get(0));
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 4 with BehaviorDescr

use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.

the class RuleParserTest method testWindowDeclaration.

@Test
public void testWindowDeclaration() throws Exception {
    final String text = "package org.drools\n" + "declare window Ticks\n" + "    @doc(\"last 10 stock ticks\")\n" + "    $s : StockTick( source == \"NYSE\" )\n" + "        over window:length( 10, $s.symbol )\n" + "        from entry-point stStream\n" + "end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertEquals("org.drools", pkg.getName());
    assertEquals(1, pkg.getWindowDeclarations().size());
    WindowDeclarationDescr wdd = pkg.getWindowDeclarations().iterator().next();
    assertEquals("Ticks", wdd.getName());
    assertEquals(1, wdd.getAnnotations().size());
    assertEquals("\"last 10 stock ticks\"", wdd.getAnnotation("doc").getValue());
    PatternDescr pd = wdd.getPattern();
    assertNotNull(pd);
    assertEquals("$s", pd.getIdentifier());
    assertEquals("StockTick", pd.getObjectType());
    assertEquals("stStream", pd.getSource().getText());
    assertEquals(1, pd.getBehaviors().size());
    BehaviorDescr bd = pd.getBehaviors().get(0);
    assertEquals("window", bd.getType());
    assertEquals("length", bd.getSubType());
    assertEquals(2, bd.getParameters().size());
    assertEquals("10", bd.getParameters().get(0));
    assertEquals("$s.symbol", bd.getParameters().get(1));
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) Test(org.junit.Test)

Example 5 with BehaviorDescr

use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.

the class ModelGenerator method addVariable.

private static void addVariable(BlockStmt ruleBlock, DeclarationSpec declaration, RuleContext context, boolean domainClass) {
    if (declaration.getDeclarationClass() == null) {
        context.addCompilationError(new UnknownDeclarationError(declaration.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(declaration);
    ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(declaration.getBindingId()), Modifier.finalModifier());
    MethodCallExpr declarationOfCall = createDslTopLevelMethod(DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(declaration.getBoxedType()));
    if (domainClass) {
        String domainClassSourceName = asJavaSourceName(declaration.getDeclarationClass());
        declarationOfCall.addArgument(DOMAIN_CLASSESS_METADATA_FILE_NAME + context.getPackageModel().getPackageUUID() + "." + domainClassSourceName + DOMAIN_CLASS_METADATA_INSTANCE);
    }
    declarationOfCall.addArgument(toStringLiteral(declaration.getVariableName().orElse(declaration.getBindingId())));
    declaration.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    declaration.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
        entryPointCall.addArgument(toStringLiteral(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : declaration.getBehaviors()) {
        MethodCallExpr windowCall = createDslTopLevelMethod(WINDOW_CALL);
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, var_assign)) {
        ruleBlock.addStatement(var_assign);
    }
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

BehaviorDescr (org.drools.drl.ast.descr.BehaviorDescr)5 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 PatternDescr (org.drools.drl.ast.descr.PatternDescr)3 Test (org.junit.Test)3 Behavior (org.drools.core.rule.Behavior)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 Type (com.github.javaparser.ast.type.Type)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Pattern (org.drools.core.rule.Pattern)1 AndDescr (org.drools.drl.ast.descr.AndDescr)1 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)1