Search in sources :

Example 6 with QueryDescr

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

the class ModelGenerator method generateModel.

public static void generateModel(KnowledgeBuilderImpl kbuilder, InternalKnowledgePackage pkg, PackageDescr packageDescr, PackageModel packageModel) {
    TypeResolver typeResolver = pkg.getTypeResolver();
    List<RuleDescr> ruleDescrs = packageDescr.getRules();
    if (ruleDescrs.isEmpty()) {
        return;
    }
    for (RuleDescr descr : ruleDescrs) {
        RuleContext context = new RuleContext(kbuilder, packageModel, typeResolver, descr);
        context.setDialectFromAttributes(packageDescr.getAttributes());
        if (descr instanceof QueryDescr) {
            QueryGenerator.processQueryDef(packageModel, context);
        }
    }
    int parallelRulesBuildThreshold = kbuilder.getBuilderConfiguration().getParallelRulesBuildThreshold();
    boolean parallelRulesBuild = parallelRulesBuildThreshold != -1 && ruleDescrs.size() > parallelRulesBuildThreshold;
    if (parallelRulesBuild) {
        List<RuleContext> ruleContexts = new ArrayList<>();
        int i = 0;
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            ruleContexts.add(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++));
        }
        KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(() -> ruleContexts.parallelStream().forEach(context -> processRuleDescr(context, packageDescr))).join();
    } else {
        int i = 0;
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            processRuleDescr(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++), packageDescr);
        }
    }
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) TypeResolver(org.drools.core.addon.TypeResolver) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ArrayList(java.util.ArrayList)

Example 7 with QueryDescr

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

the class XmlPackageReaderTest method testParseQuery.

@Test
public void testParseQuery() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseQuery.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get(0);
    assertEquals("my query", queryDescr.getName());
    final AndDescr lhs = queryDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Foo", patternDescr.getObjectType());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) QueryDescr(org.drools.drl.ast.descr.QueryDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 8 with QueryDescr

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

the class PatternBuilderForQueryTest method testQuery.

@Test
public void testQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$type");
    packageDescr.addRule(queryDescr);
    final AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == $type"));
    // Another query, no parameters
    QueryDescr queryDescr2 = new QueryDescr("query2");
    packageDescr.addRule(queryDescr2);
    AndDescr lhs2 = new AndDescr();
    queryDescr2.setLhs(lhs2);
    PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs2.addDescr(pattern2);
    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 Cheese("stilton", 15));
    QueryResults results = session.getQueryResults("query1", "stilton");
    assertEquals(1, results.size());
    Object object = results.iterator().next().get("stilton");
    assertEquals(new Cheese("stilton", 15), object);
    results = session.getQueryResults("query1", new Object[] { "cheddar" });
    assertEquals(0, results.size());
    session.insert(new Cheese("dolcelatte", 20));
    results = session.getQueryResults("query2", new Object[] {});
    assertEquals(2, results.size());
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 9 with QueryDescr

use of org.drools.drl.ast.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.drl.ast.descr.QueryDescr) MultiPatternDestinationDescr(org.drools.drl.ast.descr.MultiPatternDestinationDescr) Element(org.w3c.dom.Element) RuleDescr(org.drools.drl.ast.descr.RuleDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 10 with QueryDescr

use of org.drools.drl.ast.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.drl.ast.descr.QueryDescr) MultiPatternDestinationDescr(org.drools.drl.ast.descr.MultiPatternDestinationDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Element(org.w3c.dom.Element) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

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