Search in sources :

Example 16 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleBuilderTest method testBuildMetaAttributes.

@Test
public void testBuildMetaAttributes() throws Exception {
    // creates mock objects
    final RuleBuildContext context = mock(RuleBuildContext.class);
    final RuleImpl rule = mock(RuleImpl.class);
    // creates input object
    final RuleDescr ruleDescr = new RuleDescr("my rule");
    ruleDescr.addAnnotation("ruleId", "123");
    ruleDescr.addAnnotation("author", "Bob Doe");
    ruleDescr.addAnnotation("text", "\"It's a quoted\\\" string\"");
    // creates expected results
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    when(context.getKnowledgeBuilder()).thenReturn(new KnowledgeBuilderImpl());
    // calling the build method
    RuleBuilder.buildMetaAttributes(context);
    // check expectations
    verify(rule).addMetaAttribute("ruleId", 123);
    verify(rule).addMetaAttribute("author", "Bob Doe");
    verify(rule).addMetaAttribute("text", "It's a quoted\" string");
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 17 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleBuilderTest method testBuildBigDecimalLiteralConstraint.

@Test
public void testBuildBigDecimalLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigDecimal", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this == 10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MvelConstraint) {
        final MvelConstraint fc = (MvelConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigDecimal. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigDecimal);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 18 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleBuilderTest method testBuild.

@Test
public void testBuild() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
    final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    pkg.addGlobal("results", List.class);
    final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
    final String ruleClassName = "RuleClassName.java";
    ruleDescr.setClassName(ruleClassName);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
    assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
    // second GE should be a not
    final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
    assertTrue(not.isNot());
    // not has no outer declarations
    assertTrue(not.getOuterDeclarations().isEmpty());
    assertEquals(1, not.getInnerDeclarations().size());
    assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
    // second not
    final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
    assertTrue(not2.isNot());
    // not has no outer declarations
    assertTrue(not2.getOuterDeclarations().isEmpty());
    assertEquals(1, not2.getInnerDeclarations().size());
    assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 19 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testMVELDebugSymbols.

@Test
public void testMVELDebugSymbols() throws DroolsParserException {
    MVELDebugHandler.setDebugMode(true);
    try {
        final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
        final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("mvel_rule.drl")));
        // just checking there is no parsing errors
        assertFalse(parser.getErrors().toString(), parser.hasErrors());
        InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
        final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
        final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
        DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
        Dialect dialect = dialectRegistry.getDialect("mvel");
        RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
        RuleBuilder.build(context);
        assertTrue(context.getErrors().toString(), context.getErrors().isEmpty());
        final RuleImpl rule = context.getRule();
        MVELConsequence mvelCons = (MVELConsequence) rule.getConsequence();
        mvelCons.compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
        String s = DebugTools.decompile(mvelCons.getCompExpr());
        int fromIndex = 0;
        int count = 0;
        while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
            count++;
        }
        assertEquals(4, count);
    } finally {
        MVELDebugHandler.setDebugMode(false);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Dialect(org.drools.compiler.compiler.Dialect) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 20 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class PhreakJoinNodeTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11