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);
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class MVELAccumulateBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(new PackageDescr("pkg1"));
InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
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 AccumulateDescr accDescr = new AccumulateDescr();
final PatternDescr inputPattern = new PatternDescr("org.drools.compiler.Cheese", "$cheese");
accDescr.setInputPattern(inputPattern);
accDescr.setInitCode("total = 0;");
accDescr.setActionCode("total += $cheese.price;");
accDescr.setReverseCode("total -= $cheese.price;");
accDescr.setResultCode("new Integer(total)");
final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
final Accumulate acc = (Accumulate) builder.build(context, accDescr);
((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
MockLeftTupleSink sink = new MockLeftTupleSink();
final Cheese cheddar1 = new Cheese("cheddar", 10);
final Cheese cheddar2 = new Cheese("cheddar", 8);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
Object wmContext = acc.createWorkingMemoryContext();
Object accContext = acc.createContext();
acc.init(wmContext, accContext, tuple, ksession);
acc.accumulate(wmContext, accContext, tuple, f1, ksession);
acc.accumulate(wmContext, accContext, tuple, f2, ksession);
assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
acc.reverse(wmContext, accContext, tuple, f1, ksession);
assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class MVELConsequenceBuilderTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
builder = new MVELConsequenceBuilder();
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.compiler.test");
pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = new RuleImpl(ruleDescr.getName());
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));
PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
pkgBuilder.compileAll();
pkgBuilder.reloadAll();
if (pkgBuilder.hasErrors()) {
fail(pkgBuilder.getErrors().toString());
}
}
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.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 KnowledgeBuilderImpl method buildRuleBuilderContext.
private RuleBuildContext buildRuleBuilderContext(PackageRegistry pkgRegistry, RuleDescr ruleDescr) {
if (ruleDescr.getResource() == null) {
ruleDescr.setResource(resource);
}
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleBuildContext context = new RuleBuildContext(this, ruleDescr, ctr, pkgRegistry.getPackage(), ctr.getDialect(pkgRegistry.getDialect()));
RuleBuilder.preProcess(context);
return context;
}
Aggregations