Search in sources :

Example 1 with QueryDescr

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();
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) QueryDescr(org.drools.drl.ast.descr.QueryDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.mvel.compiler.Person) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with QueryDescr

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());
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 3 with QueryDescr

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;
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr)

Example 4 with 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;
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 5 with 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);
    }
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription)

Aggregations

QueryDescr (org.drools.drl.ast.descr.QueryDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)7 AndDescr (org.drools.drl.ast.descr.AndDescr)6 PackageDescr (org.drools.drl.ast.descr.PackageDescr)5 Test (org.junit.Test)5 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)3 Element (org.w3c.dom.Element)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)2 MultiPatternDestinationDescr (org.drools.drl.ast.descr.MultiPatternDestinationDescr)2 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 Cheese (org.drools.mvel.compiler.Cheese)2 KiePackage (org.kie.api.definition.KiePackage)2 KieSession (org.kie.api.runtime.KieSession)2 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1