object -oriented discrete-event simulation with java978-1-4615-1319-3/1.pdf · not a complete...

16
Object-Oriented Discrete-Event Simulation with Java A Practical Introduction

Upload: vothien

Post on 22-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Object -Oriented Discrete-Event Simulation with Java

A Practical Introduction

SERIES IN COMPUTER SYSTEMS Series Editor: Rami G. Melhem

University of Pittsburgh Pittsburgh, Pennsylvania

FUNDAMENTALS OF X PROGRAMMING Graphical User Interfaces and Beyond Theo Pavlidis

INTRODUCTION TO PARALLEL PROCESSING Algorithms and Architectures Behrooz Parhami

OBJECT-ORIENTED DISCRETE-EVENT SIMULATION WITH JAVA A Practical Introduction jose M. Garrido

PERFORMANCE MODELING OF OPERATING SYSTEMS USING OBJECT-ORIENTED SIMULATION A Practical Introduction jose M. Garrido

Object-Oriented Discrete-Event Simulation With Java

A Practical Introduction

Jose M. Garrido Kennesaw State University Kennesaw, Georgia

Springer Science+Business Media, LLC

Library of Congress Cataloging-in-Publication Data

Garrido, Jose M. Object-oriented discrete-event simulation with Java: a practical introduction/Jose M. Garrido

p. cm. - (Series in computer systems) Includes bibliographical references and index. ISBN 978-1-4613-5495-6 ISBN 978-1-4615-1319-3 (eBook) DOI 10.1007/978-1-4615-1319-3

1. Java (Computer programming language) 2. Object-oriented programming (Computer science) I. Title. II. Series.

QA76.73.]38 G365 2001 005.13'3-dc21

Additional material to this book can be downloaded from http://extras.springer.com.

©200l Springer Science+Business Media New York

Originally published byKluwer Academic/Plenum Publishers in 2001

Softcover reprint of the hardcover 1st edition 2001

All rights reserved

2001038770

No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, microfilming, recording, or otherwise, without written permission from the Publisher

PREFACE

Researches and developers of simulation models state that the Java program­ming language presents a unique and significant opportunity for important changes in the way we develop simulation models today. The most important characteristics of the Java language that are advantageous for simulation are its multi-threading capabilities, its facilities for executing programs across the Web, and its graphics facilities.

It is feasible to develop compatible and reusable simulation components that will facilitate the construction of newer and more complex models. This is possible with Java development environments. Another important trend that begun very recently is web-based simulation, i.e., and the execution of simulation models using Internet browser software.

This book introduces the application of the Java programming language in discrete-event simulation. In addition, the fundamental concepts and prac­tical simulation techniques for modeling different types of systems to study their general behavior and their performance are introduced. The approaches applied are the process interaction approach to discrete-event simulation and object-oriented modeling. Java is used as the implementation language and UML as the modeling language. The first offers several advantages compared to C++, the most important being: thread handling, graphical user interfaces (QUI) and Web computing. The second language, UML (Unified Modeling Language) is the standard notation used today for modeling systems as a collection of classes, class relationships, objects, and object behavior.

The material in this book is related to the basic modeling and simulation concepts included in my two previous books: Practical Process Simulation with Object-Oriented Techniques and C++, published by Artech House in Boston, 1999, and Performance Modeling of Operating Systems Using Object­Oriented Simulation: A Practical Introduction, published by Kluwer /Plenum Academic Publishers, NY, 2000. For a more detailed description, see the Web locations:

http://science.kennesaw.edu/-jgarrido/mybook.html http://www.wkap.com

v

vi Preface

This book concentrates on object-oriented modeling and implementation aspects of simulation models using Java and practical simulation techniques. In addition, the book illustrates the dynamic behavior of systems using the various simulation models as case studies. The material presented will be best digested by the readers with good knowledge of the Java programming language.

For every topic discussed, a complete case study is presented and explained with the corresponding model implemented in Java. The most recent version of the Psim software, the Psim-J library (a set of Java classes), and the set of simulation models discussed in this book are available from the Web site:

http://science.kennesaw.edu/-jgarrido/psim.html.

Most chapters of the book deal with general simulation principles and con­cepts. The case studies presented as simulation models, and the simulation software (the first in source Java and the second in precompiled form) are accessible from the Web (on the Web site shown above).

Since there are very few books on simulation with Java, the book, with its associated software and models, was originally designed as a teaching tool. Professional practitioners can use the book to clarify and review the impor­tant practical concepts of performance modeling with simulation and some advanced application of Java programming.

This book is aimed at college students in: computer science, engineering, and management science. It is also aimed at practitioners dealing with any type of modeling for solving or studying any type of systems.

• systems analysts

• network engineers

• software engineers

• scientists using mathematical modeling

• trainers and instructors in any of the above fields

The material presented here can be used as a textbook for an applied course in Java and/or in discrete-event simulation with Java. More appropriately, this book can be used as a supplemental book that provides students with the practical issues of constructing models and various aspects of systems in studying performance.

The main features of the book are the following:

• The presentation of practical aspects of performance measures of several types of systems using object-oriented modeling and the process inter­action approach to simulation. Performance and Functional models are introduced.

Preface vii

• Use of the process interaction approach to modeling and simulation with Java.

• When used as a teaching tool, the book facilitates the understanding of large and complex systems to students, and gives them insight of how to approach a solution to these problems.

• One of the benefits is the ease of modeling complex concepts and prob­lems, for example systems dealing with concurrency and timing con­straints.

• The practical use of object-oriented concepts to solve real problems.

• When using the book for training, the solution of practical problems is carried out from the first day.

• The use of the simulation software as part of a larger integrated software development effort.

• It is a good practical source of material to understand the complexities of modeling and simulation with J ava, before deciding to acquire a more comprehensive and expensive simulation system. Web-based simulation, now possible with Java, is rapidly becoming the dominant means for simulations today and in the future.

The Java language is a very portable programming language. The object code (byte-code) of Java programs will run on any common platform that includes any type of Java development tool.

The library (Psim-J) is provided in object byte-code form. All the simu­lation models discussed are provided in Java source code. The case studies included need as much memory as required by the Java environment used. The executable program size depends on the simulation model built. For Windows 95/98, a memory size of at least 32-Mb is recommended. If a Java development tool is used (e.g., JBuilder) on Windows, at least 64 Mb is rec­ommended.

The book does not present the detailed theory of simulation and statistical treatment that appears in standard textbooks on system simulation. It is not a complete reference on object-oriented modeling, and programming with Java, or a complete theoretic reference on performance measures of systems. The book only includes the necessary basic theory of probability to support the construction of the simulation models. Most other books on classical discrete-event simulation include a more complete treatment on simulation theory, statistical analysis, and on performance issues.

In this respect, this book is a supplemental book to simulation and concen­trates on the practical aspects of performance modeling with simulation and Java using object-oriented techniques and UML.

viii Preface

I have had continuous support and ncouragement from the chair of the department of Computer Science and Information Systems, Prof. Merle King, and from the Dean of the College of Science and Mathematics, Dr. Larry Peterson. The second simulation model in Chapter 14 was developed by Margaret Shaw; she also read part of the material in this book. Matt Smith helped test Psim-J and debug several models. I am thankful to all these persons mentioned above. As with my first two books, my ultimate gratitude and dedication goes to my wife Gisela, who has helped me in most of the work for this project.

J. M. GARRlDO

Kennesaw State University, GA

CONTENTS

1 Introduction to Simulation 1

1.1 Introduction 1

1.2 Simulation with Java 1

1.2.1 Overview 1

1.2.2 Implementing Simulation Models with Java and Psim-J 3

1.3 Systems, Models, and Modeling 4

1.4 Categories of Models 5

1.5 Conceptual Models 5

1.6 Simulation Models 6

1.6.1 General Concepts 6

1.6.2 Designing Simulation Models 8

1.6.3 Implementing Simulation Models 9

1.7 Simulation Results 9

1.8 Types of Simulation Models 10

1.9 Phases in Model Development 11

1.10 Performance Measures and Performance Modeling 13

1.10.1 Performance Metrics 13

1.10.2 Workload 14

1.10.3 Additional Performance Concepts 14

1.11 Summary 15

1.12 Exercises 15

2 Objects, Classes and UML 17

2.1 Introduction 17

2.2 o b j ect-Oriented Modeling 17

2.2.1 Conceptual Model 17

ix

x Contents

2.2.2 Modeling Objects 18 2.2.3 Encapsulation 19 2.2.4 Interfaces 19 2.2.5 Objects and Classes 19

2.3 Using UML For Object-Oriented Modeling 20 2.4 Use Case Diagrams 21 2.5 Static Modeling Diagrams 22

2.5.1 Class Diagrams 22 2.5.2 Associations 23 2.5.3 Multiplicity of the Association 24 2.5.4 Aggregation 24 2.5.5 Generalization and Inheritance 25

2.5.5.1 Class Inheritance. 25 2.5.5.2 Generalization. 25

2.6 Dynamic Modeling Diagrams 26 2.6.1 Collaboration Diagrams 26 2.6.2 Sequence Diagrams 27 2.6.3 State Diagrams 27

2.7 Concurrency 28 2.8 Summary 29 2.9 Exercises 30

3 Relevant Aspects Of The Java Programming Language 31 3.1 Introduction 31 3.2 Types Of Java Programs 31 3.3 Basic Object-Oriented Concepts Of Java 32

3.3.1 Java Classes 32 3.3.2 Java Packages 33 3.3.3 Constructors 33

3.4 A Simple Application With Two Classes 34 3.5 Dealing With Global Variables 37

3.5.1 Need For Global Variables 37 3.5.2 Global Constants 37

3.6 Arrays and Collection Classes 38 3.6.1 Collection Classes 40

3.7 Inheritance 40 3.7.1 Base and derived classes 40 3.7.2 Constructors of the Subclasses 42

Contents xi

3.8 Abstract Classes 42 3.9 Polymorphism 43 3.10 Classes and Interfaces 43 3.11 Exceptions 44 3.12 Java Threads 45

3.12.1 Using Threads 45 3.12.2 Inheriting the Thread Class 46 3.12.3 Other Basic Thread Methods 47 3.12.4 Thread Suspending Itself 48 3.12.5 Implementing the Runnable Interface 48 3.12.6 Interrupting a Thread Object 49 3.12.7 Thread Priorities 51 3.12.8 Simple Thread Synchronization 51 3.12.9 Wait/Notify Mechanism in Threads 52

3.13 Summary 53 3.14 Exercises 53

4 Discrete-Event Simulation 55 4.1 Introduction 55 4.2 Modeling System Behavior 55 4.3 Implementation of Discrete-Event Simulation 56 4.4 The Time Dimension 57 4.5 World Views For Discrete-Event Simulation 58

4.5.1 The Activity World View 59 4.5.2 The Event World View 59 4.5.3 The Process Interaction World View 59

4.6 Implementing The Process World view 60 4.7 Object-Oriented Simulation 62 4.8 Software For Discrete-Event Simulation 63 4.9 Discrete Event Simulation Packages 64

4.9.1 SimPack and Sim++ 64 4.9.2 CSIM 64 4.9.3 ModSim 64 4.9.4 GPSS 65 4.9.5 Simula 65 4.9.6 Simscript 65 4.9.7 GASP 66 4.9.8 SLAM 66 4.9.9 Task Library 66

xii Contents

4.9.10 The Simulation Package and Language, Psim and PsimL 66

4.9.11 Other Simulation Packages 67

4.10 Simulation Packages in Java 67

4.10.1 Java version of Psim 67

4.10.2 Other Sources on Simulation Languages, Conferences, and Courses 67

4.11 Summary 68

4.12 Exercises 69

5 Simulation With The Process Worldview

5.1 Introduction

5.2 Processes and Simulation Models

5.2.1 Entities and Objects

5.2.2 Single-Server Models

5.3 A Model For The Car-Wash System

5.3.1 The Conceptual Model

5.3.2 The Car-Wash Deterministic Model 5.3.3 The Car-Wash Stochastic Model

5.4 Performance and Workload Characterization

5.5 Implementing The Simulation Model

5.5.1 Using Java And Psim-J

5.5.2 Java Implementation Of The Car-wash Model

5.6 Graphical User Interfaces

5.7 Summary

5.8 Exercises

6 Models of Multi-Server Systems

6.1 Introduction

6.2 Queuing Models

6.3 Multi-Server Queuing Models

6.4 The Multi-Server Model of the Car-Wash System

6.4.1 Description Of The Server Process

6.4.2 Description Of The Customer Process

6.4.3 Description Of The Arrivals Process

6.4.4 Simulation Results

6.5 Java Implementation of the Multi-Server Model

6.6 Multi-Server Models With Multiple Queues

71

71

71

71

72

73

73

75 80 84

85

85

87

93

93

95

97

97

97

98

99

100

100

101 102

105

108

Contents Xlll

6.6.1 Description Of The Server Process 108 6.6.2 Description of the Customer Process 109

6.7 Queuing Networks 109 6.8 Summary 110 6.9 Exercises 111

7 Multi-Class Queuing Models 113 7.1 Introduction 113 7.2 Priorities With Single-Server Models 114

7.2.1 Description of the Server Process 114 7.2.2 Description of the Arrivals Process 115 7.2.3 Description of the Car Process 115 7.2.4 Results from the Simulation 116 7.2.5 Implementation Of The Car-Wash Model 120

7.3 Multi-Server Models with Priorities 123 7.4 Summary 123 7.5 Exercises 124

8 Resources 127 8.1 Introduction 127 8.2 Resource Manipulation 127 8.3 Software Implementation of Resources 128 8.4 Model Implementation in Psim-.J 130 8.5 A Model of a Busy Warehouse 131 8.6 Allocating Resources with Priorities 136 8.7 Deadlock 136 8.8 Summary 137 8.9 Exercises 138

9 Basic Process Cooperation 139 9.1 Introduction 139 9.2 Producer-Consumer Cooperation 139 9.3 Producer and Consumer Objects 140 9.4 Implementing Process Cooperation 141

9.4.1 Producer Implementation 141 9.4.2 Consumer Implementation 142

9.5 Model Of A Machine Parts-Replacement System 142 9.6 Summary 147 9.7 Exercises 147

xiv Contents

10 Synchronous Cooperation 149 10.1 Introduction 149 10.2 Master-Slave Synchronization 149

10.2.1 General Synchronous Cooperation 149 10.2.2 Synchronous Communication 150

10.3 Implementing Process Cooperation in Java 151

10.4 Model With Synchronous Communication 152

10.5 Cooperation with Several Slaves 156

10.6 Summary 157

10.7 Questions 157

11 Conditional Waiting 159 11.1 Introduction 159 11.2 Modeling With Conditional Synchronization 159 11.3 Implementation with Java and Psim-J 160

11.3.1 General Approach 160 11.3.2 Implementing Simulation Models 161

11.4 A Model Of A Port System 162

11.5 Summary 166 11.6 Exercises 166

12 Interrupts 167 12.1 Introduction 167 12.2 Basic Notion of Interruptions 167 12.3 Interrupt Handling 168 12.4 Interrupts using Java and Psim-J 169

12.5 The Parts-Replacement Model With Interrupts 171

12.6 Car-Wash Model with Interrupts 175

12.7 Summary 181

12.8 Exercises 181

13 More Simulation Models 183 13.1 Introduction 183 13.2 Model of a Simple Client-Server System 183 13.3 Model of an Ethernet Client-Server System 191

13.3.1 The Ethernet Protocol 191 13.3.2 Network Performance 192

13.3.3 Model Implementation 192

13.4 Summary 196

13.5 Exercises

14 Basic Probability Theory and Random Numbers

14.1 Introduction

14.2 Experiments and Outcomes

14.3 Long-term Behavior of Systems

14.4 Overview of Set Theory

14.5 Probabilities

14.6 Random Variable and Distribution Functions

14.7 Expected Values, Variance and Covariance

14.8 The Geometric distribution

14.9 The Binomial Distribution

14.10 The Exponential Distribution

14.11 The Poisson Distribution

14.12 The Uniform Distribution

14.13 The Normal Distribution

14.14 Stochastic Processes

14.15 Random Numbers in Simulation Models

14.16 Markov Models

14.16.1 State Dependencies

14.16.2 Markov Chains

14.17 Summary Statistics 14.18 Comparing Sample Data

14.19 Summary

14.20 Questions

15 Overview of Model Verification and Validation

15.1 Introduction

15.2 Verification Techniques For Simulation Models

15.2.1 Object-Oriented Modeling and Programming

15.2.2 Using Traces

15.2.3 Graphical Outputs

15.2.4 Testing for Verification

15.3 Validation Techniques For Simulation Models

15.3.1 Importance

15.3.2 General Validation Approach

15.3.3 Black Box Validation

15.3.4 White Box Validation

Contents xv

196

197

197

197

198

198

199

199

200

201

202

203

203

204

204

204

205

206

206

207

208 209

210

211

213

213

213

213

214

214

214

215

215

215

216

216

xvi Contents

15.4 Summary

Appendix A: A Short Manual For Psim-J

A.1 Introduction to The Psim-J Library

A.2 List Of Classes

A.3 Defining Processes in a Simulation Model A.4 The Time Dimension

A.5 Other Process Methods

A.6 Starting and Running a Simulation

A.7 Priority of a Process

A.8 Process States

A.9 Scheduling Processes

A.10 Handling Queues

A.10.1 General Description

A.1O.2 Methods in Class Squeue

A.1O.3 Members of Class Pqueue

A.11 Classes for Resources

A.11.1 General Description

A.11.2 Class Res A.11.3 Relevant Features in the Bin Class

A.12 Class Waitq

A.13 The Condq Class

A.14 Random Number Generation in Psim-J A.14.1 Class Randint

A.14.2 Class Erand

A.14.3 Class Normal

A.14.4 Class Poisson

A.14.5 Class Urand

Appendix B: Bibliography

About the Author

Index

217

219

219

220

221 222

222 226

226

227

228

228

228

228

231 235

235

235 236 238

240

241 242

243 244

245 246

247

251

253