# Converts a given typename into a corresponding pytamer type.
def _convert_type(self, typename: 'up.model.Type',
user_types_map: Dict['up.model.Type', pytamer.tamer_type]) -> pytamer.tamer_type:
# Handle boolean types.
if typename.is_bool_type():
ttype = self._bool_type
# Handle user-defined types.
elif typename.is_user_type():
ttype = user_types_map[typename]
# Handle integer types with potential bounds.
elif typename.is_int_type():
# Retrieve the lower bound of the integer type.
lb = typename.lower_bound # type: ignore
# Retrieve the upper bound of the integer type.
ub = typename.upper_bound # type: ignore
# Determine the appropriate pytamer integer type based on the presence of bounds.
if lb is None and ub is None:
ttype = pytamer.tamer_integer_type(self._env) # Unbounded integer.
elif lb is None:
ttype = pytamer.tamer_integer_type_lb(self._env, lb) # Lower-bounded integer.
elif ub is None:
ttype = pytamer.tamer_integer_type_ub(self._env, ub) # Upper-bounded integer.
else:
ttype = pytamer.tamer_integer_type_lub(self._env, lb, ub) # Integer with both bounds.
# Handle real (rational) types with potential bounds.
elif typename.is_real_type():
# Retrieve the lower bound of the real type.
lb = typename.lower_bound # type: ignore
# Retrieve the uppper bound of the real type.
ub = typename.upper_bound # type: ignore
# Determine the appropriate pytamer rational type based on the presence of bounds.
if lb is None and ub is None:
ttype = pytamer.tamer_rational_type(self._env) # Unbounded real type.
elif lb is None:
ttype = pytamer.tamer_rational_type_lb(self._env, lb) # Lower-bounded real type.
elif ub is None:
ttype = pytamer.tamer_rational_type_ub(self._env, ub) # Upper-bounded real type.
else:
ttype = pytamer.tamer_rational_type_lub(self._env, lb, ub) # Real type with both bounds.
# Raise an error for unsupported types.
else:
# Type conversion for this type is not implemented.
raise NotImplementedError
# Return the converted pytamer type.
return ttype
This Python function, _convert_type, is a method within a class that converts a given type (typename) into a pytamer.tamer_type. The function takes two arguments: typename of type up.model.Type and user_types_map of type Dict['up.model.Type', pytamer.tamer_type].
The function begins by checking if the typename is a boolean type using the is_bool_type() method. If true, it assigns the self._bool_type to the ttype variable.
If the typename is not a boolean type, it checks if it is a user-defined type using the is_user_type() method. If true, it assigns the corresponding pytamer.tamer_type from the user_types_map dictionary to the ttype variable.
If the typename is not a user-defined type, it checks if it is an integer type using the is_int_type() method. If true, it assigns a pytamer.tamer_integer_type to the ttype variable based on the lower and upper bounds of the integer type. If both bounds are None, it assigns a pytamer.tamer_integer_type without bounds. If only the lower bound is None, it assigns a pytamer.tamer_integer_type_lb with the upper bound. If only the upper bound is None, it assigns a pytamer.tamer_integer_type_ub with the lower bound. If both bounds are not None, it assigns a pytamer.tamer_integer_type_lub with both bounds.
If the typename is not an integer type, it checks if it is a real type using the is_real_type() method. If true, it assigns a pytamer.tamer_rational_type to the ttype variable based on the lower and upper bounds of the real type using the same logic as for integer types.
If the typename does not match any of the above conditions, the function raises a NotImplementedError.
Finally, the function returns the ttype variable, which is the converted pytamer.tamer_type.