Search in sources :

Example 1 with AccumulateImportDescr

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

the class RuleParserTest method testImportAccumulate.

@Test
public void testImportAccumulate() throws Exception {
    String drl = "package org.drools.mvel.compiler\n" + "import acc foo.Bar baz\n" + "import accumulate foo.Bar2 baz2\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     acc( Cheese( $price : price ),\n" + "          $v1 : baz( $price ), \n" + "          $v2 : baz2( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
    assertEquals(2, pkg.getAccumulateImports().size());
    AccumulateImportDescr imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(0);
    assertEquals("foo.Bar", imp.getTarget());
    assertEquals("baz", imp.getFunctionName());
    imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(1);
    assertEquals("foo.Bar2", imp.getTarget());
    assertEquals("baz2", imp.getFunctionName());
    assertEquals(1, pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Object", out.getObjectType());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(2, functions.size());
    assertEquals("baz", functions.get(0).getFunction());
    assertEquals("$v1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    assertEquals("baz2", functions.get(1).getFunction());
    assertEquals("$v2", functions.get(1).getBind());
    assertEquals("$price", functions.get(1).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 2 with AccumulateImportDescr

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

the class KnowledgeBuilderImpl method processAccumulateFunctions.

protected void processAccumulateFunctions(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (final AccumulateImportDescr aid : packageDescr.getAccumulateImports()) {
        AccumulateFunction af = loadAccumulateFunction(pkgRegistry, aid.getFunctionName(), aid.getTarget());
        pkgRegistry.getPackage().addAccumulateFunction(aid.getFunctionName(), af);
    }
}
Also used : AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 3 with AccumulateImportDescr

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

AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)3 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 HashSet (java.util.HashSet)1 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)1 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)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 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)1 WindowDeclarationDescr (org.drools.drl.ast.descr.WindowDeclarationDescr)1 Test (org.junit.Test)1 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)1