| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from .Common import hash_objs, state |
| 26 |
|
| 27 |
|
| 28 |
class Property: |
| 29 |
|
| 30 |
@classmethod |
| 31 |
def FromOriginalState(cls, d): |
| 32 |
return cls(d.get('type'), d.get('index'), d.get('value')) |
| 33 |
|
| 34 |
def __init__(self, tag=None, index=None, value=None): |
| 35 |
self._tag = tag |
| 36 |
self._index = index |
| 37 |
self._value = value |
| 38 |
|
| 39 |
@property |
| 40 |
def tag(self): |
| 41 |
return self._tag |
| 42 |
|
| 43 |
@property |
| 44 |
def index(self): |
| 45 |
return self._index |
| 46 |
|
| 47 |
@property |
| 48 |
def value(self): |
| 49 |
return self._value |
| 50 |
|
| 51 |
def state(self): |
| 52 |
rv = {'type': self.tag} |
| 53 |
if self.index is not None: rv['index'] = state(self.index) |
| 54 |
if self.value is not None: rv['value'] = state(self.value) |
| 55 |
return rv |
| 56 |
|
| 57 |
def __eq__(self, other): |
| 58 |
return self.__class__ == other.__class__ and \ |
| 59 |
self.tag == other.tag and \ |
| 60 |
self.value == other.value and \ |
| 61 |
self.index == other.index |
| 62 |
|
| 63 |
def __hash__(self): |
| 64 |
|
| 65 |
return hash_objs(self.tag, self.value, self.index) |
| 66 |
|
| 67 |
|
| 68 |
class Predicate(Property): |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def And(cls, predicates): |
| 72 |
predicates = tuple(predicates) |
| 73 |
if len(predicates) == 0: |
| 74 |
return cls('TruePred') |
| 75 |
if len(predicates) == 1: |
| 76 |
return predicates[0] |
| 77 |
return cls('And', value=predicates) |
| 78 |
|
| 79 |
@classmethod |
| 80 |
def Or(cls, predicates): |
| 81 |
predicates = tuple(predicates) |
| 82 |
if len(predicates) == 0: |
| 83 |
return cls('TruePred') |
| 84 |
if len(predicates) == 1: |
| 85 |
return predicates[0] |
| 86 |
return cls('Or', value=predicates) |
| 87 |
|
| 88 |
def __lt__(self, other): |
| 89 |
|
| 90 |
if other.tag == 'TruePred': |
| 91 |
if self.tag == 'TruePred': |
| 92 |
return False |
| 93 |
return True |
| 94 |
if self.tag == 'TruePred': |
| 95 |
return False |
| 96 |
|
| 97 |
|
| 98 |
if self.tag == 'EqualityMatching': |
| 99 |
if other.tag == 'EqualityMatching': |
| 100 |
return False |
| 101 |
if other.tag == 'RangeMatching': |
| 102 |
return True |
| 103 |
if other.tag == 'FreeSizeMatching': |
| 104 |
return True |
| 105 |
if self.tag == 'RangeMatching': |
| 106 |
if other.tag == 'EqualityMatching': |
| 107 |
return False |
| 108 |
if other.tag == 'RangeMatching': |
| 109 |
return False |
| 110 |
if other.tag == 'FreeSizeMatching': |
| 111 |
return True |
| 112 |
if self.tag == 'FreeSizeMatching': |
| 113 |
if other.tag == 'EqualityMatching': |
| 114 |
return False |
| 115 |
if other.tag == 'RangeMatching': |
| 116 |
return False |
| 117 |
if other.tag == 'FreeSizeMatching': |
| 118 |
return False |
| 119 |
|
| 120 |
selfValue = self.value |
| 121 |
otherValue = other.value |
| 122 |
|
| 123 |
if type(selfValue) is dict: |
| 124 |
selfTup = {v for _, v in selfValue.items()} |
| 125 |
selfValue = selfTup |
| 126 |
|
| 127 |
if type(otherValue) is dict: |
| 128 |
otherTup = {v for _, v in otherValue.items()} |
| 129 |
otherValue = otherTup |
| 130 |
|
| 131 |
|
| 132 |
return (self.tag, self.index, selfValue) < (other.tag, other.index, otherValue) |