Search in sources :

Example 1 with QueryDescr

use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.

the class QueryHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final QueryDescr queryDescr = (QueryDescr) parser.getCurrent();
    final AndDescr lhs = queryDescr.getLhs();
    if (lhs == null || lhs.getDescrs().isEmpty()) {
        throw new SAXParseException("<query> requires a LHS", parser.getLocator());
    }
    ((PackageDescr) parser.getData()).addRule(queryDescr);
    return queryDescr;
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 2 with QueryDescr

use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.

the class QueryHandler method start.

public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final String queryName = attrs.getValue("name");
    emptyAttributeCheck(localName, "name", queryName, parser);
    final QueryDescr queryDescr = new QueryDescr(queryName.trim());
    return queryDescr;
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr)

Example 3 with QueryDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.QueryDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 4 with QueryDescr

use of org.drools.compiler.lang.descr.QueryDescr in project drools by kiegroup.

the class QueryBuilderTest 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.compiler.lang.descr.QueryDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 5 with QueryDescr

use of org.drools.compiler.lang.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.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.compiler.lang.descr.GlobalDescr) QueryDescr(org.drools.compiler.lang.descr.QueryDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

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