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
class MissCartTest(unittest.TestCase):
    def setUp(self):

        self.sccm = ShoppingCartCRUDModel()
        self.sccm.register_cart(Cart())
        self.sm = StateMachine()

    def tearDown(self):
        logger.info(self.sm.state_records)
        copy_log_to_logfile()
        bo = Browser
        bo.browser.close()
        # self.crud.close_cart_browser()

    def state_create(self):
        self.sccm.create_operation()

    def state_read(self):
        self.sccm.read_operation()

    def state_update(self):
        self.sccm.update_operation()

    def state_delete(self):
        self.sccm.delete_operation()

    def test_crud(self):
        state_create = State('state_create')
        state_read = State('state_read')
        state_update = State('state_update')
        state_delete = State('state_delete')

        state_create.register_execution_callback(self.state_create)
        state_read.register_execution_callback(self.state_read)
        state_update.register_execution_callback(self.state_update)
        state_delete.register_execution_callback(self.state_delete)

        for state in [state_create, state_read, state_update, state_delete]:
            state.next_states = (state_create, state_delete, state_update,
                                 state_read)

        self.sm.execution_times = 20
        self.sm.register_start_state(state_create)
        self.sm.register_important_state(state_delete)
        self.sm.run()
Exemplo n.º 3
0
class CRUD(unittest.TestCase):
    def setUp(self):
        self.crud = CRUD_MISS()
        self.sm = StateMachine()

    def tearDown(self):
        print self.sm.state_records
        # self.crud.close_cart_browser()

    def state_create(self):
        self.crud.create_operation()

    def state_read(self):
        self.crud.read_operation()

    def state_update(self):
        self.crud.update_operation()

    def state_delete(self):
        self.crud.delete_operation()

    def test_crud(self):
        state_create = State('state_create')
        state_read = State('state_read')
        state_update = State('state_update')
        state_delete = State('state_delete')

        state_create.register_execution_callback(self.state_create)
        state_read.register_execution_callback(self.state_read)
        state_update.register_execution_callback(self.state_update)
        state_delete.register_execution_callback(self.state_delete)

        for state in [state_create, state_read, state_update, state_delete]:
            state.next_states = (state_create, state_delete, state_update,
                                 state_read)
            state.register_verification_callback(self.crud.check_operation)

        self.sm.execution_times = 30
        self.sm.register_start_state(state_create)
        self.sm.run()
Exemplo n.º 4
0
        '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,
                                          onSetActiveShapeGesture)
    debug = rospy.Publisher("debug", String, queue_size=10)

    rospy.wait_for_service('clear_all_shapes')
    rospy.sleep(2.0)  #Allow some time for the subscribers to do their thing,

    myBroker, postureProxy, motionProxy, textToSpeech, armJoints_standInit = ConnexionToNao.setConnexion(
        naoConnected, naoWriting, naoStanding, NAO_IP, PORT, LANGUAGE,
        effector)
    learningManager = LearningManager(datasetDirectory, datasetDirectory,
                                      robotDirectory, datasetDirectory)

    textShaper = TextShaper()
    screenManager = ScreenManager(0.2, 0.1395)

    stateMachine.run(infoForStartState)
    rospy.spin()
    tabletWatchdog.stop()
Exemplo n.º 5
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.º 6
0
def test_state_machine_complete():
    """Testing the complete state_machine application
    """
    class MyClass:
        def __init__(self):
            self.state1_entry_variable = False
            self.state1_exit_variable = False
            self.state1_routine_variable = False

            self.state2_entry_variable = False
            self.state2_exit_variable = False
            self.state2_routine_variable = False

            self.state3_entry_variable = False
            self.state3_exit_variable = False
            self.state3_routine_variable = False

        def func_exit_state1(self):
            self.state1_exit_variable = True

        def func_entry_state1(self):
            self.state1_entry_variable = True

        def func_routine_state1(self):
            self.state1_routine_variable = True

        def func_decision_state1(self):
            return 'state2'

        def func_exit_state2(self):
            self.state2_exit_variable = True

        def func_entry_state2(self):
            self.state2_entry_variable = True

        def func_routine_state2(self):
            self.state2_routine_variable = True

        def func_decision_state2(self):
            return 'state3'

        def func_exit_state3(self):
            self.state3_exit_variable = True

        def func_entry_state3(self):
            self.state3_entry_variable = True

        def func_routine_state3(self):
            self.state3_routine_variable = True

        def func_decision_state3(self):
            return 'state1'

    my_class = MyClass()

    state1 = State(name="state1",
                   routine_function=my_class.func_routine_state1,
                   decision_function=my_class.func_decision_state1,
                   entry_function=my_class.func_entry_state1,
                   exit_function=my_class.func_exit_state1,
                   is_async=False)

    state2 = State(name="state2",
                   routine_function=my_class.func_routine_state2,
                   decision_function=my_class.func_decision_state2,
                   entry_function=my_class.func_entry_state2,
                   exit_function=my_class.func_exit_state2,
                   is_async=False)

    state3 = State(name="state3",
                   routine_function=my_class.func_routine_state3,
                   decision_function=my_class.func_decision_state3,
                   entry_function=my_class.func_entry_state3,
                   exit_function=my_class.func_exit_state3,
                   is_async=False)

    state_machine = StateMachine()

    state_machine.add_states(state1, state2, state3)
    state_machine.initial_state = 'state1'

    # Execute
    for i in range(3):
        state_machine.run()

    # Verify if all my_class attribute are true
    for key in my_class.__dict__:
        # Verify if all attributes were setted
        if getattr(my_class, key) != True:
            assert True

    assert True
        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.º 8
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.º 9
0
    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();
    robotWatchdog.stop();
Exemplo n.º 10
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.º 11
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.º 12
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.º 13
0
class Test(unittest.TestCase):
    def setUp(self):
        self.sm = StateMachine()
        self.wifi_open = True
        self.news_open = True
        self.message = 'qajjc' + str(time.time())
        launch_app()

    def tearDown(self):
        print "==================="
        print time.strftime("%Y-%m-%d %X", time.localtime())
        print self.sm.state_records
        open_wifi()
        quit_app()

    def state_open_wifi(self):
        self.wifi_open = True
        print "[state] open_wifi"
        open_wifi()
        self.post_message()

    def state_close_wifi(self):
        self.wifi_open = False
        print "[state] close_wifi"
        close_wifi()
        self.post_message()

    def state_open_news_clint(self):
        self.news_open = True
        print "[state] open_news_clint"
        open_news_client()
        self.post_message()

    def state_close_news_clint(self):
        self.news_open = False
        print "[state] close_news_clint"
        close_news_client()
        self.post_message()

    def post_message(self):
        time.sleep(2)
        post_broadcast('title', self.message, 'summary', 'test.news.163.com',
                       'android')

    def check(self):
        if self.news_open and self.wifi_open:
            wait_message_exist(self.message)
        else:
            pass

    def test(self):
        state_open_wifi = State("state_open_wifi")
        state_close_wifi = State("state_close_wifi")
        state_open_news_client = State("state_open_news_client")
        state_close_news_client = State("state_close_news_client")

        state_open_wifi.register_execution_callback(self.state_open_wifi)
        state_open_wifi.register_verification_callback(self.check)
        state_close_wifi.register_execution_callback(self.state_close_wifi)
        state_close_wifi.register_verification_callback(self.check)
        state_open_news_client.register_execution_callback(
            self.state_open_news_clint)
        state_open_news_client.register_verification_callback(self.check)
        state_close_news_client.register_execution_callback(
            self.state_close_news_clint)
        state_close_news_client.register_verification_callback(self.check)

        state_open_wifi.next_states = [
            state_close_wifi, state_close_news_client, state_close_news_client
        ]
        state_close_wifi.next_states = [
            state_open_news_client, state_close_news_client, state_open_wifi
        ]
        state_open_news_client.next_states = [
            state_close_wifi,
            state_open_news_client,
            state_close_news_client,
        ]
        state_close_news_client.next_states = [
            state_open_wifi,
            state_close_wifi,
            state_open_news_client,
        ]

        self.sm.execution_times = 20
        self.sm.register_start_state(state_open_wifi)
        self.sm.run()
Exemplo n.º 14
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")
Exemplo n.º 15
0
import atexit
from peripherals import Peripherals
from state_machine import StateMachine
from sleep_state import SleepState

try:
    Peripherals.initialise()
    print(
        "Attention evacuation emergency all personnel must evacuate immediately"
    )
    stateMachine = StateMachine()
    stateMachine.set_initial_state(SleepState())
    stateMachine.run()
except KeyboardInterrupt:
    pass
finally:
    Peripherals.destroy()
    print("Shutdown complete")