====== Tutorial: State-based Modeling and Test Generation ====== ====Learning Objectives ==== * [[#What is a State Model]] * [[#Create Model]] * [[#Add States and Transitions]] * [[#Arranging States and Transitions]] * [[#Generate Test Cases]] * [[#Make Changes]] * [[#Model Graphs and Test Visualization]] ===== What is a State Model ====== State model is a directed graph consisting of nodes (states) and directed edges (transitions). A state represents the //state// of the system you are modeling and transition represents the event or action, when executed, changes the system's state from one to another. For example, if we were to model a vending machine, we would use a state to represent the balance (money deposited) and use a transition to represent a coin being inserted into the machine. For modeling a web application, we would use a state to represent each web page or popup dialog or the selection of a dropdown list, etc. and a transition to represent a button or link which when click will take us to another web page or a different select being made in the dropdown list. A valid model must have: * one //Initial// node * one or more //Final// node * one or more states * transitions connecting nodes/states and all states and //Final// nodes must be reachable from //Initial// node. ===== Create Model ===== //TestOptimal// supports state-based model and combinatorial model. They are both created by clicking on {{wiki:overview:ide_btn_open.png?16}} on the toolbar or clicking on menu //File / Open/New Model//: {{wiki:overview:ide_menu_file.png?200}} The //Menu File List// opens: {{wiki:overview:tut_new_model.png?200}} By default it opens at the root folder //model//. Navigate to the folder that you want the new model to be stored and click on {{wiki:overview:model.png?20}} to create state-based model. Enter the model name at the New Model prompt: {{wiki:overview:ide_newmodel_prompt.png?300}} If the model is created successfully, it will be added to the model list. Go ahead and click on the model name to open it. //IDE// opens the new model in [[../ide_model|Model Editor]]. {{wiki:overview:ide_new_model.png?600}} Our goal is to create a simple model that looks like the following when completed: {{wiki:overview:tut_new_model_state_trans_created.png?600}} =====Add States and Transitions ===== States and transitions are added by selecting the appropriate icon from the toolbar and click on the modeling canvas to add states/transition to the model. ==== Create Initial / Final Nodes ==== Since a model must have one //Initial// node and at least one //Final// node. Let's create these first and place one on the left and the other on the right of the modeling canvas: * select //Initial// node icon {{wiki:overview:ide_model_btn_initial.png?16}} * click on left side of modeling canvas to place //Initial// node * select //Final// node icon {{wiki:overview:ide_model_btn_final.png?16}} * click on right side of modeling canvas to place //Final// node The nodes were created with a default name, let's change them to "Start" and "End": * select //edit mode// by clicking on ★ * open //Property Pane// by * double clicking on the node, or * select context menu {{wiki:overview:ide_model_btn_prop.png?16}} {{wiki:overview:tut_state_mode_ctx_menu_initial.png?100}} * change the name as shown below: {{wiki:overview:tut_new_model_initial.png?600}} You can re-position the nodes by dragging {{wiki:overview:ide_model_icon_move.png?16}}. When done, close the property pane by clicking on ">" on the right side of the pane. If needed, nodes, states and transitions can be deleted by clicking on "X" on the context menu. ==== Create States ==== To add states to the model, select the state icon {{wiki:overview:ide_model_btn_state.png?16}} from the toolbar {{wiki:overview:ide_model_toolbar.png?200}} and click on modeling canvas where you want the new state to be placed. Let's create the three states and place them between //Initial// and //Final// nodes. States are created with a default names, go ahead and change their names to "State_A", "State_B" and "State_C". Your model should look like below: {{wiki:overview:tut_new_model_states_created.png?600}} Click on ★ to switch editor to //edit mode//. If you want to move the states, you can do so by dragging {{wiki:overview:ide_model_icon_move.png?16}}. ==== Create Transitions ==== Transitions connect the source state to the target state. So you have to identify the source and target state for each transition. Furthermore, you will also need to decide which side of the source and target states you want the transition to start and ends. This involves tapping the sides of the source and target states. To create a transition: * select ➜ on toolbar * click on a side of source state * click on a side of target state Transition consists of segments. //IDE// draws the appropriate segments required to connect the states without crossing over the states. {{wiki:overview:tut_new_model_trans_created.png?250}} Like creating a new state, new transitions are created with default name and they can be changed through [[../ide_model#transition_properties| Transition Properties]], which can be accessed by: * double clicking on transition label, or * transition context menu over the transition label {{wiki:idescreen:ide_model_menutrans.png?75}} Go ahead and change their names and your model should look like below, remember to select the //edit mode// by clicking on ★ before you can make changes to the transition. {{wiki:overview:tut_new_model_state_trans_created.png?500}} ==== Arranging States and Transitions ==== States and transitions can be re-positioned on the model while in //Edit// mode. === Moving State === To move a state, mouse over the state and drag on {{wiki:overview:ide_model_icon_move.png?16}}. === Moving Transition === If the transitions are not routed the way as shown above, you can change the transition routing by dragging the transition segment. When you move the start or end segment of the transition to the adjacent side of the state, the transition will be re-routed from that side of the state. === Moving Transition Label === Transition label can also be moved: mouse over the transition label and drag on {{wiki:overview:ide_model_icon_move.png?16}}. Transition label is positioned from the position of the start segment from the source state. This ensures that they are re-positioned automatically when its source state or transition start segment is moved. Be sure to save the model changes before moving on to next. ===== Generate Test Cases ===== When model is completed, we are ready to generate test cases from it. Test cases are generated by traversing the states and transitions to achieve the desired coverage using algorithms, aka model //sequencers//. For purpose of this demonstration, we'll just use the default //Optimal// sequencer as shown in the app toolbar {{wiki:idescreen:ide_model_toolbar.png?300}}. Click on {{wiki:overview:tut_model_btn_gen.png?16}} on the app toolbar to generate the test cases using //Optimal// sequencer. You should see some status messages showing up on the bottom of the screen. When done, it should auto select //RESULT// tab like below, if not click on //RESULT// tab on the app header: {{wiki:overview:tut_new_model_result_gen.png?600}} Explanation on //RESULT// tab is covered in a separate tutorial. We will just review the test cases generated in the "Test Case" section. ===== Model Graphs and Test Visualization ===== In addition to the test case list described in the previous section, you can view the test cases in graphs: * {{wiki:overview:tut_new_model_graph_trav.png?linkonly | Traversal}} * {{wiki:overview:tut_new_model_graph_cov.png?linkonly| Coverage}} * {{wiki:overview:tut_new_model_graph_msc.png?linkonly | Test Case}} Click on each of the graphs on {{wiki:overview:tut_model_btn_graphs.png?250}}. The graphs should be self-explanatory. ===== Make Changes ===== As you followed this tutorial, you should have noticed that you can easily make changes to the model and re-generate test cases with minimum effort. Here are a few more changes you can make to the model: * aligning states by selecting (marking) states and clicking on one of the alignment options from the toolbar * re-routing transitions with transition context menu: * {{wiki:idescreen:ide_model_btn_reroute_1.png?16}} - refreshing transition segments * {{wiki:idescreen:ide_model_btn_reroute_2.png?16}} - changing transition source and/or target state * refreshing all outgoing and incoming transitions of a state with state context menu {{wiki:idescreen:ide_model_btn_reroute_1.png?16}} * styling the states and transitions through [[../ide_model#state_properties|State Properties]] and [[../ide_model#transition_properties | Transition Properties]]. You may also us //Box// node to group or highlight a set of states. If you are modeling a workflow or process, you can use //Swimlanes// to distinguish multiple different process and actor.