# A Distributed Execution Pipeline for Clustering Trajectories Based on a Fuzzy Similarity Relation
## 4 Materials and Methods
### The Project's Class Diagrams  
The project integrates the Scala Stackable Trait Pattern. In detail, we provide an abstraction called _AbstractMatrix_. It is an abstract class holding the abstract definition of all the operations needed to achieve the transitive closure. Then, different traits override specific abstract definitions to customize the behavior. Traits extend java interfaces to provide much richer functionalities. In our case, traits extend the _AbstractMatrix_ with specific behavior; they are denoted _mixins_, e.g., the _MaxMinNorm_ overrides the norm function into computing the minimum of the inputs and the _Seminasive_ mix overrides the closure algorithm definition. They can only be extended by classes already extending the _AbstractMatrix_. Also, the _AbstractMatrix_ has a companion object defining a specific _KeyEntry_ Type and a static _reverse_ operation which reverses the _Similarity_ class entries for the closure's composition join. The _Similarity_ class resembles Spark's _MatrixEntry_ case class in the Spark MLlib library. However, it is not a case class and it overrides both the _equals_ and _hashCode_ operations to provide specific comparison behavior. The rationale behind this class is that we observed an anomaly when computing the transitive closure. More specifically, the transitive closure computing time was very large. After observing the computing process, we found out that when subtracting the results of the past and present, iterations in the closure's similarities were not considered equal because of their weights. Although, the weights differed from each other in a precision of \(10^{-9}\). This fact led us to provide our own comparison strategy with a custom precision of \(10^{-3}\) integrated in the _Similarity_ class. The abstraction is extended by another trait _DefaultMatrix_. Like the name suggests, the trait provides default behavior for the abstract definition. This has made it possible to leverage the abstract operations without defining a default behavior for each class. As the Figure 2 shows, the class SiMatrix can leverage the _AbstractMatrix_ operation without having to override them. This is our approach extension of the stackable trait. Furthermore, Scala traits support self types. Meaning that a trait can only be extended by a subclass of the specified type. Which is why we implemented a _MatrixImpl_, a self type of the _DefaultMatrix_, adding a restriction to the inheritance strategy. The concrete class _SiMatrix_ extends both the _DefaultMatrix_ and the _Serializable_ traits to respectively be able to leverage all the _AbstractMatrix_ traits and to execute spark stages. Note that in the other traits, we did not override the composition operation because we provided a concrete definition in the lowest level _SiMatrix_ class. To create the SiMatrix objects, we needed a factory pattern. Fortunately, the factory pattern is simplified in Scala by companion objects (A companion object of a specific class in Scala has the same name as the class and it can access the private variables and operations of the class). The _SiMatrix_ is a companion object providing the factory method for instantiating its relative class. An extract of the _apply_ method is described in Algorithm 2. The function is a higher order function based on currying. The first function takes as input the distributed similarities and outputs a function that takes the alpha threshold used for the partitioning, the chosen t-norm and transitive closure algorithm. Then, depending on the t-norm and closure algorithm it instantiates the _SiMatrix_ class while specifying  
Figure 1: The project’s main abstract class. the traits to be mixed in. Note that the order of the trait is important. Scala is based on linearization principle, the last trait is the first to be extended. Hence, the name stackable trait pattern. It is like a stack of patterns where the first one to be extended is actually the last.