State Machine Specification Directly in Java and C++ Alexander Sakharov

Similar documents
Introduction to Programming Using Java (98-388)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exercise Unit 2: Modeling Paradigms - RT-UML. UML: The Unified Modeling Language. Statecharts. RT-UML in AnyLogic

CA314 Object Oriented Analysis & Design - 7. File name: CA314_Section_07_Ver01 Author: L Tuohey No. of pages: 16

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Object Oriented Programming with Java. Unit-1

1 Lexical Considerations

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Lexical Considerations

Inheritance and Polymorphism

Chapter 5 Object-Oriented Programming

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Object Oriented Programming: Based on slides from Skrien Chapter 2

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Lexical Considerations

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

What is a Class Diagram? A diagram that shows a set of classes, interfaces, and collaborations and their relationships

What is a Class Diagram? Class Diagram. Why do we need Class Diagram? Class - Notation. Class - Semantic 04/11/51

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

List ADT. Announcements. The List interface. Implementing the List ADT

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Figure 2.1: Role of Lexical Analyzer

Advanced Systems Programming

IC Language Specification

Self-review Questions

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Composition of State Machines

Core JAVA Training Syllabus FEE: RS. 8000/-

1 Shyam sir JAVA Notes

SUMMARY: MODEL DRIVEN SECURITY

Declarations and Access Control SCJP tips

Inheritance. Transitivity

Inheritance and Interfaces

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Programming II (CS300)

System Design, Modeling, and Simulation using Ptolemy II

Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. September 9, 2016

SOFTWARE ENGINEERING UML FUNDAMENTALS. Saulius Ragaišis.

Concurrent Models of Computation

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Introduction to Software Testing Chapter 2, Sec#: 2.5 Graph Coverage for Specifications

Self-test Java Programming

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Hardware Description Languages & System Description Languages Properties

EVENTS AND SIGNALS. Figure 1: Events. kinds of events Signal Event

Meltem Özturan

15CS45 : OBJECT ORIENTED CONCEPTS

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

Java Threads. COMP 585 Noteset #2 1

Practice for Chapter 11

Pace University. Fundamental Concepts of CS121 1

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

CYES-C++: A Concurrent Extension of C++ through Compositional Mechanisms

QUIZ. How could we disable the automatic creation of copyconstructors

Combined Modeling and Programming with State Machines

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Data Abstraction. Hwansoo Han

Design Of JCSP Language Classes

Programming Exercise 14: Inheritance and Polymorphism

Java 1.8 Programming

Concurrency User Guide

Motivation State Machines

8. Polymorphism and Inheritance

Tool demonstration: Spin

Universe Type System for Eiffel. Annetta Schaad

Java: introduction to object-oriented features

What are the characteristics of Object Oriented programming language?

visualstate Reference Guide

QUIZ. How could we disable the automatic creation of copyconstructors

Hardware Description Languages & System Description Languages Properties

Configuration Provider: A Pattern for Configuring Threaded Applications

INHERITANCE WITH JAVA INTERFACES

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Computer Programming II C++ (830)

OBJECT ORİENTATİON ENCAPSULATİON

Second-generation: Assembly language. Figure 6.1 Generations of programming languages. Assembly Language Characteristics.

Java Finite State Machine Framework

NAME CHSM-Java Concurrent, Hierarchical, Finite State Machine specification language for Java

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Java Fundamentals (II)

Software Service Engineering

Object Orientated Analysis and Design. Benjamin Kenwright

5/23/2015. Core Java Syllabus. VikRam ShaRma

Chapter 6: Programming Languages

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Models of concurrency & synchronization algorithms

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Inheritance CSC 123 Fall 2018 Howard Rosenthal

State Machine Diagrams

Transcription:

State Machine Specification Directly in Java and C++ Alexander Sakharov 1

Problem Statement Finite state machines (FSM) have become a standard model for representing object behavior. UML incorporates FSM notation that is an object-oriented variant of Harel s statecharts. Specification and code generation facilities are not available in most widely used implementation languages like Java and C++. Availability of FSM-related automation directly in these languages may dramatically increase both the scope of usage of FSM specifications and the productivity of development of a broad range of software. In contrast to development of client-side visual components and server-side transactional components, automation of the development of non-visual non-transactional components, which normally play the role of listeners and which are best modeled by FSMs, has not been much addressed. Solution for Java and C++ FSMs can be specified directly in procedural object-oriented languages. Besides, FSM transitions are expanded by adding regular expressions of events and by adding unions of states. Assembly of applications out off FSM components can be specified in these languages as well. FSM components are generated from FSM specifications. Threads are generated from assembly specifications. They serve as event adapters, and implement control flow and data interchange between components. Overall, relevant UML s capabilities including concurrency regions and SynchStates are made available in Java and C++ with extra power added by regular expressions of events. Inheritance is a primary enabler of our approach that is quite generic and applicable to various specifications. An abstract base class declares static variables for storing specifications and contains code generation methods. Derivates of the base constitute FSM specifications. Execution of a compiled specification class does code generation resulting in a class derived from the specification class. Features and Benefits UML s state machines made available in procedural object-oriented languages Declarative and procedural styles combined: Specifications given in Java or C++ are declarative whereas supporting methods are procedural No additional specification languages, no extra tools required Generic approach to program specification and code generation directly in Java and C++ Powerful hybrid notation enriching FSM transitions via regular expressions of events and unions of states Focus on components: Generated components do not need further modification; they are serializable, customizable; they may be containers. Container components simulate composite states in Harel s statecharts UML s concurrent regions supported (including SynchStates) Automation of development of non-visual non-transactional listener components Complete applications generated from assembly specifications FSMs can be programmatically combined with other components, threads, etc. 2

Class Hierarchies Abstract Class Specification Class Generated Class Components Object Controllers Thread FSM * start / stop / transition / states - declaration () activate/deactivate/isactive () main - code generation () getevent / geteventindex / setevent / augmentevent FSM specifications classes * start / stop / transition - value () transition actions () guards Generated FSM classes * states - value () constructor () event methods () guards per events () eventless/eventlessguard FSMThread * containment /exchange/ source / syncpoint - declaration * data exchange references / components / containment matrix - declaration () setcomponentdataexchange () adjuststateconfiguration () dequeue () main - code generation Assembly specifications classes * containment / exchange/ source / containment matrix - value () data exchange methods () dequeue (optional) Generated controller classes * data exchange references / components / containment matrix - value () constructor () run () listener methods 3

Start state name Stop state name (optional) Transitions Action methods and guard code FSM Specification Sample FSM specification: public class TVHandler extends FSM { static { start = Off ; stop = ; transition = new String[][] { { Off, power On, turnon, { On, up On, volume(1), "!iserror()", { "On", "down", "On", "volume(-1)", "!iserror()", ; // transition methods public void turnon() { ) public void volume(double v) { // guards public boolean iserror() { Transition syntax in UML: Hybrid Transition Notation <event-signature> [ <guard-condition> ] / <action-expression> ^ <send-clause> We expand conventional FSM transitions with event and state expressions. State expressions may refer to unions of states. Their respective transition specifications denote multiple transitions. The event expression is a regular expression whose tokens are event signatures. Several actions can be given for a transition defined with an event expression. They may relate to transition events or may be default, normal, or premature. The two latter actions are execution upon transition completion. Our transition syntax: <name> : <source-state-expression> <event-expression> -> <target-state-expression> [ <guard-condition> ] / <action-expression-list> Examples of transitions with state unions and regular expressions of events: (-(Off))+ ) -> Off [ iserror() ] / (:displayerror) // eventless channel : (On Mute) ((one two three four five six seven eight nine zero)+ enter) ->. / ( (one two three four five six seven eight nine zero):displaychannel, enter:enterchannel(), ~premature:displaywarning( Channel is not set ) ) incdec : (On Mute) (enter (up down)+) ->. / (up:incchannel, down:decchannel, ~premature:displaywarning( Channel is not changed )) 4

Component Generation The classes generated from FSM specifications contain implementations of methods named as events in the transitions specified in their super classes. These generated classes also contain guard methods for the aforementioned transition event methods. Additionally, a couple of methods are generated for handling eventless transitions. Each generated FSM component class has an array with state names. For the sake of efficiency, FSM events and states are represented by integers in generated code. Generated transition event methods and their guards in the sub-classes have the form of switch statements whose cases are FSM states including pairs with hidden states. Branches in the switch statements of the guard methods test guard expressions. Every branch in the switch statements of the transition event methods calls an applicable transition action whose guard condition is satisfied and updates the FSM state. Generated FSM component: public class TVSpecificationSub extends TVSpecification { public static String[] state = { ; protected Vector incdec = new Vector(); public int enter( EventObject evt ) { switch (stateindex) { if ( transitionindex==n ) { action(); stateindex = m; break; public int eventless() { switch (stateindex) { public boolean enterguard( EventObject evt ) { switch (stateindex) { if ( ) { transitionindex = n; return true; public boolean eventlessguard() { switch (stateindex) { 5

Internal FSMs Transitions with regular expressions of events containing Kleene closure or concatenation are turned into internal FSMs augmenting their host FSMs. These generated internal FSMs are minimized after that. The internal FSMs add hidden states to the set of states of the host FSM. The hidden states are pairs composed of an explicitly defined state of the host and an internal automaton state. Execution of an internal FSM can be triggered by an event that can be the first token in a string of tokens from the language defined by the respective regular expression. Transitions with event expressions containing unions only are unfolded into multiple conventional transitions. Example illustrating generated method enter: void enter(eventobject eventvalue) { switch (stateindex) { case <On> : incdec.addelement(eventvalue); stateindex = <On,Incdec:1> ; break; case <On,Channel:1> : // hidden state channel. addelement(eventvalue); enterchannel(); stateindex = <On,Channel:2> ; break; case <On,Channel:2> : // hidden state channel.removeallelements(); stateindex = <On > ; enter(eventvalue); break; case <On,Incdec:1> : // hidden state displaywarning( Channel is not changed )); incdec.removeallelements(); stateindex = <On > ; enter(eventvalue); break; case <On,Incdec:2> : // hidden state incdec.removeallelements(); stateindex = <On > ; enter(eventvalue); break; case // other states code for Mute is similar to above default: break; 6

Assembly Specification Containment Specified by list of { container component, container state, sub-component Multiple components mapped to the same (component, state) pair are run concurrently This relation extends onto relevant hidden states Data exchange methods for component pairs Specified by list of { container component, sub-component, method1, method2, flag The flag indicates whether data exchange happens at invocation and at reaching a stop state, or at invocation and at every termination of the sub-component. Synchronization points for concurrent regions (a la SynchState in UML) Specified by list of { component1, component2, state/transition, state/transition Event sources and mapping of listener interface methods to transition events Specified by list of { component, source, transition event, listener interface method Event delivery mode (synchronous, asynchronous without queuing, or asynchronous with queuing) Sample assembly specifications: public class TVApplication extends FSMThread { static { containment = new String[][] { { TVHandler, On, TVSet, { TVHandler, On, TVDevice.cd, ); exchange = new String[][] { { TVHandler, TVSet, inset, outset, { TVHandler, TVDevice.cd indevice, outdevice, ); source = new String[][] { { TVHandler, TVRemote.rc, power, power, enter, enter,, { TVSet TVRemote.rc, toggle, swap, ), { TVDevice.cd, TVRemote.rc init, device, exec, init, ); syncpoint = new String[][] { { TVSet, TVDevice.cd, stateset, transitiondevice, transitionset, statedevice,, ; delivery = s ; // data exchange methods public static void inset(tvhandler h, TVSet s) { ) public static void outset(tvhandler h, TVSet s) { 7

Application Generation The controllers generated from assembly specifications are threads that implement control flow between containers and their sub-components and play the role of event adapters for all relevant FSM components. The generated controllers implement all listener interface methods for specified event sources. Each generated controller has a static Boolean array employed for fast determination of containment and another static array with references to data exchange methods. These controllers construct instances of generated FSM component classes. They construct and fire event sources, and then register themselves as listeners to the event sources. The controllers queue events when so specified. The controllers call counterparts of listener interface methods and eventless methods of the classes generated from FSM specifications. The default order of processing events in the queue is FIFO, which can be overridden. Generated controller: public class TVAppAdapter extends TVApp implements Runnable, EventListener, EventSource1,... { public TVAppAdapter() {... // sets data exchange private Vector eventqueue; private Vector eventindexqueue; private static EventSource source[] = {... ; static { component = new FSM[] {... ; issubcomponent = new boolean[][][] {... ; private Thread sourcethread[]; protected int dequeue() {... // to override FIFO... Event procedures in generated controllers: public void enter(eventobject e) { synchronized (eventqueue) { synchronized (indexqueue) { eventqueue.addelement(e); indexqueue.addelement(new Integer( <enter> )); The core of method run of generated controller: for (;;) { switch( eventindex ) { case <foo > : iterator = active.iterator(); label: while ( (point = (StateConfiguration)iterator.postorder())!=null ) { switch ( point.getcomponent() ) { case <componentx> : ((ComponentxSub)component[ <componentx> ]).setevent(event, eventindex); if ( ((ComponentxSub)component[ <componentx> ]).fooguard(eventvalue) ) { stateindex = ((ComponentxSub)component[ <componentx> ]).foo(eventvalue); adjuststateconfiguration(point, stateindex); break label; break; 8

Call Graph Generated FSM Sub-component Sub-component Concurent sub-components Generated controller transition actions and guards EventSource run listener interface methods constructs and fires addeventlistener removeeventlisten run 9