28
1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License Franco Gasperoni [email protected] http://libre.act-europe.fr

Ada 95 - Structured programming

Embed Size (px)

DESCRIPTION

Author: Franco Gasperoni. License: GFDL

Citation preview

Page 1: Ada 95 - Structured programming

1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Franco [email protected]://libre.act-europe.fr

Page 2: Ada 95 - Structured programming

2http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Copyright NoticeCopyright Notice

• © ACT Europe under the GNU Free Documentation License

• Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at:

• http://www.fsf.org/licenses/fdl.html

Page 3: Ada 95 - Structured programming

3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Page 4: Ada 95 - Structured programming

4http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

When creating a new systemWhen creating a new systemyou must identify its ...you must identify its ...

• Data types(what kind of data will be manipulated)

• Functionalities(what kind of manipulations are allowed)

Page 5: Ada 95 - Structured programming

5http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Software System OrganizationSoftware System Organization

• Around its functionalities(structured programming)

• around its data types(object-oriented programming)

Page 6: Ada 95 - Structured programming

6http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Functionality-Oriented Org.

– variant programming– modifying functionality-oriented sys.– when to use functionality-oriented

Page 7: Ada 95 - Structured programming

7http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Design an alert systemDesign an alert systemfor an industrial plantfor an industrial plant

• log all the incoming alerts

• handle an alert (inform right people, etc.)

Page 8: Ada 95 - Structured programming

8http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Calendar;

package Alerts istype Alert is private;

procedure Handle (A : in out Alert);procedure Log (A : Alert);

privatetype Alert is record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

with Calendar;

package Alerts istype Alert is private;

procedure Handle (A : in out Alert);procedure Log (A : Alert);

privatetype Alert is record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

alerts.adsalerts.ads

Page 9: Ada 95 - Structured programming

9http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Having several kind of alertsHaving several kind of alerts

• Low

• Medium– dispatch a technician

• High– dispatch an engineer– if problem not fixed within a delay ring an alarm

Page 10: Ada 95 - Structured programming

10http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Calendar; use Calendar;with Persons; use Persons;

package Alerts istype Priority is (Low, Medium, High);type Alert (P : Priority) is private;

procedure Handle (A : in out Alert);procedure Log (A : Alert);

procedure Set_Alarm (A : in out Alert; Wait : Duration);

privatetype Alert (P : Priority) is record ... end record;

end Alerts;

Page 11: Ada 95 - Structured programming

11http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

privatetype Alert (P : Priority) is record

Time_Of_Arrival : Time;Cause : String (1 .. 100);

case P iswhen Low =>

null;

when Medium =>Technician : Person;

when High =>Engineer : Person;Ring_Alarm_At : Time;

end case;end record;

end Alerts;

discriminant

variantrecord

Page 12: Ada 95 - Structured programming

12http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Functionality-Oriented Org.

– variant programming– modifying functionality-oriented sys.– when to use functionality-oriented

Page 13: Ada 95 - Structured programming

13http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Handle (A : in out Alert) isbegin

A.Time_Of_Arrival := Calendar.Clock;A.Cause := Get_Cause (A);Log (A);case A.P is

when Low => null;

when Medium =>A.Technician := Assign_Technician;

when High =>A.Engineer := Assign_Engineer;Set_Alarm (A, Wait => 1800);

end case;end Handle;

Variant ProgrammingVariant Programming

Page 14: Ada 95 - Structured programming

14http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Some_Routine (A : in out Alert) isbegin

. . .case A.P is

when Low => . . .

when Medium =>. . .

when High =>. . .

end case;end Some_Routine;

Typical Routine for Alert ObjectsTypical Routine for Alert Objects(version 1)(version 1)

Page 15: Ada 95 - Structured programming

15http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Some_Routine (A : in out Alert) isbegin

. . .if A.P = Low then

. . .elsif A.P = Medium then

. . .elsif A.P = High then

. . .else

raise Internal_Error; -- defensive programmingend if;

end Some_Routine;

Typical Routine for Alert ObjectsTypical Routine for Alert Objects(version 2)(version 2)

Page 16: Ada 95 - Structured programming

16http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Set_Alarm (A : in out Alert; Wait : Duration) isbegin

A.Ring_Alarm_At := A.Time_Of_Arrival + Wait;end Handle;

Variant Programming is checkedVariant Programming is checked

Constraint_Error

raised ifA.Priority /= High

Page 17: Ada 95 - Structured programming

17http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Handling an AlertHandling an Alert

• You have a Get_Alert routine• Connected to the sensors in the factory• Collects the alerts

with Alerts; use Alerts;function Get_Alert return Alert;with Alerts; use Alerts;function Get_Alert return Alert; Returns an

unconstrained Alertthe discriminant value

is not knownat compile time

Page 18: Ada 95 - Structured programming

18http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• The case inside Handle selects the right to execute depending on the discriminant

• Handling code centralized in Handle

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopdeclare

A : Alert := Get_Alert;begin

Handle (A); -- could have written Handle (Get_Alert);end;

end loop;end Process_Alerts;

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopdeclare

A : Alert := Get_Alert;begin

Handle (A); -- could have written Handle (Get_Alert);end;

end loop;end Process_Alerts;

Probably a blocking call

Page 19: Ada 95 - Structured programming

19http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Functionality-Oriented Org.

– variant programming– modifying functionality-oriented sys.– when to use functionality-oriented

Page 20: Ada 95 - Structured programming

20http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Adding NEW Functionality...Adding NEW Functionality...

• Do not modify what is working already– No need to retest what you already did

since you do not need to touch it

• Just add the functionality in a separate child unit (subprogram or package)

Page 21: Ada 95 - Structured programming

21http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Adding new functionality Adding new functionality

procedure Alerts.New_Functionality (A : in out Alert) isbegin

. . .case A.P is

when Low => . . .

when Medium =>. . .

when High =>. . .

end case;end Alerts.New_Functionality;

It’s simpleIt’s simple: Use child subprograms/packages

Page 22: Ada 95 - Structured programming

22http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Adding a NEW Data Variant...Adding a NEW Data Variant...

• You have to modify the spec containing your data type

• have to modify all the routines that manipulate the data type to process new variant– Error Prone & labor intensive

– need to retest everything for regressions

Page 23: Ada 95 - Structured programming

23http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Adding a data typeAdding a data type

package Alerts is. . .

privatetype Alert (P : Priority) is record

. . . case P is

when Low => . . . when Medium => . . . when High => . . . when Emergency => . . .

end case;end record;

end Alerts;

Much more workMuch more work: need to modify the spec...

Page 24: Ada 95 - Structured programming

24http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… as well as ALL the routines using Alert

procedure Some_Routine (A : in out Alert) isbegin

. . .case A.P is

when Low => . . .

when Medium =>. . .

when High =>. . .

when Emergency =>. . .

end case;end Some_Routine;

Page 25: Ada 95 - Structured programming

25http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Some_Routine (A : in out Alert) isbegin

. . .if A.P = Low then

. . .elsif A.P = Medium then

. . .elsif A.P = High then

. . .elsif A.P = Emergency then

. . .else

raise Internal_Error; -- defensive programmingend if;

end Some_Routine;

... ALL the routines !

Page 26: Ada 95 - Structured programming

26http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Important RemarkImportant Remark

• For either type of change client routines such as Process_Alerts do not need modifications

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopHandle (Get_Alert);

end loop;end Process_Alerts;

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopHandle (Get_Alert);

end loop;end Process_Alerts;

Page 27: Ada 95 - Structured programming

27http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Functionality-Oriented Org.

– variant programming– modifying functionality-oriented sys.– when to use functionality-oriented

Page 28: Ada 95 - Structured programming

28http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Data types are well known before starting the design

• Adding new data variants will happen infrequently

• Will add lots of new functionalities on existing data types over the life time of the system