|
5 | 5 | import itertools
|
6 | 6 | import logging
|
7 | 7 | import traceback
|
8 |
| -from typing import Any, Dict, Iterator, List, Tuple |
| 8 | +from typing import Any, Dict, Generator, Iterator, List, Tuple |
9 | 9 |
|
10 | 10 | from sympy import And, Implies, Not, Symbol
|
11 | 11 | from sympy.assumptions.cnf import EncodedCNF
|
@@ -162,12 +162,6 @@ def build_scenarios(self, condition_names: List[str]) -> Iterator[Dict[str, bool
|
162 | 162 | if len(condition_names) == 0:
|
163 | 163 | return
|
164 | 164 |
|
165 |
| - # if only one condition we will assume its True/False |
166 |
| - # if len(condition_names) == 1: |
167 |
| - # yield {condition_names[0]: True} |
168 |
| - # yield {condition_names[0]: False} |
169 |
| - # return |
170 |
| - |
171 | 165 | try:
|
172 | 166 | # build a large matric of True/False options based on the provided conditions
|
173 | 167 | scenarios_returned = 0
|
@@ -255,3 +249,41 @@ def check_implies(self, scenarios: Dict[str, bool], implies: str) -> bool:
|
255 | 249 | # KeyError is because the listed condition doesn't exist because of bad
|
256 | 250 | # formatting or just the wrong condition name
|
257 | 251 | return True
|
| 252 | + |
| 253 | + def build_scenerios_on_region( |
| 254 | + self, condition_name: str, region: str |
| 255 | + ) -> Generator[bool, None, None]: |
| 256 | + """Based on a region validate if the condition_name coudle be true |
| 257 | +
|
| 258 | + Args: |
| 259 | + condition_name (str): The name of the condition we are validating against |
| 260 | + region (str): the name of the region |
| 261 | +
|
| 262 | + Returns: |
| 263 | + Generator[bool]: Returns True, False, or True and False depending on if the |
| 264 | + condition could be True, False or both based on the region parameter |
| 265 | + """ |
| 266 | + if not isinstance(condition_name, str): |
| 267 | + return |
| 268 | + cnf_region = self._cnf.copy() |
| 269 | + for eql in self._conditions[condition_name].equals: |
| 270 | + is_region, equal_region = eql.is_region |
| 271 | + if is_region: |
| 272 | + if equal_region == region: |
| 273 | + cnf_region.add_prop(And(self._solver_params[eql.hash])) |
| 274 | + else: |
| 275 | + cnf_region.add_prop(Not(self._solver_params[eql.hash])) |
| 276 | + |
| 277 | + cnf_test = cnf_region.copy() |
| 278 | + cnf_test.add_prop( |
| 279 | + self._conditions[condition_name].build_true_cnf(self._solver_params) |
| 280 | + ) |
| 281 | + if satisfiable(cnf_test): |
| 282 | + yield True |
| 283 | + |
| 284 | + cnf_test = cnf_region.copy() |
| 285 | + cnf_test.add_prop( |
| 286 | + self._conditions[condition_name].build_false_cnf(self._solver_params) |
| 287 | + ) |
| 288 | + if satisfiable(cnf_test): |
| 289 | + yield False |
0 commit comments