def create_guard_hole(self, hole_name): """ variable is the name of the variable to update """ signature = [] domains = [] for variable in self.variables: variable_sort = variable.sort() signature.append(variable_sort) if variable.sort() == z3p.IntSort(): domains.append(range(self.variable_ranges[variable][0], self.variable_ranges[variable][1] + 1)) elif variable.sort() == z3p.BoolSort(): domains.append([z3p.BoolVal(False), z3p.BoolVal(True)]) else: raise NotImplementedError("Unimplemented type of variable {}".format(variable_sort)) signature.append(z3p.BoolSort()) f = z3p.Function(hole_name, *signature) return (f, domains, None, None)
def efsm_variable_type(self, variable): a = next(a for a in self.automata if variable in a.variables) if variable.sort() == z3p.BoolSort(): return 'TheLTS->MakeRangeType(0, 1)' elif variable.sort() == z3p.IntSort(): low, high = a.variable_ranges[variable] return "TheLTS->MakeRangeType({}, {})".format(low, high) else: raise NotImplementedError("Channel type cannot be defined for channel {} with sort {}.".format(variable, variable.sort()))
def z3_to_python_value(value): """ is_expr: Return `True` if `a` is a Z3 expression, is_expr(1) is False """ assert z3p.is_expr(value) if z3p.is_int_value(value): return value.as_long() elif value.sort() == z3p.BoolSort(): if z3p.is_true(value): return True elif z3p.is_false(value): return False raise NotImplementedError
def efsm_channel_type(self, channel): if isinstance(channel, z3p.ArrayRef): return "{}Type".format(to_camel_case(channel.decl().name())) if channel.sort() == util.UNIT_SORT: return "RangeType" elif channel.sort() == z3p.BoolSort(): return "Range01Type" elif channel.sort() == z3p.IntSort(): variables = self.reading_variables_for_channel(channel) for v in variables: a = next(a for a in self.automata if v in a.variables) low, high = a.variable_ranges[v] return "Range{}{}Type".format(low, high) else: raise NotImplementedError("Channel type cannot be defined for channel {} with sort {}.".format(channel, channel.sort()))
def lookup_table_for_function(self, function_name): """Returns a model for the function as a dictionary. The dictionary has tuples of argument values as keys. In particular, if the function has a single argument, the key will still be a single element tuple. """ assert (function_name in self.function_names and function_name in self.z3_functions_by_name) z3_function = self.z3_functions_by_name[function_name] model = self.solver.model() table = {} for i in self.function_name_to_domain_values[function_name]: table[i] = model.evaluate(z3_function(i)) # If a value is unconstrained the z3 model might just return # a variable back, therefore for the boolean case if not z3p.is_bool_or_int_value( table[i]) and table[i].sort() == z3p.BoolSort(): # a constant value is returned here. table[i] = z3p.BoolVal(False) return table
def channel_range(self, channel): if isinstance(channel, z3p.ArrayRef): channel_ranges = [] for i in range(self.array_channel_size(channel)): variables = self.reading_variables_for_channel_field(channel[i]) ranges = set([self.automaton_with_variable(v).variable_ranges[v] for v in variables]) assert len(ranges) == 1 channel_ranges.append(ranges) return channel_ranges # return "{}Type".format(to_camel_case(channel.decl().name())) elif channel.sort() == util.UNIT_SORT: return (0, 0) elif channel.sort() == z3p.BoolSort(): return (0, 1) elif channel.sort() == z3p.IntSort(): variables = self.reading_variables_for_channel_or_channel_field(channel) ranges = set([self.automaton_with_variable(v).variable_ranges[v] for v in variables]) assert len(ranges) == 1 return ranges.pop() else: raise NotImplementedError("Channel type cannot be defined for channel {} with sort {}.".format(channel, channel.sort()))
def create_update_hole(self, variable_or_variable_name, hole_name): """ variable is the name of the variable to update """ if isinstance(variable_or_variable_name, str): variable_to_update = next(v for v in self.variables if v.decl().name() == variable_or_variable_name) else: variable_to_update = variable_or_variable_name signature = [] domains = [] for variable in self.variables: variable_sort = variable.sort() signature.append(variable_sort) if variable.sort() == z3p.IntSort(): domains.append([z3p.IntVal(i) for i in range(self.variable_ranges[variable][0], self.variable_ranges[variable][1] + 1)]) elif variable.sort() == z3p.BoolSort(): domains.append([z3p.BoolVal(False), z3p.BoolVal(True)]) else: raise NotImplementedError("Unimplemented type of variable {}".format(variable_sort)) signature.append(variable_to_update.sort()) f = z3p.Function(hole_name, *signature) constraint = z3p.And([z3p.Or([z3p.Eq(f(*arg), z3p.IntVal(result)) for result in range(self.variable_ranges[variable_to_update][0], self.variable_ranges[variable_to_update][1] + 1)]) for arg in itertools.product(*domains)]) return (f, domains, constraint, (variable_to_update, self.variables.index(variable_to_update)))