Coverage for src / local_deep_research / advanced_search_system / constraints / base_constraint.py: 100%
33 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:55 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:55 +0000
1"""
2Base constraint classes for query decomposition.
3"""
5from dataclasses import dataclass
6from enum import Enum
7from typing import Any, Dict, Optional
10class ConstraintType(Enum):
11 """Types of constraints in queries."""
13 PROPERTY = "property" # e.g., "formed during ice age"
14 NAME_PATTERN = "name_pattern" # e.g., "contains body part"
15 EVENT = "event" # e.g., "fall between 2000-2021"
16 STATISTIC = "statistic" # e.g., "84.5x ratio"
17 TEMPORAL = "temporal" # e.g., "in 2014"
18 LOCATION = "location" # e.g., "in Colorado"
19 COMPARISON = "comparison" # e.g., "more than X"
20 EXISTENCE = "existence" # e.g., "has a viewpoint"
23@dataclass
24class Constraint:
25 """A single constraint extracted from a query."""
27 id: str
28 type: ConstraintType
29 description: str
30 value: Any
31 weight: float = 1.0 # Importance of this constraint
32 metadata: Optional[Dict[str, Any]] = None
34 def __post_init__(self):
35 """Initialize metadata if not provided."""
36 if self.metadata is None:
37 self.metadata = {}
39 def to_search_terms(self) -> str:
40 """Convert constraint to search terms."""
41 if self.type == ConstraintType.PROPERTY:
42 return self.value
43 if self.type == ConstraintType.NAME_PATTERN:
44 return f"{self.value} name trail mountain"
45 if self.type == ConstraintType.EVENT:
46 return f"{self.value} accident incident"
47 if self.type == ConstraintType.STATISTIC:
48 return f"{self.value} statistics data"
49 return str(self.value)
51 def is_critical(self) -> bool:
52 """Determine if this is a critical constraint that must be satisfied."""
53 # Consider NAME_PATTERN constraints as critical regardless of weight
54 if self.type == ConstraintType.NAME_PATTERN:
55 return True
56 # Otherwise use weight to determine criticality
57 return self.weight > 0.8