use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method createBasicPackageWithOneRule.
private PackageDescr createBasicPackageWithOneRule(int line, int col) {
PackageDescr packageDescr = new PackageDescr("p1");
RuleDescr ruleDescr = new RuleDescr("rule-1");
ruleDescr.setLocation(line, col);
packageDescr.addRule(ruleDescr);
AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
return packageDescr;
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationNewBean.
@Test
public void testTypeDeclarationNewBean() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("NewBean");
TypeFieldDescr f1 = new TypeFieldDescr("name", new PatternDescr("String"));
TypeFieldDescr f2 = new TypeFieldDescr("age", new PatternDescr("int"));
typeDescr.addField(f1);
typeDescr.addField(f2);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("NewBean");
assertEquals("NewBean", type.getTypeName());
assertEquals(Role.Type.FACT, type.getRole());
assertEquals("org.drools.compiler.test.NewBean", type.getTypeClass().getName());
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
Class newBean = bp.getPackageClassLoader().loadClass("org.drools.compiler.test.NewBean");
assertNotNull(newBean);
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testDuplicateRuleNames.
@Test
public void testDuplicateRuleNames() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
RuleDescr ruleDescr;
AndDescr lhs;
PatternDescr patternDescr;
FieldConstraintDescr literalDescr;
final PackageDescr packageDescr = createBasicPackageWithOneRule(1, 1);
ruleDescr = new RuleDescr("rule-1");
ruleDescr.setLocation(42, 43);
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
ruleDescr = new RuleDescr("rule-2");
ruleDescr.setLocation(42, 43);
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
assertLength(2, builder.getErrors().getErrors());
final ParserError err = (ParserError) builder.getErrors().getErrors()[0];
assertEquals(42, err.getRow());
assertEquals(43, err.getCol());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testWarnings.
@Test
public void testWarnings() {
System.setProperty("drools.kbuilder.severity." + DuplicateRule.KEY, "WARNING");
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = createBasicPackageWithOneRule(11, 1);
final PackageDescr packageDescr2 = createBasicPackageWithOneRule(22, 2);
builder.addPackage(packageDescr1);
builder.addPackage(packageDescr2);
assertFalse(builder.hasErrors());
assertTrue(builder.hasWarnings());
}
use of org.drools.compiler.lang.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));
}
Aggregations