Developing Rich, Web-Based User Interfaces with the Statecharts Interpretation and Optimization Engine
Total Page:16
File Type:pdf, Size:1020Kb
Developing Rich, Web-Based User Interfaces with the Statecharts Interpretation and Optimization Engine Jacob Beard Supervised by: Hans Vangheluwe School of Computer Science McGill University Montreal, Quebec, Canada March 23rd, 2013 A thesis submitted to McGill University in partial fulfilment of the requirements of the degree of Master of Science in Computer Science Copyright ©2013 Jacob Beard. All rights reserved. Abstract Today's Web browsers provide a platform for the development of complex, richly interactive user interfaces. But, with this complexity come additional challenges for Web develop- ers. The complicated behavioural relationships between user interface components are often stateful, and are difficult to describe, encode and maintain using conventional programming techniques with ECMAScript, the general-purpose scripting language embedded in all Web browsers. Furthermore, user interfaces must be performant, reacting to user input quickly; if the system responds too slowly, the result is a visible UI \lag," degrading the user's experience. Statecharts, a visual modelling language created in the 1980's for developing safety- critical embedded systems, can provide solutions to these problems, as it is well-suited to describing the reactive, timed, state-based behaviour that often comprises the essential com- plexity of a Web user interface. The contributions of this thesis are then twofold. First, in order to use Statecharts effectively, an interpreter is required to execute the Statecharts models. Therefore, the primary contribution of this thesis is the design, description, implementation and empirical evaluation of the Statecharts Interpretation and Optimization eNgine (SCION), a Statecharts interpreter implemented in ECMAScript that can run in all Web browsers, as well as other ECMAScript environments such as Node.js and Rhino. This thesis first describes a syntax and semantics for SCION which aims to be maximally intuitive for Web developers. Next, test-driven development is used to verify the correct implementation of this semantics. Finally, SCION is optimized and rigorously evaluated to maximize performance and minimize memory usage when run in various Web browser environments. While SCION began as a research project toward the completion of this master thesis, it has grown into an established open source software project, and is currently being used for real work in production environments by several organizations. The secondary contribution of this thesis is the introduction of a new Statecharts-based design pattern called Stateful Command Syntax, which can be used to guide Web user interface development with Statecharts. This design pattern can be applied to a wide class of Web user interfaces, specifically those whose behaviour comprises a command syntax that varies depending on high-level application state. Its use is illustrated through detailed case studies of two highly interactive, but very different applications. i ii Abr´eg´e Les navigateurs web modernes permettent la cr´eationd'interfaces utilisateur complexes et extr^emement interactives. Mais cela engendre ´egalement des difficult´esadditionnelles pour les d´eveloppeurs web. Les relations complexes qui existent entre les diff´erentes composantes des interfaces utilisateur sont souvent `a´etats,et sont difficile `ad´ecrire,encoder et entretenir en utilisant les techniques de programmation conventionnelles qu’offre ECMAScript, un lan- gage banalis´eque l'on retrouve dans tous les navigateurs. De plus, les interfaces doivent ^etreperformantes et r´epondre rapidement aux actions de l'utilisateur; un syst`emetrop lent engendre un d´ecalageperceptible qui nuit `al'exp´erienced'utilisation. Statecharts, un langage de mod´elisationvisuelle cr´e´edans les ann´ees80 pour assurer la s´ecurit´edes syst`emesembarqu´es,peut offrir des solutions `aces d´efis: il est adapt´e`a la description du comportement r´eactif,chronom´etr´e,et fond´esur l'´etat, qui repr´esente en somme ce qui est le plus difficile avec les interfaces utilisateur sur le web. L'apport de ce m´emoire est double. Tout d'abord, l'ex´ecution des mod`elesStatecharts, pour ^etre efficace, n´ecessiteun interpr`ete. La premi`erecontribution de cet article consiste donc en le design, l'´elaboration, l'impl´ementation et l'´evaluation empirique du Statecharts In- terpretation and Optimization eNgine (SCION), un interpr`eteen ECMAScript qui peut ^etre ex´ecut´edans tous les navigateurs web ainsi que dans d'autres environnements ECMAScript, comme Node.js et Rhino. Notre th`esed´ecriten premier lieu une syntaxe et une s´emantique pour SCION qui se veut la plus intuitive possible pour les d´eveloppeurs web. De nombreux tests sont ensuite effectu´es pour en assurer le bon fonctionnement. Enfin, SCION est rigoureusement optimis´eafin de maximiser la performance et de minimiser l'utilisation de la m´emoirevive dans diff´erents navigateurs web. Bien que SCION ait d´ebut´ecomme un projet de recherche personnel en vue de l'obtention du dipl^omede matrise, il est devenu un logiciel libre bien ´etabliet utilis´epar plusieurs organisations dans leurs environnements de travail. La deuxi`emecontribution de ce m´emoireest la pr´esentation d'un nouveau patron de con- ception cr´e´e`apartir de Statecharts nomm´eStateful Command Syntax, qui peut ^etreutilis´e pour guider la cr´eationd'interfaces utilisateur sur le web. Ce patron de conception peut ^etre employ´esur une grande vari´et´ed'interfaces, notamment celles dont le comportement inclut une syntaxe de commande qui varie selon le degr´ed'abstraction de l'application. Son utilisation est illustr´ee`atravers quelques ´etudesde cas ainsi que deux logiciels tr`es diff´erents mais hautement interactifs. iii iv Acknowledgements I would like to thank my supervisor Professor Hans Vangheluwe for providing research op- portunities and setting me down an interesting path. I would also like to thank my parents for raising me, my girlfriend Tamar Swartz for her continued love and support, my employer, INFICON, for sponsoring the development of SCION, my supervisor at INFICON, Dr. Pierre Wellner, for reading my thesis and providing useful feedback, and my colleagues Xavier Peich, who translated my thesis abstract into French, Rohan Shiloh Shah, for introducing me to Computer Science in my freshman year, and Anton Dubrau, who gave me the push I needed to start writing this thesis. Finally, I would like to thank Linus Torvalds and Richard Stallman for their contributions to the free and open source software movement. Without the GNU/Linux operating system, I would not be a fraction as productive as I am now. v vi Contents 1 Introduction 1 1.1 Background . .2 1.1.1 State Machines for User Interface Development . .2 1.1.2 Interactive Web-based User Interfaces . .2 1.1.3 Statecharts for Web User Interfaces . .3 1.2 Contributions . .3 1.3 Thesis Outline . .4 2 SCION Syntax and Semantics 5 2.1 Motivation for Using SCXML . .5 2.2 Syntax . .6 2.2.1 States . .6 2.2.2 Transitions . .7 2.2.3 Actions . .8 2.2.4 History . 10 2.3 Common Basic Semantics . 10 2.3.1 Enabled Transitions . 12 2.3.2 Selecting States to Enter and States to Exit in a Small-Step . 12 2.4 Examples of Common Basic Semantics . 13 2.4.1 Basic States . 13 2.4.2 OR States . 14 2.4.3 AND States . 15 2.4.4 History . 16 2.5 Semantic Aspects . 19 2.5.1 General Influences on SCION Semantics . 19 2.5.2 Memory Protocol . 20 2.5.3 Maximality . 22 2.5.4 Event Lifelines . 26 2.5.5 External Event Communication . 27 2.5.6 Transition Consistency . 30 2.5.7 Transition Priority . 36 2.5.8 Concurrency . 37 vii 3 Pseudocode for SCION step algorithm 41 3.1 Data Structures . 41 3.2 Constants . 42 3.3 Global Objects . 42 3.4 Utility Functions . 42 3.5 procedure init . 43 3.6 procedure start . 43 3.7 procedure performBigStep . 43 3.8 procedure performSmallStep . 43 3.9 function getStatesExited . 44 3.10 function getStatesEntered . 46 3.11 procedure recursivelyAddStatesToEnter . 46 3.12 function selectTransitions . 47 3.13 function getActiveTransitions . 48 3.14 function selectPriorityEnabledTransitions . 48 3.15 function getTransitionWithHigherSourceChildPriority . 49 3.16 function getInconsistentTransitions . 49 3.17 function isArenaOrthogonal . 50 3.18 procedure gen . 50 3.19 procedure evaluateAction . 50 4 Optimizing SCION for ECMAScript and the World Wide Web 55 4.1 Problem Statement . 55 4.2 Project Background . 56 4.3 Statecharts Optimization Strategies . 56 4.3.1 Transition Selection . 56 4.3.2 Set Data Structure . 59 4.3.3 Transition Flattening Transformation . 62 4.3.4 Cached Structural Information . 64 4.4 SCION Architecture . 64 4.5 Experimental Framework . 65 4.5.1 Optimization Profiles . 65 4.5.2 Test Cases . 65 4.5.3 ECMAScript Interpreters . 71 4.5.4 Testing Methodology . 74 4.6 Results . 75 4.6.1 Performance . 76 4.6.2 Memory Usage . 94 4.6.3 Memory Usage versus Performance . 100 4.6.4 Conclusion . 102 viii 5 Web User Interface Development with the Stateful Command Syntax De- sign Pattern 105 5.1 Overview . 105 5.2 Case Study 1: Application of SCS to Vim Modal Text Editor . 106 5.2.1 Introduction . 106 5.2.2 Case Study Goals . 107 5.2.3 Editor Requirements . 107 5.2.4 Application Architecture . 108 5.2.5 VimBehaviour Statechart Design . 111 5.3 Case Study 2: Vector Graphics Drawing Tool Based on Inkscape . 122 5.3.1 High-Level Goals . 122 5.3.2 Natural-Language Specification of User Interface Requirements . 122 5.3.3 Application Architecture . 124 5.3.4 Mapping DOM User Interface Events to Statecharts Events . 125 5.3.5 InkscapeBehaviour Statechart Design . 125 5.4 Critique of SCS . 131 6 Conclusion 133 Bibliography 136 ix x List of Figures 2.1 A Statecharts model with AND, OR and basic states, arranged hierarchically7 2.2 An example of a transition with two triggers, a condition and an action .