use of org.drools.core.definitions.InternalKnowledgePackage 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.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class ScenarioTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase);
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
use of org.drools.core.definitions.InternalKnowledgePackage 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.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method test1.
@Test
public void test1() {
KiePackage pkg1 = getKnowledgePackage1();
KiePackage pkg2 = getKnowledgePackage1();
KiePackage pkg3 = getKnowledgePackage2();
RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
// test return value
Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
Constraint rvc1 = p1.getConstraints().get(0);
Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
Constraint rvc2 = p2.getConstraints().get(0);
assertNotSame(rvc1, rvc2);
assertEquals(rvc1, rvc2);
Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
Constraint rvc3 = p3.getConstraints().get(0);
assertNotSame(rvc1, rvc3);
assertThat(rvc1, not(equalTo(rvc3)));
// test inline eval
PredicateConstraint pc1 = getPredicateConstraint(p1);
PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
PredicateConstraint pc2 = getPredicateConstraint(p2);
PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
assertNotSame(pe1, pe2);
assertEquals(pe1, pe2);
PredicateConstraint pc3 = getPredicateConstraint(p3);
PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
assertNotSame(pe1, pe3);
assertThat(pe1, not(equalTo(pe3)));
// test eval
EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
assertNotSame(ee1, ee2);
assertEquals(ee1, ee2);
EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
assertNotSame(ee1, ee3);
assertThat(ee1, not(equalTo(ee3)));
// test consequence
assertNotSame(rule1.getConsequence(), rule2.getConsequence());
assertEquals(rule1.getConsequence(), rule2.getConsequence());
assertNotSame(rule1.getConsequence(), rule3.getConsequence());
assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
// check LHS equals
assertNotSame(rule1.getLhs(), rule2.getLhs());
assertEquals(rule1.getLhs(), rule2.getLhs());
assertNotSame(rule1.getLhs(), rule3.getLhs());
assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class MVELAccumulator method init.
/* (non-Javadoc)
* @see org.kie.spi.Accumulator#init(java.lang.Object, org.kie.spi.Tuple, org.kie.rule.Declaration[], org.kie.WorkingMemory)
*/
public void init(Object workingMemoryContext, Object context, Tuple tuple, Declaration[] declarations, WorkingMemory workingMemory) throws Exception {
Object[] localVars = new Object[initUnit.getOtherIdentifiers().length];
MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext) workingMemoryContext;
VariableResolverFactory factory = factoryContext.getInitFactory();
initUnit.updateFactory(null, tuple, localVars, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
factory.setNextFactory(data.getFunctionFactory());
}
MVELSafeHelper.getEvaluator().executeExpression(this.init, null, factory);
DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
if (localVars.length > 0) {
for (int i = 0; i < df.getOtherVarsLength(); i++) {
localVars[i] = factory.getIndexedVariableResolver(df.getOtherVarsPos() + i).getValue();
}
}
((MVELAccumulatorContext) context).setVariables(localVars);
}
Aggregations