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