4-example.tex 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. \section{Example Application: Subtyping Semantics}
  2. \label{sec:example}
  3. We now present the power of our approach through an example application.
  4. Our example makes use of different notions of subtyping, which is closely related to the type/instance relation.
  5. In this application, we mainly focus on determining wheter a model conforms to a metamodel.
  6. A similar discussion is possible for instantiation, though here we assume that the model was already created one way or the other.
  7. As current tools hardcode their conformance relations, users have no other option than to use the given type system.
  8. We continue by showing how our tool, the \textit{Modelverse}~\cite{ACMSRC}, is able to cope with different kinds of type systems.
  9. First, we briefly elaborate on the typing problem at hand.
  10. \subsection{Subtyping}
  11. Throughout the years, different kinds of subtyping relations have been defined and used.
  12. Subtyping directly influences the conformance check: an instance of \textit{Class B} is also an instance of \textit{Class A}, if either \textit{B} is \textit{A}, or if \textit{B} is a subtype of \textit{A}.
  13. The definition of \textit{subtype} varies between the different type systems.
  14. We briefly present the two major classes of type systems: nominal and structural typing~\cite{Typing}.
  15. \subsubsection{Nominal Typing}
  16. The most commonly used kind of subtyping is nominal typing.
  17. With nominal typing, subtypes must be explicitly declared, such that there can be no confusion or unexpected behaviour.
  18. Most programming languages are implemented like this, with classes that can \textit{inherit} from one another.
  19. If \textit{Class B} inherits from \textit{Class A}, all instances of \textit{B} will conform to both \textit{A} and \textit{B}.
  20. Additionally, the structure of \textit{B} is automatically extended with all concepts of \textit{A}.
  21. So if \textit{Class A} had an attribute \textit{a}, all instances of \textit{B} also have this attribute, in addition to all the attributes that were also provided by \textit{B}.
  22. Nominal typing is most oftenly used in Object-Oriented General-Purpose Languages (GPL), such as C++, Python and Java.
  23. Even then, subtle differences can be seen.
  24. For example, C++ and Python allow for multiple inheritance (\textit{i.e.}, a single class can inherit from multiple classes), whereas Java restricts users to single inheritance (\textit{i.e.}, a single class can only inherit from one class).
  25. Again, several variants exist in semantics: attribute resolution is different between C++ and Python, even though both have multiple inheritance.
  26. Even worse, some versions of Python offer a different variant of multiple inheritance than other versions of the language.
  27. Nominal typing is considered safer, as the typing information can be used both for typechecking, and at run-time.
  28. Additionally, checking subtyping relations becomes almost trivial.
  29. \subsubsection{Structural Typing}
  30. At the other end of the spectrum lies structural typing, where subtypes are implicitly detected instead of explicitly defined.
  31. A \textit{Class B} is considered to be a subtype of \textit{Class A} if each feature of \textit{A} is also present in \textit{B}.
  32. Consequently, if \textit{A} and \textit{B} have exactly the same features, they will be considered equivalent, with all instances of \textit{A} being instances of \textit{B}, and vice versa.
  33. Structural typing is used by languages such as OCaml, Go, and Haskell.
  34. Now again, subtle differences exist, such as whether or not the name of the features need to be identical, in addition to the type.
  35. Structural typing is considered more elegant than nominal subtyping, as it lies closer to type systems studied in the literature.
  36. \subsection{Conformance Relation}
  37. Current (meta-)modelling tools, and even programming language compilers, hardcode their type system, and the user has to oblige.
  38. While the default type system is often a sane choice for the domain for which the tool was developed, situations will occur where the other choice might have been easier for the user.
  39. Users have no choice though, as the semantics of the conformance relation is hardcoded.
  40. Even worse, many modelling tools explicitly hardcode special \textit{instanceOf} and \textit{subtypeOf} links inside of their data structure.
  41. With explicitly modelled type/instance relations, typings become modifiable by the user, possibly even automatically using model transformations.
  42. In our case, it is further possible to create multiple conformance relations, one for each kind of type system, and use either, depending on the situation.
  43. This makes it possible for an instance of \textit{Class B} to conform to \textit{Class A} with only one kind of conformance relation, but not the others.
  44. Depending on the conformance relation chosen by the user (ideally, the one closest to the problem domain), the outcome of the conformance check and every related operation will vary.
  45. \subsection{Modelverse}
  46. In the Modelverse, users are required to specify which conformance relation they want to use, and where they want this type mapping model to be stored.
  47. When a model is instantiated, an instantiation policy also needs to be provided.
  48. Later on, when conformance is checked, the previously specified conformance relation is used.
  49. Users still have to implement the conformance checks they wish to use, except if it is provided by default, or by other users.
  50. For our example, a subtyping check for nominal typing will follow inheritance links when required, whereas a structural typing check will only look at the features of the class.
  51. Note that, for the case of nominal typing, there are links with a special semantics.
  52. In our approach, these links are just normal links, stored like any other link (\textit{i.e.}, as an edge).
  53. The conformance check, however, knows the semantics of the links, and uses them appropriately.
  54. An example is given next, resembling the class diagram given in Figure~\ref{fig:class_diagram}.
  55. The semantics is clear for nominal typing: \textit{Class B} inherits from \textit{Class C}, and \textit{Class C} is completely different, but has (for whatever reason) a superset of the attributes of \textit{Class A}.
  56. For structural typing, the inheritance link holds no semantics, and is considered just like any other association between classes.
  57. As such, the instantiation algorithm creats instances of \textit{Class B} which only hold an attribute \textit{b}, and no attribute \textit{a}.
  58. It follows that Figure~\ref{fig:object_diagram_nominal} and \ref{fig:object_diagram_structural} are the result of instantiation using nominal and structural subtyping, respectively.
  59. \begin{figure}
  60. \centering
  61. \includegraphics[width=0.4\columnwidth]{figures/class_diagram.pdf}
  62. \caption{Class Diagram of the example.}
  63. \label{fig:class_diagram}
  64. \begin{subfigure}[b]{0.49\columnwidth}
  65. \centering
  66. \includegraphics[width=0.8\columnwidth]{figures/object_diagram_nominal.pdf}
  67. \caption{Using nominal subtyping.}
  68. \label{fig:object_diagram_nominal}
  69. \end{subfigure}
  70. \begin{subfigure}[b]{0.49\columnwidth}
  71. \centering
  72. \includegraphics[width=0.8\columnwidth]{figures/object_diagram_structural.pdf}
  73. \caption{Using structural subtyping.}
  74. \label{fig:object_diagram_structural}
  75. \end{subfigure}
  76. \caption{Object Diagram of the example.}
  77. \end{figure}
  78. The results of the conformance check for nominal and structural subtyping are shown in Table~\ref{table:conf_nominal} and Table~\ref{table:conf_structural} respectively.
  79. As can be seen, results are very different, which could potentially alter the complete semantics of the model.
  80. While we do not want to argue which one is the best, we offer the user the possibility to use either of these (or others), depending on what the user believes to be the closest match to the problem domain.
  81. \begin{table}
  82. \normalsize
  83. \centering
  84. \begin{tabular}{l|c c c}
  85. conformsTo & A & B & C \\
  86. \hline
  87. a : A & \checkmark & & \\
  88. b : B & \checkmark & \checkmark & \\
  89. c : C & & & \checkmark \\
  90. \end{tabular}
  91. \caption{Conformance check results for nominal subtyping.}
  92. \label{table:conf_nominal}
  93. \end{table}
  94. \begin{table}
  95. \normalsize
  96. \centering
  97. \begin{tabular}{l|c c c}
  98. conformsTo & A & B & C \\
  99. \hline
  100. a : A & \checkmark & & \\
  101. b : B & & \checkmark & \\
  102. c : C & \checkmark & & \checkmark \\
  103. \end{tabular}
  104. \caption{Conformance check results for structural subtyping.}
  105. \label{table:conf_structural}
  106. \end{table}
  107. In the future, we plan to provide a sane default, similar to the conformance check found in most other tools.
  108. This will simplify modelling and prevents users from the associated complexity if this functionality is not needed.