design of software systems

24
KyungHee Univ. 2-1 Design of Software Systems

Upload: charla

Post on 06-Feb-2016

52 views

Category:

Documents


0 download

DESCRIPTION

Design of Software Systems. Self-Documenting Code. 이용자를 위한 Comment 유효한 입력 자료의 형태는 ? 어떠한 출력이 가능한가 ? 어떠한 일을 하는 소프트웨어 인가 ? 해당 모듈을 Call 하는 방법은 ? 에러가 발생할 조건은 ? 개발자를 위한 Comment 소프트웨어가 어떻게 동작 하나 ? 어떠한 알고리즘이 사용되었나 ? 어떻게 소프트웨어가 테스트 되었나 ? 어떻게 소프트웨어를 변경할 수 있나 ?. Self-Documenting Code. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Design of Software Systems

KyungHee Univ. 2-1

Design of Software Systems

Page 2: Design of Software Systems

KyungHee Univ. 2-2

Self-Documenting Code 이용자를 위한 Comment

유효한 입력 자료의 형태는 ?어떠한 출력이 가능한가 ?어떠한 일을 하는 소프트웨어 인가 ?해당 모듈을 Call 하는 방법은 ?에러가 발생할 조건은 ?

개발자를 위한 Comment소프트웨어가 어떻게 동작 하나 ?어떠한 알고리즘이 사용되었나 ?어떻게 소프트웨어가 테스트 되었나 ?어떻게 소프트웨어를 변경할 수 있나 ?

Page 3: Design of Software Systems

KyungHee Univ. 2-3

Self-Documenting Code 나쁜 Comment 예

X=X+4; /* add 4 to x */Flag = 0; /* set Flag = 0 */

좋은 Comment 예X=X+4; /* 4 is added to correct for the offset (mV)

in the transducer */Flag = 0; /* mean no key has been typed */

Page 4: Design of Software Systems

KyungHee Univ. 2-4

Self-Documenting Code Subroutine 과 Function 의 Comment

이용자를 위한 Comment 어떻게 Function 이 사용 되는가 ? 어떻게 Parameters 가 Pass 되는가 ? 어떤 종류의 에러가 발생될 수 있는 가 ? 어떻게 결과가 Return 되는가 ?

개발자를 위한 Comment 어떻게 함수가 동작하는가 ?

Comment 의 위치이용자를 위한 Comment

C 언어인 경우 : *.h file 에 작성 Assembly 언어 인 경우 : Function 의 시작 전에 작성

개발자를 위한 Comment C 언어인 경우 : *.c file 에 작성 Assembly 언어 인 경우 : Function 의 Body 에 작성

Page 5: Design of Software Systems

KyungHee Univ. 2-5

Self-Documenting Code 이해하기 쉽고 변경하기 쉬운 소프트웨어의

작성#define 문을 이용하여 이해하기 쉽고 변경하기

쉬운 소프트웨어의 작성 한다 .

#define 문의 부적절한 사용 예

Page 6: Design of Software Systems

KyungHee Univ. 2-6

Self-Documenting Code

#define 문의 적절한 사용 예

Page 7: Design of Software Systems

KyungHee Univ. 2-7

Module 이용자에게 필요한 Comment

Purpose of the moduleInput parameters

How passed (call by value, call by reference)적정한 범위 ( 입력이 적정한 범위에 있는가 ?)Format(8 bit/16 bit, signed/unsigned, etc)

Output parametersHow passed (return by value, return by reference)Format(8 bit/16 bit, signed/unsigned, etc)

Example input and outputError conditionExample calling sequenceLocal variables 와 그들의 의미

Page 8: Design of Software Systems

KyungHee Univ. 2-8

Naming Convention

의미 있는 Name 을 사용하라 . 모호한 ( 혼동의 우려가 있는 ) name 을 사용 하지 마라 . Data type 을 알 수 있도록 하라 . 같은 type 의 Object 에 같은 ( 유사한 ) 이름을 사용 하라 . Public object 를 구분할 수 있는 접두사를 사용하라 . 대문자와 소문자를 사용하여 Object 의 Scope 를 구분하라 .

대문자 사용소문자 로 시작 대문자로 구분대 문자로 시작 대 문자로 구분

_ 로 구분

Page 9: Design of Software Systems

KyungHee Univ. 2-9

Software Abstraction

Software Abstraction 은 복잡한 시스템을 잘 정의된 추상화된 기본 모듈을 사용 구성 한다 . 그 결과개발 기간이 단축되고 ,

미리 개발된 많은 Building Block 을 사용이해하기 쉽고 ,보수 유지가 용이 하고 ,최적화가 용이한 소프트웨어 시스템을 설계할 수

있다 .

Page 10: Design of Software Systems

KyungHee Univ. 2-10

Software Abstraction

Software Abstraction 의 대표적인 예는Finite-State Machine(FSM)

Moore FSM• Output 은 오직 현재의 State 에 의하여만 결정 된다 .• Input 은 State Transition 에 만 영향을 준다 .

Mealy FSM• 현재의 상태와 Input 에 의하여 Output 이 결정된다 .

Proportion Integral Derivative (PDI) Digital ControllerFuzzy Logic Digital ControllerNeural NetworkLinear System of Differential Equation

Page 11: Design of Software Systems

KyungHee Univ. 2-11

Moore Finite-State Machine Traffic Light Controller

2 개의 입력 센서 사용0 0 : 북 , 동쪽 모두 차량이 없음0 1 : 동쪽 길에만 차량 있음1 0 : 북쪽 길에만 차량 있음1 1 : 양쪽 길에 모두 차량 있음

북쪽 센서 동쪽 센서

Page 12: Design of Software Systems

KyungHee Univ. 2-12

Moore Finite-State Machine Traffic Light Controller

Output StategoN : 100001 동쪽 신호 Red , 북쪽 신호 GreenwateN : 100010 동쪽 신호 Red , 북쪽 신호 Yellow goE : 001100 동쪽 신호 Green, 북쪽 신호 RedwateE : 010100 동쪽 신호 Yellow, 북쪽 신호 Red MSB : 동쪽 신호 Red, Yellow, Green 북쪽 신호 Red, Yellow, Green LSB

교통 신호의 동작 기술양방향 모두 차가 없는 경우 현재의 Green 상태

유지Green 에서 Red 로 변경 시 5 초간 황색 신호 유지Green 신호는 최소 30 초간 유지만약 차가 한 방향에서만 오는 경우 그 방향의

Green 신호 를 계속 유지양 방향에서 차가 오는 경우 4 상태를 순차적 이동

Page 13: Design of Software Systems

KyungHee Univ. 2-13

Moore Finite-State Machine Traffic Light Controller

Tabular form of Moore FSM that implements a traffic light.

Graphical form of Moore FSM that implements a traffic light.

Page 14: Design of Software Systems

KyungHee Univ. 2-14

Moore Finite-State Machine Traffic Light Controller

First Step : Operation Sequence 의 결정Timer 와 I/O Port 의 초기화초기 상태 설정FSM 제어기 구현

현재 상태에 따른 신호 상태를 출력 현재 상태에 따른 Delay 실현 입력 Sensor 로부터 입력 현재 상태와 입력에 따라 다음 상태로 천이

Page 15: Design of Software Systems

KyungHee Univ. 2-15

Moore Finite-State Machine Traffic Light Controller

Second Step : Data Structure 정의C 언어 프로그래밍 예

Page 16: Design of Software Systems

KyungHee Univ. 2-16

C 언어 프로그래밍 예 : Table implementation

// Table implementationconst struct State { unsigned char Out; unsigned short Time; unsigned char Next[4];}; typedef const struct State STyp;#define goN 0#define waitN 1#define goE 2#define waitE 3STyp FSM[4]={ {0x21,3000,{goN,waitN,goN,waitN}}, {0x22, 500,{goE,goE,goE,goE}}, {0x0C,3000,{goE,goE,waitE,waitE}}, {0x14, 500,{goN,goN,goN,goN}}};

void main(void){ unsigned char n; // state number unsigned char Input; Timer_Init(); PORTF = 0x00; DDRF = 0xFF; DDRD &= ~0x03; n = goN; while(1){ PORTF = FSM[n].Out; Timer_Wait10ms(FSM[n].Time); Input = (~PORTD) & 0x03; n = FSM[n].Next[Input]; }}

Page 17: Design of Software Systems

KyungHee Univ. 2-17

Program 2.11. Two 6812 C implementations of a Moore FSM. : Pointer implementation

// Pointer implementationconst struct State { unsigned char Out; unsigned short Time; const struct State *Next[4];}; typedef const struct State STyp;#define goN &FSM[0]#define waitN &FSM[1]#define goE &FSM[2]#define waitE &FSM[3]STyp FSM[4]={ {0x21,3000,{goN,waitN,goN,waitN}}, {0x22, 500,{goE,goE,goE,goE}}, {0x0C,3000,{goE,goE,waitE,waitE}}, {0x14, 500,{goN,goN,goN,goN}}};

void main(void){ STyp *Pt; // state pointerunsigned char Input; Timer_Init(); PORTF = 0x00; DDRF = 0xFF; DDRD &= ~0x03; Pt = goN; while(1){ PORTF = Pt->Out; Timer_Wait10ms(Pt->Time); Input = (~PORTD) & 0x03; Pt = Pt->Next[Input]; }}

Page 18: Design of Software Systems

KyungHee Univ. 2-18

Mealy Finite-State Machine Robot Controller

Robot 은 Mood 센서 Port A 를 통하여을 4 가지의 가능한 입력 상태를 갖는다 . 00 : Ok, 현재 상태로 기분 좋음 . 01 : Tired, 피곤하기 때문에 현재 보다 편한 상태를 원함 . 10 : Curious, 주변에 대하여 관심을 보이는 상태 , 이 상태에서는 않거나

서있는 상태에 있어야 한다 . 11 : Anxious, 위험을 감지함 . 현재보다 긴장된 상태로 돌입하여야 함 .

PD1PD0

PF3PF2Pf1Pf0

AVR128

Page 19: Design of Software Systems

KyungHee Univ. 2-19

Mealy Finite-State Machine Robot Controller

Robot 은 Standing, Sitting, Sleeping 3 가지 상태를 갖는다 .

Robot 은 4 가지 상태의 출력을 갖는다 .PB3 SitDown, 현재 서있는 상태에서 앉아 있는 상태로PB2 StandUp, 현재 앉아 있는 상태에서 서있는 상태로PB1 LieDown, 현재 앉아 있는 상태에서 누운 상태로PB0 SitUp, 현재 누운 상태에서 앉아 있는 상태로

입력 출력

Page 20: Design of Software Systems

KyungHee Univ. 2-20

Mealy Finite-State Machine Robot Controller

Software Design 의 첫 단계에서는 동작의 순서를 결정함 . Input/Output Port 를 초기화함 .초기 상태를 기술함 .FSM Controller 를 구현함

Sensors Input 현재 상태와 입력에 따라 Robot 에 Output 현재 상태와 입력에 따라 상태를 천이 한다 .

둘째 단계에서는 Linked data structure 를 사용하여 FSM Graph 를 정의한다 .

Page 21: Design of Software Systems

KyungHee Univ. 2-21

C Implementation of a Mealy Finite State Machine.Output defined as numbers

// outputs defined as numbersconst struct State{ unsigned char Out[4]; // outputs const struct State *Next[4]; // Next};typedef const struct State StateType;#define Standing &fsm[0]#define Sitting &fsm[1]#define Sleeping &fsm[2]

#define None 0x00#define SitDown 0x08 // pulse on PF3#define StandUp 0x04 // pulse on PF2#define LieDown 0x02 // pulse on PF1#define SitUp 0x01 // pulse on PF0

StateType FSM[3]={{{None,SitDown,None,None}, //Standing {Standing,Sitting,Standing,Standing}},{{None,LieDown,None,StandUp}, //Sitting {Sitting,Sleeping,Sitting,Standing }},{{None,None,SitUp,SitUp}, //Sleeping {Sleeping,Sleeping,Sitting,Sitting}}};

void main(void){ StatePtr *Pt; // Current State unsigned char Input; DDRF = 0xFF; // Output to robot DDRD &= ~0x03; // Input from sensor Pt = Standing; // Initial State while(1){ Input = (~PORTD) & 0x03; // Input=0-3 PORTF =Pt->Out[Input]; // Pt = Pt->Next[Input]; // next state }}

Page 22: Design of Software Systems

KyungHee Univ. 2-22

C Implementation of a Mealy Finite State Machine.Output defined as functions

// outputs defined as functionsconst struct State{ void (*CmdPt)[4](void); // outputs const struct State *Next[4]; // Next};typedef const struct State StateType;Typedef StateType * StatePtr;

#define Standing &fsm[0]#define Sitting &fsm[1]#define Sleeping &fsm[2]

void None(void){};void SitDown(void){ PORTF=0x08;} // PF3void StandUp(void){ PORTF=0x04; PORTB=0;} // PF2void LieDown(void){ PORTF=0x02; PORTB=0;} // PF1void SitUp(void) { PORTF=0x01; PORTB=0;} // PF0

StateType FSM[3]={{{&None,&SitDown,&None,&None}, //Standing {Standing,Sitting,Standing,Standing}},{{&None,&LieDown,&None,&StandUp},//Sitting {Sitting,Sleeping,Sitting,Standing }},{{&None,&None,&SitUp,&SitUp}, //Sleeping {Sleeping,Sleeping,Sitting,Sitting}}};void main(void){ StatePtr Pt; // Current State unsigned char Input;

DDRF = 0xFF; // Output to robot DDRD &= ~0x03; // Input from sensor Pt = Standing; // Initial State

while(1){ Input = (~PORTD) & 0x03; // Input=0-3 (*Pt->CmdPt[Input])(); // function Pt = Pt->Next[Input]; // next state }}

Page 23: Design of Software Systems

KyungHee Univ.2-23

Vending Machine(Moore Finite-State Machine)

Vending Machine 은 센서 Port A 를 통 하여 3 가지의 가능한 입력 상태를 갖고 캔의 가격은 1500 원 이다 . 00 : 입력 없음 . 01 : 500 원 동전 입력 상태 . 10 : 1000 원 동전 입력 상태 . 11 : 불가능한 입력 .

M0

None

W500

None

M500

None

W1000

None

M1000

None

M1500

캔출력W1500

None

W2000

None

M2000

500 원 출력

입력에 관계없이 언제나

500 원 입력

100 원 입력

W 캔출력

None

Page 24: Design of Software Systems

KyungHee Univ. 2-24

Vending Machine(Moore Finite-State Machine)

Program 예