def test_recieve_input(self):
     player = AIPlayer()
     game = Checkers(player, Player())
     options = game.possible_movements()
     action = player.recieve_input(options)
     self.assertGreaterEqual(action, 0)
     self.assertLessEqual(action, len(options))
 def test_has_ended(self):
     player1 = Player()
     player2 = Player()
     game = Checkers(player1, player2)
     for cell in game.board.cells:
         cell.value = None
     self.assertTrue(game.has_ended())
 def test_play(self, input):
     player = CheckersPlayer()
     game = Checkers(player, CheckersPlayer())
     game.player_in_turn = 0
     self.assertTrue(game.board.get_cell(5, 0).value == "x")
     player.play()
     self.assertTrue(game.board.get_cell(4, 1).value == "x")
     self.assertTrue(game.board.get_cell(5, 0).value is None)
Esempio n. 4
0
    def test_single_jump(self):
        c = Checkers()

        # move a red piece in a position where black can jump
        c.move(src=(1, 2), dest=(2, 3))
        c.move(src=(2, 3), dest=(3, 4))

        # make sure the destination is empty
        self.assertTrue(c.board[3][4] is None)
        # jump a black piece over the red piece
        c.move(src=(2, 5), dest=(4, 3))
        # make sure the piece is at the destination
        self.assertTrue(c.board[3][4].color is Checkers.colors['black'])
        # make sure the jumped piece is removed from the board
        self.assertTrue(c.board[4][3] is None)
Esempio n. 5
0
 def __init__(self):
     self._model = Checkers()
     self.to_move = None
     self.moves = []
     self.description = ''
     self.black_men = []
     self.white_men = []
     self.black_kings = []
     self.white_kings = []
     self.flip_board = False
     self.num_players = 1
     self._move_check = False
     self._bm_check = False
     self._bk_check = False
     self._wm_check = False
     self._wk_check = False
Esempio n. 6
0
    def __init__(self):
        gtk.DrawingArea.__init__(self)
        
        self.tile_size = 100
        self.tile_count = 8
        self.w = self.h = self.tile_count * self.tile_size
        
        cmap = self.get_colormap()
        self.color_black = cmap.alloc_color('#000000')
        self.color_white = cmap.alloc_color('#FFFFFF')    
        self.color_red   = cmap.alloc_color('#BB3333')
        self.color_green = cmap.alloc_color('#33DD33')
        self.set_size_request(320, 320)

        self.connect("expose_event", self.exposeEvent)
        self.connect("size-allocate", self.__sizeAllocate)
        self.connect("button_press_event", self.buttonPressEvent)
        self.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)

        #TODO: use settings
        self.tile_w = gtk.gdk.pixbuf_new_from_file("images/tile_w.jpg")
        self.tile_b = gtk.gdk.pixbuf_new_from_file("images/tile_b.jpg")
        self.selector = gtk.gdk.pixbuf_new_from_file("images/selector.png")

        self.checkers = Checkers()
        self.checkers.setSelected((2,5))
Esempio n. 7
0
    def __init__(self, **props):
        self.model = Checkers()
        self._root = props['root']
        self.parent = props['parent']
        self.training = props['training']  # If true, game will be played
        # against an evolved GPController
        statusbar = Label(self._root,
                          relief=SUNKEN,
                          font=('Helvetica', 7),
                          anchor=NW)
        statusbar.pack(side='bottom', fill='x')
        self.view = BoardView(self._root,
                              model=self.model,
                              parent=self,
                              statusbar=statusbar,
                              invisible=True)
        if self.training:
            self.view.override_canvas(TrainingCanvas())
            self.fitness = props['fitness']

        self.player_color = BLACK
        self.num_players = 1
        self.set_controllers()
        self._controller1.start_turn()
        self.filename = None
Esempio n. 8
0
class TestCoder(unittest.TestCase):
    def setUp(self):
        # Init coder
        print("Initializing coder...")
        self.checker = self.checkerList[self.checkerIndex]
        self.dictionary = Dictionary(self.checker)
        self.coder = Coder(self.dictionary)
        # Load all data from DB
        print("Fetching data from database...")
        self.allData = self.db.getFixDataForChecker(self.checker)
        self.allDataLen = len(self.allData)
        print("Done, fetched {0} records".format(self.allDataLen))

    def tearDown(self):
        self.checkerIndex += 1

    @classmethod
    def setUpClass(self):
        print("Starting up...")
        self.db = CFDatabase(config.getCfDbFile())
        self.checkers = Checkers()
        self.checkerList = ['deadcode.DeadStores']
        self.checkerIndex = 0

    def testDeadcodeDeadStores(self):
        self.assertTrue(self.allDataLen > 0, msg="No data found")

        # Encode all data
        print("Testing encoding")
        i = 0
        while i < self.allDataLen:
            checkerInfo = self.checkers.extractTokensForChecker(
                self.checker, self.allData[i][4])
            encodedBugData, initialUnkList = self.coder.encode(
                self.allData[i][1], checkerData=checkerInfo)
            encodedFixData, finalUnkList = self.coder.encode(
                self.allData[i][2], unkList=initialUnkList, reverse=False)
            if -1 in encodedBugData:
                print(
                    "{0}: [{2} - {3} ({1})] Some tokens were not parsed (bug), ignoring (lenUnk = {1})"
                    .format(i + 1, len(finalUnkList), len(encodedBugData),
                            len(encodedFixData)))
            elif -1 in encodedFixData:
                print(
                    "{0}: [{2} - {3} ({1})] Some tokens were not parsed (fix), ignoring (lenUnk = {1})"
                    .format(i + 1, len(finalUnkList), len(encodedBugData),
                            len(encodedFixData)))
            else:
                print("{0}: [{2} - {3} ({1})] Done (lenUnk = {1})".format(
                    i + 1, len(finalUnkList), len(encodedBugData),
                    len(encodedFixData)))
                textBug = self.coder.decode(encodedBugData, finalUnkList, True)
                textFix = self.coder.decode(encodedFixData, finalUnkList)
                self.assertEqual(textBug, self.allData[i][1])
                self.assertEqual(textFix, self.allData[i][2])
            i += 1

        print("All done.")
 def test_get_movement(self):
     player1 = Player()
     player2 = Player()
     game = Checkers(player1, player2)
     for cell in game.board.cells:
         cell.value = None
     game.board.set_cell(7, 4, "x")
     game.board.set_cell(6, 3, "o")
     game.board.set_cell(4, 3, "o")
     game.board.set_cell(2, 3, "o")
     game.board.set_cell(0, 3, "o")
     game.board.set_cell(6, 5, "o")
     game.board.set_cell(4, 5, "o")
     game.board.set_cell(2, 5, "o")
     game.board.set_cell(0, 5, "o")
     movements = list(game._get_movement(7, 4, ["LU", "RU"], ["o", "O"]))
     self.assertEqual([((1, 2), [(2, 3), (4, 3), (6, 3)]),
                       ((1, 6), [(2, 5), (4, 3), (6, 3)]),
                       ((1, 2), [(2, 3), (4, 5), (6, 5)]),
                       ((1, 6), [(2, 5), (4, 5), (6, 5)])], movements)
     game.board.set_cell(7, 4, None)
     game.board.set_cell(5, 6, "X")
     game.board.set_cell(6, 1, "o")
     game.board.set_cell(4, 1, "o")
     game.board.set_cell(2, 1, "o")
     game.board.set_cell(0, 1, "o")
     game.board.set_cell(6, 7, "o")
     game.board.set_cell(4, 7, "o")
     game.board.set_cell(2, 7, "o")
     game.board.set_cell(0, 7, "o")
     movements = list(
         game._get_movement(5, 6, ["LU", "RU", "LD", "RD"], ["o", "O"]))
     self.assertEqual([((1, 6), [(2, 5), (4, 3), (4, 1), (2, 1), (2, 3),
                                 (4, 5)]),
                       ((7, 0), [(6, 1), (4, 1), (2, 1), (2, 3), (4, 5)]),
                       ((5, 6), [(6, 5), (6, 3), (4, 1), (2, 1), (2, 3),
                                 (4, 5)]), ((1, 6), [(2, 5), (4, 5)]),
                       ((7, 4), [(6, 5)])], movements)
Esempio n. 10
0
 def new_game(self):
     self._stop_updates()
     self._save_curr_game_if_needed()
     self.filename = None
     self._root.title('Raven ' + VERSION)
     self.model = Checkers()
     self.player_color = BLACK
     self.view.reset_view(self.model)
     self.think_time = self.parent.thinkTime.get()
     self.set_controllers()
     self.view.update_statusbar()
     self.view.reset_toolbar_buttons()
     self.view.curr_annotation = ''
     self.controller1.start_turn()
Esempio n. 11
0
 def __init__(self, **props):
     self.model = Checkers()
     self._root = props['root']
     self.parent = props['parent']
     statusbar = Label(self._root,
                       relief=SUNKEN,
                       font=('Helvetica', 7),
                       anchor=NW)
     statusbar.pack(side='bottom', fill='x')
     self.view = BoardView(self._root,
                           model=self.model,
                           parent=self,
                           statusbar=statusbar)
     self.player_color = BLACK
     self.num_players = 1
     self.set_controllers()
     self.controller1.start_turn()
     self.filename = None
     self.think_time = self.parent.thinkTime.get()
 def test_evaluate_winner(self):
     player1 = Player()
     player2 = Player()
     game = Checkers(player1, player2)
     game.player_in_turn = 0
     for cell in game.board.cells:
         if cell.value in ["o", "0"]:
             cell.value = None
     self.assertEqual(player1, game.evaluate_winner())
     game.player_in_turn = 1
     for cell in game.board.cells:
         if cell.value in ["x", "X"]:
             cell.value = None
     self.assertEqual(player2, game.evaluate_winner())
Esempio n. 13
0
def eval(agent:Agent, env: Checkers, color:str, n_games=100):
    agent.net.eval()
    opponent = Agent(gamma=agent.gamma,
                     epsilon=1,
                     lr=0,
                     input_dims=[8*8 + 1],
                     batch_size=agent.batch_size,
                     action_space=agent.action_space,
                     eps_dec=0,
                     max_mem_size=0
                     )
    opponent.net.eval()
    initial_state = env.save_state()
    score = {'black': 0, 'white': 0}

    for i in tqdm(range(n_games)):
        env.restore_state(initial_state)
        winner = None
        moves = torch.tensor(env.legal_moves())
        board, turn, last_moved_piece = env.save_state()
        brain = agent if turn == color else opponent
        board_tensor = torch.from_numpy(env.flat_board()).view(-1).float()
        encoded_turn = torch.tensor([1.]) if turn == 'black' else torch.tensor([0.])
        observation = torch.cat([board_tensor, encoded_turn])
        while not winner:
            action = brain.choose_action(observation)
            while not action_is_legal(action, moves):
                action = brain.choose_action(observation)
            new_board, new_turn, _, moves, winner = env.move(*action.tolist())
            moves = torch.tensor(moves)
            board_tensor = torch.from_numpy(env.flat_board()).view(-1).float()
            encoded_turn = torch.tensor([1. if turn == 'black' else 0.])
            observation = torch.cat([board_tensor, encoded_turn])
            brain = agent if turn == color else opponent
        score[winner] +=1
    agent.net.train()
    return score[color] / n_games
Esempio n. 14
0
              epsilon=args.epsilon,
              batch_size=args.batch_size,
              action_space=action_space,
              input_dims=[8 * 8 + 1],
              lr=args.lr,
              eps_dec=args.epsilon_decay),
        'white':
        Agent(gamma=args.gamma,
              epsilon=args.epsilon,
              batch_size=args.batch_size,
              action_space=action_space,
              input_dims=[8 * 8 + 1],
              lr=args.lr,
              eps_dec=args.epsilon_decay)
    }
    env = Checkers()
    initial_state = env.save_state()
    eps_history = []
    score = {'black': 0, 'white': 0}

    os.makedirs(args.checkpoints_dir, exist_ok=True)

    for i in range(args.games):
        print(
            f"episode={i}, score={score}, black_eps:{players['black'].epsilon}, white_eps:{players['white'].epsilon}"
        )
        score = {'black': 0, 'white': 0}
        env.restore_state(initial_state)
        winner = None
        moves = torch.tensor(env.legal_moves())
        board, turn, last_moved_piece = env.save_state()
Esempio n. 15
0
 def reset(self):
     self.checkers = Checkers()
Esempio n. 16
0
 def __init__(self):
     self.db = CFDatabase(config.getCfDbFile())
     self.lexer = CxxLexer()
     self.checkers = Checkers()
Esempio n. 17
0
 def __init__(self):
     self.ccdb = CCDatabase(config.getCcDbFile())
     self.codeChecker = CodeChecker(config.getRepoDir())
     self.code = []
     self.checkers = Checkers()
from checkers import Checkers, AIPlayer, HumanPlayer

if __name__ == '__main__':
    checkers = Checkers(AIPlayer(), AIPlayer())
    checkers.start()
    print("Game ended in {} turns".format(checkers.turn + 1))
    checkers.render()
    if checkers.winner:
        print("Winner!! Player {}".format(checkers.player_in_turn + 1))
    else:
        print("Draw!!")
Esempio n. 19
0
class DictionaryBuilder():
    def __init__(self):
        self.db = CFDatabase(config.getCfDbFile())
        self.lexer = CxxLexer()
        self.checkers = Checkers()

    def build(self, checker):
        # Load all data from DB
        print("Fetching data from database...")
        allData = self.db.getFixDataForChecker(checker)
        allDataLen = len(allData)
        print("Done, fetched {0} records".format(allDataLen))
        if allDataLen < 1:
            print("No data found")
            return

        # Tokenize all code snippets and extract extra tokens from checker's messages
        # Labelize all tokens existing only in fixed code (added data)
        # Labelize all tokens appearing more than X times
        # Labelize all C++ STL names (namespaces, constants, defines, variables, functions, headers, numeric literals)
        # Labelize all UNK token indexes
        print("Converting to tokens...")
        tokens = deque()
        tokensLen = 0
        labels = {}
        i = 0
        tokensLen = 0

        minTokens1Len = 9999
        minTokens2Len = 9999
        maxTokens1Len = 0
        maxTokens2Len = 0

        uniqTokenIDs = {}
        for tid in range(globals.firstAvailableToken):
            uniqTokenIDs[tid] = 0
        uniqTokenIDs[0] = 1  # T_ZERO
        uniqTokenIDs[349] = 1  # T_SOS
        uniqTokenIDs[351] = 1  # T_UNK

        while i < allDataLen:
            # Tokenize
            tokens1 = self.lexer.tokenize(allData[i][1])
            tokens2 = self.lexer.tokenize(allData[i][2])
            extra = self.checkers.extractTokensForChecker(
                checker, allData[i][4])
            newTokens = []

            # Extract new tokens
            for token2 in tokens2:
                matchFound = False
                for token1 in tokens1:
                    if token1['token'] == token2['token'] and token1[
                            'has_value'] == token2['has_value']:
                        if token1['has_value']:
                            if token1['value'] == token2['value']:
                                matchFound = True
                        else:
                            matchFound = True
                if not matchFound:
                    newTokens.append(token2)
            tokens1Len = len(tokens1)
            tokens2Len = len(tokens2)

            # Statistics
            if tokens1Len < minTokens1Len:
                minTokens1Len = tokens1Len
            if tokens2Len < minTokens2Len:
                minTokens2Len = tokens2Len
            if tokens1Len > maxTokens1Len:
                maxTokens1Len = tokens1Len
            if tokens2Len > maxTokens2Len:
                maxTokens2Len = tokens2Len

            # Count occurrences of each label
            allTokens = tokens1 + tokens2 + extra
            for token in allTokens:
                value = globals.emptyValue
                if token['has_value']:
                    value = token['value']
                if value in labels:
                    labels[value] += 1
                else:
                    labels[value] = 1
                uniqTokenIDs[int(token['token'])] += 1
                tokensLen += 1
            if len(newTokens) > 0:
                tokens.append(newTokens)
            i += 1
            print('Done {0}, processed {1} tokens ({2}/{3}/{4}/{5})'.format(
                i, len(allTokens), tokens1Len, tokens2Len, len(extra),
                len(newTokens)),
                  file=sys.stderr)
        print("Done, converted {0} tokens".format(tokensLen))

        # Labelizing
        labelDb = [globals.emptyValue]
        # UNK
        print("Adding UNK token labels")
        for i in range(config.cfNoOfUnkTokens):
            labelDb.append("UNK_{0}".format(i))
        print("Done, current label DB has {0} entries".format(len(labelDb)))

        # Common occurrences
        print("Filtering labels, selecting only those with > {0} occurrences".
              format(config.cfLabelThreshold))
        for key in labels.keys():
            if labels[key] > config.cfLabelThreshold:
                labelDb.append(key)
        print("Done, current label DB has {0} entries".format(len(labelDb)))

        # New tokens in fixed code
        print("Filtering labels, selecting only tokens introduced with fix")
        for entry in tokens:
            for token in entry:
                if token['has_value']:
                    labelDb.append(token['value'])
        print("Done, current label DB has {0} entries".format(len(labelDb)))

        # STL part

        # Token IDs
        for i in range(globals.firstAvailableToken):
            if uniqTokenIDs[i] > 0:
                labelDb.append("T_{0}".format(i))

        # Printout
        print("Uniqueing labels")
        labelsUnique = list(set(labelDb))
        print("Done, current label DB has {0} entries".format(
            len(labelsUnique)))
        print("Data set info")
        print("Min no of tokens (bug): {0}".format(minTokens1Len))
        print("Min no of tokens (fix): {0}".format(minTokens2Len))
        print("Max no of tokens (bug): {0}".format(maxTokens1Len))
        print("Max no of tokens (fix): {0}".format(maxTokens2Len))
        print("Extracted labels:")
        print(labelsUnique)
        print("Token uses:")
        for i in range(globals.firstAvailableToken):
            if uniqTokenIDs[i] > 0:
                print("{0}: {1}".format(i, uniqTokenIDs[i]))

        # Save to file
        print("Writing to dictionary file")
        with open(config.cfDictFilenameFormat.format(checker), "w") as f:
            f.write(json.dumps(labelsUnique))
        print("Done, exiting...")
Esempio n. 20
0
        return fixUnusedParam(code, bugData)
    if bugData.getChecker() == 'clang-diagnostic-constant-conversion':
        return fixConstConv(code, bugData)
    return None


ccdb = CCDatabase(config.getCcDbFile())
db = sqlite3.connect('../Results/db.sqlite')
cursor = db.cursor()
cursor.execute('SELECT * FROM bugs')
dataFromDb = cursor.fetchall()
bugs = []
bugsPerFile = {}
BUG_NOT_PROCESSED = 0
vcs = GitProvider(config.getRepoDir())
checkers = Checkers()
currentCommit = vcs.getAllVersions(config.getBranch())[0]
bugDataList = {}
fileContents = {}
codechecker = CodeChecker(config.getRepoDir())

if len(dataFromDb) > 0:
    print("Skipping steps 1-2, DB already filled with data")
    for bug in dataFromDb:
        if bug[2] not in bugsPerFile:
            bugsPerFile[bug[2]] = []
        if bug[3] == BUG_NOT_PROCESSED:
            bugDataList[bug[0]] = ccdb.getNotResolvedBugData(bug[0])
            bugsPerFile[bug[2]].append(bug[0])
else:
    # 1.
Esempio n. 21
0
 def setUpClass(self):
     print("Starting up...")
     self.db = CFDatabase(config.getCfDbFile())
     self.checkers = Checkers()
     self.checkerList = ['deadcode.DeadStores']
     self.checkerIndex = 0
Esempio n. 22
0
    def __init__(self, history=[]):
        # Rollout statistics
        self.child_visits = []
        # Terminal values for the first player
        # 1 for win
        # 0 for draw
        # -1 for loss
        # None for incomplete
        self.game_value = None

        # XXX Conventions:
        # - Black player moves first
        # - Ego-centric views assume the king row are at the top, i.e. starts at the bottom (Second player has the same view as absolute)
        self.ch = Checkers()

        # Action space
        self.actions = []
        # Simple moves
        for from_sq in range(self.ch.n_positions):
            for to_sq in self.ch.neighbors[from_sq]:
                if to_sq is not None:
                    simple_move = (from_sq, to_sq)
                    self.actions.append(simple_move)

        assert 98 == len(self.actions), 'There should be 98 simple moves.'
        # Jumps
        for from_sq in range(self.ch.n_positions):
            row, col = self.ch.sq2pos(from_sq)
            # For each direction
            for di, (drow, dcol) in enumerate(Checkers.dir2del):
                next_row, next_col = row + 2 * drow, col + 2 * dcol
                if 0 <= next_row < self.ch.size and 0 <= next_col < self.ch.size:
                    # Within bound
                    to_sq = self.ch.pos2sq(next_row, next_col)
                    jump = (from_sq, to_sq)
                    self.actions.append(jump)
        self.num_actions = len(self.actions)
        assert 98 + 72 == self.num_actions, 'There should be 98 simple moves and 72 jumps.'
        # Inverse dictionary
        self.action2ind = {
            action: ind
            for ind, action in enumerate(self.actions)
        }
        # Square mapping from absolute to first player's ego-centric (reflect through the center)
        self.abs2ego_sq = {}
        for sq in range(self.ch.n_positions):
            row, col = self.ch.sq2pos(sq)
            re_row, re_col = -row + self.ch.size - 1, -col + self.ch.size - 1
            re_sq = self.ch.pos2sq(re_row, re_col)
            self.abs2ego_sq[sq] = re_sq
        # Inverse
        self.ego2abs_sq = {re_sq: sq for sq, re_sq in self.abs2ego_sq.items()}

        # Move mapping from absolute to first player's ego-centric
        self.abs2ego_ac = {}
        for ac, (from_sq, to_sq) in enumerate(self.actions):
            ego_move = (self.abs2ego_sq[from_sq], self.abs2ego_sq[to_sq])
            ego_ac = self.action2ind[ego_move]
            self.abs2ego_ac[ac] = ego_ac
        # Inverse
        self.ego2abs_ac = {
            ego_ac: ac
            for ac, ego_ac in self.abs2ego_ac.items()
        }

        # Fast forward to the last state by taking actions from history
        self.history = []
        for action in history:
            self.apply(action)
Esempio n. 23
0
class BoardView(gtk.DrawingArea):

    def __init__(self):
        gtk.DrawingArea.__init__(self)
        
        self.tile_size = 100
        self.tile_count = 8
        self.w = self.h = self.tile_count * self.tile_size
        
        cmap = self.get_colormap()
        self.color_black = cmap.alloc_color('#000000')
        self.color_white = cmap.alloc_color('#FFFFFF')    
        self.color_red   = cmap.alloc_color('#BB3333')
        self.color_green = cmap.alloc_color('#33DD33')
        self.set_size_request(320, 320)

        self.connect("expose_event", self.exposeEvent)
        self.connect("size-allocate", self.__sizeAllocate)
        self.connect("button_press_event", self.buttonPressEvent)
        self.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)

        #TODO: use settings
        self.tile_w = gtk.gdk.pixbuf_new_from_file("images/tile_w.jpg")
        self.tile_b = gtk.gdk.pixbuf_new_from_file("images/tile_b.jpg")
        self.selector = gtk.gdk.pixbuf_new_from_file("images/selector.png")

        self.checkers = Checkers()
        self.checkers.setSelected((2,5))
    
    def __sizeAllocate(self, widget, rect):
        if rect.width < rect.height:
            self.tile_size = rect.width / self.tile_count
        else:
            self.tile_size = rect.height / self.tile_count
        self.w = self.h = self.tile_count * self.tile_size

    def exposeEvent(self, widget, event):
        window = self.window
        gc = window.new_gc()

        rect = gtk.gdk.Rectangle(0, 0, self.w, self.h)

        window.begin_paint_rect(rect)
        self.__drawBoard(window, gc, rect)
        window.end_paint()

    def __drawBackground(self, window, gc, rect):
        bg_w = self.tile_w.get_width()
        bg_h = self.tile_w.get_height() 
        mod = True
        for i in range(self.tile_count):
            for ii in range(self.tile_count):
                if mod:
                    window.draw_pixbuf(gc, self.tile_w, (bg_w - self.tile_size) / 2, 
                                       (bg_h - self.tile_size) / 2,
                                       i * self.tile_size, ii * self.tile_size,
                                       self.tile_size, self.tile_size)
                else:
                    window.draw_pixbuf(gc, self.tile_b, (bg_w - self.tile_size) / 2, 
                                       (bg_h - self.tile_size) / 2,
                                       i * self.tile_size, ii * self.tile_size,
                                       self.tile_size, self.tile_size)
                mod = not mod
            mod = not mod
                    
        
    def __drawPiece(self, window, gc, rect, type, x, y):
        gc
        s = self.tile_size
        if abs(type) == Checkers.WH:
            gc.set_foreground(self.color_white)
            window.draw_arc(gc, True, s*x+s/10, s*y+s/10, s - s/5, s - s/5, 0, 65*360)
            if type < 0:
                gc.set_foreground(self.color_black)
                window.draw_arc(gc, True, s*x+s/4, s*y+s/4, s/2, s/2, 0, 65*360)
        elif abs(type) == Checkers.BL:
            gc.set_foreground(self.color_black)
            window.draw_arc(gc, True, s*x+s/10, s*y+s/10, s - s/5, s - s/5,
                            0, 65*360)
            if type < 0:
                gc.set_foreground(self.color_white)
                window.draw_arc(gc, True, s*x+s/4, s*y+s/4, s/2, s/2, 0, 65*360)

    def __drawPieces(self, window, gc, rect):
        for i in range(8):
            for ii in range(8):
                if (i+ii)%2:
                    self.__drawPiece(window, gc, rect, self.checkers.checkTile(ii,i), ii, i)
        
    def __drawSelector(self, window, gc):
        a = self.checkers.getSelected()[0]
        if a:
            gc.set_line_attributes(self.tile_size / 20,
                                   gtk.gdk.LINE_SOLID,
                                   gtk.gdk.CAP_NOT_LAST,
                                   gtk.gdk.JOIN_MITER)
            gc.set_foreground(self.color_red)
            window.draw_rectangle(gc, False, a[0] * self.tile_size, a[1] * self.tile_size,
                                  self.tile_size, self.tile_size)
           
    def __drawMoves(self, window, gc):
        a = self.checkers.getSelected()[1:]
        for i in a:
            gc.set_line_attributes(self.tile_size / 20,
                                   gtk.gdk.LINE_SOLID,
                                   gtk.gdk.CAP_NOT_LAST,
                                   gtk.gdk.JOIN_MITER)
            gc.set_foreground(self.color_green)
            window.draw_rectangle(gc, False, i[0] * self.tile_size, i[1] * self.tile_size,
                                  self.tile_size, self.tile_size)
        
    def __drawBoard(self, window, gc, rect):
        self.__drawBackground(window, gc, rect)
        self.__drawPieces(window, gc, rect)
        self.__drawSelector(window, gc)
        self.__drawMoves(window, gc)

    def __getCoords(self, x, y):
        x = int(x / self.tile_size)
        y = int(y / self.tile_size)
        if x > 7 or y > 7:
            return None
        else:
            return (x, y)

    def buttonPressEvent(self, widget, event):
        if event.button != 1:
            return
        target = self.__getCoords(event.x, event.y)
        if not target:
            return
        m = self.checkers.getSelected()
        if target in m[1:]:
            self.checkers.move(m, target)
        else:
            self.checkers.setSelected(target)
        self.exposeEvent(None, None)

    def reset(self):
        self.checkers = Checkers()
Esempio n. 24
0
class Predictor():
    def __init__(self):
        self.vcs = GitProvider(config.getRepoDir())
        self.ccdb = CCDatabase(config.getCcDbFile())
        self.codeChecker = CodeChecker(config.getRepoDir())
        self.checkers = Checkers()
        self.loadCommitList()

    def loadCommitList(self):
        self.commits = self.vcs.getAllVersions(config.getBranch())
        self.currentCommitIndex = 0

    def convertFilePathToRepoRelativePath(self, path):
        return os.path.relpath(path, config.getRepoDir())

    def getDiffResolvedIds(self):
        resolved = self.codeChecker.diffResolved(config.getCcRunName(),
                                                 config.getTmpDir(), self.ccdb)
        ids = []
        for bug in resolved:
            ids.append(bug['reportId'])
        return ids

    def predict(self, id, checker):
        # Load all bugs
        print("Loading bug data...")
        ids = []
        if id == -1:
            bugs = self.ccdb.getAllBugsForChecker(checker)
            ids = [x[0] for x in bugs]
        else:
            ids.append(id)

        # Loading model
        print("Loading model...")
        model = load_model(config.cfModelFilenameFormat.format(checker))
        model.summary()
        vLabels = ['NOT OK', 'OK', 'Skipped']

        # Initialize coder
        print("Initializing coder...")
        self.dictionary = Dictionary(checker)
        self.coder = Coder(self.dictionary)
        self.totalDictionaryLength = self.dictionary.length()

        # Predicting
        print("Starting predictions...")
        for i in ids:
            allData = self.ccdb.getBugData(i)
            if allData.getChecker(
            ) not in globals.availableCheckers or allData.getChecker(
            ) != checker:
                print("Bug #{0} - checker not supported".format(i))
            else:
                # Load extra tokens from checker message
                checkerInfo = self.checkers.extractTokensForChecker(
                    allData.getChecker(), allData.getMessage())
                # Retrieve code fragment with bug
                fileRelativePath = self.convertFilePathToRepoRelativePath(
                    allData.getFile())
                fullCodeWithBug = self.vcs.getFileContents(
                    fileRelativePath, self.commits[self.currentCommitIndex])
                extractor = CodeExtractor(allData)
                extractor.loadCodeFromText(fullCodeWithBug)
                extractor.extractBugCode()
                bugCodeFragment = extractor.getBugCodeFragment()
                fixCodeFragment = ''
                # Encode it
                encodedBugData, initialUnkList = self.coder.encode(
                    bugCodeFragment, checkerData=checkerInfo)
                # Convert to one-hot
                MODEL_X_MAX_LEN = model.get_layer(index=0).input_shape[1]
                if len(encodedBugData) > MODEL_X_MAX_LEN:
                    print(
                        "Bug #{0} - Code too big for model, ignored".format(i))
                    continue
                elif id == -1:
                    print("Bug #{0} - Good to go".format(i))
                    continue
                noZerosToPad = MODEL_X_MAX_LEN - len(encodedBugData)
                if noZerosToPad > 0:
                    encodedBugData = self.coder.applyPadding(
                        encodedBugData, noZerosToPad)
                X = np.zeros((1, MODEL_X_MAX_LEN, self.totalDictionaryLength))
                X[0] = self.coder.convertToOneHot(
                    encodedBugData,
                    np.zeros((MODEL_X_MAX_LEN, self.totalDictionaryLength)))
                # Predict and convert from one-hot
                Y = self.coder.convertFromOneHot(model.predict(X)[0])
                print(Y)
                # Decode
                Y = self.coder.removePadding(Y)
                fixCodeFragment = self.coder.decode(Y, initialUnkList)

                #Verify?
                vStatus = 2
                if config.cfVerifyPrediction:
                    # Apply fix in source code file
                    extractor.applyFix(fixCodeFragment)
                    extractor.saveToFile(allData.getFile())
                    # Run CodeChecker and analyze code
                    self.codeChecker.check(True)
                    resolvedIds = self.getDiffResolvedIds()
                    # Check if ID is resolved in tmp folder
                    isFixed = i in resolvedIds
                    # Set vStatus accordingly
                    if isFixed:
                        vStatus = 1
                    else:
                        vStatus = 0
                #Print
                print("Bug #{0} - summary".format(i))
                print("== Code fragment with bug ==")
                print(bugCodeFragment)
                print("== Suggested fix ==")
                print(fixCodeFragment)
                print("Verification: {0}".format(vLabels[vStatus]))
                a = ' '
                while a != 'y' and a != 'n':
                    a = input("Apply fix? (y/n): ")
                if a == 'y':
                    if not config.cfVerifyPrediction:
                        # Apply fix in source code file
                        extractor.applyFix(fixCodeFragment)
                        extractor.saveToFile(allData.getFile())
                elif config.cfVerifyPrediction:
                    # Revert file contents
                    self.vcs.checkout(self.commits[self.currentCommitIndex])
                print('Done')
        print("All done, exiting...")
Esempio n. 25
0
    sleep(1)

    screen.fill(BLACK)

    # print winner
    score = game.scoreGame()
    font = pygame.font.SysFont('Calibri', 30, True, False)
    text = ""
    # tie
    if score == 0:
        text = font.render("TIE", True, WHITE)
    # ai wins
    elif score == 1:
        text = font.render("You lose, AI wins", True, WHITE)
    # human wins
    else:
        text = font.render("You win, AI loses", True, WHITE)
    screen.blit(text, [width / 3 + width / 12, height / 2])

    # update screen
    pygame.display.flip()
    sleep(1.5)

    # end game
    pygame.quit()

    print('Starting Checkers game...')
    game = Checkers()
    agent = Minimax(game, 3, False)

    print('Program terminated')
Esempio n. 26
0
class FixTautOORCmp():
    def __init__(self):
        self.ccdb = CCDatabase(config.getCcDbFile())
        self.codeChecker = CodeChecker(config.getRepoDir())
        self.code = []
        self.checkers = Checkers()

    def loadCodeFromFile(self):
        with open(self.bugData.getFile(), 'r') as file:
            code = file.readlines()
            self.code = []
            for line in code:
                self.code.append(line.replace("\r\n", "\n"))
            """
            code = file.read()
            noCrLf = code.count("\r\n")
            noCr = code.count("\r") - noCrLf
            noLf = code.count("\n") - noCrLf
            if noCrLf > noCr:
                if noCrLf > noLf:
                    self.lineEnding = "\r\n"
                else:
                    self.lineEnding = "\n"
            else:
                if noCr > noLf:
                    self.lineEnding = "\r"
                else:
                    self.lineEnding = "\n"
            self.code = code.split(self.lineEnding)
            """

    def saveCodeToFile(self):
        with open(self.bugData.getFile(), 'w') as file:
            #file.write(self.lineEnding.join(self.code))
            file.writelines(self.code)

    def findMatchingDefine(self, value):
        patValue = '({0})'
        patName = '[ \t]([0-9a-zA-Z_]+)[ \t]'
        for line in self.code:
            if line[:7] == '#define':
                if re.search(patValue.format(value), line):
                    return re.search(patName, line).group(1)
        return None

    def fix(self, file):
        fileData = self.ccdb.getFileData(file)
        print(fileData)
        bugs = self.ccdb.getAllBugsForChecker(
            'clang-diagnostic-tautological-constant-out-of-range-compare')
        lines = []
        for bug in bugs:
            if bug[2] == fileData[-1][0]:
                self.bugData = self.ccdb.getBugData(bug[0])
                if self.bugData.getLine() not in lines:
                    self.loadCodeFromFile()

                    tokens = self.checkers.extractTokensForChecker(
                        'clang-diagnostic-tautological-constant-out-of-range-compare',
                        self.bugData.getMessage())
                    line = self.code[self.bugData.getLine() - 1]
                    pat = "\([^\(]*{0}\)".format(tokens[0]['value'])
                    match = re.search(pat, line)
                    if match is None:
                        pat = "\([^\(]*{0}\)".format(
                            self.findMatchingDefine(tokens[0]['value']))
                        match = re.search(pat, line)
                    match = match.group(0)
                    newLine = line.replace(match,
                                           '({0})'.format(tokens[1]['value']))
                    self.code[self.bugData.getLine() - 1] = newLine

                    self.saveCodeToFile()
                    lines = lines + list(
                        range((self.bugData.getLine() - 11),
                              (self.bugData.getLine() + 10)))
                    print("Passed")
                    print(self.bugData.getLine())
                    pass
                else:
                    print("Dismissed")
                    print(self.bugData.getLine())
                    pass
 def test_possible_movements(self):
     player1 = Player()
     player2 = Player()
     game = Checkers(player1, player2)
     for cell in game.board.cells:
         cell.value = None
     game.board.set_cell(2, 3, "X")
     game.board.set_cell(2, 5, "O")
     game.board.set_cell(4, 3, "x")
     game.board.set_cell(4, 5, "o")
     game.player_in_turn = 0
     self.assertEqual(6, len(game.possible_movements()))
     game.player_in_turn = 1
     self.assertEqual(6, len(game.possible_movements()))
     game.board.set_cell(3, 4, "x")
     game.board.set_cell(2, 3, "o")
     game.board.set_cell(2, 5, "o")
     game.board.set_cell(4, 3, "o")
     game.board.set_cell(4, 5, "o")
     game.player_in_turn = 0
     self.assertEqual(2, len(game.possible_movements()))
     game.board.set_cell(3, 4, "X")
     self.assertEqual(4, len(game.possible_movements()))
     game.board.set_cell(3, 4, "o")
     game.board.set_cell(2, 3, "x")
     game.board.set_cell(2, 5, "x")
     game.board.set_cell(4, 3, "x")
     game.board.set_cell(4, 5, "x")
     game.player_in_turn = 1
     self.assertEqual(2, len(game.possible_movements()))
     game.board.set_cell(3, 4, "O")
     self.assertEqual(4, len(game.possible_movements()))
Esempio n. 28
0
class SavedGame(object):
    def __init__(self):
        self._model = Checkers()
        self.to_move = None
        self.moves = []
        self.description = ''
        self.black_men = []
        self.white_men = []
        self.black_kings = []
        self.white_kings = []
        self.flip_board = False
        self.num_players = 1
        self._move_check = False
        self._bm_check = False
        self._bk_check = False
        self._wm_check = False
        self._wk_check = False

    def _write_positions(self, f, prefix, positions):
        f.write(prefix + ' ')
        for p in sorted(positions):
            f.write('%d ' % p)
        f.write('\n')

    def _write_moves(self, f):
        f.write('<moves>\n')
        for move in reversed(self.moves):
            start = keymap[move.affected_squares[FIRST][0]]
            dest = keymap[move.affected_squares[LAST][0]]
            movestr = '%d-%d' % (start, dest)
            annotation = move.annotation
            if annotation.startswith(movestr):
                annotation = annotation.replace(movestr, '', 1).rstrip()
            f.write('%s;%s\n' % (movestr, annotation))

    def write(self, filename):
        with open(filename, 'w') as f:
            f.write('<description>\n')
            for line in self.description.splitlines():
                # numbered lists or hyperlinks are not word wrapped.
                if line.startswith('# ') or '[[' in line:
                    f.write(line + '\n')
                    continue
                else:
                    f.write(textwrap.fill(line, 80) + '\n')
            f.write('<setup>\n')
            if self.to_move == WHITE:
                f.write('white_first\n')
            elif self.to_move == BLACK:
                f.write('black_first\n')
            else:
                raise ValueError, "Unknown value for to_move variable"
            if self.num_players >=0 and self.num_players <=2:
                f.write('%d_player_game\n' % self.num_players)
            else:
                raise ValueError, "Unknown value for num_players variable"
            if self.flip_board:
                f.write('flip_board 1\n')
            else:
                f.write('flip_board 0\n')
            self._write_positions(f, 'black_men', self.black_men)
            self._write_positions(f, 'black_kings', self.black_kings)
            self._write_positions(f, 'white_men', self.white_men)
            self._write_positions(f, 'white_kings', self.white_kings)
            self._write_moves(f)

    def read(self, filename):
        with open(filename, 'r') as f:
            lines = f.readlines()

        linelen = len(lines)
        i = 0
        while True:
            if i >= linelen:
                break

            line = lines[i].strip()
            if line.startswith('<description>'):
                self.description = ''
                i += 1
                while i < linelen and not lines[i].startswith('<setup>'):
                    self.description += lines[i]
                    i += 1
            elif line.startswith('<setup>'):
                i = self._parse_setup(lines, i, linelen)
            elif line.startswith('<moves>'):
                i = self._parse_moves(lines, i, linelen)
            else:
                raise IOError, 'Unrecognized section in file, line %d' % (i+1)

    def _parse_items(self, line):
        men = line.split()[1:]
        return map(int, men)

    def _add_men_to_board(self, locations, val):
        squares = self._model.curr_state.squares
        try:
            for loc in locations:
                idx = squaremap[loc]
                squares[idx] = val
        except ValueError:
            raise IOError, 'Checker location not valid, line %d' % (i+1)

    def _parse_setup(self, lines, idx, linelen):
        curr_state = self._model.curr_state
        curr_state.clear()
        idx += 1
        while idx < linelen and '<moves>' not in lines[idx]:
            line = lines[idx].strip().lower()
            if line == 'white_first':
                self.to_move = curr_state.to_move = WHITE
                self._move_check = True
            elif line == 'black_first':
                self.to_move = curr_state.to_move = BLACK
                self._move_check = True
            elif line.endswith('player_game'):
                numstr, _ = line.split('_', 1)
                self.num_players = int(numstr)
            elif line.startswith('flip_board'):
                _, setting = line.split()
                val = int(setting)
                self.flip_board = True if val else False
            elif line.startswith('black_men'):
                self.black_men = self._parse_items(line)
                self._add_men_to_board(self.black_men, BLACK | MAN)
                self._bm_check = True
            elif line.startswith('white_men'):
                self.white_men = self._parse_items(line)
                self._add_men_to_board(self.white_men, WHITE | MAN)
                self._wm_check = True
            elif line.startswith('black_kings'):
                self.black_kings = self._parse_items(line)
                self._add_men_to_board(self.black_kings, BLACK | KING)
                self._bk_check = True
            elif line.startswith('white_kings'):
                self.white_kings = self._parse_items(line)
                self._add_men_to_board(self.white_kings, WHITE | KING)
                self._wk_check = True
            idx += 1
        if (not self._move_check and not self._bm_check and not self._wm_check
            and not self._bk_check and not self._wk_check):
            raise IOError, 'Error in <setup> section: not all required items found'
        return idx

    def _is_move(self, delta):
        return delta in KING_IDX

    def _is_jump(self, delta):
        return delta not in KING_IDX

    def _try_move(self, idx, start, dest, state_copy, annotation):
        legal_moves = self._model.legal_moves(state_copy)
        # match move from file with available moves on checkerboard
        found = False
        startsq, destsq = squaremap[start], squaremap[dest]
        for move in legal_moves:
            if (startsq == move.affected_squares[FIRST][0] and
                destsq == move.affected_squares[LAST][0]):
                self._model.make_move(move, state_copy, False, False)
                move.annotation = annotation
                self.moves.append(move)
                found = True
                break
        if not found:
            raise IOError, 'Illegal move found in file, line %d' % (idx+1)

    def _try_jump(self, idx, start, dest, state_copy, annotation):
        if not self._model.captures_available(state_copy):
            return False
        legal_moves = self._model.legal_moves(state_copy)
        # match jump from file with available jumps on checkerboard
        startsq, destsq = squaremap[start], squaremap[dest]
        small, large = min(startsq, destsq), max(startsq, destsq)
        found = False
        for move in legal_moves:
            # a valid jump may either have a single jump in it, or
            # multiple jumps. In the multiple jump case, startsq is the
            # source of the first jump, and destsq is the endpoint of the
            # last jump.
            if (startsq == move.affected_squares[FIRST][0] and
                destsq == move.affected_squares[LAST][0]):
                self._model.make_move(move, state_copy, False, False)
                move.annotation = annotation
                self.moves.append(move)
                found = True
                break
        return found

    def _parse_moves(self, lines, idx, linelen):
        """ Each move in the file lists the beginning and ending square, along
        with an optional annotation string (in Creole format) that describes it.
        Since the move listing in the file contains less information than
        we need inside our Checkerboard model, I make sure that each move works
        on a copy of the model before I commit to using it inside the code. """
        state_copy = copy.deepcopy(self._model.curr_state)
        idx += 1
        while idx < linelen:
            line = lines[idx].strip()
            if line == "":
                idx += 1
                continue # ignore blank lines

            try:
                movestr, annotation = line.split(';', 1)
            except ValueError:
                raise IOError, 'Unrecognized section in file, line %d' % (idx+1)

            # move is always part of the annotation; I just don't want to
            # have to repeat it explicitly in the file.
            annotation = movestr + annotation

            # analyze affected squares to perform a move or jump.
            try:
                start, dest = [int(x) for x in movestr.split('-')]
            except ValueError:
                raise IOError, 'Bad move format in file, line %d' % idx
            delta = squaremap[start] - squaremap[dest]
            if self._is_move(delta):
                self._try_move(idx, start, dest, state_copy, annotation)
            else:
                jumped = self._try_jump(idx, start, dest, state_copy,
                                        annotation)
                if not jumped:
                    raise IOError, 'Bad move format in file, line %d' % idx
            idx += 1
        self.moves.reverse()
        return idx
 def test_recieve_input(self, input):
     player = HumanPlayer()
     game = Checkers(player, Player())
     self.assertEqual(player.recieve_input(game.possible_movements()), 4)
Esempio n. 30
0
    def test_invalid_jump(self):
        c = Checkers()

        # try jumping over the same colored piece
        with self.assertRaises(MoveError):
            c.move(src=(2, 1), dest=(0, 3))
Esempio n. 31
0
class Verifier():
    def __init__(self):
        self.vcs = GitProvider(config.getRepoDir())
        self.ccdb = CCDatabase(config.getCcDbFile())
        self.codeChecker = CodeChecker(config.getRepoDir())
        self.checkers = Checkers()

    def convertFilePathToRepoRelativePath(self, path):
        return os.path.relpath(path, config.getRepoDir())

    def getDiffResolvedIds(self):
        resolved = self.codeChecker.diffResolved(config.getCcRunName(), config.getTmpDir(), self.ccdb)
        ids = []
        for bug in resolved:
            ids.append(bug['reportId'])
        return ids
    
    def getBugDataFromDiff(self, obj):
        return BugData(int(obj['location']['line']), int(obj['location']['line']), obj['location']['file_name'], obj['check_name'], 'New', obj['description'], int(obj['location']['line']), None)

    def getDiffNew(self):
        new = self.codeChecker.diffNew(config.getCcRunName(), config.getTmpDir(), self.ccdb)
        ids = []
        for bug in new:
            ids.append(self.getBugDataFromDiff(bug))
        return ids
    
    def isBugDataEqual(self, b1, b2):
        if b1.getLine() != b2.getLine():
            return False
        if b1.getChecker() != b2.getChecker():
            return False
        if b1.getMessage() != b2.getMessage():
            return False
        if b1.getFile() != b2.getFile():
            return False
        if b1.getStatus() != b2.getStatus():
            return False
        if b1.getReviewStatus() != b2.getReviewStatus():
            return False
        return True
    
    def displaySuggestions(self, suggestions):
        statuses = ['Negative', 'Positive', 'Skipped']
        for s in suggestions:
            print("File: {2}, L{0}, Type: {1}".format(s.bug.getLine(), s.bug.getChecker(), s.file))
            print("Verification status: {0}".format(statuses[s.verificationStatus]))
            if s.verificationStatus in [1, 2]:
                print("Code fragment with bug: \n{0}".format(s.bugCode))
                print("Suggested code fragment for replacement: \n{0}".format(s.fixCode))
    
    def applyValidFixes(self, suggestions, files):
        for f in files:
            bugs = []
            for s in suggestions:
                if s.file == f and s.verificationStatus in [1, 2]:
                    bugs.append(s)
            bugs.sort(key=lambda x: x.bug.getLine(), reverse=True)
            for b in bugs:
                print("File: {2}, L{0}, Type: {1}... ".format(b.bug.getLine(), b.bug.getChecker(), s.file), end='')
                self.applyFix(b)
                print("Applied")
            self.vcs.applyChangeForFile(f)
    
    def applyFix(self, s):
        extractor = CodeExtractor(s.bug)
        extractor.loadCodeFromFile()
        extractor.extractBugCode()
        extractor.applyFix(s.fixCode)
        extractor.saveToFile(s.bug.getFile())
    
    def main(self):
        # Do analysis
        shutil.rmtree(config.getTmpDir())
        self.codeChecker.check(True)

        # Diff new
        newBugs = self.getDiffNew()

        if len(newBugs) < 1:
            print('No new bugs introduced, commit is accepted!')
            return
        
        print("New bugs found! Count: {0}. Attempting repairs...".format(len(newBugs)))

        # Load models
        models = {}
        for checker in globals.availableCheckers:
            models[checker] = load_model(config.cfModelFilenameFormat.format(checker))

        # Load all content from files having new
        files = set([self.convertFilePathToRepoRelativePath(x.getFile()) for x in newBugs])
        fileContents = {}
        for f in files:
            fn = config.getRepoDir() + f
            with open(fn, 'r') as fh:
                fileContents[f] = ''.join(fh.readlines())

        # For each file sort by bug line desc
        suggestions = []
        validSuggestions = 0
        for f in files:
            bugs = [x for x in newBugs if self.convertFilePathToRepoRelativePath(x.getFile()) == f]
            bugs.sort(key=lambda x: x.getLine(), reverse=True)
            print("=== File: {0} ===".format(f))
            # For each bug get a suggestion and test it
            for b in bugs:
                print("L{0}, Type: {1}".format(b.getLine(), b.getChecker()))
                # Prepare useful data
                dictionary = Dictionary(b.getChecker())
                coder = Coder(dictionary)
                totalDictionaryLength = dictionary.length()
                # Prepare and extract bug fragment
                checkerInfo = self.checkers.extractTokensForChecker(b.getChecker(), b.getMessage())
                extractor = CodeExtractor(b)
                extractor.loadCodeFromText(fileContents[f])
                extractor.extractBugCode()
                bugCodeFragment = extractor.getBugCodeFragment()
                fixCodeFragment = ''
                # Encode it
                encodedBugData, initialUnkList = coder.encode(bugCodeFragment, checkerData = checkerInfo)
                # Convert to one-hot
                MODEL_X_MAX_LEN = models[b.getChecker()].get_layer(index = 0).input_shape[1]

                if len(encodedBugData) > MODEL_X_MAX_LEN:
                    print("Ignored: Code too big for model")
                    continue

                noZerosToPad = MODEL_X_MAX_LEN - len(encodedBugData)
                if noZerosToPad > 0:
                    encodedBugData = coder.applyPadding(encodedBugData, noZerosToPad)
                X = np.zeros((1, MODEL_X_MAX_LEN, totalDictionaryLength))
                X[0] = coder.convertToOneHot(encodedBugData, np.zeros((MODEL_X_MAX_LEN, totalDictionaryLength)))
                # Predict and convert from one-hot
                Y = coder.convertFromOneHot(models[b.getChecker()].predict(X)[0])
                Y = coder.removePadding(Y)
                # Decode
                fixCodeFragment = coder.decode(Y, initialUnkList)[:-1]
                
                #Verify?
                vStatus = 2
                if config.cfVerifyPrediction:
                    # Apply fix in source code file
                    extractor.applyFix(fixCodeFragment)
                    extractor.saveToFile(b.getFile())
                    # Run CodeChecker and analyze code
                    shutil.rmtree(config.getTmpDir())
                    compilationLog = self.codeChecker.check(True)
                    newBugsAfterFix = self.getDiffNew()
                    # Check if ID is resolved in tmp folder
                    isFixed = 'Build failed' not in compilationLog
                    for nb in newBugsAfterFix:
                        if self.isBugDataEqual(b, nb):
                            isFixed = False
                    # Set vStatus accordingly
                    if isFixed:
                        vStatus = 1
                    else:
                        vStatus = 0
                    # Revert file
                    extractor.loadCodeFromText(fileContents[f])
                    extractor.saveToFile(b.getFile())
                if vStatus == 0:
                    print("Verification: Negative, cannot be applied")
                elif vStatus == 1:
                    print("Verification: Positive, can be applied")
                    validSuggestions += 1
                elif vStatus == 2:
                    print("Verification: Skipped")
                    validSuggestions += 1
                sugg = SuggestionData(f, b, bugCodeFragment, fixCodeFragment, vStatus)
                suggestions.append(sugg)
        print("Valid suggestions prepared for {0} / {1} bugs.".format(validSuggestions, len(newBugs)))

        if validSuggestions > 0:
            print("Apply valid suggestions (a), display them (d), ignore them (i) or abort commit (q)?")
            apply = False
            choice = True
            while choice:
                c = sys.stdin.read(1)
                if c == 'a':
                    apply = True
                    choice = False
                    print("Applying fixes...")
                elif c == 'i':
                    choice = False
                    print("Fixes ignored...")
                elif c == 'd':
                    self.displaySuggestions(suggestions)
                    print("Apply valid suggestions (a), ignore them (i) or abort commit (q)?")
                elif c == 'q':
                    print("Aborting commit...")
                    sys.exit(1)
            if apply:
                self.applyValidFixes(suggestions, files)
                print("Fixes applied!")
        if validSuggestions != len(newBugs):
            print("Unable to fix all bugs, continue with commit (c) or abort (q)?")
            choice = True
            while choice:
                c = sys.stdin.read(1)
                if c == 'c':
                    choice = False
                    print("Continuing...")
                elif c == 'q':
                    print("Aborting commit...")
                    sys.exit(1)
        else:
            print("Bugs corrected, commit is good to go!")
Esempio n. 32
0
class LearningDataBuilder():
    def __init__(self):
        self.db = CFDatabase(config.getCfDbFile())
        self.checkers = Checkers()

    def build(self, checker):
        # Initialize coder
        print("Initializing coder...")
        self.dictionary = Dictionary(checker)
        self.coder = Coder(self.dictionary)

        # Load all data from DB
        print("Fetching data from database...")
        allData = self.db.getFixDataForChecker(checker)
        allDataLen = len(allData)
        print("Done, fetched {0} records".format(allDataLen))
        if allDataLen < 1:
            print("No data found")
            return

        # Encode all data
        print("Encoding all data and writing to output file...")
        i = 0
        (maxBug, maxFix,
         maxUnk) = self.checkers.getModelStatsForChecker(checker)
        with open(config.cfTrainFilenameFormat.format(checker), 'w') as f:
            while i < allDataLen:
                checkerInfo = self.checkers.extractTokensForChecker(
                    checker, allData[i][4])
                encodedBugData, initialUnkList = self.coder.encode(
                    allData[i][1], checkerData=checkerInfo)
                encodedFixData, finalUnkList = self.coder.encode(
                    allData[i][2], unkList=initialUnkList, reverse=False)
                if -1 in encodedBugData:
                    print(
                        "{0}: [{2} - {3} ({1})] Some tokens were not parsed (bug), ignoring (lenUnk = {1})"
                        .format(i + 1, len(finalUnkList), len(encodedBugData),
                                len(encodedFixData)))
                elif -1 in encodedFixData:
                    print(
                        "{0}: [{2} - {3} ({1})] Some tokens were not parsed (fix), ignoring (lenUnk = {1})"
                        .format(i + 1, len(finalUnkList), len(encodedBugData),
                                len(encodedFixData)))
                elif len(encodedBugData) > maxBug or len(
                        encodedFixData) > maxFix or len(finalUnkList) > maxUnk:
                    print(
                        "{0}: [{2} - {3} ({1})] Some tokens were not parsed (lengths), ignoring (lenUnk = {1})"
                        .format(i + 1, len(finalUnkList), len(encodedBugData),
                                len(encodedFixData)))
                else:
                    print("{0}: [{2} - {3} ({1})] Done (lenUnk = {1})".format(
                        i + 1, len(finalUnkList), len(encodedBugData),
                        len(encodedFixData)))
                    f.write(
                        json.dumps({
                            'x': encodedBugData,
                            'y': encodedFixData
                        }) + '\n')

                i += 1
                print('Done {0}'.format(i), file=sys.stderr)

        print("All done, exiting...")
Esempio n. 33
0
    def test_move(self):
        c = Checkers()

        # COMMENTS ARE FROM THE PLAYER'S PERSPECTIVE

        # move red forward left
        self.assertTrue(c.board[3][2] is None)
        c.move(src=(1, 2), dest=(2, 3))
        self.assertTrue(c.board[3][2] is not None)

        # move red forward right
        self.assertTrue(c.board[4][1] is None)
        c.move(src=(2, 3), dest=(1, 4))
        self.assertTrue(c.board[4][1] is not None)

        # try moving red where an enemy is
        self.assertTrue(c.board[5][0].color is Checkers.colors['black'])
        with self.assertRaises(MoveError):
            c.move(src=(1, 4), dest=(0, 5))
        self.assertTrue(c.board[5][0].color is Checkers.colors['black'])

        # try moving red backwards as a regular piece
        self.assertTrue(c.board[3][2] is None)
        with self.assertRaises(MoveError):
            c.move(src=(1, 4), dest=(2, 3))

        # try moving a piece that doesn't exist
        self.assertTrue(c.board[3][1] is None)
        with self.assertRaises(KeyError):
            c.move(src=(1, 3), dest=(2, 2))
Esempio n. 34
0
class FixTautOORCmp():
    def __init__(self):
        self.ccdb = CCDatabase(config.getCcDbFile())
        self.codeChecker = CodeChecker(config.getRepoDir())
        self.code = []
        self.checkers = Checkers()

    def loadCodeFromFile(self):
        with open(self.bugData.getFile(), 'r') as file:
            code = file.readlines()
            self.code = []
            for line in code:
                self.code.append(line.replace("\r\n", "\n"))

    def saveCodeToFile(self):
        with open(self.bugData.getFile(), 'w') as file:
           #file.write(self.lineEnding.join(self.code))
           file.writelines(self.code)
    
    def findMatchingDefine(self, value):
        patValue = '({0})'
        patName = '[ \t]([0-9a-zA-Z_]+)[ \t]'
        for line in self.code:
            if line[:7] == '#define':
                if re.search(patValue.format(value), line):
                    return re.search(patName, line).group(1)
        return None
    
    def fix(self, file):
        fileData = self.ccdb.getFileData(file)
        print(fileData)
        bugs = self.ccdb.getAllBugsForChecker('clang-diagnostic-constant-conversion')
        lines = []
        linesSingle = {}
        linesToSuppress = []
        for bug in bugs:
            if bug[2] == fileData[-1][0]:
                self.bugData = self.ccdb.getBugData(bug[0])
                if self.bugData.getLine() in linesSingle:
                    linesSingle[self.bugData.getLine()] += 1
                else:
                    linesSingle[self.bugData.getLine()] = 1
        for k in linesSingle:
            if linesSingle[k] > 5:
                linesToSuppress.append(k)
        inserts = 0
        for bug in bugs:
            if bug[2] == fileData[-1][0]:
                self.bugData = self.ccdb.getBugData(bug[0])
                if self.bugData.getLine() not in lines:
                    self.loadCodeFromFile()

                    if self.bugData.getLine() not in linesToSuppress:
                        tokens = self.checkers.extractTokensForChecker('clang-diagnostic-constant-conversion', self.bugData.getMessage())
                        test = tokens
                        line = self.code[self.bugData.getLine() - 1 + inserts]
                        newLine = line.replace(tokens[0]['value'], '({0}){1}'.format(tokens[1]['value'], tokens[0]['value']))
                        if line == newLine:
                            tokens[0]['value'] = self.findMatchingDefine(tokens[0]['value'])
                            if tokens[0]['value'] is not None:
                                newLine = line.replace(tokens[0]['value'], '({0}){1}'.format(tokens[1]['value'], tokens[0]['value']))
                        if line == newLine:
                            s = re.search('= (.*);', line)
                            expr = s.group(1)
                            newLine = line.replace(expr, '({0})({1})'.format(tokens[1]['value'], expr))
                        self.code[self.bugData.getLine() - 1 + inserts] = newLine
                    else:
                        line = self.code[self.bugData.getLine() - 1 + inserts]
                        self.code[self.bugData.getLine() - 1 + inserts] = '// codechecker_intentional [clang-diagnostic-constant-conversion] Suppress\n{0}'.format(line)
                        inserts += 1

                    self.saveCodeToFile()
                    lines = lines + list(range((self.bugData.getLine() - 11), (self.bugData.getLine() + 10)))
                    
                    print("Passed, suppressed: {0}".format(self.bugData.getLine() in linesToSuppress))
                    print(self.bugData.getLine())
                    pass
                else:
                    print("Dismissed")
                    print(self.bugData.getLine())
                    pass
Esempio n. 35
0
 def __init__(self):
     self.vcs = GitProvider(config.getRepoDir())
     self.ccdb = CCDatabase(config.getCcDbFile())
     self.codeChecker = CodeChecker(config.getRepoDir())
     self.checkers = Checkers()
     self.loadCommitList()
Esempio n. 36
0
class CheckersGame(Game):
    def __init__(self, history=[]):
        # Rollout statistics
        self.child_visits = []
        # Terminal values for the first player
        # 1 for win
        # 0 for draw
        # -1 for loss
        # None for incomplete
        self.game_value = None

        # XXX Conventions:
        # - Black player moves first
        # - Ego-centric views assume the king row are at the top, i.e. starts at the bottom (Second player has the same view as absolute)
        self.ch = Checkers()

        # Action space
        self.actions = []
        # Simple moves
        for from_sq in range(self.ch.n_positions):
            for to_sq in self.ch.neighbors[from_sq]:
                if to_sq is not None:
                    simple_move = (from_sq, to_sq)
                    self.actions.append(simple_move)

        assert 98 == len(self.actions), 'There should be 98 simple moves.'
        # Jumps
        for from_sq in range(self.ch.n_positions):
            row, col = self.ch.sq2pos(from_sq)
            # For each direction
            for di, (drow, dcol) in enumerate(Checkers.dir2del):
                next_row, next_col = row + 2 * drow, col + 2 * dcol
                if 0 <= next_row < self.ch.size and 0 <= next_col < self.ch.size:
                    # Within bound
                    to_sq = self.ch.pos2sq(next_row, next_col)
                    jump = (from_sq, to_sq)
                    self.actions.append(jump)
        self.num_actions = len(self.actions)
        assert 98 + 72 == self.num_actions, 'There should be 98 simple moves and 72 jumps.'
        # Inverse dictionary
        self.action2ind = {
            action: ind
            for ind, action in enumerate(self.actions)
        }
        # Square mapping from absolute to first player's ego-centric (reflect through the center)
        self.abs2ego_sq = {}
        for sq in range(self.ch.n_positions):
            row, col = self.ch.sq2pos(sq)
            re_row, re_col = -row + self.ch.size - 1, -col + self.ch.size - 1
            re_sq = self.ch.pos2sq(re_row, re_col)
            self.abs2ego_sq[sq] = re_sq
        # Inverse
        self.ego2abs_sq = {re_sq: sq for sq, re_sq in self.abs2ego_sq.items()}

        # Move mapping from absolute to first player's ego-centric
        self.abs2ego_ac = {}
        for ac, (from_sq, to_sq) in enumerate(self.actions):
            ego_move = (self.abs2ego_sq[from_sq], self.abs2ego_sq[to_sq])
            ego_ac = self.action2ind[ego_move]
            self.abs2ego_ac[ac] = ego_ac
        # Inverse
        self.ego2abs_ac = {
            ego_ac: ac
            for ac, ego_ac in self.abs2ego_ac.items()
        }

        # Fast forward to the last state by taking actions from history
        self.history = []
        for action in history:
            self.apply(action)

    def clone(self):
        game = CheckersGame()
        state = self.ch.save_state()
        game.ch.restore_state(state)
        return game

    def apply(self, action_index):
        from_sq, to_sq = self.actions[action_index]
        board, turn, last_moved_piece, all_next_moves, winner = self.ch.move(
            from_sq, to_sq)

        # Terminate when one player wins
        if winner == 'black':
            self.game_value = 1
        elif winner == 'white':
            self.game_value = -1

        self.history.append(action_index)

    def legal_actions(self):
        moves = self.ch.legal_moves()
        action_idices = {self.action2ind[move] for move in moves}
        return action_idices

    def is_first_player_turn(self):
        return self.ch.turn == 'black'

    def ego_board_representation(self):
        # XXX Channels
        # 0 my men
        # 1 my kings
        # 2 opponent's men
        # 3 opponent's kings
        # 4 my last moved piece
        # QUESTION: try indicating the king row and skipping ego transform?
        rep = np.zeros((self.ch.size, self.ch.size, 5))
        if self.ch.turn == 'white':
            # Same as the absolute view
            for sq in self.ch.board['white']['men']:
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 0] = 1
            for sq in self.ch.board['white']['kings']:
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 1] = 1
            for sq in self.ch.board['black']['men']:
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 2] = 1
            for sq in self.ch.board['black']['kings']:
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 3] = 1
            if self.ch._last_moved_piece is not None:
                row, col = self.ch.sq2pos(self.ch._last_moved_piece)
                rep[row, col, 4] = 1
        else:
            # Need to invert the board
            for sq in self.ch.board['black']['men']:
                sq = self.abs2ego_sq[sq]
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 0] = 1
            for sq in self.ch.board['black']['kings']:
                sq = self.abs2ego_sq[sq]
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 1] = 1
            for sq in self.ch.board['white']['men']:
                sq = self.abs2ego_sq[sq]
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 2] = 1
            for sq in self.ch.board['white']['kings']:
                sq = self.abs2ego_sq[sq]
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 3] = 1
            if self.ch._last_moved_piece is not None:
                sq = self.abs2ego_sq[self.ch._last_moved_piece]
                row, col = self.ch.sq2pos(sq)
                rep[row, col, 4] = 1
        return rep

    def ego_sample(self, state_index: int):
        # Fast forward
        game = CheckersGame(list(self.history[:state_index]))
        # Ego-centric views of the current player
        rep = game.ego_board_representation()
        # Zero-sum game
        ego_val = self.game_value if game.is_first_player_turn() else (
            0 - self.game_value)
        # Ego-centric actions
        if game.is_first_player_turn():
            # Invert actions for the first player
            visits = np.zeros(self.num_actions)
            for i in range(self.num_actions):
                visits[self.abs2ego_ac[i]] = self.child_visits[state_index][i]
        else:
            visits = np.asarray(self.child_visits[state_index])
        return rep, ego_val, visits

    def ego2abs_policy(self, is_first_player, ego_policy):
        if is_first_player:
            policy = np.zeros(self.num_actions)
            for ego_ac, pr in enumerate(ego_policy):
                policy[self.ego2abs_ac[ego_ac]] = pr
        else:
            policy = ego_policy
        return policy