use of org.drools.drl.ast.descr.QueryDescr 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.QueryDescr in project drools by kiegroup.
the class RuleParserTest method testSimpleQuery.
@Test
public void testSimpleQuery() throws Exception {
final QueryDescr query = (QueryDescr) parseResource("query", "simple_query.drl");
assertNotNull(query);
assertEquals("simple_query", query.getName());
final AndDescr lhs = query.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// Check first pattern
final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("foo3", first.getIdentifier());
assertEquals("Bar", first.getObjectType());
assertEquals(1, first.getConstraint().getDescrs().size());
AndDescr and = (AndDescr) first.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertNotNull(fld);
assertEquals("a==3", fld.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
assertEquals(1, second.getDescrs().size());
// check it has field bindings.
final ExprConstraintDescr bindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
assertEquals("a4:a==4", bindingDescr.getExpression());
}
use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.
the class QueryHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final String queryName = attrs.getValue("name");
emptyAttributeCheck(localName, "name", queryName, parser);
final QueryDescr queryDescr = new QueryDescr(queryName.trim());
return queryDescr;
}
use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.
the class QueryHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final QueryDescr queryDescr = (QueryDescr) parser.getCurrent();
final AndDescr lhs = queryDescr.getLhs();
if (lhs == null || lhs.getDescrs().isEmpty()) {
throw new SAXParseException("<query> requires a LHS", parser.getLocator());
}
((PackageDescr) parser.getData()).addRule(queryDescr);
return queryDescr;
}
use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.
the class ModelGenerator method processRuleDescr.
private static void processRuleDescr(RuleContext context, PackageDescr packageDescr) {
if (context.getRuleDescr() instanceof QueryDescr) {
QueryGenerator.processQuery(context.getPackageModel(), (QueryDescr) context.getRuleDescr());
return;
}
context.setDialectFromAttributes(packageDescr.getAttributes());
processRule(packageDescr, context);
RuleUnitDescription rud = context.getRuleUnitDescr();
if (rud != null) {
context.getPackageModel().addRuleUnit(rud);
}
}
Aggregations