use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testEvalMethodCompare.
@Test
public void testEvalMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createEvalRule(packageDescr1, "1==1");
builder1.addPackage(packageDescr1);
final EvalCondition eval1 = (EvalCondition) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createEvalRule(packageDescr2, "1==1");
builder2.addPackage(packageDescr2);
final EvalCondition eval2 = (EvalCondition) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr3 = new PackageDescr("package3");
createEvalRule(packageDescr3, "1==3");
builder3.addPackage(packageDescr3);
final EvalCondition eval3 = (EvalCondition) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
assertEquals(eval1, eval2);
assertFalse(eval1.equals(eval3));
assertFalse(eval2.equals(eval3));
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testNull.
@Test
public void testNull() {
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 patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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"));
}
Aggregations