use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicateMethodCompare.
@Test
public void testPredicateMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createPredicateRule(packageDescr1, "eval(x==y)");
builder1.addPackage(packageDescr1);
if (builder1.hasErrors()) {
fail(builder1.getErrors().toString());
}
final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate1 = (PredicateConstraint) pattern1.getConstraints().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createPredicateRule(packageDescr2, "eval(x==y)");
builder2.addPackage(packageDescr2);
if (builder2.hasErrors()) {
fail(builder2.getErrors().toString());
}
final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate2 = (PredicateConstraint) pattern2.getConstraints().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
if (builder3.hasErrors()) {
fail(builder3.getErrors().toString());
}
final PackageDescr packageDescr3 = new PackageDescr("package3");
createPredicateRule(packageDescr3, "eval(x!=y)");
builder3.addPackage(packageDescr3);
final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate3 = (PredicateConstraint) pattern3.getConstraints().get(0);
assertEquals(predicate1, predicate2);
assertFalse(predicate1.equals(predicate3));
assertFalse(predicate2.equals(predicate3));
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testCompilerConfiguration.
@Test
public void testCompilerConfiguration() throws Exception {
// test default is eclipse jdt core
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
builder.addPackage(pkgDescr);
DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
dialectField.setAccessible(true);
String dialectName = (String) dialectField.get(builder);
reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
Dialect dialect = reg.getDialect(dialectName);
final Field compilerField = dialect.getClass().getDeclaredField("compiler");
compilerField.setAccessible(true);
JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
JavaForMvelDialectConfiguration javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
switch(javaConf.getCompiler()) {
case NATIVE:
assertSame(NativeJavaCompiler.class, compiler.getClass());
break;
case ECLIPSE:
assertSame(EclipseJavaCompiler.class, compiler.getClass());
break;
default:
fail("Unrecognized java compiler");
}
// test eclipse jdt core with property settings and default source level
conf = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaForMvelDialectConfiguration.CompilerType.ECLIPSE);
builder = new KnowledgeBuilderImpl(conf);
builder.addPackage(pkgDescr);
dialectName = (String) dialectField.get(builder);
reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
dialect = reg.getDialect(dialectName);
compiler = (JavaCompiler) compilerField.get(dialect);
assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationWithFieldMetadata.
@Test
public void testTypeDeclarationWithFieldMetadata() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("TypeWithFieldMeta");
TypeFieldDescr f1 = new TypeFieldDescr("field", new PatternDescr("String"));
f1.addAnnotation("custom", null);
typeDescr.addField(f1);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
final FactType factType = bp.getFactType("org.drools.mvel.compiler.test.TypeWithFieldMeta");
assertNotNull(factType);
final FactField field = factType.getField("field");
assertNotNull(field);
final Map<String, Object> fieldMetaData = field.getMetaData();
assertNotNull("No field-level custom metadata got compiled", fieldMetaData);
assertTrue("Field metadata does not include expected value", fieldMetaData.containsKey("custom"));
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testEval.
@Test
public void testEval() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
lhs.addDescr(evalDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
String s = invoker.getMethodBytecode();
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testWarningsReportAsErrors.
@Test
public void testWarningsReportAsErrors() {
System.setProperty("drools.kbuilder.severity." + DuplicateRule.KEY, "ERROR");
KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(cfg);
final PackageDescr packageDescr1 = createBasicPackageWithOneRule(11, 1);
final PackageDescr packageDescr2 = createBasicPackageWithOneRule(22, 2);
builder.addPackage(packageDescr1);
builder.addPackage(packageDescr2);
assertTrue(builder.hasErrors());
assertFalse(builder.hasWarnings());
}
Aggregations