Business rules

From Webhuis wiki
Revision as of 16:48, 25 March 2026 by Martin (talk | contribs) (Created page with "<span id="business-rules"></span> = Business Rules = '''Project:''' pgcobol — Prime Numbers Application<br /> '''Version:''' 1.0 (reverse-engineered)<br /> '''Date:''' 2026-03-17 ----- <span id="primality-algorithm"></span> == 1. Primality Algorithm == <span id="br-algo-01-input-domain"></span> === BR-ALGO-01 — Input domain === Only odd integers are tested for primality. The sieve begins at <code>test-number = 3</code> and advances in steps of 2 (<code>ADD 2 TO...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Business Rules[edit]

Project: pgcobol — Prime Numbers Application
Version: 1.0 (reverse-engineered)
Date: 2026-03-17



1. Primality Algorithm[edit]

BR-ALGO-01 — Input domain[edit]

Only odd integers are tested for primality. The sieve begins at test-number = 3 and advances in steps of 2 (ADD 2 TO test-number), implicitly excluding all even numbers. The even prime 2 is intended as a seed but is not currently inserted in the active code path.

BR-ALGO-02 — Upper bound[edit]

Generation terminates when test-number = 999,999,999. This value is the terminal condition of the PERFORM loop in primesgen.

PERFORM r80-test-number UNTIL test-number = 999999999

BR-ALGO-03 — Divisibility test[edit]

A candidate is composite if the remainder of dividing it by the current trial divisor is zero:

DIVIDE test-number BY test-divider
       GIVING test-quot REMAINDER test-rest

WHEN test-rest = 0  →  composite; skip to next candidate

BR-ALGO-04 — Square-root bound (trial-division cutoff)[edit]

If the current trial divisor exceeds the square root of the candidate, no factor can exist and the candidate is prime. The square root is computed using COBOL exponentiation:

COMPUTE test-number-sqr = test-number ** 0.5

WHEN test-divider > test-number-sqr  →  prime confirmed

test-number-sqr is re-computed every time test-number advances (in r82-next-test-number).

BR-ALGO-05 — Trial divisors sourced from the database[edit]

Trial divisors are not held in memory; they are fetched from the primes table by ident sequence:

SELECT prime FROM primes WHERE ident = :new-ident

The index old-ident is initialised to 1 for each new candidate and incremented by 1 on each next-divider call. This means the algorithm uses previously stored primes as divisors — a characteristic of a sieve-of-Eratosthenes variant rather than pure trial division.

BR-ALGO-06 — Candidate advance rule[edit]

After a composite or prime determination, the candidate advances to the next odd integer and the divisor index resets:

ADD 2 TO test-number
COMPUTE test-number-sqr = test-number ** 0.5
MOVE 1 TO old-ident
PERFORM r89-get-next-divider   ← loads test-divider = first prime (ident 1)

2. Data Validation Rules[edit]

BR-VAL-01 — Command-line argument[edit]

The application accepts exactly one of two valid argument values:

Value Effect
"report" Execute reporting path
"generate" Execute generation path
anything else Log error; STOP RUN immediately

Rule is implemented by 88-level conditions and an EVALUATE TRUE with WHEN OTHER:

88 report-primes   VALUE "report".
88 generate-primes VALUE "generate".
...
WHEN OTHER
  LOG "Bad parameter, program initialisation failed."
  STOP RUN

BR-VAL-02 — UI initialisation must succeed before processing[edit]

If primesui returns ui-method-result ≠ 0 on a "start" call, the program issues an emergency console message and halts unconditionally. No database activity is attempted.

IF ui-method-ok THEN ...
ELSE
  DISPLAY "Emergency console message program stops." UPON scherm
  STOP RUN

BR-VAL-03 — Database connection must succeed before processing[edit]

In both r90-start-primes-report and r91-start-primes-generation, a failed "connect" call sets session-result = 1 and processing does not continue. The condition is checked via:

IF dal-method-ok THEN ...
ELSE
  LOG "Database initialisation failed."
  session-result = 1
  (falls through to r99-close-primes)

BR-VAL-04 — Cursor must open successfully before fetching[edit]

In the report path, cursor failure sets session-result = 1 and no fetch loop is entered. The guard is:

IF dal-method-ok THEN
  PERFORM r86-report-primes UNTIL session-method-eof

BR-VAL-05 — SQLCODE = 0 is the sole SQL success criterion[edit]

All SQL error handling uses IF SQLCODE = 0 THEN ... ELSE .... No other status codes (e.g., +100 NOT FOUND) are explicitly handled with named conditions; they fall into the ELSE branch.



3. Calculations[edit]

BR-CALC-01 — Square root computation[edit]

COMPUTE test-number-sqr = test-number ** 0.5

test-number-sqr is declared as PIC 9(9)V9(9) — an 18-digit decimal with 9 integer and 9 fractional digits. The comparison test-divider > test-number-sqr relies on COBOL’s decimal comparison across the two fields.

BR-CALC-02 — Remainder computation[edit]

DIVIDE test-number BY test-divider
       GIVING test-quot REMAINDER test-rest

Both test-quot and test-rest are declared PIC 9(9)V9(9). The condition test-rest = 0 detects exact divisibility.



4. Output Formatting Rules[edit]

BR-FMT-01 — Six primes per print line[edit]

The primes-table working-storage in primesui holds 6 cells. A line is written to the print file only when all 6 are filled (primes-idx = 7 after increment):

IF primes-idx = 7 THEN
  WRITE print line
  SET primes-idx TO 1

BR-FMT-02 — Zero suppression[edit]

Sequence numbers and prime values are formatted with PIC Z(9) (leading-zero suppression) in the print cells t-ident and t-prime.

BR-FMT-03 — Page trigger at line 53[edit]

End-of-page processing fires when linage-counter = 53 (3 lines before the 56-line page boundary), leaving room for the footing:

IF linage-counter = 53 THEN PERFORM r94-eop

BR-FMT-04 — New-page flag controls heading output[edit]

print-new-page (88 condition new-page, VALUE 1) is the gate for writing the heading. It is set to 1 at initialisation and after each page footing; cleared to 0 after the heading is written:

IF new-page THEN PERFORM r93-new-page
...
r93-new-page:
  MOVE ZERO TO print-new-page

BR-FMT-05 — Partial line flush at stop[edit]

When primesui receives "stop", it pads the remaining cells with zeros and keeps calling r92-write-primesui until new-page triggers (i.e., until the partial line fills, writes, and a new page would be needed), ensuring the last data line is always written:

MOVE 0 TO u-sequence
MOVE 0 TO u-number
PERFORM r92-write-primesui UNTIL new-page

5. Session and Error-Handling Rules[edit]

BR-ERR-01 — Result-code convention[edit]

All three tier interfaces use the same pattern:

Code Meaning
0 Success
1 Failure / error
9 or 99 End-of-data / EOF

BR-ERR-02 — No retry on failure[edit]

Errors at any tier cause a log message and a result code of 1. The calling tier checks the result and either skips subsequent steps or falls through to cleanup. There is no retry logic.

BR-ERR-03 — Cleanup always attempted[edit]

r99-close-primes in primesgen and r99-stop-session in primesmain are always performed before EXIT PROGRAM or STOP RUN, regardless of whether prior steps succeeded. This ensures the database connection and print file are always closed.


Terug naar: Primes_programs_specifications | Cobol and PostgreSQL