def _list_contains(self, other): # for being able to use 'in' when expressing extension constraints if not OpOverrider.activated: return self.__contains__(other) if isinstance(other, types.GeneratorType): other = list(other) if is_containing(other, Variable) and len(self) > 0 and isinstance(self[0], (list, tuple, int)): queue_in.append((self, other)) return True if is_containing(other, Variable) and len(self) == 0: return other in set(self) error_if(is_containing(other, Variable), "It seems that you should use a set and not a list, as in x in {...}." + " Your arguments are " + str(other) + " " + str(self)) if isinstance(other, int) and (is_1d_list(self, Variable) or is_1d_tuple(self, Variable)): # member/element constraint queue_in.append((self, other)) return True return self.__contains__(other)
def _enumerate_contains(self, other): if not OpOverrider.activated: return self.__contains__(other) if is_containing(other, Variable): tmp = list(self) if len(tmp) > 0 and isinstance(tmp[0], (tuple, int)): queue_in.append((tmp, other)) return True return self.__contains__(other)
def _compact_constraint_arguments(arguments): for arg in list(arguments.values()): if isinstance(arg.content, list) and len(arg.content) > 0 and arg.content_compressible: if not isinstance(arg.content[0], list): # It is only one list arg.content = compact(arg.content, preserve_order=arg.content_ordered) elif arg.lifted is True: arg.content = [compact(l, preserve_order=arg.content_ordered) for l in arg.content] elif arg.name == TypeCtrArg.MATRIX: # Special case for matrix sc = None if is_containing(arg.content_compressible, int) else _simple_compact(flatten(arg.content_compressible)) arg.content = sc if sc is not None else arg.content
def _tuple_contains(self, other): if not OpOverrider.activated: return self.__contains__(other) if is_containing(other, Variable) and len(self) > 0 and isinstance(self[0], (tuple, int)): queue_in.append((list(self), other)) return True if isinstance(other, int) and (is_1d_list(self, Variable) or is_1d_tuple(self, Variable)): # member/element constraint queue_in.append((self, other)) return True return self.__contains__(other)
def get_item_with_name(s): if '[' in s: # we need to look for arrays pos = s.index("[") prefix, suffix = s[:pos], s[pos:] assert prefix in VarEntities.prefixToEVarArray va = VarEntities.prefixToEVarArray[prefix] indexes = [ int(v) if len(v) > 0 else None for v in re.split("\]\[", suffix[1:-1]) ] if is_containing(indexes, int): res = va.variables for v in indexes: res = res[v] return res else: assert is_containing(indexes, type(None)) return va else: for item in VarEntities.items: if isinstance(item, EVar) and item.id == s: return item return None
def _set_contains(self, other): # for being able to use 'in' when expressing intension/extension constraints if not OpOverrider.activated: return self.__contains__(other) if isinstance(other, types.GeneratorType): other = list(other) tself = unique_type_in(self) # if isinstance(other, Variable) and len(self) > 0 and is_containing(self, int): # unary table constraint if isinstance(other, Variable) and tself in {int, str}: # unary table constraint queue_in.append((list(self), other)) return True # if isinstance(other, (Variable, PartialConstraint)) or isinstance(other, (int, str)) and is_containing(self, Variable): # intension constraint if isinstance(other, (Variable, PartialConstraint)) or isinstance(other, (int, str)) and tself and issubclass(tself, Variable): # intension constraint queue_in.append((self, other)) return True # if is_1d_tuple(other, Variable) or is_1d_list(other, Variable): # non-unary table constraint # queue_in.append((list(self), other)) # return True if is_containing(other, Variable): # non-unary table constraint queue_in.append((list(self), flatten(other))) return True return self.__contains__(other)
def _compact_constraint_group(group): preserve_order = False cnt = 0 if isinstance(group.abstraction, dict): for key, value in group.abstraction.items(): if "%" in str(value): cnt += 1 argument = group.entities[0].constraint.arguments[key] if argument.content_compressible: if "%" not in str(value): if key == TypeCtrArg.MATRIX: # Special case for matrix sc = None if is_containing(argument.content_compressible, int) else _simple_compact(flatten(argument.content_compressible)) group.abstraction[key] = sc if sc is not None else group.abstraction[key] else: group.abstraction[key] = compact(value, preserve_order=argument.content_ordered) elif argument.content_ordered is True: preserve_order = True else: preserve_order = True if cnt > 1: preserve_order = True for i in range(len(group.all_args)): group.all_args[i] = compact(group.all_args[i], preserve_order=preserve_order, group_args=True)
def __mul__(self, other): assert is_containing(self, (Variable, Node)) return ScalarProduct(self, list(other) if isinstance(other, (tuple, range)) else other)
def __mul__(self, other): if is_containing(other, (Variable, Node)): return ScalarProduct(other, self) assert is_containing(self, (Variable, Node)) return ScalarProduct(self, other)
def __contains__li(self, other): if is_containing(other, Variable) and len(self) > 0 and isinstance(self[0], (tuple, int)): queue_in.append((self, other)) return True return list.__contains__(self, other)
def _list_mul(self, other): # for being able to use scalar products if is_containing(self, (Variable, Node), check_first_only=True): return ScalarProduct(self, other) return list.__mul__(self, other)
def _tuple_mul(self, other): # for being able to use scalar products if is_containing(self, (Variable, Node), check_first_only=True): return ScalarProduct(self, other) if is_containing(self, int) and isinstance(other, (list, tuple)) and is_containing(other, (Variable, Node), check_first_only=True): return ScalarProduct(other, self) return tuple.__mul__(self, other)
def is_containing_hole(self): if self.containing_hole is None: self.containing_hole = is_containing(self.variables, type(None), check_first_only=True) return self.containing_hole