How yaomancy/liuyao-engine works
Purpose and Public Entry Points
liuyao-engine is a Python 3.11+ deterministic Liù Yáo chart-construction engine. It converts a coin cast and timestamp into structured divination facts, not interpretive or AI-generated advice. The package public surface is rooted in liuyao/__init__.py: toss, compute_four_pillars, cast_chart, and build_reading.
1. Cast Acquisition
liuyao/cast.py represents six-line casts and generates coin tosses using CSPRNG-derived outcomes. Its result preserves primary-hexagram bits and moving-line state, which feed both primary and changed charts. The engine can also accept caller-provided bits/moving lines, keeping casting separate from chart assembly.
2. Calendar and Four-Pillars Boundary
liuyao/calendar.py converts a local timestamp into the gan-zhi year, month, day, and hour pillars, including xúnkōng. This is the principal external-runtime boundary: it depends on sxtwl for Chinese calendrical calculations; tests validate timezone and day-boundary behavior. The resulting FourPillars data is passed into chart construction rather than having downstream modules query time.
3. Hexagram Identity, Transformations, and Metadata
liuyao/hexagram.py maps line bits to one of 64 hexagrams and derives changed hexagrams from moving lines. liuyao/guaxiang.py supplies hexagram/palace-oriented facts, while liuyao/gua_slug.py and liuyao/gua_page.py support stable naming/page-oriented representations. These modules establish the canonical identity boundary before rule attribution.
4. Najia Chart Assembly
liuyao/analysis.py and liuyao/relations.py assemble the rule-derived chart: nàjiǎ stem/branch attribution, five-element relationships, six relations, six spirits, shì/yìng positions, and fúshén. liuyao/chizhi.py provides Heavenly Stem/Earthly Branch and five-element domain primitives. cast_chart produces a structured HexagramChart from primary bits plus the day stem, making this stage deterministic and independent of question text.
5. Strength and Use-God Analysis
liuyao/analysis.py derives the wàng-shuāi skeleton from chart facts and time context, then identifies yòngshén according to question category. This is a deliberate scope boundary: it encodes hard, inspectable rule outputs but excludes free-form divinatory interpretation and shénshà.
6. Reading Composition and Output Contract
liuyao/reading.py is the orchestration layer behind build_reading. It combines question metadata, four pillars, palace and hexagram relations, primary/changed charts, yòngshén, and assessment into plain Python dictionaries. Its externally meaningful output shape is anchored by cases/hexagram-json-contract.md; callers can serialize it without framework, database, network, or I/O coupling.
7. Rules, Reference Data, and Verification
Rules are implemented in package modules and audited against reference assets, especially data/yigram-najia-tables.json. data/liuyao-glossary.json preserves domain terminology; classical sources in data/卜筮正宗_ctext公有领域全文.txt and data/增删卜易_ctext公有领域全文.txt are reference material rather than runtime services. CI in .github/workflows/ci.yml runs regression and differential checks, including comparison against the independent najia implementation. The only material runtime dependency is sxtwl; the engine otherwise operates locally with no network access.
Main entry points: analysis.py Nàjiǎ chart assembly · reading.py Reading composition · calendar.py Four Pillars · __init__.py Package exports
Components 16
Public API
- Package exports · Exports toss, compute_four_pillars, cast_chart, and build_reading.
liuyao/__init__.py
Deterministic Engine
- Cast acquisition · Represents six lines, primary bits, and moving-line state.
liuyao/cast.py - CSPRNG coin tosses
- Four Pillars · Converts local timestamps to pillars and xúnkōng.
liuyao/calendar.py - Hexagram transforms · Maps bits to canonical primary and changed hexagrams.
liuyao/hexagram.py - Palace facts
liuyao/guaxiang.py - Stable gua representations
liuyao/gua_slug.py - Nàjiǎ chart assembly · Builds HexagramChart from primary bits and day stem.
liuyao/analysis.py - Relations & positions · Derives relations, spirits, shì/yìng, and fúshén.
liuyao/relations.py - Stem, branch & elements
liuyao/chizhi.py - Strength & use-god · Derives wàng-shuāi and question-category yòngshén.
liuyao/analysis.py - Reading composition · Produces plain Python dictionaries without I/O coupling.
liuyao/reading.py
Rules & Verification
- Nàjiǎ reference tables · Audits implemented rule mappings.
data/yigram-najia-tables.json - JSON output contract · Defines the externally meaningful reading shape.
cases/hexagram-json-contract.md - Regression & differential CI · Checks regressions and an independent najia implementation.
.github/workflows/ci.yml
Other components
- sxtwl · Chinese calendrical calculation runtime.
Connections 24
- Package exports → Cast acquisition: toss
- Package exports → Four Pillars: compute_four_pillars
- Package exports → Nàjiǎ chart assembly: cast_chart
- Package exports → Reading composition: build_reading
- CSPRNG coin tosses → Cast acquisition: coin outcomes
- sxtwl → Four Pillars: calendar calculations
- Cast acquisition → Hexagram transforms: bits and moving lines
- Hexagram transforms → Palace facts: canonical identity
- Hexagram transforms → Stable gua representations: hexagram identity
- Stem, branch & elements → Relations & positions: stems, branches, elements
- Palace facts → Nàjiǎ chart assembly: palace facts
- Relations & positions → Nàjiǎ chart assembly: attributions
- Hexagram transforms → Nàjiǎ chart assembly: primary hexagram
- Four Pillars → Nàjiǎ chart assembly: day stem
- Nàjiǎ chart assembly → Strength & use-god: chart facts
- Four Pillars → Strength & use-god: time context
- Nàjiǎ chart assembly → Reading composition: primary and changed charts
- Four Pillars → Reading composition: four pillars
- Strength & use-god → Reading composition: assessment and yòngshén
- Palace facts → Reading composition: palace relations
- Nàjiǎ reference tables → Nàjiǎ chart assembly: audits rules
- JSON output contract → Reading composition: output shape
- Regression & differential CI → Nàjiǎ reference tables: validates mappings
- Regression & differential CI → Reading composition: regression checks