15 Nov Structure clashes and program decomposition.
-
Upload
margaretmargaret-cox -
Category
Documents
-
view
224 -
download
4
Transcript of 15 Nov Structure clashes and program decomposition.
Structure clashes
The design method that has been presented in the preceding chapters works when there is a correspondence between input and output data structures.
What happens when such correspondences don't exist, as in the following example?
Structure clashes:Net Movement Report
A warehouse records a transaction for every item received into or issued out of the warehouse. At the end of the day, the transaction file is sorted by item number, and a "Daily Net Movement Summary" showing the net movement of each item into or out of the warehouse is produced. The format is shown below:
Daily Net Movement SummaryA12345 40A23456 -30..........Z13579 25End Summary
Warehouse Net Movementinput – output structure diagrams
Transaction file
Transaction group
Transaction*
Report
Title line
Report body
End line
Total line
*
*
Input file Report file
Warehouse Net MovementProgram Structure
* Consume records; accumulate net movement
Consume daily transaction file; Produce report
Consume file body; Produce report body
Close file; Display
ending line
Open file; Display report title
Consume group; Produce report line
*
Warehouse Net Movement:Elaborated program structure
Consume group; Accumulate netqty
9
* Consume record; accumulate net movement
Consume daily transaction file; Produce report
Consume file body; Produce report body
Close file; Display
ending line
Open file; Display report title
Consume group; Produce report line
*
Initialize group
5,6
7,8
1,2, 8, 10 3,4,
10
Modified Net Movement Problem
Report
Title line Report body End line
Report line*
Report file
Daily transaction
file
Block*
Record count Block body
*
Input file
Transaction record
Suppose the input file is blocked, with each block containing a record count followed by a number of records.
Modified Net Movement Problem:The essence of the difficulty
The program must have an operation that is executed once per block and an operation that is executed once per group
So there must be both a block component and a group component
But we cannot have a single program structure with process block and process group components.
We have a boundary clash--the boundaries of blocks are not synchronized with the boundaries of groups.
Solution to Boundary Clash:Decomposition
Daily Net Movement Report
Input file PA
Inter- mediate
filePB
Input, intermediate and report files, together with their correspondences
Daily transaction
file
Block *
Record count
Block body
*
Daily transaction
file
Transaction record
Transaction record
Input file Intermediate Intermediate Report file
*
Daily transaction
file
*
*Transaction
record
Part group
Daily net movement
report
Title lineReport body End line
Total line *
Program PA Program PB
file file
JSP Solution
PA seq reset(infile); xread(infile, block); rewrite(outfile); PA-BLOCK iter <while not eofbit> j := 1; PA-DEBLOCK iter <while not (j>reccnt)> outrec.itemno := inrec.i temno[j]; outrec.transcode := inrec.transcode[j]; outrec.qty := inrec.qty[ j]; write(outfile, outrec); j := j + 1; PA-DEBLOCK end xread(infile,block); PA-BLOCK end close(infile); close(outfile); PA end
PB seq reset(infile); xread(infile, rec); writeln(' Daily Net Movement Summary '); writeln; PB-REPORTBODY iter <while not eofbit> groupid := rec.itemno; netqty := 0; PB-GROUP iter <while not eofbit and (groupid = rec.itemno)> netqty := netqty + rec.qty; xread(infile, rec); PB-GROUP end writeln(' ', groupid, ' ', n etqty); PB-REPORTBODY end close(infile); PB end
Comments on JSP Solution
• The programs we obtain are distinct. – A serial file forms a boundary between any pair of programs.
We don't have to think "dynamically". • For example, we don't need to ask, "What if a group extends over
several blocks?" or • "What if a group has no data records?“
– We know our programs are correct, because we can think in terms of static data structures
• Inefficient– By introducing an intermediate file, we have roughly doubled the
execution time (in comparison with a program that produced a report without an intermediate file).
– We will learn a little later how to optimize our design by a simple program transformation, program inversion.
Interleaving clash
Let us suppose that our input file is incompletely sorted by part number. Total lines for each part group on the report may be in any order.
Daily transaction
file
*Transaction
Part group
Transaction*
Solution to Interleaving ClashSince the input file is not sorted completely by part number, we cannot
show the group structure and the input file structure on one diagram. • Our input file is an interleaving of part groups. • To resolve this "interleaving" clash, we split the input file into part groups
as shown below:
Psplit Input file
PG1
PGn
PG1
PGn
Output PreportDaily Net
Movement Report
JSP Solution
Input file
Transaction record
*
Intermediate file(s)
Part Group°
Transaction record
*
Net Movement Report
Report title
Report body
Part group total line
*
<PG1...PGn>
End line
To resolve this "interleaving" clash, we split the input file into part groups as shown below:
Structure ClashesThree types of structural clash:1. Boundary clash (physical blocks on input
don’t coincide with logical groups on output)
Boundary ClashSolution to Bounday Clash
2. Ordering clash – for example, we wish a report sorted by a person’s last name (surname), but input is unsorted)
3. Interleaving clash (wish to produce report of user job times, inputs are interleaved)
Solution to Structural Clashes
• General view of problemStructure diagram
• Solution: program decompositionProgram decomposition
There is no structure clash between A and I, so the structure of PA can be constructed without difficulty; the same is true of I and X and PB
Processing of general view:batch processing (пакетная обработка данных)
1. Batch processing
2. Parallel processing
3. Quasi-parallel processing (program inversion)
program inversion
Program Inversion инверсия программ
• We wish to design PA and PB independently of each other so that the structure of each is based on the data structure representation of its own problem environment.
• Program inversion is a purely mechanical transformation of the independent programs, PA and PB, into a main program and subroutine
• The subroutine has a single ENTRY point and stores its return address within its state vector so that it is resumable.
state vector (вектор состояния)
A program consists of:– program text
and– variables– text pointer to next instructionj to be executed
state vector