Mais conteúdo relacionado
Semelhante a Ada 95 - Structured programming (9)
Mais de Gneuromante canalada.org (9)
Ada 95 - Structured programming
- 1. Franco Gasperoni
gasperon@act-europe.fr
http://libre.act-europe.fr
1
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 2. Copyright 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
2
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 4. When creating a new system
you must identify its ...
• Data types
(what kind of data will be manipulated)
• Functionalities
(what kind of manipulations are allowed)
4
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 5. Software System Organization
• Around its functionalities
(structured programming)
• around its data types
(object-oriented programming)
5
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 6. • Functionality-Oriented Org.
– variant programming
– modifying functionality-oriented sys.
– when to use functionality-oriented
6
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 7. Design an alert system
for an industrial plant
• log all the incoming alerts
• handle an alert (inform right people, etc.)
7
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 8. alerts.ads
alerts.ads
with Calendar;
with Calendar;
package Alerts is
package Alerts is
type Alert is private;
type Alert is private;
procedure Handle (A ::in out Alert);
procedure Handle (A in out Alert);
procedure Log (A :: Alert);
(A Alert);
procedure Log
private
private
type Alert is record
type Alert is record
Time_Of_Arrival ::Calendar.Time;
Time_Of_Arrival Calendar.Time;
Cause ::String (1 .. 200);
String (1 .. 200);
Cause
end record;
end record;
end Alerts;
end Alerts;
8
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 9. Having several kind of alerts
• Low
• Medium
– dispatch a technician
• High
– dispatch an engineer
– if problem not fixed within a delay ring an alarm
9
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 10. with Calendar; use Calendar;
with Persons; use Persons;
package Alerts is
type 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);
private
type Alert (P : Priority) is record ... end record;
end Alerts;
10
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 11. discriminant
private
type Alert (P : Priority) is record
Time_Of_Arrival : Time;
Cause : String (1 .. 100);
case P is
when Low =>
null;
variant
record when Medium =>
Technician : Person;
when High =>
Engineer : Person;
Ring_Alarm_At : Time;
end case;
end record; 11
end Alerts; © ACT Europe under the GNU Free Documentation License
http://libre.act-europe.fr
- 12. • Functionality-Oriented Org.
– variant programming
– modifying functionality-oriented sys.
– when to use functionality-oriented
12
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 13. Variant Programming
procedure Handle (A : in out Alert) is
begin
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;
13
end Handle;
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 14. Typical Routine for Alert Objects
(version 1)
procedure Some_Routine (A : in out Alert) is
begin
...
case A.P is
when Low =>
...
when Medium =>
...
when High =>
...
end case;
end Some_Routine;
14
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 15. Typical Routine for Alert Objects
(version 2)
procedure Some_Routine (A : in out Alert) is
begin
...
if A.P = Low then
...
elsif A.P = Medium then
...
elsif A.P = High then
...
else
raise Internal_Error; -- defensive programming
end if;
end Some_Routine; 15
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 16. Variant Programming is checked
procedure Set_Alarm (A : in out Alert; Wait : Duration) is
begin
A.Ring_Alarm_At := A.Time_Of_Arrival + Wait;
end Handle;
Constraint_Error
raised if
A.Priority /= High
16
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 17. Handling an Alert
• You have a Get_Alert routine
• Connected to the sensors in the factory
• Collects the alerts
with Alerts; use Alerts;
with Alerts; use Alerts;
function Get_Alert return Alert; Returns an
function Get_Alert return Alert;
unconstrained Alert
the discriminant value
is not known
at compile time
17
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 18. with Alerts; use Alerts;
with Alerts; use Alerts;
with Get_Alert;
with Get_Alert;
procedure Process_Alerts is
procedure Process_Alerts is
begin
begin
Probably a
loop -- infinite loop
loop -- infinite loop
blocking call
declare
declare
A ::Alert := Get_Alert;
A Alert := Get_Alert;
begin
begin
Handle (A); -- could have written Handle (Get_Alert);
Handle (A); -- could have written Handle (Get_Alert);
end;
end;
end loop;
end loop;
end Process_Alerts;
end Process_Alerts;
• The case inside Handle selects the right to
execute depending on the discriminant
• Handling code centralized in Handle 18
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 19. • Functionality-Oriented Org.
– variant programming
– modifying functionality-oriented sys.
– when to use functionality-oriented
19
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 20. 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)
20
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 21. Adding new functionality
It’s simple: Use child subprograms/packages
simple
procedure Alerts.New_Functionality (A : in out Alert) is
begin
...
case A.P is
when Low =>
...
when Medium =>
...
when High =>
...
end case;
end Alerts.New_Functionality; 21
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 22. 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
22
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 23. Adding a data type
Much more work: need to modify the spec...
work
package Alerts is
...
private
type Alert (P : Priority) is record
...
case P is
when Low => . . .
when Medium => . . .
when High => . . .
when Emergency => . . .
end case;
end record;
end Alerts; 23
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 24. … as well as ALL the routines using Alert
procedure Some_Routine (A : in out Alert) is
begin
...
case A.P is
when Low =>
...
when Medium =>
...
when High =>
...
when Emergency =>
...
end case;
end Some_Routine; 24
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 25. ... ALL the routines !
procedure Some_Routine (A : in out Alert) is
begin
...
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 programming
end if;
end Some_Routine;
25
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 26. Important Remark
• For either type of change client routines such as
Process_Alerts do not need modifications
with Alerts; use Alerts;
with Alerts; use Alerts;
with Get_Alert;
with Get_Alert;
procedure Process_Alerts is
procedure Process_Alerts is
begin
begin
loop -- infinite loop
loop -- infinite loop
Handle (Get_Alert);
Handle (Get_Alert);
end loop;
end loop;
end Process_Alerts;
end Process_Alerts; 26
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 27. • Functionality-Oriented Org.
– variant programming
– modifying functionality-oriented sys.
– when to use functionality-oriented
27
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 28. • 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
28
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License