<commit_before>"""Implementation of the State design pattern for line-following strategy"""

import string
import sys


class StateTable(object):
    """Maintains key variables for top-level decision-making"""

    obj = None

    def __init__(self):
        self.start = False
        self.lineFound = True
        self.blueFound = False
        self.redFound = False
        self.xFound = False
        self.centered = False
        self.lastHeading = 180
        self.currentHeading = 180
        self.intersections = 0
        self.positions = 0
        self.shotsTaken = 0

    @classmethod
    def getInstance(cls):
        if cls.obj is None:
            cls.obj = StateTable()
        return cls.obj


class State(object):
    """Provide an interface for the State subclasses"""

    def __init__(self):
        self.stateTable = StateTable.getInstance()

    def run(self):
        assert 0, "run() not implemented"

    def next(self):
        assert 0, "next() not implemented"


class StateMachine:
    """Initializes and execute states, and manage transitions"""

    def __init__(self, initialState):
        self.currentState = initialState

    def runAll(self):
        while (self.currentState != self.finish):
            Robot.test.run()  # for testing only
            self.currentState.run()
            self.currentState = self.currentState.next()
            if self.currentState is self.finish:
                self.currentState.run()


class TestStates(State):
    """Read from a test file and modify the state table"""

    f = open("./tests/input/RobotNormalTest.txt")

    def __init__(self):
        super(TestStates, self).__init__()

    def run(self):
        input = self.f.readline()
        testCase = input.strip()
        print "Next Test Case: " + testCase
        if testCase == "start":
            self.stateTable.start = True
        elif testCase == "blue":
            self.stateTable.blueFound = True
        elif testCase == "red":
            self.stateTable.redFound = True
        elif testCase == "intersection":
            self.stateTable.xFound = True
        elif testCase == "line":
            self.stateTable.lineFound = True
        elif testCase == "centered":
            self.stateTable.centered = True

    def next(self):
        print "Test Next"


class WaitingForStart(State):
    """Waits for the course remote start signal after startup"""

    def __init__(self):
        super(WaitingForStart, self).__init__()

    def run(self):
        if self.stateTable.start is False:
            print "Execute: Still waiting\n"
        else:
            print "Execute: Starting up!\n"

    def next(self):
        if self.stateTable.start is False:
            print "Transition to next State: Waiting\n"
            return Robot.waiting
        else:
            print "Transition to next State: Jerk\n"
            return Robot.jerk


class Jerk(State):
    """Execute rote behavior to move out of the start box"""

    def __init__(self):
        super(Jerk, self).__init__()

    def run(self):
        print "Execute: Rote initial move.\n"

    def next(self):
        print "Transition to next State: FindLine\n"
        return Robot.findLine


class FindLine(State):
    """Checks for a line to follow or calls a rescue behavior"""

    def __init__(self):
        super(FindLine, self).__init__()

    def run(self):
        if self.stateTable.start is False:
            #oscillate
            print "Execute: Looking for line.\n"
        else:
            print "Line found!\n"
            pass

    def next(self):
        print "Transition to next State: Following\n"
        return Robot.following


class Following(State):
    """Follows a line until an event is indicated"""

    def __init__(self):<commit_msg>Added Notes from Code Review<commit_after>"""Implementation of the State design pattern for line-following strategy"""

import string
import sys


class StateTable(object):
    """Maintains key variables for top-level decision-making"""

    obj = None

    def __init__(self):
        self.start = False
        self.lineFound = True
        self.blueFound = False
        self.redFound = False
        self.xFound = False
        self.centered = False
        self.lastHeading = 180  # tracks the last heading for navigation
        self.currentHeading = 180
        self.intersections = 0
        self.positions = 0 # firing positions tracker
        self.shotsTaken = 0

    @classmethod
    def getInstance(cls):
        if cls.obj is None:
            cls.obj = StateTable()
        return cls.obj


class State(object):
    """Provide an interface for the State subclasses"""

    def __init__(self):
        self.stateTable = StateTable.getInstance()

    def run(self):
        assert 0, "run() not implemented"

    def next(self):
        assert 0, "next() not implemented"


class StateMachine:
    """Initializes and execute states, and manage transitions"""

    def __init__(self, initialState):
        self.currentState = initialState

    def runAll(self):
        while (self.currentState != self.finish):
            Robot.test.run()  # for testing only
            self.currentState.run()
            self.currentState = self.currentState.next()
            if self.currentState is self.finish:
                self.currentState.run()


class TestStates(State):
    """Read from a test file and modify the state table"""
    # TODO: (PaladinEng) Move this string to config
    f = open("./tests/input/RobotNormalTest.txt")

    def __init__(self):
        super(TestStates, self).__init__()
    #TODO: (avsmith5) Switch print statements to logging
    def run(self):
        input = self.f.readline()
        testCase = input.strip()
        print "Next Test Case: " + testCase
        if testCase == "start":
            self.stateTable.start = True
        elif testCase == "blue":
            self.stateTable.blueFound = True
        elif testCase == "red":
            self.stateTable.redFound = True
        elif testCase == "intersection":
            self.stateTable.xFound = True
        elif testCase == "line":
            self.stateTable.lineFound = True
        elif testCase == "centered":
            self.stateTable.centered = True

    def next(self):
        print "Test Next"


class WaitingForStart(State):
    """Waits for the course remote start signal after startup"""

    def __init__(self):
        super(WaitingForStart, self).__init__()
    #TODO: (avsmith5) Docstrongs on methods
    def run(self):
        if self.stateTable.start is False:
            print "Execute: Still waiting\n"
        else:
            print "Execute: Starting up!\n"

    def next(self):
        if self.stateTable.start is False:
            print "Transition to next State: Waiting\n"
            return Robot.waiting
        else:
            print "Transition to next State: Jerk\n"
            return Robot.jerk


class Jerk(State):
    """Execute rote behavior to move out of the start box"""

    def __init__(self):
        super(Jerk, self).__init__()

    def run(self):
        print "Execute: Rote initial move.\n"

    def next(self):
        print "Transition to next State: FindLine\n"
        return Robot.findLine


class FindLine(State):
    """Checks for a line to follow or calls a rescue behavior"""

    def __init__(self):
        super(FindLine, self).__init__()

    def run(self):
        if self.stateTable.lineFound is False:
            #oscillate
            print "Execute: Looking for line.\n"
        else:
            print "Line found!\n"
            pass

    def next(self):
        print "Transition to next State: Following\n"
        return Robot.following


class Following(State):
    """Follows a line until an event is indicated"""

    def __init__(self):
{'fraction_non_alphanumeric': 0.09749232343909929, 'fraction_numerical': 0.0030706243602865915, 'mean_word_length': 2.169910786699108, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '774493', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2114, 'n_words': 675}