| import logging |
| from typing import Dict, Any, List |
| from config import settings |
| from schema import PetProfile |
|
|
| logger = logging.getLogger(__name__) |
|
|
| class RulesEngine: |
|
|
| @staticmethod |
| def create_engine() -> "RulesEngine": |
| return RulesEngine() |
|
|
| def __init__(self): |
| pass |
|
|
| def apply_rules(self, profile: PetProfile, nutrition_targets: Dict[str, Any]) -> Dict[str, Any]: |
| adjustments = {} |
|
|
| for rule_name, rule_config in settings.NUTRITION_RULES.items(): |
| if self._evaluate_condition(profile, rule_config["condition"]): |
| adjustments.update(rule_config["adjustments"]) |
|
|
| logger.info(f"Applied {len(adjustments)} rules for pet profile") |
| return adjustments |
|
|
| def _evaluate_condition(self, profile: PetProfile, condition: Dict[str, Any]) -> bool: |
| for key, value in condition.items(): |
| if key == "life_stage": |
| if profile.life_stage.value != value: |
| return False |
| elif key == "neutered_spayed": |
| if profile.neutered_spayed.value != value: |
| return False |
| elif key == "activity_level": |
| if profile.activity_level.value != value: |
| return False |
| elif key == "pet_type": |
| if profile.pet_type.value != value: |
| return False |
| elif key == "bcs": |
| if isinstance(value, dict): |
| if "lt" in value and profile.bcs >= value["lt"]: |
| return False |
| if "gt" in value and profile.bcs <= value["gt"]: |
| return False |
| elif profile.bcs != value: |
| return False |
| return True |
|
|
| def validate_plan_safety(self, profile: PetProfile, targets: Dict[str, Any]) -> List[str]: |
| warnings = [] |
|
|
| |
| if targets.get("mer", 0) > 5000: |
| warnings.append("Very high energy requirement - consult veterinarian") |
|
|
| |
| if targets.get("protein_g", 0) > 200: |
| warnings.append("High protein recommendation - ensure adequate water intake") |
|
|
| |
| ca = targets.get("calcium_mg", 0) |
| p = targets.get("phosphorus_mg", 0) |
| if p > 0 and (ca / p) < 1.0: |
| warnings.append("Calcium:Phosphorus ratio may be suboptimal") |
|
|
| |
| if profile.bcs < 3: |
| warnings.append("Very underweight pet - gradual refeeding required") |
| elif profile.bcs > 7: |
| warnings.append("Obese pet - weight management plan essential") |
|
|
| return warnings |
|
|