use of org.drools.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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());
}
use of org.drools.drl.ast.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.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclaration.
@Test
public void testTypeDeclaration() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("StockTick");
typeDescr.addAnnotation(Role.class.getCanonicalName(), "Event");
typeDescr.addAnnotation(TypeSafe.class.getCanonicalName(), "true");
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
if (builder.hasErrors()) {
fail(builder.getErrors().toString());
}
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("StockTick");
assertTrue(type.isTypesafe());
assertEquals(Role.Type.EVENT, type.getRole());
assertEquals(StockTick.class, type.getTypeClass());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testLiteral.
@Test
public void testLiteral() 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);
pattern.addConstraint(new ExprConstraintDescr("type == 'stilton'"));
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
}
Aggregations