Embedded Real-Time Systems (TI-IRTS) GoF State Pattern a Behavioral Pattern



Similar documents
Embedded Real-Time Systems (TI-IRTS) State Machine Implementation

This sequence diagram was generated with EventStudio System Designer (

UML By Examples. 0. Introduction. 2. Unified Modeling Language. Home UML. Resources

Lab Manual: Using Rational Rose

How To Design Software

Testing high-power hydraulic pumps with NI LabVIEW (RT) and the StateChart module. Jeffrey Habets & Roger Custers

Life of a Packet CS 640,

point to point and point to multi point calls over IP

Object Oriented Design

Java Application Developer Certificate Program Competencies

Chapter 1 Fundamentals of Java Programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Questions? Assignment. Techniques for Gathering Requirements. Gathering and Analysing Requirements

KCU-02 Monitor. Software Installation User Manual. For Windows XP and Windows 7

History OOP languages Year Language 1967 Simula Smalltalk

Types of UML Diagram. UML Diagrams OOAD. Computer Engineering Sem -IV

Transport Layer. Chapter 3.4. Think about

Design Document For Insulin Pump (Version 1.1)

Prefix AggregaNon. Company X and Company Y connect to the same ISP, and they are assigned the prefixes:

Real Time Programming: Concepts

Old Company Name in Catalogs and Other Documents

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

A UML Documentation for an Elevator System. Distributed Embedded Systems, Fall 2000 PhD Project Report

Ethernet. Ethernet. Network Devices

Chapter 46 Terminal Server

Software Engineering. System Modeling

Fundamentals of Java Programming

Firewall Port Handling in TENA Applications

Object Oriented Software Models

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

DUAL PUMPS COMMUNICATIONS CABLE

INDUSTRIAL AUTOMATION Interactive Graphical SCADA System INSIGHT AND OVERVIEW. IGSS Online Training. Exercise 8: Creating Templates

Special Note Ethernet Connection Problems and Handling Methods (CS203 / CS468 / CS469)

Wharf T&T Limited DDoS Mitigation Service Customer Portal User Guide

Computer Networks. Chapter 5 Transport Protocols

NMS300 Network Management System

UML-based Test Generation and Execution

How To Develop Software

PK5500 v1.1 Installation Instructions

Changing Your Cameleon Server IP

How do I get to

Communication Diagrams

Software engineering for real-time systems

Chapter 8 The Enhanced Entity- Relationship (EER) Model

Assignment # 2: Design Patterns and GUIs

IP Subnetting and Addressing

CS330 Design Patterns - Midterm 1 - Fall 2015

New York University Computer Science Department Courant Institute of Mathematical Sciences

From Control Loops to Software

Global Monitoring + Support

Checking Access to Protected Members in the Java Virtual Machine

TDDC88 Lab 2 Unified Modeling Language (UML)

8-ch RAID0 Design by using SATA Host IP Manual Rev1.0 9-Jun-15

Backup Server DOC-OEMSPP-S/6-BUS-EN

Trends in Embedded Software Development in Europe. Dr. Dirk Muthig

Routing concepts in Cyberoam

Java (12 Weeks) Introduction to Java Programming Language

Case Study: F5 Load Balancer and TCP Idle Timer / fastl4 Profile

Designing a Home Alarm using the UML. And implementing it using C++ and VxWorks

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

Configuring Static and Dynamic NAT Translation

MS Project Tutorial for Senior Design Using Microsoft Project to manage projects

This sequence diagram was generated with EventStudio System Designer (

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

How To Understand The Dependency Inversion Principle (Dip)

UML for the C programming language.

Interaction Diagrams. Use Cases and Actors INTERACTION MODELING

Software Engineering. System Models. Based on Software Engineering, 7 th Edition by Ian Sommerville

Secure Network Access System (SNAS) Indigenous Next Generation Network Security Solutions

Umbrello UML Modeller Handbook

1 An application in BPC: a Web-Server

UML FOR OBJECTIVE-C. Excel Software

Integration, testing and debugging of C code together with UML-generated sources

ivms-4200 Client Software Quick Start Guide V1.02

Design by Contract beyond class modelling

PT Activity: Configure Cisco Routers for Syslog, NTP, and SSH Operations

Bitter, Rick et al "Object-Oriented Programming in LabVIEW" LabVIEW Advanced Programming Techinques Boca Raton: CRC Press LLC,2001

Getting Started - SINAMICS Startdrive. Startdrive. SINAMICS Getting Started - SINAMICS Startdrive. Introduction 1

C++ INTERVIEW QUESTIONS

What is a Firewall? A choke point of control and monitoring Interconnects networks with differing trust Imposes restrictions on network services

Architecture & Design of Embedded Real-Time Systems (TI-AREM)

Designing Real-Time and Embedded Systems with the COMET/UML method

USTC Course for students entering Clemson F2013 Equivalent Clemson Course Counts for Clemson MS Core Area. CPSC 822 Case Study in Operating Systems

UML Activities & Actions. Charles ANDRE - UNSA

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

Kepware Technologies Optimizing KEPServerEX V5 Projects

How To Set Up A Modbus Cda On A Pc Or Maca (Powerline) With A Powerline (Powergen) And A Powergen (Powerbee) (Powernet) (Operating System) (Control Microsci

PROGRAMMABLE LOGIC CONTROL

FILE TRANSFER PROTOCOL INTRODUCTION TO FTP, THE INTERNET'S STANDARD FILE TRANSFER PROTOCOL

Application of UML in Real-Time Embedded Systems

Port Scanning. Objectives. Introduction: Port Scanning. 1. Introduce the techniques of port scanning. 2. Use port scanning audit tools such as Nmap.

THE STEP7 PROGRAMMING LANGUAGE

The first program: Little Crab

Cyclic Architectures in UML

4D v11 SQL Release 6 (11.6) ADDENDUM

Engineering Design. Software. Theory and Practice. Carlos E. Otero. CRC Press. Taylor & Francis Croup. Taylor St Francis Croup, an Informa business

SNMP Manager User s Manual

Business Vehicle Tracking USER MANUAL

Transcription:

Embedded Real-Time Systems (TI-IRTS) GoF State Pattern a Behavioral Pattern Version: 5-2-2010

State Pattern Behavioral Intent: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class (its state). An implementation technique for realizing a state machine described by a UML State Chart. Each state is implemented as a class with the events as operations. Slide 2

TCP State Chart Example TCPClosed ActiveOpen PassiveOpen Close Transmit TCPEstablished Close Send TCPListen Slide 3

Client State Pattern Class Diagram * TCPConnection +ActiveOpen() +PassiveOpen() +Close() +Acknowledge() +Synchronize() +Send() +Transmit() _state 1 TCPState +ActiveOpen() +PassiveOpen() +Close() +Acknowledge() +Synchronize() +Send() +Transmit() _state->activeopen() TCPEstablished TCPListen TCPClosed Close() Transmit() Close() Send() ActiveOpen () PassiveOpen() Slide 4

State Pattern - GoF Structure Client * Context +Request() _state 1 State +Handle() _state->handle(); ConcreteStateA +Handle() ConcreteStateB +Handle() Slide 5

State Pattern C++ Example (1) TCPConnection ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() -ChangeState() «friend» _state TCPState ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() -ChangeState() class TCPConnection public: TCPConnection(); void ActiveOpen(); void PassiveOpen(); void Close(); void Acknowledge(); void Synchronize(); void Send(); void Transmit(); void ProcessOctet(TCPOctetStream*); private: friend class TCPState; void ChangeState(TCPState*); TCPState* _state; ; Slide 6

State Pattern C++ Example (2) TCPConnection::TCPConnection () _state = TCPClosed::Instance(); void TCPConnection::ActiveOpen () _state->activeopen(this); void TCPConnection::PassiveOpen () _state->passiveopen(this); void TCPConnection::Close () _state->close(this); void TCPConnection::Acknowledge () _state->acknowledge(this); void TCPConnection::Synchronize () _state->synchronize(this); void TCPConnection::ChangeState (TCPState* s) _state = s; Slide 7

State Pattern C++ Example (3) class TCPState public: virtual void ActiveOpen(TCPConnection*); virtual void PassiveOpen(TCPConnection*); virtual void Close(TCPConnection*); virtual void Acknowledge(TCPConnection*); virtual void Synchronize(TCPConnection*); virtual void Send(TCPConnection*); virtual void Transmit(TCPConnection*, TCPOctetStream*); protected: TCPState() void ChangeState(TCPConnection*, TCPState*); ; Slide 8

State Pattern C++ Example (4) Implementation of TCPState with default code: void TCPState::ActiveOpen (TCPConnection*) default(); void TCPState::PassiveOpen (TCPConnection*) default(); void TCPState::Close (TCPConnection*) default(); void TCPState::Acknowledge (TCPConnection*) default(); void TCPState::Synchronize (TCPConnection*) default(); void TCPState::Send (TCPConnection*) default(); void TCPState::Transmit (TCPConnection*, TCPOctetStream*) default(); void TCPState::ChangeState (TCPConnection* t, TCPState* s) t->changestate(s); Slide 9

State Pattern C++ Example (5) TCPConnection::TCPConnection () _state = TCPClosed::Instance(); // start state TCPClosed state object create :TCPConnection :TCPClosed Slide 10

State Pattern C++ Example (6) 1 ActiveOpen 1.1 ActiveOpen :TCPConnection 1.1.2.1 ChangeState :TCPClosed 1.1.2 ChangeState 1.1.1 Instance void TCPConnection::ActiveOpen () _state->activeopen(this); TCPEstablished void TCPClosed::ActiveOpen (TCPConnection* t) // send SYN, receive SYN, ACK, etc. ChangeState(t, TCPEstablished::Instance()); // state shift Slide 11

State Pattern C++ Example (7) void TCPClosed::PassiveOpen (TCPConnection* t) ChangeState(t, TCPListen::Instance()); void TCPEstablished::Transmit (TCPConnection* t, TCPOctetStream* o) t->processoctet(o); // no state change void TCPEstablished::Close (TCPConnection* t) // send FIN, receive ACK of FIN ChangeState(t, TCPListen::Instance()); Slide 12

State Pattern C++ Example (8) Singleton pattern class Singleton public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; ; Singleton* Singleton::_instance = 0; Singleton* Singleton::Instance () if (_instance == 0) _instance = new Singleton; return _instance; Slide 13

State Pattern C++ Example (9) Singleton pattern used on TCPClosed class class TCPClosed : public TCPState public: static TCPState* Instance(); virtual void ActiveOpen(TCPConnection*); virtual void PassiveOpen(TCPConnection*); protected: TCPClosed(); private: static TCPState* _instance; TCPState* TCPClosed::_instance = 0; TCPState* TCPClosed::Instance() if (_instance = = 0) _instance = new TCPClosed; return _instance; Slide 14

State Pattern Implementation Details (1) TCPState ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() 1. Design choice 1. All event operations specified as pure virtual (C++) Requires that all operations are defined in the subclasses (forced by the compiler) 2. Design choice 2. All event operations have default implementation in superclass Only necessary to implement event operations for the actual state Slide 15

State Pattern Implementation Details (2) Event parameters, guard, entry and exit actions void TCPClosed::event1(TCPConnection* t,type parameter1) if ( t->guard1() ) exit(); // explicit call of exit action t->action_1(); ChangeState(t,TCPListen::Instance()); void TCPConnection::ChangeState(State* ps) _state= ps; _state->entry(); // call entry in new state Slide 16

Three Solutions for Event Handling 1. 2. Class_X +event1() +event2() +event3() _state->event1(); StatePattern Class_X +handleevent(eventno) StatePattern 3. Class_X +handlecommand(command* pc) switch (eventno) case START: _state->start(); StatePattern pc->execute(_state); Slide 17

Event Handling - Solution 1. Class_X +event1() +event2() +event3() _state->event1(); StatePattern Each event modeled as an operation in the Context class TCPConnection ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() _state TCPState ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() _state->activeopen() Slide 18

Event Handling - Solution 2. Class_X +handleevent(eventno) Only one operation in the Context class switch based StatePattern switch (eventno) case START: _state->start(); TCPConnection TCPState +handleevent(eventno) switch (eventno) case ACTIVE_OPEN: _state->activeopen(); break; Slide 19 _state ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit()

Event Handling - Solution 3. Class_X +handlecommand(command* pc) StatePattern pc->execute(_state); Only one operation in the Context class polymorphic based An example of the Command Pattern used in concert with the State Pattern Slide 20

Command (GoF) & State Pattern (C++) Command +excecute() TCPConnection handlecommand() _state TCPState ActiveOpen () PassiveOpen() Close() Acknowledge() Synchronize() Send() Transmit() ActiveOpen PassiveOpen Close excecute() excecute() excecute() TCPEstablished TCPListen TCPClosed Close() Transmit() Close() Send() ActiveOpen () PassiveOpen() excecute(tcpstate* pstate) pstate->activeopen(); handlecommand(command* pcommand) pcommand->excecute(_state); Slide 21

State Pattern Infusion Pump Example InfusionPump Control «friend» +start() +stop() +alarmacknowledged() +alarmdetected() +dooropened() +clear() -startalarm() -stopalarm() -startinfusion() -stopinfusion() -cleartotal() currentstate STATE abstract +start() +stop() +alarmacknowledged() +alarmdetected() +dooropened() +clear() ALARM +alarmacknowledged() INFUSION_INACTIVE +start() +clear() INFUSION_ACTIVE +stop() +alarmdetected() +dooropened() Slide 22

Example with Hierarchic State Machine (1) Infusion Pump Operating state setin Maintenance Infusion Pump Maintenance state setinoperation /start_alarm Alarm alarmacknowledged / stopalarm stop / stopinfusio n Infusion Inactive clear / cleartotal start[ door_closed ] / startinfusion /stopinfusion; startalarm Infusion Active alarmdetected dooropened Slide 23

Hierarchic State Pattern Example STATE InfusionPump Control currentstate start() stop() alarmacknowledged() alarmdetecteded() dooropened() clear() setinmaintenance() setinoperation() abstract classes Operation setinmaintenance() Maintenance setinoperation() ALARM alarmacknowledged() INFUSION_INACTIVE start() clear() INFUSION_ACTIVE stop() alarmdetected() dooropened() Slide 24

Definition of Interface IPump Client «interface» InfusionPump Control-IF +start() +stop() +alarmacknowledged() +alarmdetected() +dooropened() +clear() +setinoperation() +setinmaintenance() action op. only called from state machine Slide 25 InfusionPump Control +start() +stop() +alarmacknowledged() +alarmdetected() +dooropened() +clear() +setinoperation() +setinmaintenance() +startalarm() +stopalarm() +startinfusion() +stopinfusion() +cleartotal()

Discussion of State Pattern Implementation Disadvantages Results in many small classes Breaks encapsulation (of the state machine class) Advantages Easy to implement most UML state chart notations (guards, event parameters, entry, exit) State Pattern can be reverse engineered from code All state handling logic in separate state classes Easy to extend with new states Recommendation: Very useful for nontrivial flat and hierarchical state machines Slide 26

Pacemaker Class Diagram Classes with state machines abstract class Slide 27

Pacemaker State Chart for Class Coil Driver Slide 28

Step 1: Construct Class Hierarchy CoilDriver CoilDriver State Disabled Enabled Idle Receiving Bit Waiting ForBit WaitingTo Transmit Transmitting Bit WaitingTo Pulse Pulsing Slide 29

Step 2: Add Event Operations CoilDriver EnableComm() DisableComm() Outgoing(Byte) Pulse() mystate CoilDriver State EnableComm() DisableComm() Outgoing(Byte) Pulse() abstract class with abstract operations Disabled EnableComm() Enabled DisableComm() Add operations for outgoing arrows on state chart Idle Receiving Bit Waiting ForBit WaitingTo Transmit Transmitting Bit Outgoing(Byte) Pulse() WaitingTo Pulse Pulsing Slide 30

Summary State extremely useful Slide 31