## 4 Sequence-Matching Algorithm Review
### Pseudo-Code for the SMA  
Now that the mathematical definitions have been established, the SMA can be presented in a more pragmatic form. The scheme written in Algorithm 2 presents the general algorithm of the SMA. It requires \(S\), \(C\), and \(R\), as defined in the previous subsections. A look at the algorithm quickly reveals the five principal parts presented in Figure 8. The _Detect_ step is conducted by the DetectAction subroutine. The _Recognize_ and _Match_ steps are processed inside the CheckCurrentSequence subroutine, the function to _Map_ is \(R\). Finally, _Execute_ represents the execution of the command \(c\) (\(\mathtt{execute}(c)\)) when found. Algorithm 3 details the procedure to ensure that the correct sequence is matched as fast as possible to a sequence in the library (\(S\)). Given the current sequence inputted by the user, it returns a boolean value (_sequenceMatch_) indicating whether or not the current sequence \(s\) is an element of the set \(S\). The function also returns the current sequence itself, which may have been emptied if there was no possibility that it might lead to one of the sequences of \(S\).  
The DetectAction subroutine is defined in Algorithm 1. It takes a sensor's signal as an input and returns an integer \(a\), indicating the detected action. If this action is not in \(A\) (i.e., no action performed by the user), \(a\) will be given a zero value.  
\begin{table}
\begin{tabular}{c c c c} \hline  & \(\boldsymbol{k=1}\) & \(\boldsymbol{k=2}\) & \(\boldsymbol{k=3}\) \\ \hline \(|A|=2\) & 2 & 6 & 14 \\ \(|A|=3\) & 3 & 12 & 39 \\ \(|A|=4\) & 4 & 20 & 84 \\ \(|A|=5\) & 5 & 30 & 155 \\ \hline \end{tabular}
\end{table}
Table 4: Number of possible sequences, with respect to the number of distinct actions \(|A|\) and the chosen maximum number of actions per sequence \(k\).  
```
1:functionSequenceMatchingLoop
2:\(t_{action}\leftarrow\)start timer
3:loop
4:\(s\leftarrow()\)
5:\(sequenceMatch\leftarrow\)False
6:whileNOT sequenceMatchdo
7:\(r\leftarrow\)sensor reading
8:\(a\leftarrow\)DetectAction\((r)\)
9:if\(a\
eq\)NULLthen
10:append\(a\)to\(s\)
11:reset\((t_{action})\)
12:endif
13:\((sequenceMatch,s)\leftarrow\)CheckCurrentSequence\((s,t_{action})\)
14:endwhile
15:\(t_{exec}\leftarrow\)start timer
16:\(S\leftarrow\)Initial State
17:\(c\gets R(s)\)
18:\(r\leftarrow\)sensor reading
19:while\(r\
eq 0\)OR\(t_{exec}\)NOT timeoutdo
20:if\(r\
eq 0\)then
21:reset\((t_{exec})\)
22:execute\((c)\)
23:endif
24:endwhile
25:endloop
26:endfunction
```  
**Algorithm 2** General sequence matching algorithm.  
```
1:functionCheckCurrentSequence(Sequence s, Timer t)
2:if\(t\) NOT timeoutthen
3:\(n_{S}\leftarrow\texttt{length}(S)\)
4:for\(element\) in \(S\)do
5:if\(element[0:n_{s}]\
eq s\)then
6:\(S.remove(element)\)
7:endif
8:endfor
9:if\(n_{S}==0\)then
10:returnFalse,()
11:elseif\(n_{S}==1\)then
12:\(sequenceMatch\leftarrow(s\in S)\)
13:return\(sequenceMatch,s\)
14:else
15:returnFalse,\(s\)
16:endif
17:else
18:\(sequenceMatch\leftarrow(s\in S)\)
19:return\(sequenceMatch,s\)
20:endif
21:endfunction\(\triangleright\)boolean sequenceMatch,Sequence s
```  
**Algorithm 3** Dynamic matching sub-routine of the SMA.