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);
}
}
}
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();
}
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());
}
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));
}
Aggregations