Search in sources :

Example 1 with Behavior

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.");
        }
    }
}
Also used : BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) Behavior(org.drools.core.rule.Behavior)

Example 2 with Behavior

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();
    }
}
Also used : WindowNode(org.drools.core.reteoo.WindowNode) Behavior(org.drools.core.rule.Behavior) WindowReference(org.drools.core.rule.WindowReference)

Example 3 with Behavior

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);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Behavior(org.drools.core.rule.Behavior)

Example 4 with Behavior

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());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.compiler.StockTick) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

Behavior (org.drools.core.rule.Behavior)4 BehaviorDescr (org.drools.compiler.lang.descr.BehaviorDescr)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 StockTick (org.drools.compiler.StockTick)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 WindowNode (org.drools.core.reteoo.WindowNode)1 Pattern (org.drools.core.rule.Pattern)1 WindowReference (org.drools.core.rule.WindowReference)1 ObjectType (org.drools.core.spi.ObjectType)1 Test (org.junit.Test)1