460 likes | 673 Views
Software Engineering ? Introduction. STATE DIAGRAMRemove any dashes that separate an alpha character followed by a numeric character or a numeric character followed by an alpha character. i.e. P-101 becomes P101 If a dash separates an alpha character from an alpha character, leave the dash in th
E N D
1. Software Engineering Introduction STATE DIAGRAM
Case Study: Automotive Industry
Pep Boys handles millions of parts throughout its chain.
Information about these parts is acquired through various sources.
Unfortunately, all of these sources do not format their information with the same set of rules.
Most critical was the use of varying formats for part numbers.
For consistency Pep Boys wishes to store all of their part number information in a standard format.
Therefore they developed the following conversion rules:
2. Software Engineering Introduction STATE DIAGRAM
Remove any dashes that separate an alpha character followed by a numeric character or a numeric character followed by an alpha character. i.e. P-101 becomes P101
If a dash separates an alpha character from an alpha character, leave the dash in the part number. i.e. P-ABC stays P-ABC
If a dash separates a numeric character from a numeric character, leave the dash in the part number. i.e. P1-101 stays P1-101
If there is a space between numeric characters, the space is replaced with a dash. i.e. P1 101 becomes P1-101
If there is a space between alpha characters, the space is replaced with a dash. i.e. PA BCD becomes PA-BCD
If there is a space between alpha and numeric characters or numeric and alpha characters, the space should be removed. i.e. P 101 becomes P101
3. Software Engineering Introduction STATE DIAGRAM
If there is a dash (-) followed by a dot (.), then you should remove the dash.
A part number can not start with a dash (-) or a dot (.), but may appear elsewhere within a part number.
Remove any leading spaces from the part number.
If there is a plus sign, left parentheses, or right parentheses anywhere in the part number, remove it. i.e. P+101 becomes P101
An alpha character can follow another alpha character.
A numeric character can follow a numeric character.
An alpha character can follow a numeric character.
A numeric character can follow an alpha character
4. Software Engineering Introduction STATE DIAGRAM
Tackling this problem definitely relies on developing a good strategy.
There are many approaches you can take.
You could attempt to solve the problem using brute force.
To solve our problem, we could take the approach of processing one rule at a time.
By searching through the part number we can look for the characters that the rule effects and then upon finding them, convert the string to the proper format.
The solution is simple to implement if we are comfortable with the string functions required.
5. Software Engineering Introduction STATE DIAGRAM
A more elegant and efficient solution is to use the technique of a state diagram.
A state diagram stores valid transitions of input from one possible state to another.
It allows us to group input and determine the proper course of action for each circumstance.
A complete state diagram encompasses all of the transitions and associated actions that are dictated by the rules required by the business model.
By using the state diagram, we essentially make one pass over the part number, correcting the violations to the rules as we go.
By not needing to pass over the part number for each rule, we save considerable time.
The drawback to this method is it does require the user to develop a new technique.
It also requires external documentation, because the key to this solution is using a diagram that represents all the possible transitions from one type of input to another.
6. Software Engineering Introduction STATE DIAGRAM
The first step in producing a state diagram is to list the type of input that we may find within a part number. They are:
Letters (both lower and uppercase)
Numeric digits (0,1,2,3,4,5,6,7,8,9)
Dots (.)
Dashes(-)
Spaces
Plus Sign (+)
Any other input found in a part number is an illegal character and will result in producing an error upon converting from the input to the corrected part number.
Now we proceed by developing a series of states and transitions that are valid for each type of input.
The best way to explain this is to show how it is implemented with our current problem.
7. Software Engineering Introduction STATE DIAGRAM
State S
We start by developing a diagram that shows a starting state, S, and all the possible states that it can reach by each type of input.
8. Software Engineering Introduction STATE DIAGRAM
State S
9. Software Engineering Introduction STATE DIAGRAM
State S
10. Software Engineering Introduction STATE DIAGRAM
State 1
We continue by drawing all the possible transitions from state 1. We ask ourselves if each type of input has a valid transition from this state.
Since an alpha character can follow an alpha character, rule 11, then alpha characters have a valid transition from state 1.
Since a numeric character can follow an alpha character, rule 14, then numeric characters have a valid transition from state 1.
Since a space can appear after an alpha character, rules 5 and 6, then a space has a valid transition from state 1.
Since a dash can appear after an alpha character, rules 1, 2, and 7, a dash is a valid transition from state 1.
Since a dot can appear after an alpha character, rule 8, a dot is a valid transition from state 1.
11. Software Engineering Introduction STATE DIAGRAM
Therefore, when the last character processed was an alpha character, the following character can be any of the valid input values.
12. Software Engineering Introduction STATE DIAGRAM
For each transition we must decide whether a new state is reached or if we transition to a previously existing state.
Remember that we reached state 1 by processing an alpha character.
Lets start by reviewing the rules and look if any special transition comes from alpha to alpha.
There are none.
Therefore, we can represent the transition from state 1 on processing an alpha character to remain in state 1.
This is shown in our figure by drawing an arrow from the current state and point it back to the current state.
13. Software Engineering Introduction STATE DIAGRAM
Next we evaluate the transition of a numeric character from state 1.
When we transition from an alpha character to a numeric character, we find no action occurs in the output string.
Therefore, we can draw the transition line from state 1 to the previously existing state 2.
State 2 represents the state reached when the previous transition was the processing of a numeric character.
Since this is the same whether we transition from the start or from state 1, we can use the same state.
Each of the remaining three valid inputs, ( , -, and .), lead to a new state.
Therefore we draw an arrow from state 1 to states 4, 5, and 6 for transitions on , -, and . respectively.
14. Software Engineering Introduction State 2
We continue by repeating the process used for state 1 with state 2. We ask ourselves if each type of input has a valid transition from this state.
Since an alpha character can follow an numeric character, rule 13, then alpha characters have a valid transition from state 2.
Since a numeric character can follow an numeric character, rule 12, then numeric characters have a valid transition from state 2.
Since a space can appear after a numeric character, rules 4 and 6, then a space is a valid transition from state 2.
Since a dash can appear after a numeric character, rules 3, 7, and 8, a dash is a valid transition from state 2.
Since a dot can appear after a numeric character, rule 8, a dot is a valid transition from state 2.
Therefore, when the last character processed was an alpha character, the following character can be any of the valid input values.
The five new transitions are shown in the following figure:
15. Software Engineering Introduction State 2
We continue by repeating the process used for state 1 with state 2. We ask ourselves if each type of input has a valid transition from this state.
Since an alpha character can follow an numeric character, rule 13, then alpha characters have a valid transition from state 2.
Since a numeric character can follow an numeric character, rule 12, then numeric characters have a valid transition from state 2.
Since a space can appear after a numeric character, rules 4 and 6, then a space is a valid transition from state 2.
Since a dash can appear after a numeric character, rules 3, 7, and 8, a dash is a valid transition from state 2.
Since a dot can appear after a numeric character, rule 8, a dot is a valid transition from state 2.
Therefore, when the last character processed was an alpha character, the following character can be any of the valid input values.
The five new transitions are shown in the following figure:
16. Software Engineering Introduction State 2
17. Software Engineering Introduction State 2
The transitions from state 2 are virtually a mirror image of the transitions from state
1.
We reached state 2 by processing a numeric character. As with state 1s alpha transition, we can represent the transition from state 2 on a numeric to remain in state 2.
We draw this by drawing an arrow from the current state and point it back to the current state.
The next output we evaluate is when we process an alpha character.
From a numeric character to an alpha character, we find nothing happens.
Therefore, we can draw the transition line from state 2 to the previously existing state 1.
Each of the remaining three valid inputs, ( , -, and .), lead to a new state.
Therefore we draw an arrow from state 1 to states 4, 5, and 6 for transitions on , -, and . respectively.
18. Software Engineering Introduction State 3
Since state 3 is associated with an action, we do not look for additional states to transition to.
We only have to add to the figure is a dashed arrow to return from the action to the starting state.
19. Software Engineering Introduction State 4
State 4 was reached after a space was processed after an alpha character was processed.
There are only two possible transitions from state 4.
The rules allow for an alpha character followed by a space followed by an alpha character, rule 5, or for an alpha character to be followed by a space to be followed by a numeric character, rule 6.
These two transitions are shown in the following diagram:
20. Software Engineering Introduction State 4
If an alpha character is processed from state 4, we have processed an alpha character, followed by a space, followed by another alpha character.
This transition would bring us to state 9.
State 9 is represented with a bold circle, since rule 5 indicates the space should be replaced with a dash.
Then the state is returned to state 1, since an alpha character is the last character processed.
If a numeric character is processed from state 4, we have processed a numeric character, followed by a space, followed by a numeric character.
This transition would bring us to state 10.
State 10 is represented with a bold circle, since rule 6 indicates the space should be removed.
Then the state is returned to state 2, since the numeric character is the last character processed.
21. Software Engineering Introduction State 5
State 5 is reached when an alpha character is followed by a dash.
There are three possible valid transitions.
An input of an alpha character from state 5 is valid, rule 2, because we would have processed an alpha character followed by a dash followed by an alpha character.
An input of a numeric character from state 5 is valid, rule 1, because we would have processed an alpha character followed by a dash followed by a numeric character.
An input of a dot from state 5 is valid, rule 7, because we would have processed an alpha character followed by a dash followed by a dot.
These three transitions are shown in the following figure:
22. Software Engineering Introduction State 5
23. Software Engineering Introduction State 6
We can reach State 6 from multiple states, but in each case the last character processed was a dot.
There is no action that occurs when a dot is in-between an alpha character and an alpha character, a numeric character and a numeric character, or an alpha character and a numeric character.
Therefore, the valid transitions from this state are on an alpha character or a numeric character.
If an alpha is entered, then we transfer back to state 1.
If a numeric is entered then we transfer back to state 2.
24. Software Engineering Introduction State 7
State 7 is reached when a numeric character is followed by a dash.
There are three possible valid transitions.
An input of a numeric character from state 7 is valid, rule 3, because we would have processed a numeric character followed by a dash followed by a numeric character.
An input of an alpha character from state 7 is valid, rule 1, because we would have processed a numeric character followed by a dash followed by an alpha character.
An input of a dot from state 7 is valid, rule 7, because we would have processed an numeric character followed by a dash followed by a dot.
These three transitions are shown in the following figure:
25. Software Engineering Introduction State 7
If an additional numeric character is processed, then we return to state 2.
Since an numeric character, dash, and numeric character sequence requires no action, we can return to state 2 since it is in essence the neutral state after a numeric character is read.
However, if an alpha character is processed, rule 1 states that we must remove the dash.
Therefore, from state 7 on an alpha character, we reach state 14.
State 14 performs the action to remove the dash and returns to state 2, since the last character processed was a numeric.
If a '.' follows the dash, we are transferred to state 13 where the dash is removed.
26. Software Engineering Introduction State 8
State 8 was reached after a space was processed after a numeric character was processed.
There are only two possible transitions from state 8.
The rules allow for a numeric character followed by a space followed by an numeric character, rule 4, or for a numeric character to be followed by a space to be followed by an alpha character, rule 6.
These two transitions are shown in the following diagram:
27. Software Engineering Introduction State 8
If a numeric character is processed from state 8, we have processed a numeric character, followed by a space, followed by another numeric character.
This transition would bring us to state 15.
State 15 is represented with a bold circle, since rule 4 indicates the space should be replaced with a dash.
Then the state is returned to state 2, since a numeric character is the last character processed.
If an alpha character is processed from state 8, we have processed a numeric character, followed by a space, followed by an alpha character.
This transition would bring us to state 16.
State 16 is represented with a bold circle, since rule 6 indicates the space should be removed.
Then the state is returned to state 1, since the numeric character is the last character processed.
28. Software Engineering Introduction State 9
State 9 is reached when an alpha character is followed by a space followed by an alpha character.
State 9 has an action associated with it according to rule 5.
The action replaces the space with a dash.
Since the last character of the new string is still an alpha character, we draw a dashed line back to state 1, where we can continue processing the input.
29. Software Engineering Introduction State 10
State 10 is reached when an alpha character is followed by a space followed by a numeric character.
State 10 has an action associated with it according to rule 6.
The action removes the space.
Since the last character of the new string is a numeric character, we draw a dashed line back to state 2, where we can continue processing the input.
30. Software Engineering Introduction State 11
State 11 is reached when an alpha character is followed by a dash followed by a numeric character.
According to rule 1, state 11 has an action associated with it to remove the dash.
Since the last character of new string is a numeric character, we draw a dashed line back to state 2, where we can continue processing input.
31. Software Engineering Introduction State 12
State 12 was reached when an alpha character is followed by a dash followed by a dot.
According to rule 7, when a dash is followed by a dot, we remove the dash.
Because the last character in the new string is now a dot, we can not return to state 1 or state 2, since they imply that an alpha or numeric character was the last one processed.
Therefore, we draw a dashed line from state 12 to state 6.
32. Software Engineering Introduction State 14
State 14 is reached when a numeric character is followed by a dash followed by an alpha character.
According to rule 1, state 14 has an action associated with it to remove the dash.
Since the last character of new string is an alpha character, we draw a dashed line back to state 1, where we can continue processing input.
33. Software Engineering Introduction State 15
State 15 is reached when a numeric character is followed by a space is followed by a numeric character.
According to rule 5, the space should be replaced with a dash.
Then the state is returned to state 2, since a numeric character is the last character processed.
34. Software Engineering Introduction State 16
State 15 is reached when a numeric character is followed by a space is followed by an alpha character.
According to rule 5, the space should be replaced with a dash.
Then the state is returned to state 1, since an alpha character is the last character processed.
35. Software Engineering Introduction Final Diagram
36. Software Engineering Introduction Create a State Table
Once the diagram is complete, we must create a table that shows the transitions from state to state given each of the following transitions.
We list each of the valid types of input across the top and list each of the states down the first column.
We enter the state number that each state would transition to given each type of input.
If a transition does not exist, then we indicate so by placing an x in the appropriate cell of the table.
37. Software Engineering Introduction The table for our state diagram follows:
38. Software Engineering Introduction Here is the actual code that will implement the state diagram.
The code is actually the simplest part.
Creating the correct state diagram is the most difficult aspect of this solution.
We start by declaring a two dimensional array that will store the state table.
We have called it position, because by using it we find the next position in the state diagram.
It is followed by an array that stores the action associated with each state.
The action array is filled with values that represent each type of action.
If we inspect carefully, we see that there is only four actual actions that occur in our entire diagram.
39. Software Engineering Introduction NO
The first action, NO, is the simplest.
When a NO action is specified, it means that we are passing from one state of the diagram to another without a specific action being taken.
In this case the only objective is to copy the current source character to the destination string.
SPACE
The SPACE action is only taken from one state, 3.
When we perform this action we are removing spaces from the beginning of the input string.
Therefore, the only action performed when this option is selected is to advance the pointer of the source string.
REMOVE
This action is actual used to perform many actions.
It removed the second to last character read from the result string.
This is accomplished by copying the current input character over the previous character in the result string.
40. Software Engineering Introduction ADD_DASH
The final action is taken when either an alpha character followed by a space followed by an alpha character is processed, or a numeric character followed by a space followed by a numeric character is processed.
This action replaces the space in the result string with a dash and then copies the current input character to the result string.
These actions are implemented using a switch statement.
It is assumed that in this application that only valid options are used in the action array so there is no need to have a default value for invalid input.
It is important to notice that the order of the switch statement is important for performance sake.
The choices are listed in the order from most prevalent to least.
This way when the switch statement is evaluated the least number of options must be tried.
41. Software Engineering Introduction The main routine asks the user to enter a part number.
The program then calls the ConvertPartNumber function that will output the converted part number.
The function operates by looping through the input one character at a time.
If it encounters a '+', '(', or ')' character, it simply moves on to the next character.
We conveniently left this rule out of our state diagram, because it was simpler to check for it at the beginning of the loop that to add it to almost every state in our diagram.
Next the function determines which type of valid input is the current character.
It then moves down the state diagram based on that input.
If an action exists for that state, then it processes the appropriate action.
When the function reaches the end of the string, it outputs the newly converted string to the screen.
42. Software Engineering Introduction //State Diagram Program
//Includes
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <ctype.h>
//Constants
#define NO -1
#define SPACE 0
#define REMOVE 1
#define ADD_DASH 2
#define PART_NMB_LENGTH 20
//Array to store the state diagram
int position[17][5]=
{{1,2,-1,-1,3}, //start
{1,2,6,5,4}, //1
{1,2,6,7,8}, //2
{0,0,0,0,0}, //3
{9,10,-1,-1,-1}, //4
{1,11,12,-1,-1}, //5
{1,2,-1,-1,-1}, //6
{14,2,13,-1,-1}, //7
{16,15,-1,-1,-1}, //8
{1,1,1,1,1} , //9
{2,2,2,2,2}, //10
{2,2,2,2,2}, //11
{6,6,6,6,6}, //12
{6,6,6,6,6}, //13
{1,1,1,1,1}, //14
{2,2,2,2,2}, //15
{1,1,1,1,1}}; //16
43. Software Engineering Introduction //Array to store the actions associated with each
//state
int action[17]={NO,NO,NO,SPACE,NO,
NO,NO,NO,NO,ADD_DASH,
REMOVE,REMOVE,REMOVE,
REMOVE,REMOVE,ADD_DASH,REMOVE};
44. Software Engineering Introduction //Function to perform the conversion
void ConvertPartNumber(char * source)
{
//Array to store the result
char dest[PART_NMB_LENGTH];
//Pointer to the array for efficiency
char * temp=dest;
int len;
int i=0;
int pos=0;
len=strlen(source);
//Loop thru the input values
while (i < len)
{
//Strip specific characters
if (*source == '+' || *source=='(' ||
*source==')')
source++;
45. Software Engineering Introduction else
{
// Check for alpha character
if (isalpha(*source))
pos=position[pos][0];
//Check for numeric character
else if (isdigit(*source))
pos=position[pos][1];
//Check for dot character
else if (*source == '.')
pos=position[pos][2];
//Check for dash character
else if (*source == '-')
pos=position[pos][3];
//Check for space
else if (*source == ' ')
pos=position[pos][4];
//Check for invalid input
else
{
cout<<"ERROR!";
return;
}
46. Software Engineering Introduction //Handle action
switch (action[pos])
{
case NO:
//Copy current character over.
*temp++=*source++;
break;
case REMOVE:
//Copy second character over the
// previously written one
*(temp-1) = *source++;
pos=position[pos][4];
break;
case SPACE:
//Skip over the space
source++;
pos=position[pos][4];
break;
case ADD_DASH:
//Replace space with Dash
*(temp-1) = '-';
*temp++=*source++;
pos=position[pos][4];
break;
} //Close switch
47. Software Engineering Introduction } //close if
i++;
} //End While
*temp ='\0';
cout<<endl<<dest<<endl;
}
//Main Routine
void main()
{
char buffer[PART_NMB_LENGTH+1]={"FILLER"};
while (buffer[0] != '\0')
{
cout<<"Enter a part number to be converted"<<endl;
cin.getline(buffer,PART_NMB_LENGTH,'\n');
ConvertPartNumber(buffer);
}
return 0;
}