@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 RuleBuilder builder = new RuleBuilder(); final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg); final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration(); DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry(); Dialect dialect = dialectRegistry.getDialect("mvel"); RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect); builder.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); } }
@Test public void testImperativeCodeError() throws Exception { InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1"); final RuleDescr ruleDescr = new RuleDescr("rule 1"); ruleDescr.setConsequence("if (cheese.price == 10) { cheese.price = 5; }"); Properties properties = new Properties(); properties.setProperty("drools.dialect.default", "mvel"); KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl(properties); KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, cfg1); final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration(); PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName()); DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry(); MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect(pkgRegistry.getDialect()); final InstrumentedBuildContent context = new InstrumentedBuildContent(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect); final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver(); final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class); final Pattern pattern = new Pattern(0, cheeseObjeectType); final PatternExtractor extractor = new PatternExtractor(cheeseObjeectType); final Declaration declaration = new Declaration("cheese", extractor, pattern); final Map<String, Declaration> map = new HashMap<String, Declaration>(); map.put("cheese", declaration); declarationResolver.setDeclarations(map); context.setDeclarationResolver(declarationResolver); final MVELConsequenceBuilder builder = new MVELConsequenceBuilder(); builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME); InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newStatefulKnowledgeSession(); final Cheese cheddar = new Cheese("cheddar", 10); final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar); final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true); final AgendaItem item = new AgendaItemImpl(0, tuple, 10, null, null, null); final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession); kbHelper.setActivation(item); try { ((MVELConsequence) context.getRule().getConsequence()) .compile( (MVELDialectRuntimeData) pkgBuilder .getPackageRegistry(pkg.getName()) .getDialectRuntimeRegistry() .getDialectData("mvel")); context.getRule().getConsequence().evaluate(kbHelper, ksession); fail("should throw an exception, as 'if' is not allowed"); } catch (Exception e) { } assertEquals(10, cheddar.getPrice()); }