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