-- allium: 3 -- book-author-parsing.allium -- Scope: Construction of book author attribution string from EPUB and FB2 metadata in Asteria -- Includes: -- - Extraction of Dublin Core dc:creator elements with role attributes -- - Fallback to structured author elements (first-name, middle-name, last-name, nickname) -- - MARC role mapping (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized) -- - Role grouping in document order -- - Deduplication of creator names within each role group -- - Formatted output string representation ("Author: Bob, Charlie, Editor: Alice") -- Excludes: -- - Low-level XML streaming and archive decompression -- - UI rendering and line-breaking ------------------------------------------------------------ -- Value Types ------------------------------------------------------------ value StructuredAuthor { first_name: String? middle_name: String? last_name: String? nickname: String? } value CreatorItem { role_code: String? name: String } ------------------------------------------------------------ -- Contracts ------------------------------------------------------------ contract AuthorAttributionService { resolve_attribution: (file_path: String) -> String @invariant Precedence -- If Dublin Core creators are present, they take precedence; -- structured author elements are used only as a fallback when DC creators are absent. @invariant RoleMapping -- Roles "aut", empty, or unspecified map to "Author". -- Standard MARC codes map to human-readable names: -- "edt" -> "Editor", "trl" -> "Translator", "ill" -> "Illustrator". -- Unrecognized or custom roles are capitalized as-is. @invariant GroupingAndDeduplication -- Creators are grouped by resolved role in order of first appearance. -- Identical names within the same role are deduplicated, preserving first appearance. -- Each role group is formatted as ": , ". -- Role groups are joined with ", ". @invariant EmptyFallback -- If no valid creator or structured author elements exist, the attribution is empty string. } ------------------------------------------------------------ -- Entities and Variants ------------------------------------------------------------ entity Book { file_path: String is_active: Boolean } entity BookMetadata { book: Book status: unparsed | parsed transitions status { unparsed -> parsed terminal: parsed } raw_dc_creators: List raw_structured_authors: List author_attribution: String when status = parsed } ------------------------------------------------------------ -- Rules ------------------------------------------------------------ rule LoadMetadata { when: LoadMetadataTrigger(book, creators, authors) ensures: BookMetadata.created( book: book, status: unparsed, raw_dc_creators: creators, raw_structured_authors: authors ) } rule ParseMetadataWithDcCreators { when: ParseMetadata(metadata) requires: metadata.status = unparsed requires: metadata.raw_dc_creators.count > 0 ensures: metadata.status = parsed metadata.author_attribution = format_creators(metadata.raw_dc_creators) } rule ParseMetadataFallbackToStructuredAuthors { when: ParseMetadata(metadata) requires: metadata.status = unparsed requires: metadata.raw_dc_creators.count = 0 ensures: metadata.status = parsed metadata.author_attribution = format_structured_authors(metadata.raw_structured_authors) } ------------------------------------------------------------ -- Invariants ------------------------------------------------------------ invariant FormattedAttributionRequiresParsedState { for metadata in BookMetadatas where status = parsed: metadata.author_attribution != null } ------------------------------------------------------------ -- Actor Declarations ------------------------------------------------------------ actor ReaderEngine { identified_by: Book where is_active = true } ------------------------------------------------------------ -- Surfaces ------------------------------------------------------------ surface BookMetadataSurface { facing engine: ReaderEngine context metadata: BookMetadata contracts: fulfils AuthorAttributionService exposes: metadata.book.file_path metadata.author_attribution when metadata.status = parsed provides: LoadMetadataTrigger(metadata.book, metadata.raw_dc_creators, metadata.raw_structured_authors) when engine.is_active ParseMetadata(metadata) when metadata.status = unparsed and engine.is_active }