Skip to content

Commit bde80fb

Browse files
committed
pattern composition
1 parent 218a28b commit bde80fb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

mellea/stdlib/pattern.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
from string import Template
55
from pydantic import BaseModel, create_model
66

7+
8+
def supercede(cls1: type, cls2: type) -> type:
9+
"""
10+
Return the more specific class if one is a subclass of the other.
11+
Raise ValueError if neither is a subclass of the other.
12+
"""
13+
if issubclass(cls1, cls2) and cls1 is not cls2:
14+
return cls1
15+
elif issubclass(cls2, cls1) and cls1 is not cls2:
16+
return cls2
17+
elif cls1 is cls2:
18+
return cls1
19+
else:
20+
raise ValueError(f"{cls1.__name__} and {cls2.__name__} are unrelated.")
21+
22+
723
class Pattern:
824

925
def __init__(self, pattern:str, **types:type[Any]):
@@ -46,6 +62,21 @@ def format(self) -> str:
4662
)
4763

4864

65+
def __and__(self, other) -> 'Pattern':
66+
# WIP. not a final state.
67+
# We should actually keep the original Pattern instance, rather than immediately merging them,
68+
# because we must perform query optimization later.
69+
70+
types = self.types.copy()
71+
for v in other.types.keys():
72+
if v in types:
73+
types[v] = supersede(other.types[v], types[v])
74+
75+
return Pattern(
76+
self.pattern.template + " " + other.pattern.template,
77+
**types)
78+
79+
4980
if __name__ == "__main__":
5081

5182
from mellea import start_session, SimpleContext

0 commit comments

Comments
 (0)