Search in sources :

Example 21 with DialectCompiletimeRegistry

use of org.drools.compiler.compiler.DialectCompiletimeRegistry in project drools by kiegroup.

the class MVELConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    builder = new MVELConsequenceBuilder();
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.compiler.test");
    pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = new RuleImpl(ruleDescr.getName());
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));
    PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    pkgBuilder.compileAll();
    pkgBuilder.reloadAll();
    if (pkgBuilder.hasErrors()) {
        fail(pkgBuilder.getErrors().toString());
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 22 with DialectCompiletimeRegistry

use of org.drools.compiler.compiler.DialectCompiletimeRegistry in project drools by kiegroup.

the class JavaAccumulateBuilderTest method testBuildRuleBuildContextBaseDescr.

@Test
public void testBuildRuleBuildContextBaseDescr() {
    // $total : Integer() from accumulate( Cheese( $price : price ) init( int x = 0; ) action( x += $price ) result( new Integer( x ) ) )
    AccumulateDescr accumDescr = new AccumulateDescr();
    BindingDescr price = new BindingDescr("$price", "price");
    PatternDescr cheeseDescr = new PatternDescr("org.drools.compiler.Cheese");
    cheeseDescr.addConstraint(price);
    accumDescr.setInputPattern(cheeseDescr);
    accumDescr.setInitCode("int x = 0; int y = 0;");
    accumDescr.setActionCode("x += $price;");
    accumDescr.setResultCode("new Integer( x )");
    // org.drools.core.rule.Package pkg = new org.kie.rule.Package( "org.kie" );
    final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("org.drools"));
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    PackageRegistry pkgReg = pkgBuilder.getPackageRegistry("org.drools");
    InternalKnowledgePackage pkg = pkgReg.getPackage();
    DialectCompiletimeRegistry dialectRegistry = pkgReg.getDialectCompiletimeRegistry();
    Dialect dialect = dialectRegistry.getDialect("java");
    RuleDescr ruleDescr = new RuleDescr("test rule");
    RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
    Accumulate accumulate = (Accumulate) builder.build(context, accumDescr);
    String generatedCode = (String) context.getMethods().get(0);
    assertTrue(generatedCode.contains("private int x;"));
    assertTrue(generatedCode.contains("private int y;"));
    assertTrue(generatedCode.contains("x = 0;y = 0;"));
// System.out.println( context.getInvokers() );
// System.out.println( context.getMethods() );
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Accumulate(org.drools.core.rule.Accumulate) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Dialect(org.drools.compiler.compiler.Dialect) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 23 with DialectCompiletimeRegistry

use of org.drools.compiler.compiler.DialectCompiletimeRegistry in project drools by kiegroup.

the class KnowledgeBuilderImpl method buildRuleBuilderContext.

private RuleBuildContext buildRuleBuilderContext(PackageRegistry pkgRegistry, RuleDescr ruleDescr) {
    if (ruleDescr.getResource() == null) {
        ruleDescr.setResource(resource);
    }
    DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
    RuleBuildContext context = new RuleBuildContext(this, ruleDescr, ctr, pkgRegistry.getPackage(), ctr.getDialect(pkgRegistry.getDialect()));
    RuleBuilder.preProcess(context);
    return context;
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry)

Example 24 with DialectCompiletimeRegistry

use of org.drools.compiler.compiler.DialectCompiletimeRegistry 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 25 with DialectCompiletimeRegistry

use of org.drools.compiler.compiler.DialectCompiletimeRegistry in project drools by kiegroup.

the class DrlxCompilerTest method testCompileUnit.

@Test
@Ignore("Rule Unit compiler is not available in Drools 8 yet")
public void testCompileUnit() throws IOException {
    InputStream p = getClass().getClassLoader().getResourceAsStream("drlx1/Example.drlx");
    InputStreamResource r = new InputStreamResource(p);
    DrlxCompiler drlxCompiler = new DrlxCompiler();
    PackageDescr packageDescr = drlxCompiler.toPackageDescr(r);
    assertTrue("Should not have compiler errors", drlxCompiler.getResults().isEmpty());
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    PackageRegistry registry = kbuilder.getOrCreatePackageRegistry(packageDescr);
    kbuilder.getAndRegisterTypeDeclaration(org.drools.modelcompiler.drlx.Example.class, "org.drools.modelcompiler.drlx");
    InternalKnowledgePackage knowledgePackage = registry.getPackage();
    PackageModel packageModel = new PackageModel("com.example:dummy:1.0.0", packageDescr.getName(), new KnowledgeBuilderConfigurationImpl(), new DialectCompiletimeRegistry(), new DRLIdGenerator());
    ModelGenerator.generateModel(kbuilder, knowledgePackage, packageDescr, packageModel);
    assertEquals(1, packageModel.getRuleUnits().size());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) InputStream(java.io.InputStream) DRLIdGenerator(org.drools.modelcompiler.builder.generator.DRLIdGenerator) PackageModel(org.drools.modelcompiler.builder.PackageModel) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InputStreamResource(org.drools.core.io.impl.InputStreamResource) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)34 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)30 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)29 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)22 Test (org.junit.Test)22 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)17 Pattern (org.drools.core.rule.Pattern)15 ClassObjectType (org.drools.core.base.ClassObjectType)14 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)13 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)13 Declaration (org.drools.core.rule.Declaration)12 RuleDescr (org.drools.drl.ast.descr.RuleDescr)12 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10 ImportDeclaration (org.drools.core.rule.ImportDeclaration)10 HashMap (java.util.HashMap)8 Dialect (org.drools.compiler.compiler.Dialect)8 InternalFactHandle (org.drools.core.common.InternalFactHandle)8 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)8 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8