use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.
the class PatternBuilder method processBehaviors.
private void processBehaviors(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
for (BehaviorDescr behaviorDescr : patternDescr.getBehaviors()) {
if (pattern.getObjectType().isEvent()) {
Behavior window = createWindow(behaviorDescr);
if (window != null) {
pattern.addBehavior(window);
context.setNeedStreamMode();
} else {
registerDescrBuildError(context, patternDescr, "Unknown window type '" + behaviorDescr.getSubType() + "'");
}
} else {
// Some behaviors can only be assigned to patterns declared as events
registerDescrBuildError(context, patternDescr, "A Sliding Window can only be assigned to types declared with @role( event ). The type '" + pattern.getObjectType() + "' in '" + context.getRule().getName() + "' is not declared as an Event.");
}
}
}
use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTimeWindowBehavior.
@Test
public void testTimeWindowBehavior() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
typeDeclDescr.addAnnotation("role", "event");
packageDescr.addTypeDeclaration(typeDeclDescr);
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
final BehaviorDescr windowDescr = new BehaviorDescr("window");
windowDescr.setSubType("time");
windowDescr.setParameters(Collections.singletonList("60000"));
patternDescr.addBehavior(windowDescr);
lhs.addDescr(patternDescr);
ruleDescr.setConsequence("System.out.println( $tick );");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
final RuleImpl rule = pkg.getRule("rule-1");
assertNotNull(rule);
final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
final Behavior window = pattern.getBehaviors().get(0);
assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.
the class RuleParserTest method testSlidingWindow.
@Test
public void testSlidingWindow() throws Exception {
final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("symbol==\"ACME\"", fcd.getExpression());
List<BehaviorDescr> behaviors = pattern.getBehaviors();
assertNotNull(behaviors);
assertEquals(1, behaviors.size());
BehaviorDescr descr = behaviors.get(0);
assertEquals("window", descr.getType());
assertEquals("length", descr.getSubType());
assertEquals("10", descr.getParameters().get(0));
}
use of org.drools.drl.ast.descr.BehaviorDescr 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));
}
use of org.drools.drl.ast.descr.BehaviorDescr in project drools by kiegroup.
the class ModelGenerator method addVariable.
private static void addVariable(BlockStmt ruleBlock, DeclarationSpec declaration, RuleContext context, boolean domainClass) {
if (declaration.getDeclarationClass() == null) {
context.addCompilationError(new UnknownDeclarationError(declaration.getBindingId()));
return;
}
Type declType = classToReferenceType(declaration);
ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(declaration.getBindingId()), Modifier.finalModifier());
MethodCallExpr declarationOfCall = createDslTopLevelMethod(DECLARATION_OF_CALL);
declarationOfCall.addArgument(new ClassExpr(declaration.getBoxedType()));
if (domainClass) {
String domainClassSourceName = asJavaSourceName(declaration.getDeclarationClass());
declarationOfCall.addArgument(DOMAIN_CLASSESS_METADATA_FILE_NAME + context.getPackageModel().getPackageUUID() + "." + domainClassSourceName + DOMAIN_CLASS_METADATA_INSTANCE);
}
declarationOfCall.addArgument(toStringLiteral(declaration.getVariableName().orElse(declaration.getBindingId())));
declaration.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
declaration.getEntryPoint().ifPresent(ep -> {
MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
entryPointCall.addArgument(toStringLiteral(ep));
declarationOfCall.addArgument(entryPointCall);
});
for (BehaviorDescr behaviorDescr : declaration.getBehaviors()) {
MethodCallExpr windowCall = createDslTopLevelMethod(WINDOW_CALL);
if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("org.drools.model.Window.Type.TIME");
windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
}
if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("org.drools.model.Window.Type.LENGTH");
windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
}
declarationOfCall.addArgument(windowCall);
}
AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, var_assign)) {
ruleBlock.addStatement(var_assign);
}
}
Aggregations