use of org.drools.core.rule.Behavior 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.core.rule.Behavior in project drools by kiegroup.
the class PatternBuilder method buildBehaviors.
private void buildBehaviors(BuildContext context, BuildUtils utils, Pattern pattern, Constraints constraints) {
final List<Behavior> behaviors = pattern.getBehaviors();
if (pattern.getSource() == null || (!(pattern.getSource() instanceof WindowReference) && (context.getCurrentEntryPoint() != EntryPointId.DEFAULT || !behaviors.isEmpty()))) {
attachObjectTypeNode(context, utils, pattern);
}
if (!behaviors.isEmpty()) {
// build the window node:
WindowNode wn = context.getComponentFactory().getNodeFactoryService().buildWindowNode(context.getNextId(), constraints.alphaConstraints, behaviors, context.getObjectSource(), context);
context.setObjectSource(utils.attachNode(context, wn));
// alpha constraints added to the window node already
constraints.alphaConstraints.clear();
}
}
use of org.drools.core.rule.Behavior in project drools by kiegroup.
the class PatternBuilder method attachObjectTypeNode.
private void attachObjectTypeNode(final BuildContext context, final BuildUtils utils, final Pattern pattern) {
boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
ObjectType objectType = pattern.getObjectType();
if (pattern.getObjectType() instanceof ClassObjectType) {
// Is this the query node, if so we don't want any memory
if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
context.setTupleMemoryEnabled(false);
context.setObjectTypeNodeMemoryEnabled(false);
}
}
ObjectTypeNode otn = context.getComponentFactory().getNodeFactoryService().buildObjectTypeNode(context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
if (objectType.isEvent() && EventProcessingOption.STREAM.equals(context.getKnowledgeBase().getConfiguration().getEventProcessingMode())) {
ExpirationSpec expirationSpec = getExpirationForType(context, objectType);
if (expirationSpec.offset != NEVER_EXPIRES && expirationSpec.hard) {
// hard expiration is set, so use it
otn.setExpirationOffset(expirationSpec.offset);
} else {
// otherwise calculate it based on behaviours and temporal constraints
long offset = NEVER_EXPIRES;
for (Behavior behavior : pattern.getBehaviors()) {
if (behavior.getExpirationOffset() != NEVER_EXPIRES) {
offset = Math.max(behavior.getExpirationOffset(), offset);
}
}
// if there's no implicit expiration uses the (eventually set) soft one
if (offset == NEVER_EXPIRES && !expirationSpec.hard) {
offset = expirationSpec.offset;
}
long distance = context.getExpirationOffset(pattern);
if (distance == NEVER_EXPIRES) {
// it means the rules have no temporal constraints, or
// the constraints require events to be hold forever. In this
// case, we allow type declarations to override the implicit
// expiration offset by defining an expiration policy with the
// @expires tag
otn.setExpirationOffset(offset);
} else {
otn.setExpirationOffset(Math.max(distance, offset));
}
}
}
context.setObjectSource(utils.attachNode(context, otn));
context.setObjectTypeNodeMemoryEnabled(objectMemory);
}
use of org.drools.core.rule.Behavior 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());
}
Aggregations