Class DroolsPatternBuilder<T>

  • Type Parameters:
    T - generic type of the pattern to be eventually created by this builder

    public final class DroolsPatternBuilder<T>
    extends Object
    Instances of PatternDSL.PatternDef are mutable and as such can not be reused by constraint streams that could result in mutually different rules. However, multiple rules can be based on the same constraint streams through stream sharing. This class makes it nearly impossible for PatternDSL.PatternDefs to be unknowingly shared.

    Instances of this class are immutable. Mutating methods (such as expand(UnaryOperator)) create a new instance, effectively preventing streams from sharing patterns. Patterns are only built when build() or build(DeclarationSource) is called. Callers must ensure that PatternDSL.PatternDefs obtained by these methods are never mutated, as that defeats the purpose.

    Example: Stream 1 joins A and B. Stream 2 filters Stream 1. Stream 3 also filters Stream 1, but with a different filter. If the pattern inside Stream 1 was shared by Stream 2 and Stream 3, filters from Stream 2 would also apply to Stream 3, and vice versa. Therefore, Stream 2 and Stream 3 each get their own immutable copy of Stream 1's DroolsPatternBuilder, one which only applies their individual filters.

    • Constructor Detail

      • DroolsPatternBuilder

        public DroolsPatternBuilder​(org.drools.model.Variable<T> baseVariable)
        Parameters:
        baseVariable - will be used during the PatternDSL.pattern(Variable) call within build() or build(DeclarationSource)
    • Method Detail

      • expand

        public DroolsPatternBuilder<T> expand​(UnaryOperator<org.drools.model.PatternDSL.PatternDef<T>> expander)
        Mutate the existing PatternDSL.PatternDef, adding a new operation, such as a new filter or a new variable binding.
        Returns:
        copy of the DroolsPatternBuilder with the new expanding operation included.
      • build

        public org.drools.model.PatternDSL.PatternDef<T> build()
        Builds a new instance of the pattern, with all the UnaryOperators from expand(UnaryOperator) applied.
        Returns:
        should no longer be mutated to guarantee that rules can not influence one another
      • build

        public org.drools.model.PatternDSL.PatternDef<T> build​(org.drools.model.DeclarationSource declarationSource)
        Builds a new instance of the pattern, with all the UnaryOperators from expand(UnaryOperator) applied.
        Parameters:
        declarationSource - will be applied to the newly created pattern
        Returns:
        should no longer be mutated to guarantee that rules can not influence one another
      • getBaseVariable

        public org.drools.model.Variable<T> getBaseVariable()