use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testReturnValue.
@Test
public void testReturnValue() 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"));
pattern.addConstraint(new ExprConstraintDescr("price == (( (Integer) map.get( new Integer( x )) ).intValue() * y)"));
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertEquals("Should not have any errors", 0, builder.getErrors().getErrors().length);
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class DrlParserTest method testDeclaredSuperType.
@Test
public void testDeclaredSuperType() throws Exception {
String drl = "package foo \n" + "declare Bean1 \n" + "age: int \n" + "name : String \n" + "end \n" + "declare Bean2 extends Bean1\n" + "cheese : String \n" + "end";
DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
PackageDescr pkgDescr = parser.parse(null, drl);
TypeDeclarationDescr bean1Type = pkgDescr.getTypeDeclarations().get(0);
assertNull(bean1Type.getSuperTypeName());
TypeDeclarationDescr bean2Type = pkgDescr.getTypeDeclarations().get(1);
assertEquals("Bean1", bean2Type.getSuperTypeName());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class PatternBuilderForQueryTest method testRuleWithQuery.
@Test
public void testRuleWithQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$name");
queryDescr.addParameter("int", "$age");
queryDescr.addParameter("String", "$likes");
packageDescr.addRule(queryDescr);
AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
PatternDescr pattern = new PatternDescr(Person.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$name", "name", true));
pattern.addConstraint(new BindingDescr("$age", "age", true));
pattern.addConstraint(new BindingDescr("$likes", "likes", true));
RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
pattern = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$type", "type"));
pattern = new PatternDescr("query1");
pattern.setQuery(true);
lhs.addDescr(pattern);
ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'");
expr.setPosition(0);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$age");
expr.setPosition(1);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$type");
expr.setPosition(2);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
ruleDescr.setConsequence("System.out.println(\"age: \" + $age);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
final KieSession session = kbase.newKieSession();
session.insert(new Person("bobba", "stilton", 90));
session.insert(new Person("bobba", "brie", 80));
session.insert(new Person("bobba", "brie", 75));
session.insert(new Person("darth", "brie", 100));
session.insert(new Person("luke", "brie", 25));
session.insert(new Cheese("brie", 25));
session.fireAllRules();
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testCompatibleRestriction.
@Test
public void testCompatibleRestriction() throws Exception {
String source = "package com.sample rule test when Test( ( text == null || text2 matches \"\" ) ) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertEquals("com.sample", pkg.getName());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("test", rule.getName());
ExprConstraintDescr expr = (ExprConstraintDescr) ((PatternDescr) rule.getLhs().getDescrs().get(0)).getDescrs().get(0);
assertEquals("( text == null || text2 matches \"\" )", expr.getText());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testWithRetval.
@Test
public void testWithRetval() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_retval.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(1, col.getConstraint().getDescrs().size());
assertEquals("Foo", col.getObjectType());
final ExprConstraintDescr fld = (ExprConstraintDescr) col.getConstraint().getDescrs().get(0);
assertEquals("name== (a + b)", fld.getExpression());
}
Aggregations