def generate_assignments(thresh = 2):

    # create the initial z3 solver
    solver = z3.Solver()
    # declare a block
    block_1 = {'b1_1': 'move', 'b1_2': 'turn_left', 'b1_3': 'move', 'b1_4': 'turn_right', 'b1_5': 'move'}
    block_1_obj = SMT_Block(block_1, thresh)

    # declare the values that each of the variables can take
    X =  [ele.var for ele in block_1_obj.block_z3_vars]

    values = block_1_obj.block_values
    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:
        a = [type_to_str[VariableType(model[ele.var].as_long())]
            for ele in block_1_obj.block_z3_vars
             ]

        assignments.append(a)
        #print(a)



    #print('Found #{} SAT values'.format(len(models)))
    return assignments
Example #2
0
def generate_assignments(thresh=2):

    solver = z3.Solver()
    # declare a block
    block_1 = {
        'b1_1': 'move',
        'b1_2': 'turn_left',
        'b1_3': 'move',
        'b1_4': 'turn_right'
    }
    block_1_obj = SMT_Block(block_1, thresh)

    # declare the values that each of the variables can take
    X = [ele.var for ele in block_1_obj.block_z3_vars]

    # declare the values that each of the variables can take
    values = block_1_obj.block_values

    # additional empty block added in the end of the code
    block_2 = {'b2_1': 'phi'}
    block_2_obj = SMT_Block(block_2, thresh)
    values.extend(block_2_obj.block_values)

    # all block objects
    block_objs = [block_1_obj, block_2_obj]

    # declare the values that each of the variables can take
    X.extend([ele.var for ele in block_2_obj.block_z3_vars])

    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars]

    # declare the constraints on the variables
    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + block_2_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:
        a = ['repeat_until_goal (bool_goal)']
        a.extend(
            [type_to_str[VariableType(model[ele].as_long())] for ele in X])
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_2_vars
        ])
        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
Example #3
0
    def evaluate_exemplars(self, inputs, outputs, instance):
        outcomes = []
        solver = None
        for _input, _output in zip(inputs, outputs):
            outcome = instance.fcn(_input[0])
            # TODO rewrite so solver can be called before needing to evaluate
            if solver is None:
                solver = z3.Solver()
                solver.add(instance.increment == 1)
                _c = solver.check()
                if _c == z3.sat:
                    print(f"Solver model:{solver.model()}")
                else:
                    break

            outcomes.append(outcome == _output[0])

        return outcomes
Example #4
0
 def get_test(solver_input, alternative_input):
     t = None
     kind = None
     s = z3.Solver()
     s.push()
     s.add(solver_input)
     if s.check() == z3.sat:
         t = s.model()
         kind = True
     else:
         # If test cannot be generated with this solver input try alternative
         s.pop()
         s.push()
         s.add(alternative_input)
         if s.check() == z3.sat:
             t = s.model()
             kind = False
     return t, kind
Example #5
0
def check_kills(f_pos, f_neg, mutants, test, aps):
    """ Checks which mutants out of a given list get killed by a given test. """

    cnf = stl_model_to_cnf(test, aps)
    s = z3.Solver()
    count = 1  # Initialized with 1 due to mutant that was used to generate test
    for i in range(0, len(mutants)):
        m = mutants[i]
        if m.killed: continue
        s.push()
        s.add(
            z3.Or(z3.And(f_pos, z3.And(cnf), m.z3_neg),
                  z3.And(f_neg, m.z3_pos, z3.And(cnf))))
        if s.check() == z3.sat:
            mutants[i].killed = True
            count += 1
        s.pop()
    return count
Example #6
0
    def __init__(self, data: Dict[str, List[Any]], code_template) -> None:
        super(FindCharactersSymbolicExecution,
              self).__init__(data, code_template)
        self.solver = z3.Solver()
        self.code_template = """
class Cls:

    def __init__(self):
        self.increment = 0

    def fcn(self, inputs):
        {}

    def run(self, inputs, outputs):
        self.outcomes = evaluate_exemplars(inputs, outputs, self)
        return self.outcomes

instance = Cls()
outcomes = instance.run(inputs, outputs)
"""
        if code_template:
            self.code_template = code_template
Example #7
0
def does_test_kill(original_pos, original_neg, formula_pos, formula_neg, test,
                   cons):
    """
    Checks whether the given test kills the given formula.

    Args:
        original_pos (z3 instance) : Positive encoded temporal logic formula for which test was generated.
        original_neg (z3 instance) : Negative encoded temporal logic formula for which test was generated.
        formula_pos (z3 instance) : Positive encoded temporal logic formula that is checked against the given test.
        formula_neg (z3 instance) : Negative encoded temporal logic formula that is checked against the given test.
        test (z3 model) : Test signal that either does or does not kill the given formula.
        cons (list) : z3 instances of constants used in test and formula.

    Returns:
        bool : True if test kills the given formula, else false.
    """
    s = z3.Solver()
    s.add(
        z3.Or(
            z3.And(original_pos, z3.And(stl_model_to_cnf(test, cons)),
                   formula_neg),
            z3.And(original_neg, formula_pos,
                   z3.And(stl_model_to_cnf(test, cons)))))
    return True if s.check() == z3.sat else False
Example #8
0
 def __init__(self):
     self.converter = Z3Converter()
     self.solver = z3.Solver()
Example #9
0
 def exe_with_path_condition(self,
                             instruction: Instruction,
                             path_condition: list = []) -> PcPointer:
     self._update_all_ref_tracker(instruction)
     if instruction.opcode == "SSTORE":
         # save the value of every referred storage variable before SSTORE
         bak = {}
         for ref in self.reentrancy_references:
             bak[ref] = self._storage[ref.storage_addr]
     if instruction.opcode == "SSTORE" and self.pre_process:
         op0 = self._stack[-1]
         # 在可信条件下进行修改的Storage变量仍然是可信的(immutable)
         caller = z3.Int("Is")
         solver = z3.Solver()
         solver.add(path_condition)
         if "sat" == str(solver.check()):
             for storage_addr, storage_value in self._storage.get_storage(
             ).items():
                 if not utils.in_list(self.mutable_storage_addresses,
                                      storage_addr):
                     # solver.add(caller & 0xffffffffffffffffffff != storage_value & 0xffffffffffffffffffff)
                     mask = 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff
                     if utils.is_symbol(storage_value):
                         # solver.add(z3.Int(str("Is") + "&" + str(mask)) != z3.Int(str(storage_value) + "&" + str(mask)))
                         # solver.add(z3.Int(str(mask) + "&" + str("Is")) != z3.Int(str(storage_value) + "&" + str(mask)))
                         # solver.add(z3.Int(str("Is") + "&" + str(mask)) != z3.Int(str(mask) + "&" + str(storage_value)))
                         # solver.add(z3.Int(str(mask) + "&" + str("Is")) != z3.Int(str(mask) + "&" + str(storage_value)))
                         # solver.add(z3.Int(str("Is")) != z3.Int(str(storage_value)))
                         solver.add(
                             z3.Int(str("Is")) != z3.Int(str(storage_value))
                         )
                     else:
                         # solver.add(z3.Int(str("Is") + "&" + str(mask)) != storage_value & mask)
                         # solver.add(z3.Int(str(mask) + "&" + str("Is")) != storage_value & mask)
                         solver.add(
                             z3.Int(str("Is")) != storage_value & mask)
             if "sat" == str(solver.check()):
                 # caller不为任意一个可信storage变量的时候仍然可能进行SSTORE,则说明被修改的storage变量是不可靠的
                 if not utils.in_list(self.mutable_storage_addresses, op0):
                     self.mutable_storage_addresses.append(op0)
     pc_pointer = super().exe(instruction)
     if instruction.opcode == "SSTORE":
         # check if any referred storage variable is changed after SSTORE
         # if len(bak) != len(self._storage):
         #     # 如果新增了Storage变量,那么一定是做修改了
         #     ref.storage_changed = True
         #     if ref.after_used_in_condition:
         #         ref.changed_after_condition = True
         #     if not ref.after_call:
         #         ref.changed_before_call = True
         # else:
         # 如果Storage变量的个数没变,那么就检查每一个变量的值有没有改变
         for ref, value in bak.items():
             if utils.is_symbol(value) is not utils.is_symbol(self._storage[ref.storage_addr]) or \
                     utils.is_symbol(value) and not eq(simplify(value),
                                                       simplify(self._storage[ref.storage_addr])) or \
                     not utils.is_symbol(value) and value != self._storage[ref.storage_addr]:
                 ref.storage_changed = True
                 if ref.after_used_in_condition:
                     ref.changed_after_condition = True
                 if not ref.after_call:
                     ref.changed_before_call = True
     return pc_pointer
def generate_assignments(thresh=2, id='karel'):
    solver = z3.Solver()
    # declare the SMT variables for the specific code
    # Block 1

    block_1 = {'b1_1': 'put_marker'}
    block_1_obj = SMT_Block(block_1, thresh, id=id)
    values = block_1_obj.block_values
    block_1_vars = [ele.var for ele in block_1_obj.block_z3_vars
                    ]  # for the conditional constraints

    c1 = z3.Int('c1')  # bool_path_ahead (while)
    values.append(z3.Or(c1 == 7,
                        c1 == 12))  # bool_path_ahead, bool_no_path_ahead

    block_2 = {
        'b2_1': 'move',
        'b2_2': 'turn_left',
        'b2_3': 'move',
        'b2_4': 'turn_right',
        'b2_5': 'put_marker'
    }
    block_2_obj = SMT_Block(block_2, thresh, id=id)
    values.extend(block_2_obj.block_values)
    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars
                    ]  # for the conditional constraints

    block_3 = {'b3_1': 'phi'}
    block_3_obj = SMT_Block(block_3, thresh, id=id)
    values.extend(block_3_obj.block_values)
    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars
                    ]  # for the conditional constraints

    # all block objects
    block_objs = [block_1_obj, block_2_obj, block_3_obj]

    X = [c1]
    X.extend([ele.var for ele in block_1_obj.block_z3_vars
              ])  # added the variables for block 1
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2
    X.extend([ele.var for ele in block_3_obj.block_z3_vars
              ])  # added the variables for block 3



    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.flip_marker_constraints+ \
                  block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + \
                  block_2_obj.flip_marker_constraints + block_2_obj.block_elimination_constraints +\
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + \
                  block_3_obj.flip_marker_constraints + block_3_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    constraints.extend([

        # conditional constraints: while(bool_path_ahead)---while block constraints
        z3.Implies(
            c1 == 7,
            z3.Or(
                block_2_vars[0] == 1,
                block_2_vars[1] == 1,
                block_2_vars[2] == 1,
                block_2_vars[3] == 1,
                block_2_vars[4] == 1,
                block_2_vars[5] == 1,
                block_2_vars[6] == 1,
                block_2_vars[7] == 1,
                block_2_vars[8] == 1,
                block_2_vars[9] == 1,
                block_2_vars[10] == 1,
                block_2_vars[11] == 1,
                block_2_vars[12] == 1,
            )),
        z3.Implies(z3.And(c1 == 7, block_2_vars[1] == 1, block_2_vars[0] != 1),
                   z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[2] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[3] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[4] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[5] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[6] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[7] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[8] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3,
                   block_2_vars[7] != 2, block_2_vars[7] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[9] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3,
                   block_2_vars[7] != 2, block_2_vars[7] != 3,
                   block_2_vars[8] != 2, block_2_vars[8] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[10] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1,
                   block_2_vars[9] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3,
                   block_2_vars[7] != 2, block_2_vars[7] != 3,
                   block_2_vars[8] != 2, block_2_vars[8] != 3,
                   block_2_vars[9] != 2, block_2_vars[9] != 3)),

        # # ################################################ THRESH = 2
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[11] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1,
                   block_2_vars[9] != 1, block_2_vars[10] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3,
                   block_2_vars[7] != 2, block_2_vars[7] != 3,
                   block_2_vars[8] != 2, block_2_vars[8] != 3,
                   block_2_vars[9] != 2, block_2_vars[9] != 3,
                   block_2_vars[10] != 2, block_2_vars[10] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[12] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1,
                   block_2_vars[9] != 1, block_2_vars[10] != 1,
                   block_2_vars[11] != 1),
            z3.And(block_2_vars[0] != 2, block_2_vars[0] != 3,
                   block_2_vars[1] != 2, block_2_vars[1] != 3,
                   block_2_vars[2] != 2, block_2_vars[2] != 3,
                   block_2_vars[3] != 2, block_2_vars[3] != 3,
                   block_2_vars[4] != 2, block_2_vars[4] != 3,
                   block_2_vars[5] != 2, block_2_vars[5] != 3,
                   block_2_vars[6] != 2, block_2_vars[6] != 3,
                   block_2_vars[7] != 2, block_2_vars[7] != 3,
                   block_2_vars[8] != 2, block_2_vars[8] != 3,
                   block_2_vars[9] != 2, block_2_vars[9] != 3,
                   block_2_vars[10] != 2, block_2_vars[10] != 3,
                   block_2_vars[11] != 2, block_2_vars[11] != 3)),

        # # conditional constraints: while(bool_no_path_ahead)---while block constraints
        z3.Implies(c1 == 12, block_2_vars[0] != 1),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[1] == 1, block_2_vars[0] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[2] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[3] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[4] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[5] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3,
                  block_2_vars[4] == 2, block_2_vars[4] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[6] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3,
                  block_2_vars[4] == 2, block_2_vars[4] == 3,
                  block_2_vars[5] == 2, block_2_vars[5] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[7] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3,
                  block_2_vars[4] == 2, block_2_vars[4] == 3,
                  block_2_vars[5] == 2, block_2_vars[5] == 3,
                  block_2_vars[6] == 2, block_2_vars[6] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[8] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3,
                  block_2_vars[4] == 2, block_2_vars[4] == 3,
                  block_2_vars[5] == 2, block_2_vars[5] == 3,
                  block_2_vars[6] == 2, block_2_vars[6] == 3,
                  block_2_vars[7] == 2, block_2_vars[7] == 3)),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[9] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3,
                  block_2_vars[4] == 2, block_2_vars[4] == 3,
                  block_2_vars[5] == 2, block_2_vars[5] == 3,
                  block_2_vars[6] == 2, block_2_vars[6] == 3,
                  block_2_vars[7] == 2, block_2_vars[7] == 3,
                  block_2_vars[8] == 2, block_2_vars[8] == 3)),
        z3.Implies(
            z3.And(
                c1 == 12,
                block_2_vars[10] == 1,
                block_2_vars[0] != 1,
                block_2_vars[1] != 1,
                block_2_vars[2] != 1,
                block_2_vars[3] != 1,
                block_2_vars[4] != 1,
                block_2_vars[5] != 1,
                block_2_vars[6] != 1,
                block_2_vars[7] != 1,
                block_2_vars[8] != 1,
                block_2_vars[9] != 1,
            ),
            z3.Or(
                block_2_vars[0] == 2,
                block_2_vars[0] == 3,
                block_2_vars[1] == 2,
                block_2_vars[1] == 3,
                block_2_vars[2] == 2,
                block_2_vars[2] == 3,
                block_2_vars[3] == 2,
                block_2_vars[3] == 3,
                block_2_vars[4] == 2,
                block_2_vars[4] == 3,
                block_2_vars[5] == 2,
                block_2_vars[5] == 3,
                block_2_vars[6] == 2,
                block_2_vars[6] == 3,
                block_2_vars[7] == 2,
                block_2_vars[7] == 3,
                block_2_vars[8] == 2,
                block_2_vars[8] == 3,
                block_2_vars[9] == 2,
                block_2_vars[9] == 3,
            )),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[11] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1,
                   block_2_vars[9] != 1, block_2_vars[10] != 1),
            z3.Or(
                block_2_vars[0] == 2,
                block_2_vars[0] == 3,
                block_2_vars[1] == 2,
                block_2_vars[1] == 3,
                block_2_vars[2] == 2,
                block_2_vars[2] == 3,
                block_2_vars[3] == 2,
                block_2_vars[3] == 3,
                block_2_vars[4] == 2,
                block_2_vars[4] == 3,
                block_2_vars[5] == 2,
                block_2_vars[5] == 3,
                block_2_vars[6] == 2,
                block_2_vars[6] == 3,
                block_2_vars[7] == 2,
                block_2_vars[7] == 3,
                block_2_vars[8] == 2,
                block_2_vars[8] == 3,
                block_2_vars[9] == 2,
                block_2_vars[9] == 3,
                block_2_vars[10] == 2,
                block_2_vars[10] == 3,
            )),
        z3.Implies(
            z3.And(c1 == 12, block_2_vars[12] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1, block_2_vars[4] != 1,
                   block_2_vars[5] != 1, block_2_vars[6] != 1,
                   block_2_vars[7] != 1, block_2_vars[8] != 1,
                   block_2_vars[9] != 1, block_2_vars[10] != 1,
                   block_2_vars[11] != 1),
            z3.Or(
                block_2_vars[0] == 2,
                block_2_vars[0] == 3,
                block_2_vars[1] == 2,
                block_2_vars[1] == 3,
                block_2_vars[2] == 2,
                block_2_vars[2] == 3,
                block_2_vars[3] == 2,
                block_2_vars[3] == 3,
                block_2_vars[4] == 2,
                block_2_vars[4] == 3,
                block_2_vars[5] == 2,
                block_2_vars[5] == 3,
                block_2_vars[6] == 2,
                block_2_vars[6] == 3,
                block_2_vars[7] == 2,
                block_2_vars[7] == 3,
                block_2_vars[8] == 2,
                block_2_vars[8] == 3,
                block_2_vars[9] == 2,
                block_2_vars[9] == 3,
                block_2_vars[10] == 2,
                block_2_vars[10] == 3,
                block_2_vars[11] == 2,
                block_2_vars[11] == 3,
            )),
    ])

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)

    assignments = []
    for model in models:

        a = [
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_1_vars
        ]

        a.append(type_to_str[ConditionalType(model[c1].as_long())])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_2_vars
        ])
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ])

        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
def generate_assignments(thresh = 2, id = 'karel'):

    # create the initial z3 solver
    solver = z3.Solver()
    # declare a block
    block_1 = {'b1_1': 'put_marker', 'b1_2': 'move', 'b1_3': 'turn_left'}

    block_1_obj = SMT_Block(block_1, thresh, id =id )

    values = block_1_obj.block_values

    c1 = z3.Int('c1')  # 4 (repeat)
    values.append(

        z3.Or(c1 == 3, c1 == 4, c1 == 5),

    )

    # declare a block
    block_2 = {'b2_1': 'phi'}

    block_2_obj = SMT_Block(block_2, thresh, id=id)

    values.extend(block_2_obj.block_values)

    # declare a block
    block_3 = {'b3_1': 'phi'}

    block_3_obj = SMT_Block(block_3, thresh, id=id)

    values.extend(block_3_obj.block_values)

    block_objs = [block_1_obj, block_2_obj, block_3_obj]

    # declare the values that each of the variables can take
    X = [ele.var for ele in block_1_obj.block_z3_vars]
    X.append(c1)
    X.extend([ele.var for ele in block_2_obj.block_z3_vars])
    X.extend([ele.var for ele in block_3_obj.block_z3_vars])


    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + \
                  block_1_obj.flip_marker_constraints + block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + \
                  block_2_obj.flip_marker_constraints + block_2_obj.block_elimination_constraints + \
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + \
                  block_3_obj.flip_marker_constraints + block_3_obj.block_elimination_constraints


    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:
        a = [str(model[c1].as_long())]
        a.extend([type_to_str[VariableType(model[ele.var].as_long())]
            for ele in block_1_obj.block_z3_vars
             ])

        a.extend([type_to_str[VariableType(model[ele.var].as_long())]
                  for ele in block_2_obj.block_z3_vars
                  ])

        a.extend([type_to_str[VariableType(model[ele.var].as_long())]
                  for ele in block_3_obj.block_z3_vars
                  ])



        assignments.append(a)
        #print(a)



    #print('Found #{} SAT values'.format(len(models)))
    return assignments
Example #12
0
def generate_assignments(thresh=2, id='karel'):
    solver = z3.Solver()
    # declare the SMT variables for the specific code
    # Block 1
    c0 = z3.Int('c0')  # repeat (8)
    c1 = z3.Int('c1')  # bool_no_marker (if_else)

    block_1 = {'b1_1': 'put_marker'}
    block_1_obj = SMT_Block(block_1, thresh, id=id)
    values = block_1_obj.block_values
    block_1_vars = [ele.var for ele in block_1_obj.block_z3_vars
                    ]  # for the conditional constraints

    block_2 = {'b2_1': 'pick_marker'}
    block_2_obj = SMT_Block(block_2, thresh, id=id)
    values.extend(block_2_obj.block_values)
    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars
                    ]  # for the conditional constraints

    values.append(z3.Or(c0 == 7, c0 == 8, c0 == 9))
    values.append(z3.Or(c1 == 10, c1 == 11))  # bool_no_marker, bool_marker

    block_3 = {'b3_1': 'move'}
    block_3_obj = SMT_Block(block_3, thresh, id=id)
    values.extend(block_3_obj.block_values)
    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars
                    ]  # for the conditional constraints

    block_4 = {'b4_1': 'phi'}
    block_4_obj = SMT_Block(block_4, thresh, id=id)
    values.extend(block_4_obj.block_values)
    block_4_vars = [ele.var for ele in block_4_obj.block_z3_vars
                    ]  # for the conditional constraints

    block_5 = {'b5_1': 'phi'}
    block_5_obj = SMT_Block(block_5, thresh, id=id)
    values.extend(block_5_obj.block_values)
    block_5_vars = [ele.var for ele in block_5_obj.block_z3_vars
                    ]  # for the conditional constraints

    # all block objects
    block_objs = [
        block_1_obj, block_2_obj, block_3_obj, block_4_obj, block_5_obj
    ]

    X = [c0, c1]
    X.extend([ele.var for ele in block_1_obj.block_z3_vars
              ])  # added the variables for block 1
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2
    X.extend([ele.var for ele in block_3_obj.block_z3_vars
              ])  # added the variables for block 3
    X.extend([ele.var for ele in block_4_obj.block_z3_vars
              ])  # added the variables for block 4
    X.extend([ele.var for ele in block_5_obj.block_z3_vars
              ])  # added the variables for block 5



    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.flip_marker_constraints+ \
                  block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + \
                  block_2_obj.flip_marker_constraints+block_2_obj.block_elimination_constraints + \
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + \
                  block_3_obj.flip_marker_constraints+block_3_obj.block_elimination_constraints + \
                  block_4_obj.block_append_constraints + block_4_obj.flip_turns_constraints + \
                  block_4_obj.flip_marker_constraints + block_4_obj.block_elimination_constraints + \
                  block_5_obj.block_append_constraints + block_5_obj.flip_turns_constraints + \
                  block_5_obj.flip_marker_constraints + block_5_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    constraints.extend([

        # conditional constraints: if_else(bool_no_marker)---if block constraints
        z3.Implies(c1 == 11, block_1_vars[0] != 4),
        z3.Implies(
            z3.And(c1 == 11, block_1_vars[1] == 4, block_1_vars[0] != 4),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 5)),
        z3.Implies(
            z3.And(c1 == 11, block_1_vars[2] == 4, block_1_vars[0] != 4,
                   block_1_vars[1] != 4),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 5,
                  block_1_vars[1] == 1, block_1_vars[1] == 5)),
        z3.Implies(
            z3.And(c1 == 11, block_1_vars[3] == 4, block_1_vars[0] != 4,
                   block_1_vars[1] != 4, block_1_vars[2] != 4),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 5,
                  block_1_vars[1] == 1, block_1_vars[1] == 5,
                  block_1_vars[2] == 1, block_1_vars[2] == 5)),
        z3.Implies(
            z3.And(c1 == 11, block_1_vars[4] == 4, block_1_vars[0] != 4,
                   block_1_vars[1] != 4, block_1_vars[2] != 4,
                   block_1_vars[3] != 4),
            z3.And(block_1_vars[0] == 1, block_1_vars[0] == 5,
                   block_1_vars[1] == 1, block_1_vars[1] == 5,
                   block_1_vars[2] == 1, block_1_vars[2] == 5,
                   block_1_vars[3] == 1, block_1_vars[3] == 5)),

        # else block constraints
        z3.Implies(c1 == 11, block_2_vars[0] != 5),
        z3.Implies(
            z3.And(c1 == 11, block_2_vars[1] == 5, block_2_vars[0] != 5),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 4)),
        z3.Implies(
            z3.And(c1 == 11, block_2_vars[2] == 5, block_2_vars[0] != 5,
                   block_2_vars[1] != 5),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 4,
                  block_2_vars[1] == 1, block_2_vars[1] == 4)),
        z3.Implies(
            z3.And(c1 == 11, block_2_vars[3] == 5, block_2_vars[0] != 5,
                   block_2_vars[1] != 5, block_2_vars[2] != 5),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 4,
                  block_2_vars[1] == 1, block_2_vars[1] == 4,
                  block_2_vars[2] == 1, block_2_vars[2] == 4)),
        z3.Implies(
            z3.And(c1 == 11, block_2_vars[4] == 5, block_2_vars[0] != 5,
                   block_2_vars[1] != 5, block_2_vars[2] != 5,
                   block_2_vars[3] != 5),
            z3.And(block_2_vars[0] == 1, block_2_vars[0] == 4,
                   block_2_vars[1] == 1, block_2_vars[1] == 4,
                   block_2_vars[2] == 1, block_2_vars[2] == 4,
                   block_2_vars[3] == 1, block_2_vars[3] == 4)),

        # conditional constraints: if_else(bool_marker)---if block constraints
        z3.Implies(c1 == 10, block_1_vars[0] != 5),
        z3.Implies(
            z3.And(c1 == 10, block_1_vars[1] == 5, block_1_vars[0] != 5),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 4)),
        z3.Implies(
            z3.And(c1 == 10, block_1_vars[2] == 5, block_1_vars[0] != 5,
                   block_1_vars[1] != 5),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 4,
                  block_1_vars[1] == 1, block_1_vars[1] == 4)),
        z3.Implies(
            z3.And(c1 == 10, block_1_vars[3] == 5, block_1_vars[0] != 5,
                   block_1_vars[1] != 5, block_1_vars[2] != 5),
            z3.Or(block_1_vars[0] == 1, block_1_vars[0] == 4,
                  block_1_vars[1] == 1, block_1_vars[1] == 4,
                  block_1_vars[2] == 1, block_1_vars[2] == 4)),
        z3.Implies(
            z3.And(c1 == 10, block_1_vars[4] == 5, block_1_vars[0] != 5,
                   block_1_vars[1] != 5, block_1_vars[2] != 5,
                   block_1_vars[3] != 5),
            z3.And(block_1_vars[0] == 1, block_1_vars[0] == 4,
                   block_1_vars[1] == 1, block_1_vars[1] == 4,
                   block_1_vars[2] == 1, block_1_vars[2] == 4,
                   block_1_vars[3] == 1, block_1_vars[3] == 4)),

        # else block constraints
        z3.Implies(c1 == 10, block_2_vars[0] != 4),
        z3.Implies(
            z3.And(c1 == 10, block_2_vars[1] == 4, block_2_vars[0] != 4),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 5)),
        z3.Implies(
            z3.And(c1 == 10, block_2_vars[2] == 4, block_2_vars[0] != 4,
                   block_2_vars[1] != 4),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 5,
                  block_2_vars[1] == 1, block_2_vars[1] == 5)),
        z3.Implies(
            z3.And(c1 == 10, block_2_vars[3] == 4, block_2_vars[0] != 4,
                   block_2_vars[1] != 4, block_2_vars[2] != 4),
            z3.Or(block_2_vars[0] == 1, block_2_vars[0] == 5,
                  block_2_vars[1] == 1, block_2_vars[1] == 5,
                  block_2_vars[2] == 1, block_2_vars[2] == 5)),
        z3.Implies(
            z3.And(c1 == 10, block_2_vars[4] == 4, block_2_vars[0] != 4,
                   block_2_vars[1] != 4, block_2_vars[2] != 4,
                   block_2_vars[3] != 4),
            z3.And(block_2_vars[0] == 1, block_2_vars[0] == 5,
                   block_2_vars[1] == 1, block_2_vars[1] == 5,
                   block_2_vars[2] == 1, block_2_vars[2] == 5,
                   block_2_vars[3] == 1, block_2_vars[3] == 5)),
    ])

    unequal_blocks_con = block_unequal_constraint(block_1_obj, block_2_obj)
    constraints.extend(unequal_blocks_con)
    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)

    assignments = []
    for model in models:
        a = [
            str(model[c0].as_long()),
            type_to_str[ConditionalType(model[c1].as_long())]
        ]

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_1_vars
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_2_vars
        ])
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ])
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_4_vars
        ])
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_5_vars
        ])

        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
Example #13
0
def generate_assignments(thresh=2):
    solver = z3.Solver()
    # declare the SMT variables for the specific code
    # Block 1
    block_1 = {'b1_1': 'move'}
    block_1_obj = SMT_Block(block_1, thresh)
    values = block_1_obj.block_values
    block_1_vars = [ele.var for ele in block_1_obj.block_z3_vars
                    ]  # for the conditional constraints

    c1 = z3.Int('c1')  # bool_path_ahead (if_else)

    block_2 = {'b2_1': 'turn_left'}
    block_2_obj = SMT_Block(block_2, thresh)
    values.extend(block_2_obj.block_values)
    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars
                    ]  # for the conditional constraints

    values.append(c1 == 7)  # same conditional value

    block_3 = {'b3_1': 'phi'}
    block_3_obj = SMT_Block(block_3, thresh)
    values.extend(block_3_obj.block_values)
    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars
                    ]  # for the conditional constraints

    # all block objects
    block_objs = [block_1_obj, block_2_obj, block_3_obj]

    X = [c1]
    X.extend([ele.var for ele in block_1_obj.block_z3_vars
              ])  # added the variables for block 1
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2
    X.extend([ele.var for ele in block_3_obj.block_z3_vars
              ])  # added the variables for block 2


    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + block_2_obj.block_elimination_constraints + \
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + block_3_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    constraints.extend([

        # conditional constraints: if_else(bool_path_ahead)---if block constraints
        z3.Implies(
            c1 == 7,
            z3.Or(
                block_1_vars[0] == 1,
                block_1_vars[1] == 1,
                block_1_vars[2] == 1,
                block_1_vars[3] == 1,
                block_1_vars[4] == 1,
            )),
        z3.Implies(z3.And(c1 == 7, block_1_vars[1] == 1, block_1_vars[0] != 1),
                   z3.And(block_1_vars[0] != 2, block_1_vars[0] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_1_vars[2] == 1, block_1_vars[0] != 1,
                   block_1_vars[1] != 1),
            z3.And(block_1_vars[0] != 2, block_1_vars[0] != 3,
                   block_1_vars[1] != 2, block_1_vars[1] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_1_vars[3] == 1, block_1_vars[0] != 1,
                   block_1_vars[1] != 1, block_1_vars[2] != 1),
            z3.And(block_1_vars[0] != 2, block_1_vars[0] != 3,
                   block_1_vars[1] != 2, block_1_vars[1] != 3,
                   block_1_vars[2] != 2, block_1_vars[2] != 3)),
        z3.Implies(
            z3.And(c1 == 7, block_1_vars[4] == 1, block_1_vars[0] != 1,
                   block_1_vars[1] != 1, block_1_vars[2] != 1,
                   block_1_vars[3] != 1),
            z3.And(block_1_vars[0] != 2, block_1_vars[0] != 3,
                   block_1_vars[1] != 2, block_1_vars[1] != 3,
                   block_1_vars[2] != 2, block_1_vars[2] != 3,
                   block_1_vars[3] != 2, block_1_vars[3] != 3)),

        # else block constraints
        z3.Implies(c1 == 7, block_2_vars[0] != 1),
        z3.Implies(z3.And(c1 == 7, block_2_vars[1] == 1, block_2_vars[0] != 1),
                   z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[2] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[3] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3)),
        z3.Implies(
            z3.And(c1 == 7, block_2_vars[4] == 1, block_2_vars[0] != 1,
                   block_2_vars[1] != 1, block_2_vars[2] != 1,
                   block_2_vars[3] != 1),
            z3.Or(block_2_vars[0] == 2, block_2_vars[0] == 3,
                  block_2_vars[1] == 2, block_2_vars[1] == 3,
                  block_2_vars[2] == 2, block_2_vars[2] == 3,
                  block_2_vars[3] == 2, block_2_vars[3] == 3)),
    ])

    unequal_blocks_con = block_unequal_constraint(block_1_obj, block_2_obj)
    constraints.extend(unequal_blocks_con)
    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)

    assignments = []
    for model in models:
        a = [
            'repeat_until_goal(bool_goal)',
            type_to_str[ConditionalType(model[c1].as_long())]
        ]

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[1:block_1_obj.size + 1]
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[block_1_obj.size + 1:block_2_obj.size +
                         block_1_obj.size + 1]
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ])

        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
Example #14
0
def graph_mapping(g1: Graph, g2: Graph):
    s = z3.Solver()
    g1_z3 = GraphZ3(s, g1)
    g2_z3 = GraphZ3(s, g2)
    return z3_graph_mapping(s, g1_z3, g2_z3)
Example #15
0
 def __init__(self, *args, **kw):
     Context.__init__(self, *args, **kw)
     self.solver = z3.Solver()
Example #16
0
def generate_assignments(thresh=2):

    solver = z3.Solver()
    # Block 1
    block_1 = {'b1_1': 'move'}
    block_1_obj = SMT_Block(block_1, thresh)
    values = block_1_obj.block_values
    block_1_vars = [ele.var for ele in block_1_obj.block_z3_vars
                    ]  # for the conditional constraints

    c1 = z3.Int('c1')  # bool_path_left (if_only)
    values.append(z3.Or(c1 == 8, c1 == 9))

    block_2 = {'b2_1': 'turn_left'}
    block_2_obj = SMT_Block(block_2, thresh)
    values.extend(block_2_obj.block_values)
    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars
                    ]  # for the conditional constraints

    block_3 = {'b3_1': 'phi'}
    block_3_obj = SMT_Block(block_3, thresh)
    values.extend(block_3_obj.block_values)
    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars
                    ]  # for the conditional constraints

    # all block objects
    block_objs = [block_1_obj, block_2_obj, block_3_obj]

    X = [ele.var for ele in block_1_obj.block_z3_vars
         ]  # added the variables for block 1
    X.append(c1)  # added the conditional variable
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2
    X.extend([ele.var for ele in block_3_obj.block_z3_vars
              ])  # added the variables for block 3

    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + block_2_obj.block_elimination_constraints + \
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + block_3_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    constraints.extend([

        # conditional constraints: if(path_left)
        z3.Implies(c1 == 8, z3.Or(block_2_vars[0] == 2, block_2_vars[1] == 2,
                                  block_2_vars[2] == 2, block_2_vars[3] == 2,
                                  block_2_vars[4] == 2,

                                  )),
        z3.Implies(z3.And(c1 == 8, block_2_vars[1] == 2, block_2_vars[0] != 2), z3.And(block_2_vars[0] != 1, block_2_vars[0] != 3 )),
        z3.Implies(z3.And(c1 == 8, block_2_vars[2] == 2, block_2_vars[0] != 2, block_2_vars[1] != 2),
                   z3.And(block_2_vars[0] != 1, block_2_vars[0] != 3, block_2_vars[1] != 1, block_2_vars[1] != 3)),
        z3.Implies(z3.And(c1 == 8, block_2_vars[3] == 2, block_2_vars[0] != 2,
                          block_2_vars[1] != 2, block_2_vars[2] != 2), z3.And(block_2_vars[0] != 1, block_2_vars[0] != 3, block_2_vars[1] != 1, \
                                                                 block_2_vars[1] != 3, block_2_vars[2] != 1, block_2_vars[2] != 3)),
        z3.Implies(z3.And(c1 == 8, block_2_vars[4] == 2, block_2_vars[0] != 2,
                          block_2_vars[1] != 2, block_2_vars[2] != 2, block_2_vars[3] != 2), z3.And(block_2_vars[0] != 1, block_2_vars[0] != 3,
                                                                 block_2_vars[1] != 1, block_2_vars[1] != 3,
                                                                 block_2_vars[2] != 1, block_2_vars[2] != 3,
                                                                 block_2_vars[3] != 1, block_2_vars[3] != 3)),



        # conditional constraints: if(path_right)
        z3.Implies(c1 == 9, z3.Or(block_2_vars[0] == 3, block_2_vars[1] == 3,
                                  block_2_vars[2] == 3, block_2_vars[3] == 3,
                                  block_2_vars[4] == 3,

                                  )),
        z3.Implies(z3.And(c1 == 9, block_2_vars[1] == 3, block_2_vars[0] != 3,
                          ), z3.And(block_2_vars[0] != 1, block_2_vars[0] != 2)),
        z3.Implies(z3.And(c1 == 9, block_2_vars[2] == 3, block_2_vars[0] != 3,
                          block_2_vars[1] != 3), z3.And(block_2_vars[0]!= 1, block_2_vars[0] != 2,
                                                                 block_2_vars[1] != 1, block_2_vars[1] != 2)),
        z3.Implies(z3.And(c1 == 9, block_2_vars[3] == 3, block_2_vars[0] != 3,
                          block_2_vars[1] != 3, block_2_vars[2] != 3),
                   z3.And(block_2_vars[0] != 1, block_2_vars[0] != 2,
                          block_2_vars[1] != 1, block_2_vars[1] != 2,
                          block_2_vars[2] != 1, block_2_vars[2] != 2)),
        z3.Implies(z3.And(c1 == 9, block_2_vars[4] == 3, block_2_vars[0] != 3,
                          block_2_vars[1] != 3, block_2_vars[2] != 3, block_2_vars[3] != 3 ),
                   z3.And(block_2_vars[0] != 1, block_2_vars[0] != 2,
                          block_2_vars[1] != 1, block_2_vars[1] != 2,
                          block_2_vars[2] != 1, block_2_vars[2] != 2,
                          block_2_vars[3] != 1, block_2_vars[3] != 2)),

    ])

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:
        a = ['repeat_until_goal(bool_goal)']
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[:block_1_obj.size]
        ])

        a.append(type_to_str[ConditionalType(model[c1].as_long())])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[block_1_obj.size + 1:]
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ])
        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
def generate_assignments(thresh=2):

    solver = z3.Solver()
    # declare the SMT variables for the specific code
    block_1 = {'b1_1': 'turn_right'}
    block_1_obj = SMT_Block(block_1, thresh)
    values = block_1_obj.block_values

    c1 = z3.Int('c1')  # 5 (repeat)
    values.append(z3.Or(c1 == 4, c1 == 5, c1 == 6), )

    block_2 = {'b2_1': 'move'}
    block_2_obj = SMT_Block(block_2, thresh)
    values.extend(block_2_obj.block_values)

    # additional empty block added in the end of the code
    block_3 = {'b3_1': 'phi'}
    block_3_obj = SMT_Block(block_3, thresh)
    values.extend(block_3_obj.block_values)

    # all block objects
    block_objs = [block_1_obj, block_2_obj, block_3_obj]

    X = [ele.var for ele in block_1_obj.block_z3_vars
         ]  # added the variables for block 1
    X.append(c1)  # added the conditional variable
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2

    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars]
    X.extend(block_3_vars)  # added the variables for block 3


    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints  \
                  + block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + block_2_obj.block_elimination_constraints \
                  + block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + block_3_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:
        a = [
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[:block_1_obj.size]
        ]
        a.append(str(model[c1].as_long()))

        b = [
            type_to_str[VariableType(model[ele].as_long())]
            for ele in X[block_2_obj.size + 1:]
        ]
        a.extend(b)

        c = [
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ]
        a.extend(c)
        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments
def generate_assignments(thresh=2):

    solver = z3.Solver()
    # declare the SMT variables for the specific code
    block_1 = {'b1_1': 'move'}
    block_1_obj = SMT_Block(block_1, thresh)
    values = block_1_obj.block_values
    block_1_vars = [ele.var for ele in block_1_obj.block_z3_vars
                    ]  # for the conditional constraints

    c1 = z3.Int('c1')  # 4 (repeat)
    values.append(z3.Or(c1 == 3, c1 == 4, c1 == 5), )

    block_2 = {'b2_1': 'turn_left'}
    block_2_obj = SMT_Block(block_2, thresh)
    values.extend(block_2_obj.block_values)
    block_2_vars = [ele.var for ele in block_2_obj.block_z3_vars
                    ]  # for the conditional constraints

    c2 = z3.Int('c2')  # 5 (repeat)
    values.append(z3.Or(c2 == 4, c2 == 5, c2 == 6), )

    block_3 = {'b3_1': 'move'}
    block_3_obj = SMT_Block(block_3, thresh)
    values.extend(block_3_obj.block_values)
    block_3_vars = [ele.var for ele in block_3_obj.block_z3_vars
                    ]  # for the conditional constraints

    # add another block in the beginning of the code
    block_4 = {'b4_1': 'phi'}
    block_4_obj = SMT_Block(block_4, thresh)
    values.extend(block_4_obj.block_values)
    block_4_vars = [ele.var for ele in block_4_obj.block_z3_vars]

    # add another block in the end of the code
    block_5 = {'b5_1': 'phi'}
    block_5_obj = SMT_Block(block_5, thresh)
    values.extend(block_5_obj.block_values)
    block_5_vars = [ele.var for ele in block_5_obj.block_z3_vars]

    # all block objects
    block_objs = [
        block_1_obj, block_2_obj, block_3_obj, block_4_obj, block_5_obj
    ]

    X = [ele.var for ele in block_1_obj.block_z3_vars
         ]  # added the variables for block 1
    X.append(c1)  # added the conditional variable
    X.extend([ele.var for ele in block_2_obj.block_z3_vars
              ])  # added the variables for block 2
    X.append(c2)  # added the conditional variable
    X.extend([ele.var for ele in block_3_obj.block_z3_vars
              ])  # added the variables for block 3

    X.extend([ele.var for ele in block_4_obj.block_z3_vars
              ])  # added the variables for block 4
    X.extend([ele.var for ele in block_5_obj.block_z3_vars
              ])  # added the variables for block 5


    constraints = block_1_obj.block_append_constraints + block_1_obj.flip_turns_constraints + block_1_obj.block_elimination_constraints + \
                  block_2_obj.block_append_constraints + block_2_obj.flip_turns_constraints + block_2_obj.block_elimination_constraints + \
                  block_3_obj.block_append_constraints + block_3_obj.flip_turns_constraints + block_3_obj.block_elimination_constraints + \
                  block_4_obj.block_append_constraints + block_4_obj.flip_turns_constraints + block_4_obj.block_elimination_constraints + \
                  block_5_obj.block_append_constraints + block_5_obj.flip_turns_constraints + block_5_obj.block_elimination_constraints

    single_block_change_cons = single_block_change(block_objs)

    constraints.extend(single_block_change_cons)

    # add the values and the constraints
    solver.add(values + constraints)

    # generate all the assignments
    models = gen_all(solver, X)
    assignments = []
    for model in models:

        a = [str(model[c1].as_long())]
        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_1_vars
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_2_vars
        ])

        a.append(str(model[c2].as_long()))

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_3_vars
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_4_vars
        ])

        a.extend([
            type_to_str[VariableType(model[ele].as_long())]
            for ele in block_5_vars
        ])

        assignments.append(a)
        #print(a)

    #print('Found #{} SAT values'.format(len(models)))
    return assignments