Search in sources :

Example 1 with WindowDeclarationDescr

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

the class KnowledgeBuilderImpl method processWindowDeclarations.

protected void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
        WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
        // TODO: process annotations
        // process pattern
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
        RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
        dummy.setResource(packageDescr.getResource());
        dummy.addAttribute(new AttributeDescr("dialect", "java"));
        RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
        if (builder != null) {
            final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
            if (pattern.getXpathConstraint() != null) {
                context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
            }
            window.setPattern(pattern);
        } else {
            throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
        }
        if (!context.getErrors().isEmpty()) {
            for (DroolsError error : context.getErrors()) {
                addBuilderResult(error);
            }
        } else {
            pkgRegistry.getPackage().addWindowDeclaration(window);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) DroolsError(org.drools.drl.parser.DroolsError) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) WindowDeclaration(org.drools.core.rule.WindowDeclaration) RuleDescr(org.drools.drl.ast.descr.RuleDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with WindowDeclarationDescr

use of org.drools.drl.ast.descr.WindowDeclarationDescr 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 3 with WindowDeclarationDescr

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

the class CompositePackageDescr method internalAdd.

private void internalAdd(Resource resource, PackageDescr packageDescr) {
    for (ImportDescr descr : packageDescr.getImports()) {
        addImport(descr);
        descr.setResource(resource);
    }
    for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
        addFunctionImport(descr);
        descr.setResource(resource);
    }
    for (AccumulateImportDescr descr : packageDescr.getAccumulateImports()) {
        addAccumulateImport(descr);
        descr.setResource(resource);
    }
    for (AttributeDescr descr : packageDescr.getAttributes()) {
        addAttribute(descr);
        descr.setResource(resource);
    }
    for (GlobalDescr descr : packageDescr.getGlobals()) {
        addGlobal(descr);
        descr.setResource(resource);
    }
    for (FunctionDescr descr : packageDescr.getFunctions()) {
        addFunction(descr);
        descr.setResource(resource);
    }
    for (RuleDescr descr : packageDescr.getRules()) {
        addRule(descr);
        descr.setResource(resource);
    }
    // Avoid adding the same type declaration twice, see
    // TypeDeclarationTest.testDuplicatedTypeDeclarationInDifferentResources
    // IncrementalCompilationTest.testIncrementalCompilationWithAmbiguousRedeclares
    // RHDM-1738
    Set<TypeDeclarationDescr> typeDeclarationDescrs = new HashSet<>(getTypeDeclarations());
    for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
        if (!typeDeclarationDescrs.contains(descr)) {
            addTypeDeclaration(descr);
            descr.setResource(resource);
        }
    }
    for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
        addEnumDeclaration(enumDescr);
        enumDescr.setResource(resource);
    }
    for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
        addEntryPointDeclaration(descr);
        descr.setResource(resource);
    }
    for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
        addWindowDeclaration(descr);
        descr.setResource(resource);
    }
    packageDescr.getPreferredPkgUUID().ifPresent(pkgUUID -> {
        if (getPreferredPkgUUID().isPresent() && !pkgUUID.equals(getPreferredPkgUUID().get())) {
            throw new RuntimeException(String.format("Trying to overwrite preferredPkgUUID %s with a different value %s", getPreferredPkgUUID().get(), pkgUUID));
        }
        setPreferredPkgUUID(pkgUUID);
    });
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) EntryPointDeclarationDescr(org.drools.drl.ast.descr.EntryPointDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) HashSet(java.util.HashSet)

Aggregations

WindowDeclarationDescr (org.drools.drl.ast.descr.WindowDeclarationDescr)3 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 HashSet (java.util.HashSet)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)1 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 Pattern (org.drools.core.rule.Pattern)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)1 BehaviorDescr (org.drools.drl.ast.descr.BehaviorDescr)1 EntryPointDeclarationDescr (org.drools.drl.ast.descr.EntryPointDeclarationDescr)1 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)1 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)1 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)1 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)1 ImportDescr (org.drools.drl.ast.descr.ImportDescr)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1