A model is a set of ground terms that satisfy a set of axioms.
Source code in src/typedlogic/solver.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 | @dataclass
class Model:
"""
A model is a set of ground terms that satisfy a set of axioms.
"""
description: Optional[str] = None
source_object: Optional[Any] = None
ground_terms: List[Term] = field(default_factory=list)
_predicate_index: Optional[Dict[str, List[Term]]] = field(default=None, init=False, repr=False, compare=False)
_indexed_count: int = field(default=0, init=False, repr=False, compare=False)
def retrieve(self, predicate: Union[str, type], *args) -> List[Term]:
return list(self.iter_retrieve(predicate, *args))
def _ensure_predicate_index(self) -> Dict[str, List[Term]]:
# the index is rebuilt if ground_terms has grown or shrunk; in-place
# replacement of a term at the same length is not detected
if self._predicate_index is None or self._indexed_count != len(self.ground_terms):
index: Dict[str, List[Term]] = {}
for t in self.ground_terms:
index.setdefault(t.predicate, []).append(t)
self._predicate_index = index
self._indexed_count = len(self.ground_terms)
return self._predicate_index
def iter_retrieve(self, predicate: Union[str, type], *args) -> Iterator[Term]:
"""
Retrieve all ground terms with a given predicate.
:param predicate:
:return:
"""
if isinstance(predicate, type):
predicate = predicate.__name__
for t in self._ensure_predicate_index().get(predicate, []):
if args:
t_values = t.values
is_match = True
for i, arg in enumerate(args):
if arg is not None and arg != t_values[i]:
is_match = False
break
if not is_match:
continue
yield t
|
iter_retrieve(predicate, *args)
Retrieve all ground terms with a given predicate.
Parameters:
| Name |
Type |
Description |
Default |
predicate
|
Union[str, type]
|
|
required
|
Returns:
| Type |
Description |
Iterator[Term]
|
|
Source code in src/typedlogic/solver.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 | def iter_retrieve(self, predicate: Union[str, type], *args) -> Iterator[Term]:
"""
Retrieve all ground terms with a given predicate.
:param predicate:
:return:
"""
if isinstance(predicate, type):
predicate = predicate.__name__
for t in self._ensure_predicate_index().get(predicate, []):
if args:
t_values = t.values
is_match = True
for i, arg in enumerate(args):
if arg is not None and arg != t_values[i]:
is_match = False
break
if not is_match:
continue
yield t
|