Пример #1
0
 def deepcopy(self):
     path = self.path[:] if self.path != None else None
     c = MoveVector(path,
                    deepcopy(self.oldSubtree),
                    deepcopy(self.newSubtree),
                    start=deepcopy(self.start))
     return c
Пример #2
0
 def deepcopy(self):
     path = self.path[:] if self.path != None else None
     c = SwapVector(path,
                    deepcopy(self.oldSubtree),
                    deepcopy(self.newSubtree),
                    start=deepcopy(self.start))
     c.oldPath = self.oldPath[:] if self.oldPath != None else None
     c.newPath = self.newPath[:] if self.newPath != None else None
     return c
Пример #3
0
    def applyChange(self, caller=None):
        tree = deepcopy(self.start)
        treeSpot = self.traverseTree(tree)
        if treeSpot == -99:
            return None

        # Make the change in the last position
        location = self.path[0]
        if type(treeSpot) == list:
            # Remove the old line
            if location < len(treeSpot):
                if compareASTs(treeSpot[location],
                               self.oldSubtree,
                               checkEquality=True) != 0:
                    log.error("DeleteVector\tapplyChange\t" + str(caller) +
                              "\t" + "Delete old values don't match: " +
                              str(self) + "\n" +
                              str(printFunction(self.start)))
                del treeSpot[location]
            else:
                log.error("DeleteVector\tapplyChange\t\tBad location: " +
                          str(location) + "\t" + str(self.oldSubtree))
                return None
        else:
            log.error("DeleteVector\tapplyChange\t\tBroken at: " +
                      str(location))
            return None
        return tree
Пример #4
0
    def applyChange(self, caller=None):
        tree = deepcopy(self.start)
        treeSpot = self.traverseTree(tree)
        if treeSpot == -99:
            return None

        # Make the change in the last position
        location = self.path[0]
        if type(location) == tuple and hasattr(treeSpot, location[0]):
            oldSpot = getattr(treeSpot, location[0])
            # Make life easier for ourselves when applying multiple changes at once
            if self.newSubtree != None and oldSpot != None:
                if hasattr(oldSpot, "lineno"):
                    self.newSubtree.lineno = oldSpot.lineno
                if hasattr(oldSpot, "col_offset"):
                    self.newSubtree.col_offset = oldSpot.col_offset
            if compareASTs(oldSpot, self.oldSubtree, checkEquality=True) != 0:
                log.error("ChangeVector\tapplyChange\t" + str(caller) + "\t" +
                          "Change old values don't match: " + str(self) +
                          "\n" + str(printFunction(self.start)))
            setattr(treeSpot, location[0], self.newSubtree)
            # SPECIAL CASE. If we're changing the variable name, get rid of metadata
            if type(treeSpot) == ast.Name and location[0] == "id":
                if hasattr(treeSpot, "originalId"):
                    del treeSpot.originalId
                if hasattr(treeSpot, "dontChangeName"):
                    del treeSpot.dontChangeName
                if hasattr(treeSpot, "randomVar"):
                    del treeSpot.randomVar
            elif type(treeSpot) == ast.arg and location[0] == "arg":
                if hasattr(treeSpot, "originalId"):
                    del treeSpot.originalId
                if hasattr(treeSpot, "dontChangeName"):
                    del treeSpot.dontChangeName
                if hasattr(treeSpot, "randomVar"):
                    del treeSpot.randomVar
        elif type(location) == int and type(treeSpot) == list:
            # Need to swap out whatever is in this location
            if location >= 0 and location < len(treeSpot):
                if hasattr(treeSpot[location], "lineno"):
                    self.newSubtree.lineno = treeSpot[location].lineno
                if hasattr(treeSpot[location], "col_offset"):
                    self.newSubtree.col_offset = treeSpot[location].col_offset
                treeSpot[location] = self.newSubtree
            else:
                log.error("ChangeVector\tapplyChange\tDoesn't fit in list: " +
                          str(location) + "\n" + printFunction(self.start))
        else:
            log.error("ChangeVector\tapplyChange\t\tBroken at: " +
                      str(location))
        return tree
Пример #5
0
    def applyChange(self, caller=None):
        tree = deepcopy(self.start)
        treeSpot = self.traverseTree(tree)
        if treeSpot == -99:
            return None

        if type(treeSpot) == list and self.oldSubtree < len(
                treeSpot) and self.newSubtree < len(treeSpot):
            # We'll remove the item from the tree, then put it back in
            item = treeSpot.pop(self.oldSubtree)
            treeSpot.insert(self.newSubtree, item)
        else:
            log.error("MoveVector\tapplyChange\t\tBroken at: " + str(treeSpot))
            return None
        return tree
Пример #6
0
    def applyChange(self, caller=None):
        tree = deepcopy(self.start)
        treeSpot = self.traverseTree(tree)
        if treeSpot == -99:
            return None

        # Make the change in the last position
        location = self.path[0]
        if type(treeSpot) == list:
            # Add the new line
            treeSpot.insert(location, self.newSubtree)
        else:
            log.error("AddVector\tapplyChange\t\tBroken at: " + str(location))
            return None
        return tree
Пример #7
0
    def deepcopy(this):
        s = State()
        s.id = this.id
        s.name = this.name
        s.score = this.score
        s.fun = this.fun
        s.tree = deepcopy(this.tree)

        properties = [
            "count", "goal", "goal_id", "goalDist", "next", "next_id", "edit",
            "hint", "treeWeight"
        ]
        for prop in properties:
            if hasattr(this, prop):
                setattr(s, prop, getattr(this, prop))
        return s
Пример #8
0
    def applyChange(self, caller=None):
        tree = deepcopy(self.start)

        if self.oldPath == None:
            treeSpot = self.traverseTree(tree)
            if treeSpot == -99:
                return None

            if type(treeSpot) == list and self.oldSubtree < len(treeSpot) and \
               self.newSubtree < len(treeSpot):
                (treeSpot[self.oldSubtree],
                 treeSpot[self.newSubtree]) = (treeSpot[self.newSubtree],
                                               treeSpot[self.oldSubtree])
            else:
                log.error("SwapVector\tapplyChange\t\tBroken at: " +
                          str(treeSpot))
                return None
        else:
            oldTreeSpot = self.traverseTree(tree, path=self.oldPath)
            newTreeSpot = self.traverseTree(tree, path=self.newPath)
            if oldTreeSpot == -99 or newTreeSpot == -99:
                return None

            if type(self.oldPath[0]) == int:
                tmpOldValue = oldTreeSpot[self.oldPath[0]]
            else:
                tmpOldValue = getattr(oldTreeSpot, self.oldPath[0][0])

            if type(self.newPath[0]) == int:
                tmpNewValue = newTreeSpot[self.newPath[0]]
            else:
                tmpNewValue = getattr(newTreeSpot, self.newPath[0][0])

            if type(self.oldPath[0]) == int:
                oldTreeSpot[self.oldPath[0]] = tmpNewValue
            else:
                setattr(oldTreeSpot, self.oldPath[0][0], tmpNewValue)

            if type(self.newPath[0]) == int:
                newTreeSpot[self.newPath[0]] = tmpOldValue
            else:
                setattr(newTreeSpot, self.newPath[0][0], tmpOldValue)
        return tree
Пример #9
0
    def deepcopy(this):
        s = CanonicalState()
        s.id = this.id
        s.name = this.name
        s.score = this.score
        s.fun = this.fun
        s.tree = deepcopy(this.tree)

        s.count = this.count
        s.goal = this.goal
        s.goal_id = this.goal_id
        s.goalDist = this.goalDist
        s.next = this.next
        s.next_id = this.next_id
        s.edit = this.edit

        if hasattr(this, "hint"):
            s.hint = this.hint
        if hasattr(this, "treeWeight"):
            s.treeWeight = this.treeWeight
        return s