class TestStringPattern(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestStringPattern, self).__init__(*args, **kwargs)

        self.m = StateMachine()
        self.m.add_state("start_state", state_zero)
        self.m.add_state("first_state", state_one)
        self.m.add_state("second_state", state_two)
        self.m.add_state("third_state", state_three)
        self.m.add_state("last_state", None, end_state=1)
        self.m.add_state("error_state", None, end_state=1)
        self.m.set_start("start_state")

    def test_right_pattern(self):
        self.assertEqual(self.m.run("12@123#"),
                         "reached last_state which is an end state")
        self.assertEqual(self.m.run("@3445#"),
                         "reached last_state which is an end state")
        self.assertEqual(self.m.run("123@5363@968495#"),
                         "reached last_state which is an end state")
        self.assertEqual(self.m.run("123@@968495#"),
                         "reached last_state which is an end state")

    def test_wrong_pattern(self):
        self.assertEqual(self.m.run("12@2345"),
                         "reached error_state which is an end state")
        self.assertEqual(self.m.run("122345#"),
                         "reached error_state which is an end state")
        self.assertEqual(self.m.run("12@23f45#"),
                         "reached error_state which is an end state")
Exemplo n.º 2
0
    stateMachine = StateMachine()
    stateMachine.add_state("STARTING_INTERACTION", startInteraction)
    stateMachine.add_state("WAITING_FOR_ROBOT_TO_CONNECT",
                           waitForRobotToConnect)
    stateMachine.add_state("WAITING_FOR_WORD", waitForWord)
    stateMachine.add_state("RESPONDING_TO_NEW_WORD", respondToNewWord)
    stateMachine.add_state("PUBLISHING_WORD", publishWord)
    stateMachine.add_state("WAITING_FOR_LETTER_TO_FINISH",
                           waitForShapeToFinish)
    stateMachine.add_state("ASKING_FOR_FEEDBACK", askForFeedback)
    stateMachine.add_state("WAITING_FOR_FEEDBACK", waitForFeedback)
    stateMachine.add_state("RESPONDING_TO_DEMONSTRATION_FULL_WORD",
                           respondToDemonstrationWithFullWord)
    stateMachine.add_state("EXIT", None, end_state=True)
    stateMachine.set_start("WAITING_FOR_ROBOT_TO_CONNECT")
    infoForStartState = {
        'state_goTo': ["STARTING_INTERACTION"],
        'state_cameFrom': None
    }

    #listen for words to write
    words_subscriber = rospy.Subscriber(WORDS_TOPIC, String, onWordReceived)
    #listen for request to clear screen (from tablet)
    clear_subscriber = rospy.Subscriber(CLEAR_SURFACE_TOPIC, Empty,
                                        onClearScreenReceived)
    #listen for user-drawn shapes
    shape_subscriber = rospy.Subscriber(PROCESSED_USER_SHAPE_TOPIC, ShapeMsg,
                                        onUserDrawnShapeReceived)
    #listen for user-drawn finger gestures
    gesture_subscriber = rospy.Subscriber(GESTURE_TOPIC, PointStamped,
Exemplo n.º 3
0

def not_state_transitions(txt):
    splitted_txt = txt.split(None, 1)
    word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")
    if word in positive_adjectives:
        newState = "neg_state"
    elif word in negative_adjectives:
        newState = "pos_state"
    else:
        newState = "error_state"
    return (newState, txt)


if __name__ == "__main__":
    m = StateMachine()
    m.add_state("Start", start_transitions)  # 添加初始状态
    m.add_state("Python_state", python_state_transitions)
    m.add_state("is_state", is_state_transitions)
    m.add_state("not_state", not_state_transitions)
    m.add_state("neg_state", None, end_state=1)  # 添加最终状态
    m.add_state("pos_state", None, end_state=1)
    m.add_state("error_state", None, end_state=1)

    m.set_start("Start")  # 设置开始状态
    m.run("Python is great")
    m.run("Python is not fun")
    m.run("Perl is ugly")
    m.run("Python is")
    m.run("Pythoniseasy")
Exemplo n.º 4
0
class Testblock:
    def __init__(self, testblock_name, metrics):

        self.testblock_name = testblock_name
        rospy.Subscriber("/atf/" + self.testblock_name + "/Trigger", Trigger,
                         self.trigger_callback)

        self.transition = None
        self.metrics = metrics

        self.m = StateMachine(self.testblock_name)
        self.m.add_state(Status.PURGED, self.purged_state)
        self.m.add_state(Status.ACTIVE, self.active_state)
        self.m.add_state(Status.PAUSED, self.paused_state)
        self.m.add_state(Status.FINISHED, self.finished_state, end_state=True)
        self.m.add_state(Status.ERROR, self.error_state, end_state=True)
        self.m.set_start(Status.PURGED)

        self.m.run()

    def trigger_callback(self, msg):
        self.transition = msg.trigger

    def purge(self):
        for metric in self.metrics:
            metric.purge()

    def activate(self):
        for metric in self.metrics:
            metric.start()

    def pause(self):
        self.stop()

    def finish(self):
        self.stop()

    def exit(self):
        self.transition = Trigger.ERROR

    def stop(self):
        for metric in self.metrics:
            metric.stop()

    def get_state(self):
        return self.m.get_current_state()

    def purged_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue

        if self.transition == Trigger.PURGE:
            # is already purged
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            self.activate()
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            new_state = Status.ERROR
        elif self.transition == Trigger.FINISH:
            new_state = Status.ERROR
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def active_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue
        if self.transition == Trigger.PURGE:
            self.purge()
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            # is already active
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            self.pause()
            new_state = Status.PAUSED
        elif self.transition == Trigger.FINISH:
            self.finish()
            new_state = Status.FINISHED
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def paused_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue
        if self.transition == Trigger.PURGE:
            self.purge()
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            self.activate()
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            # is already paused
            new_state = Status.PAUSED
        elif self.transition == Trigger.FINISH:
            self.finish()
            new_state = Status.FINISHED
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def finished_state(self):
        pass

    def error_state(self):
        pass
Exemplo n.º 5
0
def not_state_transitions(txt):
    splitted_txt = txt.split(None, 1)
    word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")
    if word in positive_adjectives:
        new_state = "neg_state"
    elif word in negative_adjectives:
        new_state = "pos_state"
    else:
        new_state = "error_state"
    return (new_state, txt)


def neg_state(txt):
    print("Hallo")
    return ("neg_state", "")

if __name__ == "__main__":
    m = StateMachine()
    m.add_state("Start", start_transitions)
    m.add_state("Python_state", python_state_transition)
    m.add_state("is_state", is_state_transitions)
    m.add_state("not_state", not_state_transitions)
    m.add_state("neg_state", None, end_state=1)
    m.add_state("pos_state", None, end_state=1)
    m.add_state("error_state", None, end_state=1)
    m.set_start("Start")
    m.run("Python is great")
    m.run("Python is difficult")
    m.run("Perl is ugly")
        return "green", next_input
    if timer_input == 0:
        return "red", next_input


def state_green(timer_input, next_input=randint(0, 1)):
    if timer_input == 1:
        return "yellow", next_input
    if timer_input == 0:
        return "green", next_input


def state_yellow(timer_input, next_input=randint(0, 1)):
    if timer_input == 1:
        return "red", next_input
    if timer_input == 0:
        return "yellow", next_input


if __name__ == "__main__":
    m = StateMachine()
    m.add_state("red", state_red)
    m.add_state("green", state_green)
    m.add_state("yellow", state_yellow)
    m.add_state("last_state", None, end_state=1)
    m.add_state("error_state", None, end_state=1)
    m.set_start("red")

    while True:
        m.run(randint(0, 1))
Exemplo n.º 7
0
    stateMachine.add_state("WAITING_FOR_ROBOT_TO_CONNECT", waitForRobotToConnect);
    stateMachine.add_state("WAITING_FOR_WORD", waitForWord);
    stateMachine.add_state("RESPONDING_TO_NEW_WORD", respondToNewWord);
    stateMachine.add_state("PUBLISHING_WORD", publishWord);
    stateMachine.add_state("PUBLISHING_LETTER", publishShape);
    stateMachine.add_state("WAITING_FOR_LETTER_TO_FINISH", waitForShapeToFinish);
    stateMachine.add_state("ASKING_FOR_FEEDBACK", askForFeedback);
    stateMachine.add_state("WAITING_FOR_FEEDBACK", waitForFeedback);
    stateMachine.add_state("RESPONDING_TO_FEEDBACK", respondToFeedback);
    stateMachine.add_state("RESPONDING_TO_DEMONSTRATION", respondToDemonstration);
    stateMachine.add_state("RESPONDING_TO_TEST_CARD", respondToTestCard);
    #stateMachine.add_state("RESPONDING_TO_TABLET_DISCONNECT", respondToTabletDisconnect);
    stateMachine.add_state("WAITING_FOR_TABLET_TO_CONNECT", waitForTabletToConnect);
    stateMachine.add_state("STOPPING", stopInteraction);
    stateMachine.add_state("EXIT", None, end_state=True);
    stateMachine.set_start("WAITING_FOR_ROBOT_TO_CONNECT");
    infoForStartState = {'state_goTo': ["STARTING_INTERACTION"], 'state_cameFrom': None};
    
    wordToLearn = args.word;
    if(wordToLearn is not None):
        message = String();
        message.data = wordToLearn;
        onWordReceived(message);
    else:
        print('Waiting for word to write');
    
    stateMachine.run(infoForStartState);    
    
    rospy.spin();

    tabletWatchdog.stop();
Exemplo n.º 8
0
class Testblock:
    def __init__(self, testblock_name, metrics):

        self.testblock_name = testblock_name
        rospy.Subscriber("/atf/" + self.testblock_name + "/Trigger", Trigger, self.trigger_callback)

        self.transition = None
        self.metrics = metrics

        self.m = StateMachine(self.testblock_name)
        self.m.add_state(Status.PURGED, self.purged_state)
        self.m.add_state(Status.ACTIVE, self.active_state)
        self.m.add_state(Status.PAUSED, self.paused_state)
        self.m.add_state(Status.FINISHED, self.finished_state, end_state=True)
        self.m.add_state(Status.ERROR, self.error_state, end_state=True)
        self.m.set_start(Status.PURGED)

        self.m.run()

    def trigger_callback(self, msg):
        self.transition = msg.trigger

    def purge(self):
        for metric in self.metrics:
            metric.purge()

    def activate(self):
        for metric in self.metrics:
            metric.start()

    def pause(self):
        self.stop()

    def finish(self):
        self.stop()

    def exit(self):
        self.transition = Trigger.ERROR

    def stop(self):
        for metric in self.metrics:
            metric.stop()

    def get_state(self):
        return self.m.get_current_state()

    def purged_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue

        if self.transition == Trigger.PURGE:
            # is already purged
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            self.activate()
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            new_state = Status.ERROR
        elif self.transition == Trigger.FINISH:
            new_state = Status.ERROR
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def active_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue
        if self.transition == Trigger.PURGE:
            self.purge()
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            # is already active
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            self.pause()
            new_state = Status.PAUSED
        elif self.transition == Trigger.FINISH:
            self.finish()
            new_state = Status.FINISHED
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def paused_state(self):
        while not rospy.is_shutdown() and self.transition is None:
            continue
        if self.transition == Trigger.PURGE:
            self.purge()
            new_state = Status.PURGED
        elif self.transition == Trigger.ACTIVATE:
            self.activate()
            new_state = Status.ACTIVE
        elif self.transition == Trigger.PAUSE:
            # is already paused
            new_state = Status.PAUSED
        elif self.transition == Trigger.FINISH:
            self.finish()
            new_state = Status.FINISHED
        elif self.transition == Trigger.ERROR:
            new_state = Status.ERROR
        else:
            new_state = Status.ERROR
        self.transition = None
        return new_state

    def finished_state(self):
        pass

    def error_state(self):
        pass
Exemplo n.º 9
0
def not_state_transitions(txt):
    splitted_txt = txt.split(None, 1)
    word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")
    if word in positive_adjectives:
        new_state = "neg_state"
    elif word in negative_adjectives:
        new_state = "pos_state"
    else:
        new_state = "error_state"
    return (new_state, txt)


def neg_state(txt):
    print("Hallo")
    return ("neg_state", "")


if __name__ == "__main__":
    m = StateMachine()
    m.add_state("Start", start_transitions)
    m.add_state("Python_state", python_state_transition)
    m.add_state("is_state", is_state_transitions)
    m.add_state("not_state", not_state_transitions)
    m.add_state("neg_state", None, end_state=1)
    m.add_state("pos_state", None, end_state=1)
    m.add_state("error_state", None, end_state=1)
    m.set_start("Start")
    m.run("Python is great")
    m.run("Python is difficult")
    m.run("Perl is ugly")
Exemplo n.º 10
0
    rospy.init_node("activity_nao")

    # add all state to state machine
    stateMachine = StateMachine()
    stateMachine.add_state("STARTING_INTERACTION", startInteraction)
    stateMachine.add_state("WAITING_FOR_PATH", waitForPath)
    stateMachine.add_state("RESPONDING_TO_NEW_PATH", respondToNewPath)
    stateMachine.add_state("WAITING_FOR_PATH_TO_FINISH", waitForPathToFinish)
    #stateMachine.add_state("ASKING_PLAY_GAME", askToPlayGame)
    stateMachine.add_state("WAITING_FOR_GAME_TO_FINISH", waitForGameToFinish)
    stateMachine.add_state("RESPONDING_TO_CHILD_SCORE", respondToChildScore)
    stateMachine.add_state("IDLE", onIdle)
    stateMachine.add_state("EXIT", None, end_state=True)
    stateMachine.add_state("FAIL", onRobotFailed)
    stateMachine.set_start("STARTING_INTERACTION")
    infoForStartState = {'state_cameFrom': None}

    #listen for path to draw
    path_subscriber = rospy.Subscriber(PATH_TOPIC, Path, onPathReceived)
    #listen for child score
    score_subscriber = rospy.Subscriber(SCORE_TOPIC, Float32, onScoreReceived)
    #listen for state requests
    state_request_subscriber = rospy.Subscriber(STATE_REQUEST_TOPIC, String,
                                                onStateRequestReceived)
    #listen for finished path
    path_finished_subscriber = rospy.Subscriber(PATH_FINISHED_TOPIC, Empty,
                                                onPathFinished)
    #listen for dialog options
    dialog_options_subscriber = rospy.Subscriber(DIALOG_OPTION_TOPIC, String,
                                                 onDialogOptionsReceived)
Exemplo n.º 11
0
    if char == "@":
        return "first_state", text
    if char == "#":
        return "third_state", text
    return "start_state", text


def state_three(text: str):
    try:
        char, text = text[0], text[1:]
    except IndexError:
        return "last_state", text

    if char == "@":
        return "first_state", text
    return "start_state", text


if __name__ == "__main__":
    m = StateMachine()
    m.add_state("start_state", state_zero)
    m.add_state("first_state", state_one)
    m.add_state("second_state", state_two)
    m.add_state("third_state", state_three)
    m.add_state("last_state", None, end_state=1)
    m.add_state("error_state", None, end_state=1)
    m.set_start("start_state")
    m.run("@234#")
    m.run("1@638#")
    m.run("@139#1")