use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.
the class QueryBuilderTest 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.compiler.lang.descr.QueryDescr in project drools by kiegroup.
the class ModelGenerator method generateModel.
public static void generateModel(KnowledgeBuilderImpl kbuilder, InternalKnowledgePackage pkg, PackageDescr packageDescr, PackageModel packageModel, boolean isPattern) {
TypeResolver typeResolver = pkg.getTypeResolver();
packageModel.addImports(pkg.getTypeResolver().getImports());
packageModel.addGlobals(pkg.getGlobals());
packageModel.addAccumulateFunctions(pkg.getAccumulateFunctions());
packageModel.setInternalKnowledgePackage(pkg);
new WindowReferenceGenerator(packageModel, typeResolver).addWindowReferences(kbuilder, packageDescr.getWindowDeclarations());
packageModel.addAllFunctions(packageDescr.getFunctions().stream().map(FunctionGenerator::toFunction).collect(toList()));
for (RuleDescr descr : packageDescr.getRules()) {
if (descr instanceof QueryDescr) {
QueryGenerator.processQueryDef(kbuilder, typeResolver, packageModel, (QueryDescr) descr, isPattern);
}
}
for (RuleDescr descr : packageDescr.getRules()) {
if (descr instanceof QueryDescr) {
QueryGenerator.processQuery(kbuilder, packageModel, (QueryDescr) descr);
} else {
processRule(kbuilder, typeResolver, packageModel, packageDescr, descr, isPattern);
}
}
}
use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.
the class AndHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final AndDescr andDescr = (AndDescr) parser.getCurrent();
final Object parent = parser.getParent();
if (!andDescr.getDescrs().isEmpty()) {
if (parent instanceof RuleDescr || parent instanceof QueryDescr) {
final RuleDescr ruleDescr = (RuleDescr) parent;
ruleDescr.setLhs(andDescr);
} else if (parent instanceof MultiPatternDestinationDescr) {
final MultiPatternDestinationDescr mpDescr = (MultiPatternDestinationDescr) parent;
mpDescr.setInput(andDescr);
} else if (parent instanceof ConditionalElementDescr) {
final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
ceDescr.addDescr(andDescr);
}
}
return andDescr;
}
use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.
the class OrHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final OrDescr orDescr = (OrDescr) parser.getCurrent();
final Object parent = parser.getParent();
if (!orDescr.getDescrs().isEmpty()) {
if (parent instanceof RuleDescr || parent instanceof QueryDescr) {
final RuleDescr ruleDescr = (RuleDescr) parent;
ruleDescr.getLhs().addDescr(orDescr);
} else if (parent instanceof MultiPatternDestinationDescr) {
final MultiPatternDestinationDescr mpDescr = (MultiPatternDestinationDescr) parent;
mpDescr.setInput(orDescr);
} else if (parent instanceof ConditionalElementDescr) {
final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
ceDescr.addDescr(orDescr);
}
}
return orDescr;
}
use of org.drools.compiler.lang.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);
}
}
}
Aggregations