use of org.drools.compiler.rule.builder.RuleBuildContext 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 = CoreComponentFactory.get().createKnowledgePackage("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.rule.builder.RuleBuildContext in project drools by kiegroup.
the class MVELEvalBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
final Declaration declaration = new Declaration("a", extractor, pattern);
final Map map = new HashMap();
map.put("a", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final EvalDescr evalDescr = new EvalDescr();
evalDescr.setContent("a == 10");
final MVELEvalBuilder builder = new MVELEvalBuilder();
final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
org.drools.core.reteoo.MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
sink.setLeftTupleSource(source);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
f0.removeLeftTuple(tuple);
Object evalContext = eval.createContext();
assertTrue(eval.isAllowed(tuple, ksession, evalContext));
cheddar.setPrice(9);
ksession.update(f0, cheddar);
assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
ruleDescr.setConsequence("");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType personObjeectType = new ClassObjectType(Person.class);
final Pattern pattern = new Pattern(0, personObjeectType);
final PatternExtractor extractor = new PatternExtractor(personObjeectType);
final Declaration declaration = new Declaration("p", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("p", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
kBase = KnowledgeBaseFactory.newKnowledgeBase();
buildContext = new BuildContext(kBase, Collections.emptyList());
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.compiler.rule.builder.RuleBuildContext 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.mvel.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() );
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class RuleBuilderTest method testBuildAttributes.
@Test
public void testBuildAttributes() 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.addAttribute(new AttributeDescr("no-loop", "true"));
ruleDescr.addAttribute(new AttributeDescr("auto-focus", "false"));
ruleDescr.addAttribute(new AttributeDescr("agenda-group", "my agenda"));
ruleDescr.addAttribute(new AttributeDescr("activation-group", "my activation"));
ruleDescr.addAttribute(new AttributeDescr("lock-on-active", ""));
ruleDescr.addAttribute(new AttributeDescr("enabled", "false"));
ruleDescr.addAttribute(new AttributeDescr("duration", "60"));
ruleDescr.addAttribute(new AttributeDescr("calendars", "\"cal1\""));
ruleDescr.addAttribute(new AttributeDescr("date-effective", "10-Jul-1974"));
ruleDescr.addAttribute(new AttributeDescr("date-expires", "10-Jul-2040"));
// creates expected results
final Calendar effective = Calendar.getInstance();
effective.setTime(DateUtils.parseDate("10-Jul-1974"));
final Calendar expires = Calendar.getInstance();
expires.setTime(DateUtils.parseDate("10-Jul-2040"));
// 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.buildAttributes(context);
// check expectations
verify(rule).setNoLoop(true);
verify(rule).setAutoFocus(false);
verify(rule).setAgendaGroup("my agenda");
verify(rule).setActivationGroup("my activation");
verify(rule).setLockOnActive(true);
verify(rule).setEnabled(EnabledBoolean.ENABLED_FALSE);
verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("60"), 0));
verify(rule).setCalendars(new String[] { "cal1" });
verify(rule).setDateEffective(effective);
verify(rule).setDateExpires(expires);
}
Aggregations