Search in sources :

Example 21 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr 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 22 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class MVELDebugTest method testDebug.

@Test
public void testDebug() throws Exception {
    String rule = "package com.sample; dialect \"mvel\" rule myRule when then\n System.out.println( \"test\" ); end";
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr packageDescr = parser.parse(null, rule);
    RuleDescr ruleDescr = packageDescr.getRules().get(0);
    builder = new KnowledgeBuilderImpl();
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage("com.sample");
    MVELConsequence consequence = (MVELConsequence) pkg.getRule("myRule").getConsequence();
    String sourceName = ((CompiledExpression) consequence.getCompExpr()).getSourceName();
    System.out.println(sourceName);
    String ruleName = ruleDescr.getNamespace() + "." + ruleDescr.getClassName();
    System.out.println(ruleName);
    assertEquals(sourceName, ruleName);
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompiledExpression(org.mvel2.compiler.CompiledExpression) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 23 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testSoundsLike.

@Test
public void testSoundsLike() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "soundslike_operator.drl");
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    PatternDescr pat = (PatternDescr) rule.getLhs().getDescrs().get(0);
    pat.getConstraint();
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 24 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testOrCE.

@Test
public void testOrCE() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals("$p", person.getIdentifier());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Cheese", cheese1.getObjectType());
    assertEquals("$c", cheese1.getIdentifier());
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Cheese", cheese2.getObjectType());
    assertNull(cheese2.getIdentifier());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 25 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr 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.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)243 Test (org.junit.Test)194 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)46 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 InputStreamReader (java.io.InputStreamReader)27 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)24 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14