A Simple HVAC System Simulation Focusing on UML

  • Slides: 75
Download presentation
A Simple HVAC System Simulation (Focusing on UML modelling and not physics) Jan Pettersen

A Simple HVAC System Simulation (Focusing on UML modelling and not physics) Jan Pettersen Nytun HVAC stands for Heating, Ventilation, and Air Conditioning. We ignore ventilation and focus on heating and cooling (air conditioning).

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 2

Overview Room Outside Cooler Temperature Sensor Heater Temperature Setpoint Thermostat HVAC System From Wikipedia:

Overview Room Outside Cooler Temperature Sensor Heater Temperature Setpoint Thermostat HVAC System From Wikipedia: A thermostat is a device for regulating the temperature of a system so that the system's temperature is maintained near a desired setpoint temperature. The thermostat does this by controlling the flow of heat energy into or out of the system. That is, thermostat switches heating or cooling devices on or off as needed to maintain the correct temperature. 3

UML Class Diagram From Wikipedia In the Unified Modelling Language (UML), a class diagram

UML Class Diagram From Wikipedia In the Unified Modelling Language (UML), a class diagram is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, and the relationships between the classes. 4

UML Diagrams Shown With The Help Of A UML Class Diagram Italic indicates abstract

UML Diagrams Shown With The Help Of A UML Class Diagram Italic indicates abstract class Diagram Generalization Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram State Machine Diagram Interaction Diagram Sequence Diagram None-italic indicates concrete class Use Case Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 5

Behaviour Modelling Use Case Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram

Behaviour Modelling Use Case Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram Use Case Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 6

UML Use Case Diagram From Wikipedia A use case diagram is a type of

UML Use Case Diagram From Wikipedia A use case diagram is a type of behavioural diagram defined by the Unified Modelling Language (UML). Its purpose is to present a graphical overview of the functionality provided by a system in terms of actors, their goals—represented as use cases—and any dependencies between those use cases. 7

Simulation Seen From The User Point Of View From Wikipedia: Wooden mechanical horse simulator

Simulation Seen From The User Point Of View From Wikipedia: Wooden mechanical horse simulator during WWI. UML Use Case Diagram The simulation is meant* as a tool for deciding dimensions of HVAC systems – you can also see it as a test of an early version of an airconditioning simulation system. * Actually it is meant as a pedagogical tool when teaching UML. 8

Behaviour Modelling Activity Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour

Behaviour Modelling Activity Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Use Case Diagram Activity Diagram Package Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 9

Activity Diagram From Ambysoft Inc. , Scott W. Ambler: http: //www. agilemodeling. com/artifacts/activity. Diagram.

Activity Diagram From Ambysoft Inc. , Scott W. Ambler: http: //www. agilemodeling. com/artifacts/activity. Diagram. htm UML 2 activity diagrams are typically used for business process modelling, for modelling the logic captured by a single use case or usage scenario, or for modelling the detailed logic of a business rule. Although UML activity diagrams could potentially model the internal logic of a complex operation it would be far better to simply rewrite the operation so that it is simple enough that you don’t require an activity diagram. In many ways UML activity diagrams are the objectoriented equivalent of flow charts and data flow diagrams (DFDs) from structured development. 10

Overview Functioning Of Simulation Application Activity Diagram 11

Overview Functioning Of Simulation Application Activity Diagram 11

Background Information/ Decisions/Assumptions • The Kelvin scale is an absolute temp. scale where the

Background Information/ Decisions/Assumptions • The Kelvin scale is an absolute temp. scale where the coldest possible temp. is 0 Kelvin. Relation between Celsius degrees and Kelvin degrees is given by: °C = K − 273. 15 • The temperature in the room is influenced by the outside and the HVAC system. • Outside has a given temp. , stated in Kelvin; the same have heaters and coolers of the HVAC. 12

Structure Modelling Class Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour

Structure Modelling Class Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram Use Case Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 13

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 14

Analysis Model (Platform Independent Model - PIM) Temperature is a local phenomenon, i. e.

Analysis Model (Platform Independent Model - PIM) Temperature is a local phenomenon, i. e. it varies with where the measurement is done; for some simulations a more “volume oriented” approach would be better, e. g. a finite element approach. We simplify: The Outside is what surrounds our “volume”; the volume is represented by an object of type Room. 15

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 16

Introducing Behaviour • Objects communicate with each other by sending messages. In our case

Introducing Behaviour • Objects communicate with each other by sending messages. In our case a message is a call on an operation done by one object on another object. • In our case the calls will be synchronous like you have it in Java, i. e. the caller is blocked during the call (waiting for the return). • The operations will perform various actions, e. g. assigning values to fields. 17

Background Information/Decisions/Assumptions We introduce concept temperature exchanger - outside, heaters and coolers are temperature

Background Information/Decisions/Assumptions We introduce concept temperature exchanger - outside, heaters and coolers are temperature exchangers. A temperature exchanger transfer heat or chill to the room. A temperature exchanger is characterized by its temperature and also by its ability to transfer heat (chill) to the room. 18

Background Information/Decisions/Assumptions Continues… The temp transfer of a working temperature exchanger is dependent on

Background Information/Decisions/Assumptions Continues… The temp transfer of a working temperature exchanger is dependent on its environment and its own physics, e. g. the heat transfer of a radiator with large surface is greater than for one with a small surface, e. g. the Outside is a spacious temperature exchanger. The heat transfer of a cooler or heater that is turned of is assumed to be 0. 19

The Room Temp Function Assume that the start up temp of the rooms is

The Room Temp Function Assume that the start up temp of the rooms is 293. 15 K (20°C); temp at time t is then given by: We do not know this function precisely so we have to come up with some approximation! 20

The Temp Transfer Rate (Assumptions/Simplifications) • We measure the temp change rate of an

The Temp Transfer Rate (Assumptions/Simplifications) • We measure the temp change rate of an active temp exchanger in temp difference over time. A negative value indicates cooling and a positive value indicates heating. • Example: – only one temp exchanger is active and current temp is ti-1 – temp change rate for exchanger is 10 K/t when temp is ti-1 – Δt time has gone since last measurement Then we have: ti = ti-1 + 10 * Δt • A high temp change rate indicates an effective temp exchanger. • What is the difference between a heater and a cooler? 21

Calculating New Room Temp (Assumptions/Simplifications) We use the following formula to calculate new room

Calculating New Room Temp (Assumptions/Simplifications) We use the following formula to calculate new room temp: Δt is time since last measurement (the smaller the better). room. temp. Change. Rate() is given by: • update. Temp() correspond to first formula. • temp is last temp calculated and is set by update. Temp(). • temp. Change. Rate() correspond to second formula. 22

Temp. Exchanger (Assumptions/Simplifications) We use the following formula to calculate the temp change rate

Temp. Exchanger (Assumptions/Simplifications) We use the following formula to calculate the temp change rate of a temp exchanger: temp. Exchanger. temp. Change. Rate(last. Room. Temp) = (temp. Exchanger. temp – last. Room. Temp) * temp. Exchanger. transfer. Factor 23

Regulating Temperature Exchangers • Some temp exchangers can not be turned off, e. g.

Regulating Temperature Exchangers • Some temp exchangers can not be turned off, e. g. outside. We introduce Outside as a subclass of Temp. Exchanger. • Some temperature exchangers can operate in steps, e. g. a heater might have the following states: off, half-power, full-power. Such temp exchangers are model with a subclass of Temp. Exchanger called Step. Temp. Exchanger. • A Cooler is a special type of Temp. Exchanger – one which is meant for cooling. The same logic goes for heaters. 24

Temp Exchanger Classes Arrays temp. Steps and transfer. Factor. Steps are parallel arrays. Method

Temp Exchanger Classes Arrays temp. Steps and transfer. Factor. Steps are parallel arrays. Method set. Current. Step(current. Step) is setting temp to temp. Steps[current. Step] and transfer. Factor to transfer. Factor. Steps[current. Step]. A Step. Temp. Exchanger is turned of when transfer. Factor is zero. turn. Off() is equivalent to a set. Current. Step(0)-call, consequently transfer. Factor. Steps[0] must always be 0. turn. On() is equivalent to a set. Current. Step(1)-call. 25

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 26

First Design Model (PIM/PSM) 27

First Design Model (PIM/PSM) 27

Structure Modelling Object Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour

Structure Modelling Object Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram Use Case Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 28

UML Object Diagram From Wikipedia In the Unified Modelling Language (UML), an object diagram

UML Object Diagram From Wikipedia In the Unified Modelling Language (UML), an object diagram is a diagram that shows a complete or partial view of the structure of a modelled system at a specific time. This snapshot focuses on some particular set of object instances and attributes, and the links between the instances. . . Object diagrams are more concrete than class diagrams, and are often used to provide examples, or act as test cases for the class diagrams. 29

Room Outside Cooler Temperature Sensor Heater Temperature Setpoint Thermostat UML Object Diagram 30

Room Outside Cooler Temperature Sensor Heater Temperature Setpoint Thermostat UML Object Diagram 30

A Class Diagram Typically Defines an Infinite Set Of Possible Object Diagrams Object Diagram

A Class Diagram Typically Defines an Infinite Set Of Possible Object Diagrams Object Diagram Class Diagram Object Diagram 31

In Our Single-Threaded Solution We Use an Object that Drives the Simulation 32

In Our Single-Threaded Solution We Use an Object that Drives the Simulation 32

Behaviour Modelling Communication Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour

Behaviour Modelling Communication Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram Use Case Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 33

Communication Diagram From Wikipedia A Communication diagram models the interactions between objects or parts

Communication Diagram From Wikipedia A Communication diagram models the interactions between objects or parts in terms of sequenced messages… …, communication diagrams use the free-form arrangement of objects and links as used in Object diagrams. In order to maintain the ordering of messages in such a free-form diagram, messages are labelled with a chronological number and placed near the link the message is sent over. Reading a communication diagram involves starting at message 1. 0, and following the messages from object to object. 34

A Communication Diagram Without Messages Correspond To An Object diagram 35

A Communication Diagram Without Messages Correspond To An Object diagram 35

A Part of the Set Up Communication Diagram 36

A Part of the Set Up Communication Diagram 36

Behaviour Modelling Sequence Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour

Behaviour Modelling Sequence Diagram Structure Diagram Class Diagram Component Diagram Composite Structure Diagram Behaviour Diagram Object Diagram Deployment Diagram Activity Diagram Package Diagram Use Case Diagram State Machine Diagram Interaction Diagram Sequence Diagram Interaction Overview Diagram Communication Diagram Timing Diagram 37

Sequence Diagram From Wikipedia The well-known Message Sequence Chart technique has been incorporated into

Sequence Diagram From Wikipedia The well-known Message Sequence Chart technique has been incorporated into the Unified Modelling Language (UML) diagram under the name of Sequence Diagram. A sequence diagram shows, as parallel vertical lines, different processes or objects that live simultaneously, and, as horizontal arrows, the messages exchanged between them, in the order in which they occur. This allows the specification of simple runtime scenarios in a graphical manner. 38

Sequence Diagram with Error Outside do not have this operation [ref. slide 32] 39

Sequence Diagram with Error Outside do not have this operation [ref. slide 32] 39

Sequence Diagram (Simulation. Monitor. run. Simulation()) : Simulation. Monitor : Room : Outside :

Sequence Diagram (Simulation. Monitor. run. Simulation()) : Simulation. Monitor : Room : Outside : Heater : Cooler : Thermostat set. Temp(273. 15) set. Temp(253. 15) update. Temp(delta. Time) temp. Change. Rate(temp) temp. Change. Rate(last. Room. Temp) regulate() get. Temp() update. Temp(delta. Time) 40

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 41

Code Generation Using IBM Rational Software Architecture Create A Transformation First create a Java

Code Generation Using IBM Rational Software Architecture Create A Transformation First create a Java project where the produced Java files will be stored. Choose: Modeling/Transform/New Configuration in menu. 42

Source And Target For Transformation 43

Source And Target For Transformation 43

44

44

Then you run the transformation in a separate step: Select configuration and select Transform/UML

Then you run the transformation in a separate step: Select configuration and select Transform/UML to Java 5. 0 45

Some Generated Code package airtemp; public class Room { private double temp; private Temp.

Some Generated Code package airtemp; public class Room { private double temp; private Temp. Exchanger[] temp. Exchanger = null; public void get. Temp() { } public void set. Temp(double temp) { } protected double temp. Change. Rate() { return 0; } public void update. Temp(double delta. Time) { } } public class Simulation. Monitor { private Temp. Exchanger[] tempexchanger = null; private Room room; private Thermostat thermostat; public void set. Up() { } public void run. Simulation() { } } 46

More Generated Code package airtemp; public abstract class Step. Temp. Exchanger extends Temp. Exchanger

More Generated Code package airtemp; public abstract class Step. Temp. Exchanger extends Temp. Exchanger { private double[] temp. Steps = null; private Integer current. Step; private Integer number. Of. Steps; private double[] transfer. Factor. Steps = null; public void set. Temp. Steps(double[] temp. Steps) { } public void turn. On() { } public void turn. Off() { } public Integer get. Current. Step() { return null; } public void set. Current. Step(Integer current. Step) { } public void set. Transfer. Factor. Steps(double[] transfer. Factor. Steps) { } public void set. Number. Of. Steps(Integer number. Of. Steps) { } public Integer get. Number. Of. Steps() { return null; } } 47

Dependency in UML From Wikipedia • • • A dependency exists between two defined

Dependency in UML From Wikipedia • • • A dependency exists between two defined elements if a change to the definition of one would result in a change to the other. This is indicated by a dashed arrow pointing from the dependent to the independent element. A dependency can be between instances, classes, or both. Dependency is a model-level relationship and not a run-time relationship - describing the need to investigate the model definition of the client element for possible changes if the model definition of the supplier element is changed. UML-defined dependencies include: – «use» UML 2: The client element somehow "uses" the supplier «use» – «call» UML 2: The client (an operation) may call the supplier (an operation) – «create» UML 2: The client (a classifier) may create instances of the supplier (a classifier) – «derive» UML 2: The client (e. g. , attribute value, link) may be computed from the supplier(s) – «instantiate» UML 2: Operations of the client (a classifier) may create instances of the supplier (a classifier) – «refine» UML 2: The client element(s) are at a "later" semantic level than the supplier(s) – «trace» UML 2: The client elements "trace" to the suppliers. Typically used for levels of requirements 48

The IBM Tool Got A Special Type of Class Diagram Tailored for Java (Java

The IBM Tool Got A Special Type of Class Diagram Tailored for Java (Java Profile) • If we “drag” the classes generated onto such a diagram they appear stereotyped as Java Class. • Associations with multiplicity many appear as dependencies and they also appear as attributes inside the class. • “Of some reason” associations with multiplicity zero or one appear only as attributes. Another version of the Architect (older version) did not even show the dependencies! transform from transform to 49

All Generated Classes 50

All Generated Classes 50

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 51

Design Patterns From Wikipedia • In software engineering, a design pattern is a general

Design Patterns From Wikipedia • In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. • A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. • Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems. • Not all software patterns are design patterns. Design patterns deal specifically with problems at the level of software design. Other kinds of patterns, such as architectural patterns, describe problems and solutions that have alternative scopes. 52

Abstract Factory [1] Will Be Used In The Design Provides an interface for creating

Abstract Factory [1] Will Be Used In The Design Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Example: An application supporting multiple look-and-feel standards like Windows and Motif. Most of the code should be without knowledge of which look-and-feel that is used. 53

The Abstract Factory Pattern Will Be Used From Wikipedia • In software development, a

The Abstract Factory Pattern Will Be Used From Wikipedia • In software development, a Factory is the location in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage. • Use of this pattern makes it possible to interchange concrete classes without changing the code that uses them. . . 54

Abstract Factory: Example [1] Client Widget. Factory create. Scroll. Bar() create. Window() Widget. Factory

Abstract Factory: Example [1] Client Widget. Factory create. Scroll. Bar() create. Window() Widget. Factory is abstract, so either Motif. Factory or Windows. Factory is instantiated Scroll. Bar «instantiate» Windows. Scroll. Bar Motif. Factory create. Scroll. Bar() create. Window() Windows. Factory Motif. Scroll. Bar Window create. Scroll. Bar() create. Window() «instantiate» Windows. Window Motif. Window 55

Abstract Factory: Object Diagram … Widget. Factory factory = new Motif. Factory(); . .

Abstract Factory: Object Diagram … Widget. Factory factory = new Motif. Factory(); . . . Scroll. Bar sb = factory. create. Scroll. Bar(); … Window win = factory. create. Window() … Motif look-and-feel is selected; Create. Scroll. Bar and Create. Window are called. factory: Motif. Factory : Client sb: Motif. Scroll. Bar win: Motif. Window 56

Abstract Factory: a General Class Diagram Abstract. Factory Client Abstract. Product. A create. Product.

Abstract Factory: a General Class Diagram Abstract. Factory Client Abstract. Product. A create. Product. A() create. Product. B() «instantiate» Concrete. Factory 1 create. Product. A() create. Product. B() Concrete. Factory 2 Product. A 1 Abstract. Product. B create. Product. A() create. Product. B() «instantiate» Product. B 2 Product. B 1 «instantiate» 57

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 58

We Will Use Interfaces Instead Of Inheritance To Implement The Pattern package airtemp; public

We Will Use Interfaces Instead Of Inheritance To Implement The Pattern package airtemp; public class Room { private double temp; private Temp. Exchanger[] temp. Exchanger = null; public void get. Temp() { } public void set. Temp(double temp) { } protected double temp. Change. Rate() { return 0; } public void update. Temp(double delta. Time) { } } package airtemp. impl; import airtemp. Room; import airtemp. Temp. Exchanger; public class Room. Impl implements Room { private double temp; … 59

Package Structure 60

Package Structure 60

The Factory package airtemp. impl; import airtemp. Air. Temp. Factory; import airtemp. Cooler; …

The Factory package airtemp. impl; import airtemp. Air. Temp. Factory; import airtemp. Cooler; … public class Air. Temp. Factory. Impl implements Air. Temp. Factory{ public Cooler create. Cooler(){ return new Cooler. Impl(); } … } The potential benefit of using a factory in this example is not so big since the creation of objects is done in one place and not spread to many different parts of the code! 61

Overview #1 After Some “Massage” package airtemp; public class Simulation. Monitor. App extends Simulation.

Overview #1 After Some “Massage” package airtemp; public class Simulation. Monitor. App extends Simulation. Monitor { public static void main(String[] args) { Simulation. Monitor simulation. Monitor = new Simulation. Monitor(); simulation. Monitor. set. Up(); simulation. Monitor. run. Simulation(); } } 62

… public class Simulation. Monitor { private Air. Temp. Factory. Impl air. Temp. Factory.

… public class Simulation. Monitor { private Air. Temp. Factory. Impl air. Temp. Factory. Impl = new Air. Temp. Factory. Impl(); private Room room = air. Temp. Factory. Impl. create. Room(); … public void set. Up() { System. out. println("- Assembling the objects -"); temp. Exchanger = new Temp. Exchanger[3]; temp. Exchanger[0] = outside; temp. Exchanger[1] = heater; temp. Exchanger[2] = cooler; room. set. Temp. Exchanger(temp. Exchanger); … get. User. Input(); } public void get. User. Input() { System. out. println("Get parameters governing the simulation. "); Scanner scanner = new Scanner(System. in); System. out. print("Give desired temp. (C): "); // Read and convert to Kelvin double desired. Temp = scanner. next. Double() + 273. 15; thermostat. set. Desired. Temp(desired. Temp); … } public void run. Simulation() {… 63

… public class Simulation. Monitor { public void run. Simulation() System. out. println("n--- Simulation

… public class Simulation. Monitor { public void run. Simulation() System. out. println("n--- Simulation starts ---n"); room. set. Temp(273. 15); for (int outside. Temp. Centigrade = -20; outside. Temp. Centigrade<=40; outside. Temp. Centigrade++){ double outside. Temp. Kelvin = outside. Temp. Centigrade + 273. 15; outside. set. Temp(outside. Temp. Kelvin); double delta. Time = 0. 001; for (int j = 1; j< 20; j++){ room. update. Temp(delta. Time); thermostat. regulate(); } System. out. printf("n. Heater on: %b Cooler on. : %b n", (heater. get. Transfer. Factor()!=0), (cooler. get. Transfer. Factor()!=0)); double inside. Temp. Centigrade = room. get. Temp() - 273. 15; System. out. printf("Outside temp. : %d Inside temp. : %. 2 f n", outside. Temp. Centigrade, inside. Temp. Centigrade); } } } 64

Overview #2 The Temp. Exchanger hierarchy 65

Overview #2 The Temp. Exchanger hierarchy 65

Overview #3 66

Overview #3 66

Overview #4 (A Bit Chaotic) 67

Overview #4 (A Bit Chaotic) 67

public class Room. Impl implements Room { private double temp; private Temp. Exchanger[] temp.

public class Room. Impl implements Room { private double temp; private Temp. Exchanger[] temp. Exchanger = null; public void set. Temp. Exchanger(Temp. Exchanger[] temp. Exchanger){ this. temp. Exchanger = temp. Exchanger; } public double get. Temp() { return temp; } public void set. Temp(double temp) { this. temp = temp; } protected double temp. Change. Rate(double last. Room. Temp) { double rate = 0; for (int i=0; i<temp. Exchanger. length; i++){ rate = rate + temp. Exchanger[i]. temp. Change. Rate(last. Room. Temp); } return rate; } public void update. Temp(double delta. Time) { temp = temp + this. temp. Change. Rate(temp)*delta. Time; } } 68

public abstract class Temp. Exchanger. Impl implements Temp. Exchanger { private double temp; private

public abstract class Temp. Exchanger. Impl implements Temp. Exchanger { private double temp; private double transfer. Factor; public double get. Transfer. Factor() { return transfer. Factor; } public void set. Temp(double temp) { this. temp = temp; } public double get. Temp() { return temp; } public double temp. Change. Rate(double last. Room. Temp) { return (temp-last. Room. Temp)*transfer. Factor; } public void set. Transfer. Factor(double transfer. Factor) { this. transfer. Factor = transfer. Factor; } } 69

… public class Thermostat. Impl implements Thermostat { private double desired. Temp; private Temp.

… public class Thermostat. Impl implements Thermostat { private double desired. Temp; private Temp. Sensor temp. Sensor; private Heater[] heater = null; private Cooler[] cooler = null; … public void regulate() { final double temp. Delta = 0. 5; double current. Temp = temp. Sensor. get. Temp(); if (current. Temp <= (desired. Temp + temp. Delta) && current. Temp >= (desired. Temp - temp. Delta)) return; if (current. Temp < desired. Temp){ for(Cooler cool : cooler){ cool. turn. Off(); for(Heater heat : heater){ heat. turn. On(); } } else { for(Cooler cool : cooler){ cool. turn. On(); } for(Heater heat : heater){ heat. turn. Off(); } } } 70

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • •

Outline • UML Diagrams – Class, Use Case, Activity, Object, Communication, Sequence • • • Class diagram – Analysis Some Math Introducing behaviour Class diagram - Design Transform to Java Design Patterns – Abstract factory – “Abstract” factory with use of interfaces • The new code • Running the simulation 71

- Assembling the objects Get parameters governing the simulation. Give desired temp. (C): 20

- Assembling the objects Get parameters governing the simulation. Give desired temp. (C): 20 Give heat transfer factor for outside: 6 Give max transfer factor for heater: 6 Give heater max. temp (C): 200 Give max transfer factor for cooler: 6 Give cooler min. temp (C): -40 Heater on: true Cooler on. : false Outside temp. : -12 Inside temp. : 20. 39 --- Simulation starts --- Heater on: false Cooler on. : true Outside temp. : -9 Inside temp. : 19. 75 Heater on: false Cooler on. : true Outside temp. : -11 Inside temp. : 20. 05 Heater on: false Cooler on. : true Outside temp. : -10 Inside temp. : 21. 30 Heater on: true Cooler on. : false Outside temp. : -20 Inside temp. : 18. 25 Heater on: false Cooler on. : true Outside temp. : -8 Inside temp. : 21. 21 Heater on: false Cooler on. : true Outside temp. : -19 Inside temp. : 19. 95 Heater on: false Cooler on. : true Outside temp. : -7 Inside temp. : 19. 88 Heater on: false Cooler on. : true Outside temp. : -18 Inside temp. : 20. 27 Heater on: true Cooler on. : false Outside temp. : -6 Inside temp. : 20. 13 Heater on: false Cooler on. : true Outside temp. : -17 Inside temp. : 20. 76 Heater on: false Cooler on. : true Outside temp. : -5 Inside temp. : 20. 48 Heater on: true Cooler on. : false Outside temp. : -16 Inside temp. : 19. 92 Heater on: true Cooler on. : false Outside temp. : -4 Inside temp. : 19. 49 Heater on: true Cooler on. : false Outside temp. : -15 Inside temp. : 19. 16 Heater on: true Cooler on. : false Outside temp. : -3 Inside temp. : 20. 06 Heater on: false Cooler on. : true Outside temp. : -14 Inside temp. : 19. 94 Heater on: false Cooler on. : true Outside temp. : -2 Inside temp. : 20. 74 Heater on: false Cooler on. : true Outside temp. : -13 Inside temp. : 20. 84 Heater on: false Cooler on. : true Outside temp. : -1 Inside temp. : 20. 10 … … Heater on: false Cooler on. : true Outside temp. : 33 Inside temp. : 21. 25 Heater on: false Cooler on. : true Outside temp. : 34 Inside temp. : 20. 24 Heater on: true Cooler on. : false Outside temp. : 35 Inside temp. : 19. 38 Heater on: false Cooler on. : true Outside temp. : 36 Inside temp. : 20. 11 Heater on: false Cooler on. : true Outside temp. : 37 Inside temp. : 19. 53 Heater on: true Cooler on. : false Outside temp. : 38 Inside temp. : 20. 46 Heater on: false Cooler on. : true Outside temp. : 39 Inside temp. : 20. 04 Heater on: false Cooler on. : true Outside temp. : 40 Inside temp. : 19. 81 Running Simulation 72

- Assembling the objects Get parameters governing the simulation. Give desired temp. (C): 20

- Assembling the objects Get parameters governing the simulation. Give desired temp. (C): 20 Give heat transfer factor for outside: 100 Give max transfer factor for heater: 6 Give heater max. temp (C): 200 Give max transfer factor for cooler: 6 Give cooler min. temp (C): -40 Running Simulation … Heater on: true Cooler on. : false Outside temp. : 9 Inside temp. : 19. 68 Heater on: true Cooler on. : false Outside temp. : 10 Inside temp. : 19. 80 Heater on: true Cooler on. : false Outside temp. : 11 Inside temp. : 20. 32 --- Simulation starts --- … Heater on: true Cooler on. : false Outside temp. : -20 Inside temp. : -6. 68 Heater on: false Cooler on. : true Outside temp. : 24 Inside temp. : 20. 29 Heater on: true Cooler on. : false Outside temp. : -19 Inside temp. : -6. 61 Heater on: true Cooler on. : false Outside temp. : -18 Inside temp. : -5. 77 Heater on: true Cooler on. : false Outside temp. : -17 Inside temp. : -4. 84 Heater on: true Cooler on. : false Outside temp. : -16 Inside temp. : -3. 90 Heater on: true Cooler on. : false Outside temp. : -15 Inside temp. : -2. 96 … Heater on: false Cooler on. : true Outside temp. : 25 Inside temp. : 21. 20 heater to weak cooler to weak Heater on: false Cooler on. : true Outside temp. : 26 Inside temp. : 22. 14 Heater on: false Cooler on. : true Outside temp. : 27 Inside temp. : 23. 08 Heater on: false Cooler on. : true Outside temp. : 28 Inside temp. : 24. 02 Heater on: false Cooler on. : true Outside temp. : 29 Inside temp. : 24. 97 … 73

Some Comments • It seems that the simulation can be used to detect if

Some Comments • It seems that the simulation can be used to detect if dimensions of heaters and coolers are to low! • The approach of the application is: Set parameters and then calculate room temp. • The approach is a very “static” and quite different from real life, e. g. a real thermostat might sample the temp. sensor regularly and the room would dynamically change temp. We can achieve a more dynamic system by using program threads! 74

References [1] Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns Elements of

References [1] Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns Elements of Reusable Object-oriented Software Addison-Wesley, 1995 [2] Christopher Alexander et al. : A Pattern Language Oxford University Press, New York, 1977 75