Este contenido no está disponible en el idioma seleccionado.

Chapter 16. Patterns


16.1. Patterns

A pattern element is the most important Conditional Element. It can potentially match on each fact that is inserted in the working memory. A pattern contains constraints and has an optional pattern binding.

16.2. Pattern Example

This is what a pattern looks like:
rule "2 unconnected patterns"
when
    Pattern1()
    Pattern2()
then
    ... // actions
end

// The above rule is internally rewritten as:

rule "2 and connected patterns"
when
    Pattern1()
    and Pattern2()
then
    ... // actions
end
Copy to Clipboard Toggle word wrap

Note

An and cannot have a leading declaration binding. This is because a declaration can only reference a single fact at a time, and when the and is satisfied it matches both facts.

16.3. Pattern Matching

A pattern matches against a fact of the given type. The type need not be the actual class of some fact object. Patterns may refer to superclasses or even interfaces, thereby potentially matching facts from many different classes. The constraints are defined inside parentheses.

16.4. Pattern Binding

Patterns can be bound to a matching object. This is accomplished using a pattern binding variable such as $p.

16.5. Pattern Binding with Variable Example

This is what pattern binding using a variable looks like:
rule ...
when
    $p : Person()
then
    System.out.println( "Person " + $p );
end
Copy to Clipboard Toggle word wrap

Note

The prefixed dollar symbol ($) is not mandatory.

16.6. Constraints

A constraint is an expression that returns true or false. For example, you can have a constraint that states five is smaller than six.

Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat