use of org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
conf.setOption(PropertySpecificOption.ALWAYS);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
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());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl in project drools by kiegroup.
the class JavaConsequenceBuilderTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// this test was originally intended with PropertyReactive.ALLOWED:
conf.setOption(PropertySpecificOption.ALLOWED);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
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());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl in project drools by kiegroup.
the class Misc2Test method testExtendsWithStrictModeOff.
@Test
public void testExtendsWithStrictModeOff() {
// DROOLS-475
String str = "import java.util.HashMap;\n" + "dialect \"mvel\"\n" + "declare HashMap end\n" + "\n" + "declare Test extends HashMap end\n" + "\n" + "rule \"Insert\" salience 0\n" + "when\n" + "then\n" + "Test t = new Test();\n" + "t.Price = 10;\n" + "t.put(\"A\", \"a\");\n" + "t.OtherPrices = new HashMap();\n" + "t.OtherPrices.OldPrice = 8;\n" + "System.out.println(\"Inserting t=\"+t);\n" + "insert(t);\n" + "end\n" + "\n" + "rule \"Test HashMap\" salience -1\n" + "when\n" + "t: HashMap( Price < 11 )\n" + "then\n" + "t.Price = 11;\n" + "System.out.println(\"In Test HashMap\");\n" + "end\n" + "\n" + "rule \"Test Inherited\" salience -1\n" + "when\n" + "t: Test( Price < 100 )\n" + "then\n" + "t.Price = 12;\n" + "System.out.println(\"In Test Inherited!\");\n" + "end\n" + "\n" + "rule \"Print Result\" salience -5\n" + "when\n" + "t: Test()\n" + "then\n" + "System.out.println(\"Finally Price is =\"+t.Price);\n" + "//This as well doesn't print content as per toString() of HashMap is there a way to do that?\n" + "System.out.println(\"Finally t=\"+t);\n" + "end\n";
KnowledgeBuilderConfigurationImpl pkgBuilderCfg = new KnowledgeBuilderConfigurationImpl();
MVELDialectConfiguration mvelConf = (MVELDialectConfiguration) pkgBuilderCfg.getDialectConfiguration("mvel");
mvelConf.setStrict(false);
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pkgBuilderCfg);
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl in project drools by kiegroup.
the class PackageBuilderConfigurationTest method testSystemProperties.
@Test
public void testSystemProperties() {
KnowledgeBuilderConfigurationImpl cfg;
JavaDialectConfiguration javaConf;
System.setProperty("drools.dialect.java.compiler", "JANINO");
cfg = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
assertEquals(JavaDialectConfiguration.CompilerType.JANINO, javaConf.getCompiler());
KnowledgeBuilderConfigurationImpl cfg2 = new KnowledgeBuilderConfigurationImpl();
JavaDialectConfiguration javaConf2 = (JavaDialectConfiguration) cfg2.getDialectConfiguration("java");
assertEquals(javaConf.getCompiler(), javaConf2.getCompiler());
System.setProperty("drools.dialect.java.compiler", "ECLIPSE");
cfg = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
assertEquals(JavaDialectConfiguration.CompilerType.ECLIPSE, javaConf.getCompiler());
javaConf2.setCompiler(JavaDialectConfiguration.CompilerType.ECLIPSE);
assertEquals(JavaDialectConfiguration.CompilerType.ECLIPSE, javaConf2.getCompiler());
javaConf2.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
assertEquals(JavaDialectConfiguration.CompilerType.JANINO, javaConf2.getCompiler());
final KnowledgeBuilderConfigurationImpl cfg3 = new KnowledgeBuilderConfigurationImpl();
JavaDialectConfiguration javaConf3 = (JavaDialectConfiguration) cfg3.getDialectConfiguration("java");
assertEquals(javaConf.getCompiler(), javaConf3.getCompiler());
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl in project drools by kiegroup.
the class PackageBuilderConfigurationTest method testProgramaticProperties2.
@Test
public void testProgramaticProperties2() {
JavaDialectConfiguration javaConf = new JavaDialectConfiguration();
javaConf.init(new KnowledgeBuilderConfigurationImpl());
javaConf.setCompiler(JavaDialectConfiguration.CompilerType.ECLIPSE);
KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
cfg.setDialectConfiguration("java", javaConf);
JavaDialectConfiguration javaConf2 = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
assertSame(javaConf, javaConf2);
assertEquals(JavaDialectConfiguration.CompilerType.ECLIPSE, javaConf2.getCompiler());
javaConf = new JavaDialectConfiguration();
javaConf.init(new KnowledgeBuilderConfigurationImpl());
javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
cfg = new KnowledgeBuilderConfigurationImpl();
cfg.setDialectConfiguration("java", javaConf);
javaConf2 = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
assertSame(javaConf, javaConf2);
assertEquals(JavaDialectConfiguration.CompilerType.JANINO, javaConf2.getCompiler());
}
Aggregations