Skip to main content

Introduction to Drools Queries

The Query system in Aletyx Enterprise Build of Drools provides a powerful mechanism for extracting and analyzing data from the rule engine. Queries allow developers to retrieve facts from working memory that match specific conditions, without executing actions on those facts. In the context of Rule Units, queries become even more powerful as they can be scoped to specific rule units. Queries function as a bridge between the rule engine and the application, enabling a more interactive approach to working with the knowledge base.

Traditional Queries vs. Rule Unit Queries

Aletyx Enterprise Build of Drools supports two types of query approaches:

Traditional Queries

Traditional queries operate on the global working memory and are not attached to any specific rule unit:

Rule Unit Queries

Rule Unit queries are scoped to a specific Rule Unit and operate only on the data sources within that unit:
Note the key differences:
  • The unit declaration at the top
  • The OOPath-like syntax (/customers[...]) for accessing data sources

Query Definition

Basic Structure

A query definition consists of:
  1. A unique name
  2. Optional parameters
  3. A set of patterns to match against facts
  4. For Rule Unit queries, reference to the specific Rule Unit

Pattern Matching in Queries

Queries use the same pattern matching syntax as rules, allowing for complex conditions and constraints:

Parameters in Queries

Queries can accept parameters, making them more flexible and reusable:
In Rule Units, these parameters are used similarly:

Executing Queries

In Traditional Drools Applications

In Rule Unit Applications

Advanced Query Features

Nested Queries

Queries can reference other queries, allowing for composition and reuse:
The syntax ActiveCustomers($customer;) passes the $customer variable from the nested query to the outer query.

Backward Chaining

Aletyx Enterprise Build of Drools queries support backward chaining, a reasoning technique where the system works backward from a goal:
This recursive query finds if a person is a parent of another person either directly or through multiple generations.

Unification

Queries support variable unification, allowing for more concise expressions:
In this example, the parameter customerId is unified with the property access customerId in the pattern.

Live Queries

Live Queries extend the query capability with real-time updates when matching facts change:
Live queries are particularly useful for:
  • Building reactive user interfaces
  • Maintaining real-time dashboards
  • Monitoring systems

Query-based Rules

Queries can be used within rule conditions to create more modular and reusable rule structures:
This approach allows for separating the condition logic (in queries) from the action logic (in rules).

Best Practices for Queries

  1. Name Queries Appropriately: Use clear, descriptive names that indicate what the query retrieves.
  2. Parameterize When Possible: Use parameters to make queries more reusable.
  3. Keep Queries Focused: Design queries with a single, clear purpose.
  4. Consider Performance: Be mindful of the complexity of query patterns, especially with large fact bases.
  5. Manage Variable Binding: Be consistent with variable naming to avoid confusion.
  6. Document Complex Queries: Add comments to explain the purpose and logic of complex queries.
  7. Test Queries Individually: Create dedicated tests for each query to verify they return the expected results.

Troubleshooting Queries

Query Performance Considerations

  1. Indexing: Aletyx Enterprise Build of Drools automatically creates indexes for some constraints. Position the most selective constraints first in patterns.
  2. Query Complexity: Complex queries with many joins can be expensive. Consider breaking them into simpler queries if possible.
  3. Data Volume: Be cautious with queries that might return large result sets.
  4. Live Queries: Live queries have additional overhead. Use them only when necessary.

Integration with External Systems

Queries provide an excellent interface for integrating Aletyx Enterprise Build of Drools with external systems:
  • Databases: Use queries to retrieve rule engine data for storage
  • APIs: Expose queries as service endpoints
  • User Interfaces: Power UI components with query results
  • Reporting Systems: Feed business intelligence tools with query data

Conclusion

The Query system in Aletyx Enterprise Build of Drools provides a powerful mechanism for extracting and analyzing data from the rule engine. When combined with Rule Units, queries become even more effective as they can be scoped to specific domains and data sources. Mastering queries allows developers to build more interactive, dynamic, and data-driven rule-based applications. Whether used for simple data retrieval or complex pattern matching with backward chaining, queries enhance the flexibility and utility of the Aletyx Enterprise Build of Drools rule engine.