# Rules-based reasoning

At the core of every PyRel model is the rules-based reasoning that turns your model's facts into reusable business logic.
This overview gives you a high-level introduction to what rules-based reasoning is, when to use it, and how to approach writing complex logic in PyRel.

## What rules-based reasoning is

Rules-based reasoning uses declarative definitions to express business logic in a semantic model that every query can leverage without needing to re-implement the logic.
In practice, this means writing definitions that:

- **Derive new facts** based on existing facts in your model.
- **Encapsulate logic** in reusable concepts and relationships.
- **Enforce rules and policies** by requiring certain conditions to be met.

Using rules-based reasoning, you can build a rich semantic layer that can be queried and operationalized.
It is a powerful way to encode domain knowledge and business rules in a way that is transparent, reusable, and easy to maintain.

## When to use rules-based reasoning

Use rules-based reasoning when you need to:

- Encode business policy for consistent application across teams and use cases.
- Standardize definitions for key concepts across workflows.
- Consistently handle edge cases and data quality issues across applications.

You can start with exploratory queries and ad-hoc logic for initial analysis and validation.
If you start copying and pasting the same logic, or you need it to behave consistently across use cases, it's time to move that logic into a reusable definition.

## Where to go next

Pick a guide below to start using rules-based reasoning in your model:

- [Work with strings](/build/guides/reasoning/rules-based/work-with-strings/index.md): Use string expressions and std.strings functions to filter facts and define derived text logic in PyRel.
- [Work with numbers](/build/guides/reasoning/rules-based/work-with-numbers/index.md): Use numeric types, parsing, and std.math functions to filter facts and define derived numeric logic in PyRel.
- [Use dates and datetimes](/build/guides/reasoning/rules-based/use-dates-and-datetimes/index.md): Use Date and DateTime values in PyRel definitions for comparisons, time windows, and SLA-style logic.
- [Handle missing data](/build/guides/reasoning/rules-based/handle-missing-data/index.md): Handle missing values and missing relationships in PyRel definitions with null-safe logic, defaults, and presence checks.
- [Aggregate and group data](/build/guides/reasoning/rules-based/aggregate-and-group-data/index.md): Use std.aggregates with explicit grouping (per), scoped inputs, distinct, and ranking to build aggregation-driven PyRel definitions.
