Metadata-Version: 2.4
Name: fuzzy_predicates_gdrottoli
Version: 0.1.2
Summary: Specification pattern implementation with fuzzy predicates
Author: Giovanni Daián Rottoli
Author-email: Giovanni Daián Rottoli <gd.rottoli@gmail.com>
License-File: LICENSE
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Dist: loguru>=0.7.3
Requires-Dist: mypy>=1.19.1
Requires-Dist: pydantic>=2.12.5
Requires-Dist: pytest>=9.0.2
Requires-Dist: pyyaml>=6.0.3
Requires-Dist: ruff>=0.15.2
Requires-Python: >=3.13
Project-URL: Homepage, https://github.com/GdRottoli/FuzzyPredicates
Project-URL: Issues, https://github.com/GdRottoli/FuzzyPredicates/issues
Description-Content-Type: text/markdown

# FuzzyPredicates

FuzzSpec is a Python library for defining and evaluating fuzzy predicate specifications. It allows you to define complex logic rules based on fuzzy set theory, returning a degree of truth (between 0 and 1) instead of binary values.

## Features

- **Flexible Membership Functions**: 
  - Triangular (3 parameters)
  - Trapezoidal (4 parameters)
- **Configurable Logic**: Choose between different T-Norms (AND), S-Norms (OR), and Complements (NOT).
- **YAML Configuration**: Decouple logic from code using declarative `logic.yaml` files.
- **Pydantic Validation**: Automatic schema validation for your logic configurations.
- **Compositional API**: Easily combine atomic predicates using Python operators (`&`, `|`, `~`).

## Installation

```bash
uv sync
```

## Configuration (`logic.yaml`)

You can configure the global fuzzy logic behavior and define your predicates in a single file.

### 1. Global Logical Operators
Configure how the library handles `AND`, `OR`, and `NOT` operations.

```yaml
config:
    tnorm: "product"      # Options: min, product, lukasiewicz
    snorm: "probabilistic" # Options: max, probabilistic, lukasiewicz
    complement: "standard" # Options: standard (1 - x)
```

### 2. Atomic Predicates
Define attributes and their fuzzy ranges.

```yaml
atomic:
    low_att:
        variable: "att1"
        fuzzy_number: [0.1, 1.4, 4.3] # Triangular
    active_range:
        variable: "att2"
        fuzzy_number: [0.1, 0.9, 4.3, 5.5] # Trapezoidal
```

### 3. Composed Predicates
Link atomic predicates using logical operators.

```yaml
composed:
    low_and_active:
        operator: "and"
        operands: ["low_att", "active_range"]
    not_low:
        operator: "not"
        operands: ["low_att"]
```

## Usage Example

### Create your data model
```python
from dataclasses import dataclass

@dataclass
class SensorData:
    att1: float
    att2: float
    att3: int
```

### Load and Evaluate
```python
from FuzzyPredicates.utils import load_rules

# Load rules from config (validates against Pydantic schema)
rules = load_rules('logic.yaml')

# Evaluate against data
data = SensorData(att1=1.0, att2=4.4, att3=43)
result = rules['low_and_active'](data)

print(f"Confidence Degree: {result:.2f}")
```

### Advanced Usage (Command Line)
The `main.py` entry point supports configurable logging and custom configuration paths.

```bash
uv run main.py --log-level DEBUG --config my_logic.yaml
```

Available log levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`.
```

## Technical Details

### Supported Operators

| Logic Type | Name | Formula |
|------------|------|---------|
| **T-Norm (AND)** | `min` | $\min(a, b)$ |
| | `product` | $a \times b$ |
| | `lukasiewicz` | $\max(0, a + b - 1)$ |
| **S-Norm (OR)** | `max` | $\max(a, b)$ |
| | `probabilistic` | $a + b - (a \times b)$ |
| | `lukasiewicz` | $\min(1, a + b)$ |

### Project Structure
- `src/FuzzyPredicates/fuzzy/`: Core fuzzy membership functions and operators.
- `src/FuzzyPredicates/predicates.py`: High-level `Predicate` class with operator overloading.
- `src/FuzzyPredicates/models.py`: Pydantic schemas for logic validation.
- `src/FuzzyPredicates/utils.py`: Utility functions (e.g., `load_rules`).
- `main.py`: Example entry point.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
