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);
}
}
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);
}
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();
}
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());
}
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));
}
Aggregations