Run a Decision Rule with JSON Input

Save the heading rule and a JSON input as files, run them with lispex rule run, and check the saved decision.

First Decision Rule ends with a rule that reads input. Here you save that rule and its input as files and run them with Native lispex rule run. The command writes the decision into a directory that you can inspect and replay later.

Install Native Lispex. The commands use Bash, including Git Bash on Windows. Start in a new directory.

Save the rule and its input

Save the rule from First Decision Rule as house-style.lspx.

LISPEX
(let ((lines-left (car input))
      (chapter-heading? (car (cdr input))))
  (if (and (>= lines-left 2) (not chapter-heading?))
      "allow"
      "deny"))

Save the input as heading.json.

JSON
[2, false]

The JSON array arrives as the list (2 #f). (car input) reads 2, the number of lines left below a section heading. (car (cdr input)) reads #f, so this is not a chapter heading.

Set the two limit files

lispex rule run reads two limit files. The preparation limits apply while Native reads the rule and checks that it fits the decision evaluator. The evaluation limits apply while the rule runs on one input and the result is recorded. Each number is a maximum. For example, raw_source_bytes allows a rule of up to 4,096 bytes, and eval_work allows up to 1,000,000 units of evaluation work. Repeatable Results and Execution Limits shows what a run reports when it reaches a limit.

Save the preparation limits as prepare-limits.json.

JSON
{
  "raw_source_bytes": 4096,
  "prepare_work": 1000000,
  "logical_allocation": 1000000,
  "syntax_depth": 64
}

Save the evaluation limits as evaluation-limits.json.

JSON
{
  "canonical_input_bytes": 4096,
  "eval_work": 1000000,
  "logical_allocation": 1000000,
  "semantic_frames": 1000,
  "traversal_depth": 256,
  "output_bytes": 1000000,
  "diagnostic_bytes": 1000000,
  "transcript_bytes": 1000000,
  "transcript_events": 100,
  "result_bytes": 1000000
}

Run the rule

SH
lispex rule run --source house-style.lspx --input heading.json \
  --prepare-limits prepare-limits.json \
  --eval-limits evaluation-limits.json --out decision

The command creates the decision directory, prints a JSON summary, and exits with code 0. These fields come from the summary.

JSON
{
  "category": "semantic-complete",
  "code": "complete",
  "decision": "allow"
}

semantic-complete means the rule completed within the limits. decision holds the string the rule returned.

The application supplies the line count and whether this is a chapter heading. allow means the heading may stay. deny means move it to the next page. Save each input in its own file and run it with a new output directory.

InputDecisionReason
[2, false]allowTwo lines fit below the section heading
[1, false]denyToo little room below the heading
[5, true]denyA chapter heading starts on a new page

If the output directory already exists, the command prints lispex rule run: output already exists to standard error and exits with code 3.

Return the decision as a string

lispex rule run uses the decision evaluator. It reports a string result in decision. A rule that returns a list, a number, or a boolean still completes, and its decision is null.

The decision evaluator refuses a rule that uses define or quoted data before running it. The command then prints a report with request-refusal and draft-profile to standard error, creates no directory, and exits with code 3. That is why this rule names its inputs with let and returns "allow" or "deny".

Find the five saved files

rule run writes the complete decision directory in one step. Keep the original source alongside your input files.

FileContents
prepared.lpxembedPrepared rule in the prescribed executable format
canonical-input.lpxvalueExact request value in its prescribed byte representation
result.lpxembedResult and its link to the request
receipt-core.lpxreceiptPortable core linking the rule, input, limits, evaluator, execution transcript, and result
summary.jsonThe same JSON summary that rule run printed

Check the saved decision

SH
lispex rule inspect --dir decision
lispex rule verify --dir decision
lispex rule replay --dir decision

All three commands exit with code 0, print JSON, and create no files. These are selected fields from their outputs.

CommandOutput fields
rule inspect"decision": "allow", "executed": false
rule verify"status": "verified", "executed": false
rule replay"status": "agreed", "fresh_instance": true

rule inspect reads the saved data. rule verify checks the exact file set, identifiers, hashes, and links between records. rule replay runs the request in a new evaluator instance and compares both the result and portable core. Malformed or altered records cause a refusal.

Sign the decision for a recipient

To sign this decision and let a printer check it, follow Sign and authenticate the decision in Typesetting Rule Workspace. It starts from the same four files.

Repeatable Results and Execution Limits · Run a Rule Within Set Limits