| Start | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Parsing Lines using State Machine Let's apply the State Machine pattern (opens new window) for parsing lines with the specific pattern using S4 Class feature from R. PROBLEM ENUNCIATION We need to parse a file where each line provides information about a person, using a delimiter ( The second line does not provide address information. Therefore the number of delimiters may be deferent like in this case with one delimiter and for the other lines two delimiters. Because the number of delimiters may vary, one way to atack this problem is to recognize the presence or not of a given field based on its pattern. In such case we can use a regular expression (opens new window) for identifying such patterns. For example:
Notes:
The idea is to identify each line field based on previously defined patterns. The State pattern define the following entities (classes) that collaborate to control the specific behavior (The State Pattern is a behavior pattern): Let's describe each element considering the context of our problem:
Note: It is a matter of preference how to name the method that carries out the action, PERSON CLASS Using the S4 syntax we can define a Person class like this: It is a good recommendation to initialize the class attributes. The Because the initialize method is already a standard generic method of package It returns the entire function definition, you can see at the top who the function is defined like: Therefore when we use Another existing generic method is Note: We use the same convention as in the default Let's say we want to save the parsed information (a list of R does not provide a sugar syntax for OO because the language was initially conceived to provide valuable functions for Statisticians. Therefore each user method requires two parts: 1) the Definition part (via STATE CLASS Following S4 syntax, let's define the abstract Every sub-class from In order to understand the process, let's define the transition matrix for each state based on the input received:
Note: The cell It means that under the state Name it can receive two inputs: an address or a phone number. Another way to represents the transaction table is using the following UML State Machine (opens new window) diagram: Let's implement each particular state as a sub-state of the class STATE SUB-CLASSES Init State: The initial state will be implemented via the following class: In R to indicate a class is a sub-class of other class is using the attribute Because the sub-classes just implement the generic methods, without adding additional attributes, then the The initial state does not have associated a pattern, it just represents the beginning of the process, then we initialize the class with an Now lets to implement the generic methods from the For this particular state (without The It is a good recommendation in all Name State Here is the definition of this class definition: We use the function Now we define the action to carry out for a given state: Here we consider to possible transitions: one for Address state and the other one for Phone state. In all cases we update the context information: The way to identify the state is to invoke the method: The logic for the other sub-classes (one per state) implementation is very similar. Address State Phone State Here is where we add the person information into the list of CONTEXT CLASS Now the lets to explain the Where Note: Optionally, we can add a With such generic methods, we control the entire behavior of the parsing process: Let's go on now with the corresponding implementations:
First, we split the original line in an array using the delimiter to identify each element via the R-function |
Read Article
https://develop.horje.com/learn/1434/reference