-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfamily_node.go
400 lines (323 loc) · 9.38 KB
/
family_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package gedcom
import (
"fmt"
"time"
)
// FamilyNode represents a family.
type FamilyNode struct {
*simpleDocumentNode
cachedHusband, cachedWife bool
husband *HusbandNode
wife *WifeNode
}
func newFamilyNode(document *Document, pointer string, children ...Node) *FamilyNode {
return &FamilyNode{
newSimpleDocumentNode(document, TagFamily, "", pointer, children...),
false, false, nil, nil,
}
}
// If the node is nil the result will also be nil.
func (node *FamilyNode) Husband() (husband *HusbandNode) {
if node == nil {
return nil
}
if node.cachedHusband {
return node.husband
}
defer func() {
node.husband = husband
node.cachedHusband = true
}()
possibleHusband := First(NodesWithTag(node, TagHusband))
if IsNil(possibleHusband) {
return nil
}
return possibleHusband.(*HusbandNode)
}
// If the node is nil the result will also be nil.
func (node *FamilyNode) Wife() (wife *WifeNode) {
if node == nil {
return nil
}
if node.cachedWife {
return node.wife
}
defer func() {
node.wife = wife
node.cachedWife = true
}()
possibleWife := First(NodesWithTag(node, TagWife))
if IsNil(possibleWife) {
return nil
}
return possibleWife.(*WifeNode)
}
// TODO: Needs tests
//
// If the node is nil the result will also be nil.
func (node *FamilyNode) Children() ChildNodes {
if node == nil {
return nil
}
children := ChildNodes{}
for _, n := range NodesWithTag(node, TagChild) {
children = append(children, n.(*ChildNode))
}
return children
}
// TODO: Needs tests
//
// If the node is nil the result will also be nil.
func (node *FamilyNode) HasChild(individual *IndividualNode) bool {
if node == nil {
return false
}
for _, n := range NodesWithTag(node, TagChild) {
if n.Value() == "@"+individual.Pointer()+"@" {
return true
}
}
return false
}
// Similarity calculates the similarity between two families.
//
// The depth controls how many generations should be compared. A depth of 0 will
// only compare the husband/wife and not take into account any children. At the
// moment only a depth of 0 is supported. Any other depth will raise panic.
//
// The options.MaxYears allows the error margin on dates to be adjusted. See
// DefaultMaxYearsForSimilarity for more information.
func (node *FamilyNode) Similarity(other *FamilyNode, depth int, options SimilarityOptions) float64 {
if depth != 0 {
panic("depth can only be 0")
}
// It does not matter if any of the partners are nil, Similarity will handle
// these gracefully.
husband := node.Husband().Similarity(other.Husband(), options)
wife := node.Wife().Similarity(other.Wife(), options)
return (husband + wife) / 2
}
func (node *FamilyNode) addChild(value string) *ChildNode {
n := newChildNode(node, value)
node.AddNode(n)
return n
}
func (node *FamilyNode) AddChild(individual *IndividualNode) *ChildNode {
n := newChildNodeWithIndividual(node, individual)
node.AddNode(n)
return n
}
func (node *FamilyNode) SetHusband(individual *IndividualNode) *FamilyNode {
if IsNil(individual) {
husband := node.Husband().Individual()
if IsNil(husband) {
return node
}
nodes := husband.Nodes()
for _, subNode := range nodes {
if subNode.Tag() == TagFamilySpouse && subNode.Value() == node.Identifier() {
husband.DeleteNode(subNode)
}
}
DeleteNodesWithTag(node, TagHusband)
node.husband = nil
node.cachedHusband = true
return node
}
n := NewNode(TagFamilySpouse, node.Identifier(), "")
individual.AddNode(n)
return node.SetHusbandPointer(individual.Pointer())
}
func (node *FamilyNode) SetWife(individual *IndividualNode) *FamilyNode {
if IsNil(individual) {
wife := node.Wife().Individual()
if IsNil(wife) {
return node
}
nodes := wife.Nodes()
for _, subNode := range nodes {
if subNode.Tag() == TagFamilySpouse && subNode.Value() == node.Identifier() {
wife.DeleteNode(subNode)
}
}
DeleteNodesWithTag(node, TagWife)
node.wife = nil
node.cachedWife = true
return node
}
n := NewNode(TagFamilySpouse, node.Identifier(), "")
individual.AddNode(n)
return node.SetWifePointer(individual.Pointer())
}
func (node *FamilyNode) SetWifePointer(pointer string) *FamilyNode {
wife := node.Wife()
value := fmt.Sprintf("@%s@", pointer)
if wife != nil {
wife.value = value
}
node.AddNode(newNode(nil, node, TagWife, value, ""))
node.cachedWife = false
return node
}
func (node *FamilyNode) SetHusbandPointer(pointer string) *FamilyNode {
husband := node.Husband()
value := fmt.Sprintf("@%s@", pointer)
if husband != nil {
husband.value = value
}
husbandNode := newNode(nil, node, TagHusband, value, "")
node.AddNode(husbandNode)
node.cachedHusband = false
return node
}
func (node *FamilyNode) resetCache() {
node.cachedHusband = false
node.cachedWife = false
node.husband = nil
node.wife = nil
}
func (node *FamilyNode) childrenBornBeforeParentsWarnings() (warnings Warnings) {
fatherBirth, _ := node.Husband().Individual().Birth()
motherBirth, _ := node.Wife().Individual().Birth()
for _, child := range node.Children() {
childBirth, _ := child.Individual().Birth()
if !childBirth.IsValid() {
continue
}
if fatherBirth.IsValid() && childBirth.IsBefore(fatherBirth) {
warning := NewChildBornBeforeParentWarning(
node.Husband().Individual(),
child,
)
warnings = append(warnings, warning)
}
if motherBirth.IsValid() && childBirth.IsBefore(motherBirth) {
warning := NewChildBornBeforeParentWarning(
node.Wife().Individual(),
child,
)
warnings = append(warnings, warning)
}
}
return
}
func (node *FamilyNode) siblingsBornTooCloseWarnings() (warnings Warnings) {
pairs := IndividualNodePairs{}
nineMonths := time.Duration(274 * 24 * time.Hour)
twoDays := time.Duration(2 * 24 * time.Hour)
for _, child1 := range node.Children() {
child1Birth, _ := child1.Individual().Birth()
// If the date range is greater than 9 months we do not have enough
// accuracy, so bail out.
if child1Birth.DateRange().Duration().Duration >= nineMonths {
continue
}
for _, child2 := range node.Children() {
// Exclude matching siblings to themselves. Technically we do not
// need to do this check because children born on the same day would
// be considered twins. However, its better to have it here for
// completeness.
if child1.Individual().Is(child2.Individual()) {
continue
}
child2Birth, _ := child2.Individual().Birth()
min, max, err := child1Birth.Sub(child2Birth)
if err != nil {
continue
}
// If the date range is greater than 9 months we do not have enough
// accuracy, so bail out.
if child2Birth.DateRange().Duration().Duration >= nineMonths {
continue
}
// Twins or greater multiples may be born in the same day. We allow
// for two days to compensate for rounding. Also it's possible for
// multiple children to be born on either side of the midnight
// barrier.
if min.Duration < twoDays {
continue
}
if min.Duration < nineMonths || max.Duration < nineMonths {
pair := &IndividualNodePair{
Left: child1.Individual(),
Right: child2.Individual(),
}
if !pairs.Has(pair) {
warning := NewSiblingsBornTooCloseWarning(
child1,
child2,
)
warnings = append(warnings, warning)
pairs = append(pairs, pair)
}
}
}
}
return
}
func (node *FamilyNode) appendMarriedOutOfRange(warnings Warnings, age Age, spouse *IndividualNode) Warnings {
if age.IsKnown && age.Years() < DefaultMinMarriageAge {
warning := NewMarriedOutOfRangeWarning(
node,
spouse,
age.Years(),
"young",
)
warnings = append(warnings, warning)
}
if age.Years() > DefaultMaxMarriageAge {
warning := NewMarriedOutOfRangeWarning(
node,
spouse,
age.Years(),
"old",
)
warnings = append(warnings, warning)
}
return warnings
}
func (node *FamilyNode) marriedOutOfRange() (warnings Warnings) {
marriages := NodesWithTag(node, TagMarriage)
for _, marriage := range marriages {
if husband := node.Husband().Individual(); husband != nil {
_, maxAge := husband.AgeAt(marriage)
warnings = node.appendMarriedOutOfRange(warnings, maxAge, husband)
}
if wife := node.Wife().Individual(); wife != nil {
_, maxAge := wife.AgeAt(marriage)
warnings = node.appendMarriedOutOfRange(warnings, maxAge, wife)
}
}
return
}
func (node *FamilyNode) inversePartnerWarnings() (warnings Warnings) {
husband := node.Husband().Individual()
wife := node.Wife().Individual()
// We only consider the case when both spouses exist, have sexes and they
// are exactly opposites. We do not want to catch same sex partnerships, of
// which GEDCOM has no reasonable way to encode this.
switch {
case husband.Sex().IsFemale() && wife.Sex().IsMale():
warning := NewInverseSpousesWarning(node, husband, wife)
warnings = append(warnings, warning)
}
return
}
func (node *FamilyNode) Warnings() (warnings Warnings) {
warnings = append(warnings, node.childrenBornBeforeParentsWarnings()...)
warnings = append(warnings, node.siblingsBornTooCloseWarnings()...)
warnings = append(warnings, node.marriedOutOfRange()...)
warnings = append(warnings, node.inversePartnerWarnings()...)
return
}
func (node *FamilyNode) String() string {
symbol := "—"
switch {
case len(NodesWithTag(node, TagDivorce)) > 0:
symbol = "⚮"
case len(NodesWithTag(node, TagMarriage)) > 0:
symbol = "⚭"
}
return fmt.Sprintf("%s %s %s", node.Husband().String(),
symbol, node.Wife().String())
}