use of org.drools.compiler.lang.descr.WindowDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
private void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
// TODO: process annotations
// process pattern
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
dummy.setResource(packageDescr.getResource());
dummy.addAttribute(new AttributeDescr("dialect", "java"));
RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
if (builder != null) {
final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
if (pattern.getXpathConstraint() != null) {
context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
}
window.setPattern(pattern);
} else {
throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
}
if (!context.getErrors().isEmpty()) {
for (DroolsError error : context.getErrors()) {
addBuilderResult(error);
}
} else {
pkgRegistry.getPackage().addWindowDeclaration(window);
}
}
}
use of org.drools.compiler.lang.descr.WindowDeclarationDescr in project drools by kiegroup.
the class RuleParserTest method testWindowDeclaration.
@Test
public void testWindowDeclaration() throws Exception {
final String text = "package org.drools\n" + "declare window Ticks\n" + " @doc(\"last 10 stock ticks\")\n" + " $s : StockTick( source == \"NYSE\" )\n" + " over window:length( 10, $s.symbol )\n" + " from entry-point stStream\n" + "end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
assertEquals("org.drools", pkg.getName());
assertEquals(1, pkg.getWindowDeclarations().size());
WindowDeclarationDescr wdd = pkg.getWindowDeclarations().iterator().next();
assertEquals("Ticks", wdd.getName());
assertEquals(1, wdd.getAnnotations().size());
assertEquals("\"last 10 stock ticks\"", wdd.getAnnotation("doc").getValue());
PatternDescr pd = wdd.getPattern();
assertNotNull(pd);
assertEquals("$s", pd.getIdentifier());
assertEquals("StockTick", pd.getObjectType());
assertEquals("stStream", pd.getSource().getText());
assertEquals(1, pd.getBehaviors().size());
BehaviorDescr bd = pd.getBehaviors().get(0);
assertEquals("window", bd.getType());
assertEquals("length", bd.getSubType());
assertEquals(2, bd.getParameters().size());
assertEquals("10", bd.getParameters().get(0));
assertEquals("$s.symbol", bd.getParameters().get(1));
}
Aggregations