pubid 2.0.58 → 2.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +6 -0
  3. data/data/parg/ieee.json +1 -1
  4. data/lib/pubid/bsi/CLAUDE.md +2 -0
  5. data/lib/pubid/bsi/identifiers/consolidated_identifier.rb +3 -0
  6. data/lib/pubid/bsi/pubid_urn_schema.rb +3 -0
  7. data/lib/pubid/bsi/urn.adoc +1 -1
  8. data/lib/pubid/bsi_pfs/identifiers/consolidated_identifier.rb +4 -0
  9. data/lib/pubid/bsi_pfs/pubid_urn_schema.rb +3 -0
  10. data/lib/pubid/bsi_pfs/urn.adoc +4 -1
  11. data/lib/pubid/builder/base.rb +5 -0
  12. data/lib/pubid/cen_cenelec/CLAUDE.md +2 -0
  13. data/lib/pubid/cen_cenelec/identifiers/consolidated_identifier.rb +4 -0
  14. data/lib/pubid/cen_cenelec/pubid_urn_schema.rb +3 -0
  15. data/lib/pubid/cen_cenelec/urn.adoc +1 -1
  16. data/lib/pubid/cen_pfs/identifiers/consolidated_identifier.rb +4 -0
  17. data/lib/pubid/cen_pfs/pubid_urn_schema.rb +3 -0
  18. data/lib/pubid/cen_pfs/urn.adoc +4 -1
  19. data/lib/pubid/components/relation.rb +290 -0
  20. data/lib/pubid/components.rb +1 -0
  21. data/lib/pubid/errors.rb +12 -1
  22. data/lib/pubid/identifier.rb +66 -0
  23. data/lib/pubid/iec/CLAUDE.md +2 -0
  24. data/lib/pubid/iec/identifier.rb +3 -0
  25. data/lib/pubid/iec/identifiers/consolidated_identifier.rb +4 -0
  26. data/lib/pubid/iec/pubid_urn_schema.rb +3 -0
  27. data/lib/pubid/iec/urn.adoc +3 -1
  28. data/lib/pubid/ieee/CLAUDE.md +6 -0
  29. data/lib/pubid/ieee/builder.rb +83 -7
  30. data/lib/pubid/ieee/identifiers/adopted_standard.rb +9 -0
  31. data/lib/pubid/ieee/identifiers/base.rb +6 -0
  32. data/lib/pubid/ieee/identifiers/corrigendum.rb +5 -1
  33. data/lib/pubid/ieee/identifiers/dual_published.rb +9 -0
  34. data/lib/pubid/ieee/identifiers/joint_development.rb +12 -0
  35. data/lib/pubid/ieee/parser.rb +6 -2
  36. data/lib/pubid/ieee/pubid_urn_schema.rb +4 -0
  37. data/lib/pubid/ieee/renderer.rb +2 -0
  38. data/lib/pubid/ieee/urn.adoc +6 -1
  39. data/lib/pubid/iso/CLAUDE.md +2 -0
  40. data/lib/pubid/iso/identifier.rb +3 -0
  41. data/lib/pubid/iso/identifiers/amendment.rb +12 -0
  42. data/lib/pubid/iso/pubid_urn_schema.rb +2 -0
  43. data/lib/pubid/iso/urn.adoc +6 -2
  44. data/lib/pubid/pubid_urn/decoder.rb +46 -0
  45. data/lib/pubid/pubid_urn/encoder.rb +43 -0
  46. data/lib/pubid/pubid_urn/schema.rb +4 -2
  47. data/lib/pubid/version.rb +1 -1
  48. data/lib/pubid.rb +245 -3
  49. metadata +3 -2
@@ -108,3 +108,5 @@ and `typed_stage` serialize every registry field, so each field is its own
108
108
  atom (`typeabbr-`, `stagehs-`, `tsabbr-` …; `typed_stage.abbr` is a list)
109
109
  — the NIST ruling; reducing them would change `to_hash` and need a
110
110
  re-crawl.
111
+
112
+ - **The consolidated bundle states `consolidates` (issue #806)**: `ConsolidatedIdentifier` includes `Components::Relation::ConsolidatedEdge` — one edge toward `identifiers.first` on the additive `relations` wire key; the URN schema carries the `edges-` atom.
@@ -7,6 +7,9 @@ module Pubid
7
7
  # Example: "BS 4592-0:2006+A1:2012" = [BS 4592-0:2006, Amendment 1:2012]
8
8
  class ConsolidatedIdentifier < SingleIdentifier
9
9
  include RootIdentity
10
+ # The document-graph edge (issue #806): the bundle consolidates its
11
+ # first member.
12
+ include ::Pubid::Components::Relation::ConsolidatedEdge
10
13
 
11
14
  attribute :identifiers, ::Pubid::Identifier, polymorphic: true,
12
15
  collection: true
@@ -74,6 +74,9 @@ module Pubid
74
74
  atom "bundle_type", "btype"
75
75
  atom "identifiers", "ids", kind: :ids
76
76
  atom "na_supplements", "nasup", kind: :ids
77
+ # The document graph (issue #806): the consolidated edge toward the
78
+ # first member.
79
+ atom "relations", "edges", kind: :edges
77
80
 
78
81
  type "pubid:bsi:addendum-document", "ad"
79
82
  type "pubid:bsi:adopted-european-norm", "aen", core: []
@@ -54,7 +54,7 @@ Corpus: 1499 of 1501 pass ids parse.
54
54
  | `pubid:bsi:value-added-publication` | `vap` | number | 6 | `PP 7722:2006 BOOK`
55
55
  |===
56
56
 
57
- Atoms, canonical order: `pubbody-` (`publisher.body`), `typeabbr-` (`type.abbr`), `typename-` (`type.name`), `typetypecode-` (`type.type_code`), `part-` (`part`), `year-` (`year`), `month-` (`month`, int), `ed-` (`edition`), `stageabbr-` (`stage.abbr`), `stageharmonizedstages-` (`stage.harmonized_stages`), `stagename-` (`stage.name`), `stagestagecode-` (`stage.stage_code`), `addendumnumber-` (`addendum_number`), `addendumyear-` (`addendum_year`, int), `amdsuffixform-` (`amd_suffix_form`, bool), `bundletype-` (`bundle_type`), `commonyearyear-` (`common_year.year`), `explicitpublisher-` (`explicit_publisher`, bool), `format-` (`format`), `isplural-` (`is_plural`, bool), `issuenumber-` (`issue_number`), `iteration-` (`iteration`), `methodand-` (`method_and`), `methodcode-` (`method_code`), `methodto-` (`method_to`), `prefix-` (`prefix`), `secondnumber-` (`second_number.value`), `sectionid-` (`section_id`), `separators-` (`separators`, list), `speccode-` (`spec_code.value`), `supplementnumber-` (`supplement_number`), `supplementyear-` (`supplement_year`, int), `testid-` (`test_id`), `testseries-` (`test_series`), `topic-` (`topic`), `translationlang-` (`translation_lang`), `typedstageabbr-` (`typed_stage.abbr`), `typedstage-` (`typed_stage.code`), `typedstageharmonizedstages-` (`typed_stage.harmonized_stages`), `typedstagename-` (`typed_stage.name`), `typedstagestagecode-` (`typed_stage.stage_code`), `typedstagetypecode-` (`typed_stage.type_code`).
57
+ Atoms, canonical order: `pubbody-` (`publisher.body`), `typeabbr-` (`type.abbr`), `typename-` (`type.name`), `typetypecode-` (`type.type_code`), `part-` (`part`), `year-` (`year`), `month-` (`month`, int), `ed-` (`edition`), `stageabbr-` (`stage.abbr`), `stageharmonizedstages-` (`stage.harmonized_stages`), `stagename-` (`stage.name`), `stagestagecode-` (`stage.stage_code`), `addendumnumber-` (`addendum_number`), `addendumyear-` (`addendum_year`, int), `amdsuffixform-` (`amd_suffix_form`, bool), `bundletype-` (`bundle_type`), `commonyearyear-` (`common_year.year`), `explicitpublisher-` (`explicit_publisher`, bool), `format-` (`format`), `isplural-` (`is_plural`, bool), `issuenumber-` (`issue_number`), `iteration-` (`iteration`), `methodand-` (`method_and`), `methodcode-` (`method_code`), `methodto-` (`method_to`), `prefix-` (`prefix`), `secondnumber-` (`second_number.value`), `sectionid-` (`section_id`), `separators-` (`separators`, list), `speccode-` (`spec_code.value`), `supplementnumber-` (`supplement_number`), `supplementyear-` (`supplement_year`, int), `testid-` (`test_id`), `testseries-` (`test_series`), `topic-` (`topic`), `translationlang-` (`translation_lang`), `typedstageabbr-` (`typed_stage.abbr`), `typedstage-` (`typed_stage.code`), `typedstageharmonizedstages-` (`typed_stage.harmonized_stages`), `typedstagename-` (`typed_stage.name`), `typedstagestagecode-` (`typed_stage.stage_code`), `typedstagetypecode-` (`typed_stage.type_code`), `ids-` (`identifiers`), `nasup-` (`na_supplements`), `edges-` (`relations`, issue #806 — a consolidated bundle states `edges-consolidates-((<first member>))`).
58
58
 
59
59
  Nested identifiers: `ids-(…)` (`identifiers`, ids), `nasupplements-(…)` (`na_supplements`, ids).
60
60
 
@@ -8,6 +8,10 @@ module Pubid
8
8
  attribute :identifiers, ::Pubid::BsiPfs::Identifier,
9
9
  polymorphic: true, collection: true
10
10
 
11
+ # The document-graph edge (issue #806): the bundle consolidates its
12
+ # first member.
13
+ include ::Pubid::Components::Relation::ConsolidatedEdge
14
+
11
15
  # The origin document. Members live in `identifiers`, so
12
16
  # walk the first member to its root.
13
17
  def root
@@ -24,6 +24,9 @@
24
24
  atom "typed_stage.type_code", "tstypecode"
25
25
  atom "typed_stage.harmonized_stages", "tshs", kind: :list
26
26
  atom "identifiers", "ids", kind: :ids
27
+ # The document graph (issue #806): the consolidated edge toward the
28
+ # first member.
29
+ atom "relations", "edges", kind: :edges
27
30
  type "pubid:bsi:adopted-european-norm", "aen", core: []
28
31
  type "pubid:bsi:adopted-international-standard", "ais", core: []
29
32
  type "pubid:bsi:amendment", "amd", core: ["number"]
@@ -11,7 +11,10 @@ The PFS-compiled module-composition flavor of bsi (pubid/pubid#706 §4 —
11
11
  the first module-composition proof flavor, `definitions/bsi.pfs` in
12
12
  pubid-grammar). The composed definition declares the scheme-canonical
13
13
  tags with a narrowed atom table (`pub-`, `part-`, `year-`, the adopted
14
- node and the consolidated `ids`), registered under the `bsi_pfs` flavor
14
+ node and the consolidated `ids`, followed by `edges-` (`relations`,
15
+ issue #806 — a consolidated bundle states
16
+ `edges-consolidates-((<first member>))`)), registered under the
17
+ `bsi_pfs` flavor
15
18
  key. to_pubid_urn emits and round-trips on every absorbed face: the
16
19
  plain BS face, the composed consolidated bundles and the PAS
17
20
  designation, and the adoption wrappers — the codec composes nested
@@ -78,6 +78,11 @@ module Pubid
78
78
  identifier.public_send(setter, realized) if attrs.key?(key.to_sym)
79
79
  end
80
80
  end
81
+ # The construction tail (issue #806): the loop above assigns through
82
+ # setters — the parse path for every flavor building on this shared
83
+ # loop — so the relation faces (ISO's nested `base`, the parsed
84
+ # relationships) are on the identifier only now.
85
+ identifier.derive_relations!
81
86
  end
82
87
 
83
88
  def handle_key(_identifier, _key, _value)
@@ -85,3 +85,5 @@ them; `dateyear-`/`datemonth-` after `month-`), so a GOST-nested URN
85
85
  encodes identically. The nested `type`/`typed_stage` of an EN whose stage
86
86
  is not in the registry stays unplaced — no parse makes that shape today,
87
87
  and it keeps raising "does not place" instead of a silent drop.
88
+
89
+ - **The consolidated bundle states `consolidates` (issue #806)**: `ConsolidatedIdentifier` includes `Components::Relation::ConsolidatedEdge` — one `amends`-style edge toward `identifiers.first` on the additive `relations` wire key (CEN has no `key_value` block, so the key serializes by default; the URN schema carries the `edges-` atom). Equality skips the key, so stored rows round-trip byte-identically. The nested CEN node's edge also reaches GOST/BSI adoptions that nest a consolidated EN.
@@ -17,6 +17,10 @@ module Pubid
17
17
  attribute :identifiers, Pubid::CenCenelec::Identifier,
18
18
  polymorphic: true, collection: true
19
19
 
20
+ # The document-graph edge (issue #806): the bundle consolidates its
21
+ # first member.
22
+ include ::Pubid::Components::Relation::ConsolidatedEdge
23
+
20
24
  # The origin document. Members live in `identifiers` (not `base`), so
21
25
  # walk the first member to its root.
22
26
  def root
@@ -30,6 +30,9 @@ module Pubid
30
30
  atom "stage", "stage"
31
31
  atom "adopted", "adopted", kind: :id
32
32
  atom "identifiers", "ids", kind: :ids
33
+ # The document graph (issue #806): the consolidated edge toward the
34
+ # first member.
35
+ atom "relations", "edges", kind: :edges
33
36
 
34
37
  type "pubid:cencenelec:adopted-european-norm", "aen", core: []
35
38
  type "pubid:cencenelec:amendment", "amd"
@@ -37,7 +37,7 @@ Corpus: 192 of 192 pass ids parse.
37
37
  | `pubid:cencenelec:technical-specification` | `ts` | number | 4 | `CEN/CLC/TS 17880:2022`
38
38
  |===
39
39
 
40
- Atoms, canonical order: `pub-` (`publisher`), `co-` (`copublishers`, list), `part-` (`part`), `year-` (`year`), `month-` (`month`), `v-` (`version`), `stage-` (`stage`).
40
+ Atoms, canonical order: `pub-` (`publisher`), `co-` (`copublishers`, list), `part-` (`part`), `year-` (`year`), `month-` (`month`), `v-` (`version`), `stage-` (`stage`), `adopted-` (kind `:id`), `ids-` (`identifiers`), `edges-` (`relations`, issue #806 — a consolidated bundle states `edges-consolidates-((<first member>))`).
41
41
 
42
42
  Nested identifiers: `adopted-(…)` (`adopted`, id), `ids-(…)` (`identifiers`, ids).
43
43
 
@@ -7,6 +7,10 @@ module Pubid
7
7
  attribute :identifiers, ::Pubid::CenPfs::Identifier,
8
8
  polymorphic: true, collection: true
9
9
 
10
+ # The document-graph edge (issue #806): the bundle consolidates its
11
+ # first member.
12
+ include ::Pubid::Components::Relation::ConsolidatedEdge
13
+
10
14
  # The origin document. Members live in `identifiers`, so
11
15
  # walk the first member to its root.
12
16
  def root
@@ -14,6 +14,9 @@
14
14
  atom "stage", "stage"
15
15
  atom "adopted", "adopted", kind: :id
16
16
  atom "identifiers", "ids", kind: :ids
17
+ # The document graph (issue #806): the consolidated edge toward the
18
+ # first member.
19
+ atom "relations", "edges", kind: :edges
17
20
  type "pubid:cencenelec:adopted-european-norm", "aen", core: []
18
21
  type "pubid:cencenelec:amendment", "amd", core: []
19
22
  type "pubid:cencenelec:cen-report", "cr", core: []
@@ -10,7 +10,10 @@ file is `docs/URN-SCHEMES.adoc`.
10
10
  The PFS-compiled module-composition surface of cen_cenelec
11
11
  (pubid/pubid#706 §4 — the NSB port). The composed definition models the
12
12
  published EN face and declares the scheme-canonical tag
13
- (`pubid:cencenelec:european-norm`) with the atoms of the composed model
13
+ (`pubid:cencenelec:european-norm`) with the atoms of the composed
14
+ model — `ids-` (`identifiers`) followed by `edges-` (`relations`,
15
+ issue #806 — a consolidated bundle states
16
+ `edges-consolidates-((<first member>))`)
14
17
  (`part-`, `year-`); its URN parity with the coded flavor on to_urn /
15
18
  to_mr_string is the measured boundary (the typed-stage machinery is
16
19
  blocker 2 of `definitions/cen.pfs`), while to_pubid_urn emits and
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+
5
+ module Pubid
6
+ module Components
7
+ # One typed edge of the document graph (issue #806).
8
+ #
9
+ # The relation faces the parsers already capture — "Supersedes X",
10
+ # "Revision of X", "Amendment to X (as amended by Y…)", the IEEE
11
+ # reaffirmation/redesignation faces, the consolidated +AMD+COR chains —
12
+ # become first-class edges on the identifier under the additive wire key
13
+ # `relations`:
14
+ #
15
+ # Pubid::Ieee.parse("IEEE Std 16-1955 (Supersedes C48-1931)").relations
16
+ # # => [#<Components::Relation type: "supersedes", targets: [C48-1931]>]
17
+ #
18
+ # The edge type is the canonical verb for the faces issue #806 names —
19
+ # `supersedes`, `amends`, `consolidates`, `revises` — and the attested
20
+ # type string of {Relationship} for every other face (the reaffirmation
21
+ # and redesignation faces among them). FACE_TYPES is the whole mapping;
22
+ # nothing outside these two sources is a valid type.
23
+ #
24
+ # A target-less edge is not a graph statement, so +targets+ is required;
25
+ # a face the parser captured without its related identifiers yields no
26
+ # edge (Derivation below, and this is what keeps an old index row that
27
+ # stores only `relationship_type` re-serializing byte-identically).
28
+ #
29
+ # == Derivation
30
+ #
31
+ # Edges are derived once, at construction, from the relation-bearing
32
+ # attributes the identifier already stores (Identifier.derive_relations);
33
+ # the wire carries them because `relations` is a declared attribute, so
34
+ # `from_hash(to_hash)` restores them verbatim. Equality skips `relations`
35
+ # (`equality_ignored`): a parsed face and a stored row that keeps only
36
+ # the face's type must stay one document to a search — the same rule as
37
+ # every spelling-only value. Assert the edges themselves, not `==`, when
38
+ # a spec needs them to round-trip.
39
+ class Relation < Lutaml::Model::Serializable
40
+ include ::Pubid::SubsetMatch
41
+
42
+ # The canonical verbs of issue #806.
43
+ SUPERSEDES = "supersedes"
44
+ AMENDS = "amends"
45
+ CONSOLIDATES = "consolidates"
46
+ REVISES = "revises"
47
+
48
+ # Attested face type => edge type. Only these three faces rename:
49
+ # "Supersedes X" already speaks the verb; "Revision of X" and
50
+ # "Amendment to X" map to `revises` and `amends`. Every other face
51
+ # (reaffirmation_of, redesignation_of, corrigendum_to, the draft
52
+ # variants …) keeps its attested {Relationship} type verbatim.
53
+ FACE_TYPES = {
54
+ ::Pubid::Components::Relationship::SUPERSEDES => SUPERSEDES,
55
+ ::Pubid::Components::Relationship::REVISION_OF => REVISES,
56
+ ::Pubid::Components::Relationship::AMENDMENT_TO => AMENDS,
57
+ }.freeze
58
+
59
+ # The edge vocabulary: the four canonical verbs plus every attested
60
+ # {Relationship} type a face can name.
61
+ VALID_TYPES =
62
+ ([CONSOLIDATES] + FACE_TYPES.values +
63
+ ::Pubid::Components::Relationship::VALID_TYPES).uniq.freeze
64
+
65
+ TYPE_NAME = /\A[a-z][a-z0-9_]*\z/
66
+
67
+ # `initialize_empty` keeps the two construction paths in agreement
68
+ # (parse derives `[]`, a wire without the key restores `[]`); lutaml
69
+ # still omits an empty collection from the serialization.
70
+ # `polymorphic` routes each element through the root dispatcher, the
71
+ # shape every pubid collection of identifiers uses (a target carries
72
+ # its own `_type`).
73
+ attribute :type, :string
74
+ attribute :targets, ::Pubid::Identifier,
75
+ collection: true, initialize_empty: true, polymorphic: true
76
+ attribute :intermediate_amendments, ::Pubid::Identifier,
77
+ collection: true, initialize_empty: true, polymorphic: true
78
+ attribute :approved_amendments_flag, :boolean
79
+
80
+ def initialize(**args)
81
+ super
82
+ validate!
83
+ end
84
+
85
+ # The face this edge derives from, as the {Relationship} type it was
86
+ # parsed from. The inverse of FACE_TYPES; the canonical verbs that no
87
+ # attested face names (`consolidates`) have no inverse.
88
+ def self.face_type(type)
89
+ FACE_TYPES.key(type) || type
90
+ end
91
+
92
+ # The edges one identifier's relation-bearing attributes state.
93
+ #
94
+ # The generic sources are the attributes every flavor shares through
95
+ # the IEEE machinery: the parsed `relationships` faces (whose targets
96
+ # ride the runtime `related_identifiers`), and the direct `supersedes`
97
+ # / `revision_of` attributes an index row written before this change
98
+ # carries. A wrapper flavor overrides Identifier.derive_relations for
99
+ # its own shape: the consolidated bundles (iec, bsi, cen_cenelec and
100
+ # their PFS aliases) state `consolidates` toward the first member, and
101
+ # the ISO amendment states `amends` toward its base.
102
+ #
103
+ # @param attrs [Hash] the constructor attributes, before assignment
104
+ # @return [Array<Components::Relation>] may be empty; never nil
105
+ def self.derive(attrs)
106
+ faces = Array(attrs[:relationships] || attrs["relationships"])
107
+ edges = faces.map { |face| from_face(face) }
108
+ edges.concat(direct_edges(attrs))
109
+ edges.uniq
110
+ end
111
+
112
+ # The edge one parsed face states, or nil when the face carries no
113
+ # target (an old row stores only `relationship_type`) or no type.
114
+ def self.from_face(face)
115
+ type = edge_type_of(face)
116
+ targets = face_targets(face, :related_identifiers)
117
+ return nil if type.nil? || targets.empty?
118
+
119
+ new(
120
+ type: type,
121
+ targets: targets,
122
+ intermediate_amendments:
123
+ face_targets(face, :intermediate_amendments),
124
+ approved_amendments_flag: approved_flag_of(face),
125
+ )
126
+ end
127
+
128
+ # The face's edge type: nil for a face that states no type.
129
+ def self.edge_type_of(face)
130
+ face_type = face_value(face, :relationship_type)
131
+ FACE_TYPES.fetch(face_type, face_type) if face_type.is_a?(String)
132
+ end
133
+
134
+ # The flag is carried only when the face states it, so `nil` — never
135
+ # `false` — is what both construction paths store (the wire omits it
136
+ # either way; the two paths must agree on the value too).
137
+ def self.approved_flag_of(face)
138
+ true if face_value(face, :approved_amendments_flag)
139
+ end
140
+
141
+ # The face's identifiers that can sit on the wire. Two kinds of
142
+ # target never do, and both are prose the IEEE parser met inside a
143
+ # face rather than a document:
144
+ #
145
+ # * a *degraded* target — the stand-in `parse_identifier_list` builds
146
+ # for related-id text that is not an identifier ("incorporating
147
+ # IEEE Std Qcd-2015"); its hash is `_type` plus the prose in
148
+ # `parenthetical_content` and nothing else;
149
+ # * an *uncastable* target — one that does not survive the exact
150
+ # wire path an edge uses, lutaml's collection cast. A comma-list
151
+ # face parses its items through the whole IEEE grammar and an item
152
+ # can come out as a shape the cast resolves onto another class
153
+ # (a mis-split dual designation among them) — the edge would not
154
+ # round-trip and the pubid URN would refuse itself. The edge states
155
+ # only documents the graph can carry; the face itself keeps
156
+ # everything on `relationships`, exactly as before this change.
157
+ def self.face_targets(face, name)
158
+ identifier_list(face_value(face, name))
159
+ .reject { |target| degraded_target?(target) || !castable?(target) }
160
+ end
161
+
162
+ def self.degraded_target?(target)
163
+ hash = target.to_hash
164
+ hash.size == 2 && hash.key?("parenthetical_content")
165
+ end
166
+
167
+ # The probe edge serializes and deserializes through lutaml exactly
168
+ # as a real edge would; the target is castable when it comes back
169
+ # as itself.
170
+ def self.castable?(target)
171
+ probe = new(type: SUPERSEDES, targets: [target])
172
+ from_hash(probe.to_hash).targets.first == target
173
+ rescue ::StandardError
174
+ false
175
+ end
176
+
177
+ # The edges the direct relation attributes state.
178
+ def self.direct_edges(attrs)
179
+ edges = []
180
+ identifier_list(attrs[:supersedes] || attrs["supersedes"])
181
+ .then { |targets| edges << named_edge(SUPERSEDES, targets) }
182
+ identifier_list(attrs[:revision_of] || attrs["revision_of"])
183
+ .then { |targets| edges << named_edge(REVISES, targets) }
184
+ edges.compact
185
+ end
186
+
187
+ # @return [Relation, nil] nil when +targets+ is empty
188
+ def self.named_edge(type, targets)
189
+ new(type: type, targets: targets) unless targets.empty?
190
+ end
191
+
192
+ # @return [Array<Pubid::Identifier>] the Identifier objects of +value+;
193
+ # a String target is parsed, a Hash is routed through from_hash, and
194
+ # anything else is dropped, because a target that is not an
195
+ # identifier cannot sit on the wire
196
+ def self.identifier_list(value)
197
+ Array(value).filter_map do |item|
198
+ case item
199
+ when ::Pubid::Identifier then item
200
+ when ::Hash then ::Pubid::Identifier.from_hash(item)
201
+ when String, ::Array then derive_targets(item)
202
+ end
203
+ end
204
+ end
205
+ private_class_method :identifier_list
206
+
207
+ # Parse a target the way the IEEE builder does (`Identifier.parse` on
208
+ # the extracted text); unparseable text yields no target. An Array is
209
+ # a list of such texts.
210
+ def self.derive_targets(value)
211
+ targets = value.is_a?(::Array) ? value : [value]
212
+ targets.filter_map do |text|
213
+ text = text.to_s.strip
214
+ next if text.empty?
215
+
216
+ ::Pubid::Identifier.parse(text)
217
+ rescue Pubid::Errors::Error, ::Parslet::ParseFailed
218
+ nil
219
+ end
220
+ end
221
+ private_class_method :derive_targets
222
+
223
+ # Read one face field whether the face arrived as a Relationship
224
+ # object (the parse path) or a Hash (a hand-built constructor call).
225
+ def self.face_value(face, name)
226
+ return face.public_send(name) if face.respond_to?(name)
227
+
228
+ face[name.to_s] || face[name.to_sym] if face.is_a?(::Hash)
229
+ end
230
+ private_class_method :face_value
231
+
232
+ def to_s
233
+ rendered = "#{type} #{targets.join(', ')}"
234
+ rendered += " as amended by #{intermediate_amendments.join(', ')}" if
235
+ intermediate_amendments&.any?
236
+ rendered += " and its approved amendments" if approved_amendments_flag
237
+ rendered
238
+ end
239
+
240
+ def render(context: nil)
241
+ to_s
242
+ end
243
+
244
+ # The edge every consolidated bundle states (issue #806): the chain
245
+ # `BASE+A1+A2` folds its amendments into the first member, so the
246
+ # bundle consolidates that document. Included by the consolidated
247
+ # identifiers of iec, bsi, cen_cenelec and their PFS aliases — the
248
+ # five classes that store their members in `identifiers`.
249
+ module ConsolidatedEdge
250
+ def derive_relations
251
+ member = Array(identifiers).first
252
+ return [] unless member
253
+
254
+ [Relation.new(type: Relation::CONSOLIDATES, targets: [member])]
255
+ end
256
+ end
257
+
258
+ private
259
+
260
+ # lutaml constructs a wire element empty and assigns its attributes
261
+ # afterwards, so the fully-empty state is an under-construction edge,
262
+ # not a defect — only a state with values is validated.
263
+ def validate!
264
+ return if under_construction?
265
+
266
+ validate_type!
267
+ return if Array(targets).any?
268
+
269
+ raise ArgumentError,
270
+ "a relation edge needs targets: #{type} states none"
271
+ end
272
+
273
+ # The lutaml deserialization intermediate: no value set on any
274
+ # attribute.
275
+ def under_construction?
276
+ type.nil? && Array(targets).empty? && intermediate_amendments.empty? &&
277
+ approved_amendments_flag.nil?
278
+ end
279
+
280
+ def validate_type!
281
+ return if type.is_a?(String) && TYPE_NAME.match?(type) &&
282
+ VALID_TYPES.include?(type)
283
+
284
+ raise ArgumentError,
285
+ "Invalid relation type: #{type.inspect}. " \
286
+ "Valid types: #{VALID_TYPES.join(', ')}"
287
+ end
288
+ end
289
+ end
290
+ end
@@ -10,6 +10,7 @@ module Pubid
10
10
  autoload :Language, "pubid/components/language"
11
11
  autoload :Locality, "pubid/components/locality"
12
12
  autoload :Publisher, "pubid/components/publisher"
13
+ autoload :Relation, "pubid/components/relation"
13
14
  autoload :Relationship, "pubid/components/relationship"
14
15
  autoload :Stage, "pubid/components/stage"
15
16
  autoload :Supplement, "pubid/components/supplement"
data/lib/pubid/errors.rb CHANGED
@@ -52,17 +52,28 @@ module Pubid
52
52
  # @return [String, nil]
53
53
  attr_reader :flavor
54
54
 
55
+ # The byte position where the engine gave up, when it reported one and
56
+ # it is an Integer. Held as a plain number: the failure path never
57
+ # retains the engine's error object (whose cause tree and Source pin
58
+ # the subject string), so this field is the only diagnostic that
59
+ # survives the dispatch (issue #803; the engine-side half is
60
+ # parsanol-ruby#123).
61
+ # @return [Integer, nil]
62
+ attr_reader :position
63
+
55
64
  # @param message [String]
56
65
  # @param parse_failure_cause [Object, nil] the engine's structured
57
66
  # cause, nil for a failure raised without running a grammar
58
67
  # @param input [String, nil]
59
68
  # @param flavor [String, nil]
69
+ # @param position [Integer, nil] the byte position of the failure
60
70
  def initialize(message, parse_failure_cause = nil, input: nil,
61
- flavor: nil)
71
+ flavor: nil, position: nil)
62
72
  super(message)
63
73
  @parse_failure_cause = parse_failure_cause
64
74
  @input = input
65
75
  @flavor = flavor
76
+ @position = position
66
77
  end
67
78
 
68
79
  # @return [Object, nil]
@@ -532,6 +532,19 @@ module Pubid
532
532
  attribute :stage, Components::Stage
533
533
  attribute :locality, Components::Locality
534
534
  attribute :typed_stage, Components::TypedStage
535
+ # The document graph (issue #806): the typed relation edges the
536
+ # identifier's own faces state — "Supersedes X", "Revision of X",
537
+ # "Amendment to X (as amended by …)", the consolidated +AMD+COR chains.
538
+ # Derived once at construction ({Identifier.derive_relations}), carried
539
+ # on the wire additively: an identifier without relation faces
540
+ # serializes exactly as before, the empty collection lutaml omits.
541
+ # Equality skips it (`equality_ignored`): a parsed face and a stored row
542
+ # that keeps only the face's type must stay one document to a search —
543
+ # the derivation of the one cannot survive the other's wire. Assert the
544
+ # edges themselves, not `==`, when a spec needs them to round-trip.
545
+ attribute :relations, Components::Relation, collection: true,
546
+ initialize_empty: true
547
+ equality_ignored :relations
535
548
  # "All parts" is a class, not a flag: see {Pubid::AllParts}. These two
536
549
  # readers keep a caller that read the old attribute working.
537
550
  # rubocop:disable Naming/PredicateMethod
@@ -776,6 +789,59 @@ module Pubid
776
789
  attrs = self.class.normalize_init_attributes(attrs)
777
790
  attrs[:_type] ||= self.class.polymorphic_name
778
791
  super
792
+ derive_relations!
793
+ end
794
+
795
+ # Fill `relations` once, from the relation-bearing attributes already
796
+ # assigned on this identifier. The construction tail — the base
797
+ # `initialize`, the IEEE and NIST overrides at the end of their
798
+ # hand-assignment loop, and the shared builder loop after
799
+ # `assign_attributes` — so a parsed face and a manually built
800
+ # identifier derive alike. Edges the caller stated explicitly win:
801
+ # `new(relations: [...])` is never overwritten. `from_hash` restores
802
+ # `relations` from the wire through the same assignment path and never
803
+ # passes through here with attributes, so a stored row is never
804
+ # re-derived — an index row that predates the graph key round-trips
805
+ # byte-identically.
806
+ def derive_relations!
807
+ return if relations&.any?
808
+
809
+ edges = derive_relations
810
+ self.relations = edges unless edges.empty?
811
+ end
812
+
813
+ # The relation edges (issue #806, {Components::Relation}) this
814
+ # identifier's own relation-bearing attributes state. The generic
815
+ # derivation reads only the attributes this class declares, so every
816
+ # flavor without the IEEE machinery answers `[]`: the parsed
817
+ # `relationships` faces (whose targets ride the runtime
818
+ # `related_identifiers`), and the direct `supersedes` / `revision_of`
819
+ # attributes an index row written before this change carries. A flavor
820
+ # whose relation face lives elsewhere overrides this — the consolidated
821
+ # bundles state `consolidates` toward their first member
822
+ # ({Components::Relation::ConsolidatedEdge}), the ISO amendment states
823
+ # `amends` toward its base.
824
+ #
825
+ # @return [Array<Components::Relation>] may be empty; never nil
826
+ def derive_relations
827
+ edges = []
828
+ if self.class.attributes.key?(:relationships)
829
+ Array(relationships).each do |face|
830
+ edges << Components::Relation.from_face(face)
831
+ end
832
+ end
833
+ edges.concat(Components::Relation.direct_edges(
834
+ supersedes: attribute_when_declared(:supersedes),
835
+ revision_of: attribute_when_declared(:revision_of),
836
+ ))
837
+ edges.compact.uniq
838
+ end
839
+
840
+ # The attribute value when the class declares the attribute, else nil —
841
+ # the generic derivation runs on flavors that declare none of the
842
+ # relation attributes.
843
+ def attribute_when_declared(name)
844
+ self.class.attributes.key?(name) ? public_send(name) : nil
779
845
  end
780
846
 
781
847
  # Pure function of the class name; relaton resolves it per registry
@@ -69,3 +69,5 @@ WorkingDocument URNs per document instead of the shared type-only
69
69
  `cispr-`. All landed URNs are unchanged.
70
70
 
71
71
  - **The official-URN stage slot follows the correspondence table's CD code (issue #787 — the pubid-ts#187 parity round closed).** The TS mirror's pubid#727-era finding ("URN stage-slot gaps, stage-30.20/50.00/dcor/dish", pubid-ts PR #186 verification notes → issue pubid-ts#187) listed four official-`urn:iec:std:` stage slots where the two engines minted different URNs. The issue text (#787) inverted the direction for three of the four. Measured verdict, side by side: **`dcor`/`dish` and `50.00` were TS-side gaps, already closed mirror-side** — the mirror's `stageToken` now follows the gem ("no stage-code fallback … leaves the slot empty (#187)", `pubid-ts/src/flavors/iec/index.ts`), and the corpus ATTESTS the empty slot for the draft-supplement identities (`tests/iec/corrigendum.yaml`: `urn:iec:std:iec:60038-1::::::cor:1:` for the CDCOR row; `tests/iec/interpretation_sheet.yaml`: `…::::::ish:1:` for DISH) — the slot reads the ROOT document (`stage_or_type_slot(doc)`), so a supplement-layer stage never reaches it, and IEC's table names no DCOR row at all. **`30.20` was the one gem-side gap**: the IS/TR/TS committee-draft rows led their harmonized lists with 30.00 — a code in NO row of `data/iec/stage-codes.md` — while the table's CD row is `30.20 | CD | Draft circulated as CD | IS, TR, TS` and the mirror (before its #187 alignment) emitted exactly that. The fix is a LEAD REORDER, not a #733 row split: `harmonized_stages` on the `:cd`/`:cd_tr`/`:cd_ts` rows now lead 30.20 with the rest of the 30.xx ladder behind them, so the folded range honor form is unchanged (no reclassification in `stage_codes_ssot_spec.rb`), no `code:` moves (a split would have colliding `:cd` codes and broken `stage_from_kv`'s first-match resolution for the folded faces), no `to_hash`/pubid-URN movement, and `locate_stage_by_harmonized_code` (membership-based, the #570 rule) is unaffected. `IEC CD 60038` → `urn:iec:std:iec:60038::stage-30.20::`, and the reverse lookup `(is, 30.20)` resolves the `:cd` row so `parse_urn` round-trips exactly. **No attested row moves**: the relaton-data-iec corpus carries no `stage-*` value in any slot (S7 holds only `tr`/`ts`/`pas`/`guide`/`srd`), the testsuite ledgers' only stage-bearing rows are the work-programme `stage-00.00`/`stage-10.20` ones, and no gem fixture carries a CD-family face — the only pins that moved are the synthetic shapes in `spec/pubid/iec/urn_stage_edition_spec.rb` (CD, CD TR, and their `parse_urn` round trips), which now also pin the attested empty slot for the DCOR/DISH/FDAM faces. Mirror-side residue: `pubid-ts/src/flavors/iec/model.ts` hand-copies the leads (`TS("cd", …, "30.00")`, `cd_tr`, `cd_ts`) and needs the same three-value flip to restore byte parity. (hand-off: iec-urn-stage-slots.)
72
+
73
+ - **The consolidated bundle states `consolidates` (issue #806)**: `ConsolidatedIdentifier` includes `Components::Relation::ConsolidatedEdge`, so the chain derives one edge toward its first member on the additive `relations` wire key (derived at the shared builder tail — IEC builds through `Builder::Base#assign_attributes`). The top-class `key_value` block maps `relations`, the URN schema carries the `edges-` atom, and equality skips the key, so a stored row without it round-trips byte-identically.
@@ -189,6 +189,9 @@ module Pubid
189
189
  map "stage", with: { to: :stage_to_kv, from: :stage_from_kv }
190
190
  # A Boolean attribute whose `false` default ::Pubid::Identifier#to_hash
191
191
  # already drops, so a plain map suffices (the GB spelling).
192
+ # The document graph (issue #806): the consolidated edge toward the
193
+ # first member.
194
+ map "relations", to: :relations
192
195
  end
193
196
 
194
197
  # --- publisher: primary only when non-default; copublishers as a list ---
@@ -10,6 +10,10 @@ module Pubid
10
10
  class ConsolidatedIdentifier < Identifier
11
11
  attribute :identifiers, Identifier, polymorphic: true, collection: true
12
12
 
13
+ # The document-graph edge (issue #806): the bundle consolidates its
14
+ # first member.
15
+ include ::Pubid::Components::Relation::ConsolidatedEdge
16
+
13
17
  # Common fields are delegated to identifiers.first, so suppress the
14
18
  # inherited common maps and serialize the wrapped identifiers (base
15
19
  # document + supplements) instead. Route each nested hash back through
@@ -51,6 +51,9 @@ module Pubid
51
51
  atom "cispr_identifier", "cispr", kind: :id
52
52
  atom "languages.*.code", "lang"
53
53
  atom "identifiers", "ids", kind: :ids
54
+ # The document graph (issue #806): the consolidated edge toward the
55
+ # first member.
56
+ atom "relations", "edges", kind: :edges
54
57
 
55
58
  facet "languages.*.original_code", "langsrc"
56
59
 
@@ -48,7 +48,9 @@ Corpus: 12331 of 12331 pass ids parse.
48
48
  | `pubid:iec:working-document` | `wd` | number | 2350 | `1/2457/FDIS`
49
49
  |===
50
50
 
51
- Atoms, canonical order: `pub-` (`publisher`), `co-` (`copublishers`, list), `part-` (`part`), `subpart-` (`subpart`), `year-` (`year`), `month-` (`month`), `ed-` (`edition`), `lang-` (`languages.*.code`, list), `database-` (`database`, bool), `fragmentnumber-` (`fragment_number`), `sheetnumber-` (`sheet_number`), `sheetyear-` (`sheet_year`), `undated-` (`undated`, bool), `vap-` (`vap`, list), `tc-` (`technical_committee`), `wdlang-` (`wd_language`), `wdstage-` (`wd_stage`), `wpstage-` (`wp_stage`), `wptype-` (`wp_type`).
51
+ Atoms, canonical order: `pub-` (`publisher`), `co-` (`copublishers`, list), `part-` (`part`), `subpart-` (`subpart`), `year-` (`year`), `month-` (`month`), `ed-` (`edition`), `lang-` (`languages.*.code`, list), `database-` (`database`, bool), `fragmentnumber-` (`fragment_number`), `sheetnumber-` (`sheet_number`), `sheetyear-` (`sheet_year`), `undated-` (`undated`, bool), `vap-` (`vap`, list), `tc-` (`technical_committee`), `wdlang-` (`wd_language`), `wdstage-` (`wd_stage`), `wpstage-` (`wp_stage`), `wptype-` (`wp_type`), `edges-`
52
+ (`relations`, issue #806 — a consolidated bundle states
53
+ `edges-consolidates-((<first member>))`; the `ids-` atom precedes it).
52
54
 
53
55
  Nested identifiers: `ids-(…)` (`identifiers`, ids), `cispr-(…)` (`cispr_identifier`, id).
54
56