Search in sources :

Example 11 with QueryDescr

use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.

the class RuleBuilder method build.

/**
 * Build the give rule into the
 */
public static void build(final RuleBuildContext context) {
    RuleDescr ruleDescr = context.getRuleDescr();
    final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(ruleDescr.getLhs().getClass());
    if (builder != null) {
        // this is established during pre-processing, if it's query
        Pattern prefixPattern = context.getPrefixPattern();
        final GroupElement ce = (GroupElement) builder.build(context, getLhsForRuleUnit(context.getRule(), ruleDescr.getLhs()), prefixPattern);
        context.getRule().setLhs(ce);
    } else {
        throw new RuntimeException("BUG: builder not found for descriptor class " + ruleDescr.getLhs().getClass());
    }
    // build all the rule's attributes
    // must be after building LHS because some attributes require bindings from the LHS
    buildAttributes(context);
    // generate the main rule from the previously generated s.
    if (!(ruleDescr instanceof QueryDescr)) {
        // do not build the consequence if we have a query
        ConsequenceBuilder consequenceBuilder = context.getDialect().getConsequenceBuilder();
        consequenceBuilder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
        for (String name : ruleDescr.getNamedConsequences().keySet()) {
            consequenceBuilder.build(context, name);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryDescr(org.drools.drl.ast.descr.QueryDescr) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 12 with QueryDescr

use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.

the class RuleBuilder method preProcess.

public static void preProcess(final RuleBuildContext context) {
    RuleDescr ruleDescr = context.getRuleDescr();
    // Query and get object instead of using String
    if (null != ruleDescr.getParentName() && null != context.getPkg().getRule(ruleDescr.getParentName())) {
        context.getRule().setParent(context.getPkg().getRule(ruleDescr.getParentName()));
    }
    parseUnitAnnotations(context, context.getRule(), ruleDescr);
    // add all the rule's meta attributes
    buildMetaAttributes(context);
    if (context.getRuleDescr() instanceof QueryDescr) {
        context.getDialect().getPatternBuilderForQuery(((QueryImpl) context.getRule())).build(context, (QueryDescr) context.getRuleDescr());
    }
    context.initRule();
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 13 with QueryDescr

use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.

the class RuleParserTest method testQueryRuleMixed.

@Test
public void testQueryRuleMixed() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "query_and_rule.drl");
    assertEquals(4, // as queries are rules
    pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("bar", rule.getName());
    QueryDescr query = (QueryDescr) pkg.getRules().get(1);
    assertEquals("simple_query", query.getName());
    rule = (RuleDescr) pkg.getRules().get(2);
    assertEquals("bar2", rule.getName());
    query = (QueryDescr) pkg.getRules().get(3);
    assertEquals("simple_query2", query.getName());
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 14 with QueryDescr

use of org.drools.drl.ast.descr.QueryDescr in project drools by kiegroup.

the class QueryGenerator method processQueryDef.

public static void processQueryDef(PackageModel packageModel, RuleContext context) {
    QueryDescr queryDescr = ((QueryDescr) context.getRuleDescr());
    packageModel.registerQueryName(queryDescr.getName());
    String queryName = queryDescr.getName();
    final String queryDefVariableName = toQueryDef(queryName);
    context.setQueryName(Optional.of(queryDefVariableName));
    parseQueryParameters(context, packageModel, queryDescr);
    ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
    MethodCallExpr queryCall = createDslTopLevelMethod(QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(toStringLiteral(queryDescr.getNamespace()));
    }
    queryCall.addArgument(toStringLiteral(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(toClassOrInterfaceType(qp.getType())));
        queryCall.addArgument(toStringLiteral(qp.getName()));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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