org.optaplanner.core.impl.score.stream.drools.common

## Class DroolsPatternBuilder<T>

- java.lang.Object
- - org.optaplanner.core.impl.score.stream.drools.common.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.PatternDef`s 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.PatternDef`s 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 Summary

    Constructors
    Constructor and Description
    `DroolsPatternBuilder(org.drools.model.Variable<T> baseVariable)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `org.drools.model.PatternDSL.PatternDef<T>` | `build()` Builds a new instance of the pattern, with all the `UnaryOperator`s from `expand(UnaryOperator)` applied.
    `org.drools.model.PatternDSL.PatternDef<T>` | `build(org.drools.model.DeclarationSource declarationSource)` Builds a new instance of the pattern, with all the `UnaryOperator`s from `expand(UnaryOperator)` applied.
    `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.
    `org.drools.model.Variable<T>` | `getBaseVariable()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 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 `UnaryOperator`s 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 `UnaryOperator`s 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()

Copyright © 2006–2021 JBoss by Red Hat. All rights reserved.