Search in sources :

Example 6 with QueryDescr

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

Example 7 with QueryDescr

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);
        }
    }
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Example 8 with QueryDescr

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;
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) MultiPatternDestinationDescr(org.drools.compiler.lang.descr.MultiPatternDestinationDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) Element(org.w3c.dom.Element) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 9 with QueryDescr

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;
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) MultiPatternDestinationDescr(org.drools.compiler.lang.descr.MultiPatternDestinationDescr) Element(org.w3c.dom.Element) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 10 with QueryDescr

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);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryDescr(org.drools.compiler.lang.descr.QueryDescr) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Aggregations

QueryDescr (org.drools.compiler.lang.descr.QueryDescr)12 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)7 AndDescr (org.drools.compiler.lang.descr.AndDescr)6 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 Test (org.junit.Test)5 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)4 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)3 Element (org.w3c.dom.Element)3 Cheese (org.drools.compiler.Cheese)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)2 MultiPatternDestinationDescr (org.drools.compiler.lang.descr.MultiPatternDestinationDescr)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 KiePackage (org.kie.api.definition.KiePackage)2 KieSession (org.kie.api.runtime.KieSession)2 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Person (org.drools.compiler.Person)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)1