-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
708 lines (592 loc) · 18.7 KB
/
event.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package model
import (
"sort"
)
type TimelineEvent interface {
GetDate() *Date
GetDateType() EventDateType
GetPlace() *Place
GetTitle() string
GetDetail() string
GetNarrative() Text
GetCitations() []*GeneralCitation
Type() string
ShortDescription() string // returns the abbreviated name of the event and its date, e.g. "b. 4 Jul 1928"
What() string // married, born, divorced
When() string // text description of date
Where() string // text description of place
IsInferred() bool // whether or not the event was inferred to exist, i.e. has no supporting evidence
DirectlyInvolves(p *Person) bool // whether or not the event directly involves a person as a principal or party
GetParticipants() []*EventParticipant
GetParticipantsByRole(EventRole) []*EventParticipant
SortsBefore(other TimelineEvent) bool
}
// IndividualTimelineEvent is a timeline event involving one individual.
type IndividualTimelineEvent interface {
TimelineEvent
GetPrincipal() *Person
}
// UnionTimelineEvent is a timeline event involving the union of two parties.
type UnionTimelineEvent interface {
TimelineEvent
GetHusband() *Person
GetWife() *Person
GetOther(p *Person) *Person // returns the first of party1 or party2 that is not p
}
// MultipartyTimelineEvent is a timeline event involving multiple principal parties.
type MultipartyTimelineEvent interface {
TimelineEvent
GetPrincipals() []*Person
}
type EventRole string
const (
EventRoleUnknown EventRole = "unknown"
EventRolePrincipal EventRole = "principal"
EventRoleHusband EventRole = "husband"
EventRoleWife EventRole = "wife"
EventRoleWitness EventRole = "witness" // witness to a marriage, will or other legal document
EventRoleGodparent EventRole = "godparent"
EventRoleBeneficiary EventRole = "beneficiary" // beneficiary of a will
EventRoleExecutor EventRole = "executor" // executor of a will
)
type EventParticipant struct {
Person *Person
Role EventRole
}
func (e *EventParticipant) IsUnknown() bool {
return e == nil || e.Person.IsUnknown()
}
func SortTimelineEvents(evs []TimelineEvent) {
sort.Slice(evs, func(i, j int) bool {
return evs[i].SortsBefore(evs[j])
})
}
type GeneralEvent struct {
Date *Date
Place *Place
Title string
Detail string
Citations []*GeneralCitation
Inferred bool
Narrative Text // hand written narrative, if any
}
func (e *GeneralEvent) GetDate() *Date {
return e.Date
}
func (e *GeneralEvent) When() string {
return e.Date.When()
}
func (e *GeneralEvent) GetDateType() EventDateType {
return EventDateTypeOccurred
}
func (e *GeneralEvent) GetPlace() *Place {
return e.Place
}
func (e *GeneralEvent) Where() string {
return e.Place.Where()
}
func (e *GeneralEvent) GetTitle() string {
return e.Title
}
func (e *GeneralEvent) GetDetail() string {
return e.Detail
}
func (e *GeneralEvent) GetNarrative() Text {
return e.Narrative
}
func (e *GeneralEvent) GetCitations() []*GeneralCitation {
return e.Citations
}
func (e *GeneralEvent) EventDate() *Date {
return e.Date
}
func (e *GeneralEvent) Type() string {
return "general event"
}
func (e *GeneralEvent) ShortDescription() string {
return e.abbrev("")
}
func (e *GeneralEvent) IsInferred() bool {
return e.Inferred
}
func (e *GeneralEvent) abbrev(prefix string) string {
if prefix == "" {
return e.Date.String()
}
return prefix + ". " + e.Date.String()
}
func (e *GeneralEvent) What() string { return "had an event" }
func (e *GeneralEvent) SortsBefore(other TimelineEvent) bool {
if e == nil || e.Date == nil {
return false
}
if other == nil || other.GetDate() == nil {
return true
}
return e.Date.SortsBefore(other.GetDate())
}
// GeneralPartyEvent is a general event involving one individual.
type GeneralIndividualEvent struct {
Principal *Person
OtherParticipants []*EventParticipant
}
func (e *GeneralIndividualEvent) GetPrincipal() *Person {
return e.Principal
}
func (e *GeneralIndividualEvent) DirectlyInvolves(p *Person) bool {
return e.Principal.SameAs(p)
}
func (e *GeneralIndividualEvent) GetParticipants() []*EventParticipant {
return []*EventParticipant{{
Person: e.Principal,
Role: EventRolePrincipal,
}}
}
func (e *GeneralIndividualEvent) GetParticipantsByRole(r EventRole) []*EventParticipant {
if r == EventRolePrincipal {
return []*EventParticipant{{
Person: e.Principal,
Role: EventRolePrincipal,
}}
}
var eps []*EventParticipant
for _, ep := range e.OtherParticipants {
if ep.Role == r {
eps = append(eps, ep)
}
}
return eps
}
// GeneralUnionEvent is a general event involving the union of two parties.
type GeneralUnionEvent struct {
Husband *Person
Wife *Person
OtherParticipants []*EventParticipant
}
func (e *GeneralUnionEvent) GetHusband() *Person {
return e.Husband
}
func (e *GeneralUnionEvent) GetWife() *Person {
return e.Wife
}
func (e *GeneralUnionEvent) GetHusband1() *Person {
return e.Husband
}
func (e *GeneralUnionEvent) GetWife1() *Person {
return e.Wife
}
func (e *GeneralUnionEvent) DirectlyInvolves(p *Person) bool {
return e.Husband.SameAs(p) || e.Wife.SameAs(p)
}
func (e *GeneralUnionEvent) GetOther(p *Person) *Person {
if !e.Husband.SameAs(p) {
return e.Husband
}
if !e.Wife.SameAs(p) {
return e.Wife
}
return p
}
func (e *GeneralUnionEvent) GetParticipants() []*EventParticipant {
return []*EventParticipant{
{
Person: e.Husband,
Role: EventRoleHusband,
},
{
Person: e.Wife,
Role: EventRoleWife,
},
}
}
func (e *GeneralUnionEvent) GetParticipantsByRole(r EventRole) []*EventParticipant {
switch r {
case EventRoleHusband:
return []*EventParticipant{
{
Person: e.Husband,
Role: EventRoleHusband,
},
}
case EventRoleWife:
return []*EventParticipant{
{
Person: e.Wife,
Role: EventRoleWife,
},
}
default:
var eps []*EventParticipant
for _, ep := range e.OtherParticipants {
if ep.Role == r {
eps = append(eps, ep)
}
}
return eps
}
}
// GeneralMultipartyEvent is a general event involving multiple parties.
type GeneralMultipartyEvent struct {
Participants []*EventParticipant
}
func (e *GeneralMultipartyEvent) DirectlyInvolves(p *Person) bool {
for _, ep := range e.Participants {
if ep.Person.SameAs(p) && ep.Role == EventRolePrincipal {
return true
}
}
return false
}
func (e *GeneralMultipartyEvent) GetPrincipals() []*Person {
var ps []*Person
for _, p := range e.Participants {
if p.Role == EventRolePrincipal {
ps = append(ps, p.Person)
}
}
return ps
}
func (e *GeneralMultipartyEvent) GetParticipants() []*EventParticipant {
return e.Participants
}
func (e *GeneralMultipartyEvent) GetParticipantsByRole(r EventRole) []*EventParticipant {
var eps []*EventParticipant
for _, ep := range e.Participants {
if ep.Role == r {
eps = append(eps, ep)
}
}
return eps
}
// POV represents a point of view. It is used to provide contect when constructing a description of an event.
type POV struct {
Person *Person // the person observing or experiencing the event
Place *Place // the place in which the observing is taking place
}
// BirthEvent represents the birth of a person in their timeline
type BirthEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *BirthEvent) Type() string { return "birth" }
func (e *BirthEvent) ShortDescription() string { return e.abbrev("b") }
func (e *BirthEvent) What() string { return "born" }
var (
_ TimelineEvent = (*BirthEvent)(nil)
_ IndividualTimelineEvent = (*BirthEvent)(nil)
)
// BaptismEvent represents the baptism of a person in their timeline
type BaptismEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *BaptismEvent) Type() string { return "baptism" }
func (e *BaptismEvent) ShortDescription() string { return e.abbrev("bap") }
func (e *BaptismEvent) What() string { return "baptised" }
var (
_ TimelineEvent = (*BaptismEvent)(nil)
_ IndividualTimelineEvent = (*BaptismEvent)(nil)
)
// DeathEvent represents the death of a person in their timeline
type DeathEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *DeathEvent) Type() string { return "death" }
func (e *DeathEvent) ShortDescription() string { return e.abbrev("d") }
func (e *DeathEvent) What() string { return "died" }
var (
_ TimelineEvent = (*DeathEvent)(nil)
_ IndividualTimelineEvent = (*DeathEvent)(nil)
)
// BurialEvent represents the burial of a person in their timeline
type BurialEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *BurialEvent) Type() string { return "burial" }
func (e *BurialEvent) ShortDescription() string { return e.abbrev("bur") }
func (e *BurialEvent) What() string { return "buried" }
var (
_ TimelineEvent = (*BurialEvent)(nil)
_ IndividualTimelineEvent = (*BurialEvent)(nil)
)
// CremationEvent represents the cremation of a person in their timeline
type CremationEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *CremationEvent) Type() string { return "cremation" }
func (e *CremationEvent) ShortDescription() string { return e.abbrev("crem") }
func (e *CremationEvent) What() string { return "cremated" }
var (
_ TimelineEvent = (*CremationEvent)(nil)
_ IndividualTimelineEvent = (*CremationEvent)(nil)
)
// DepartureEvent represents the departure of a person from a place
type DepartureEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *DepartureEvent) Type() string { return "departure" }
func (e *DepartureEvent) ShortDescription() string { return e.abbrev("dep") }
func (e *DepartureEvent) What() string { return "departed" }
var (
_ TimelineEvent = (*DepartureEvent)(nil)
_ IndividualTimelineEvent = (*DepartureEvent)(nil)
)
// ArrivalEvent represents the arrival of a person at a place
type ArrivalEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *ArrivalEvent) Type() string { return "arrival" }
func (e *ArrivalEvent) ShortDescription() string { return e.abbrev("arr") }
func (e *ArrivalEvent) What() string { return "arrived" }
var (
_ TimelineEvent = (*ArrivalEvent)(nil)
_ IndividualTimelineEvent = (*ArrivalEvent)(nil)
)
// ApprenticeEvent represents the commencement of an apprenticeship of a person
type ApprenticeEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *ApprenticeEvent) Type() string { return "apprentice" }
func (e *ApprenticeEvent) ShortDescription() string { return e.abbrev("app") }
func (e *ApprenticeEvent) What() string { return "apprenticed" }
var (
_ TimelineEvent = (*ApprenticeEvent)(nil)
_ IndividualTimelineEvent = (*ApprenticeEvent)(nil)
)
// IndividualNarrativeEvent represents some narrative that can be used as-is
type IndividualNarrativeEvent struct {
GeneralEvent
GeneralIndividualEvent
}
func (e *IndividualNarrativeEvent) ShortDescription() string { return e.abbrev("narr") }
func (e *IndividualNarrativeEvent) What() string { return e.GetTitle() }
var (
_ TimelineEvent = (*IndividualNarrativeEvent)(nil)
_ IndividualTimelineEvent = (*IndividualNarrativeEvent)(nil)
)
// CensusEvent represents the taking of a census in a timeline
// It is a shared event
type CensusEvent struct {
GeneralEvent
Entries []*CensusEntry
}
type CensusEntry struct {
Principal *Person
RelationToHead CensusEntryRelation // cleaned
Name string // as recorded
Sex string // as recorded
MaritalStatus CensusEntryMaritalStatus
Age string // as recorded
Occupation string // as recorded
PlaceOfBirth string // as recorded
Impairment string // as recorded (deaf and dumb, blind, imbecile etc)
Detail string // any remaining unparsed detail
Narrative string // hand written narrative, if any
}
func (e *CensusEvent) Type() string { return "census" }
func (e *CensusEvent) ShortDescription() string { return e.abbrev("cens") }
func (e *CensusEvent) What() string { return "appeared in census" }
func (e *CensusEvent) DirectlyInvolves(p *Person) bool {
if p.IsUnknown() {
return false
}
for _, en := range e.Entries {
if p.SameAs(en.Principal) {
return true
}
}
return false
}
func (e *CensusEvent) Entry(p *Person) (*CensusEntry, bool) {
for _, en := range e.Entries {
if en.Principal.SameAs(p) {
return en, true
}
}
return nil, false
}
func (e *CensusEvent) Head() *Person {
for _, en := range e.Entries {
if en.RelationToHead == CensusEntryRelationHead {
return en.Principal
}
}
return nil
}
func (e *CensusEvent) GetParticipants() []*EventParticipant {
var ps []*EventParticipant
// TODO deduplicate
for _, ce := range e.Entries {
if ce.Principal != nil {
ps = append(ps, &EventParticipant{Person: ce.Principal})
}
}
return ps
}
func (e *CensusEvent) GetParticipantsByRole(r EventRole) []*EventParticipant {
panic("GeneralPartyEvent.GetParticipantsByRole not implemented")
}
var _ TimelineEvent = (*CensusEvent)(nil)
// ProbateEvent represents the granting of probate for a person in their timeline
type ProbateEvent struct {
GeneralEvent
GeneralIndividualEvent
}
var (
_ TimelineEvent = (*ProbateEvent)(nil)
_ IndividualTimelineEvent = (*ProbateEvent)(nil)
)
func (e *ProbateEvent) Type() string { return "probate" }
func (e *ProbateEvent) ShortDescription() string { return e.abbrev("prob") }
func (e *ProbateEvent) What() string { return "had probate granted" }
// WillEvent represents the writing of a will by a person in their timeline
type WillEvent struct {
GeneralEvent
GeneralIndividualEvent
}
var (
_ TimelineEvent = (*WillEvent)(nil)
_ IndividualTimelineEvent = (*WillEvent)(nil)
)
func (e *WillEvent) Type() string { return "will" }
func (e *WillEvent) ShortDescription() string { return e.abbrev("will") }
func (e *WillEvent) What() string { return "wrote a will" }
// ResidenceRecordedEvent represents the event of a person's residence being recorded / noted
type ResidenceRecordedEvent struct {
GeneralEvent
GeneralMultipartyEvent
}
func (e *ResidenceRecordedEvent) Type() string { return "residence" }
func (e *ResidenceRecordedEvent) ShortDescription() string { return e.abbrev("res") }
func (e *ResidenceRecordedEvent) What() string { return "resided" }
func (e *ResidenceRecordedEvent) GetDateType() EventDateType { return EventDateTypeRecorded }
func (e *ResidenceRecordedEvent) GetTitle() string { return "residence" }
var (
_ TimelineEvent = (*ResidenceRecordedEvent)(nil)
_ MultipartyTimelineEvent = (*ResidenceRecordedEvent)(nil)
)
// SaleOfPropertyEvent represents the sale of some property person in their timeline
type SaleOfPropertyEvent struct {
GeneralEvent
GeneralIndividualEvent
}
var (
_ TimelineEvent = (*SaleOfPropertyEvent)(nil)
_ IndividualTimelineEvent = (*SaleOfPropertyEvent)(nil)
)
func (e *SaleOfPropertyEvent) Type() string { return "sale of property" }
func (e *SaleOfPropertyEvent) ShortDescription() string { return e.abbrev("sale") }
func (e *SaleOfPropertyEvent) What() string { return "sold some property" }
// MarriageEvent represents the joining of two people in marriage in a timeline
type MarriageEvent struct {
GeneralEvent
GeneralUnionEvent
}
var (
_ TimelineEvent = (*MarriageEvent)(nil)
_ UnionTimelineEvent = (*MarriageEvent)(nil)
)
func (e *MarriageEvent) Type() string { return "marriage" }
func (e *MarriageEvent) ShortDescription() string { return e.abbrev("m") }
func (e *MarriageEvent) What() string { return "married" }
// MarriageLicenseEvent represents the event where two people obtain a license to marry
type MarriageLicenseEvent struct {
GeneralEvent
GeneralUnionEvent
}
var (
_ TimelineEvent = (*MarriageLicenseEvent)(nil)
_ UnionTimelineEvent = (*MarriageLicenseEvent)(nil)
)
func (e *MarriageLicenseEvent) Type() string { return "marriage license" }
func (e *MarriageLicenseEvent) ShortDescription() string { return e.abbrev("lic.") }
func (e *MarriageLicenseEvent) What() string { return "obtained licensed to marry" }
// MarriageBannsEvent represents the event that public notice is given that two people intend to marry
type MarriageBannsEvent struct {
GeneralEvent
GeneralUnionEvent
}
var (
_ TimelineEvent = (*MarriageBannsEvent)(nil)
_ UnionTimelineEvent = (*MarriageBannsEvent)(nil)
)
func (e *MarriageBannsEvent) Type() string { return "marriage banns" }
func (e *MarriageBannsEvent) ShortDescription() string { return e.abbrev("ban") }
func (e *MarriageBannsEvent) What() string { return "had marriage banns read" }
// DivorceEvent represents the ending of a marriage by divorce in a timeline
type DivorceEvent struct {
GeneralEvent
GeneralUnionEvent
}
var (
_ TimelineEvent = (*DivorceEvent)(nil)
_ UnionTimelineEvent = (*DivorceEvent)(nil)
)
func (e *DivorceEvent) Type() string { return "divorce" }
func (e *DivorceEvent) ShortDescription() string { return e.abbrev("div") }
func (e *DivorceEvent) What() string { return "divorced" }
// AnnulmentEvent represents the ending of a marriage by anulment in a timeline
type AnnulmentEvent struct {
GeneralEvent
GeneralUnionEvent
}
var (
_ TimelineEvent = (*AnnulmentEvent)(nil)
_ UnionTimelineEvent = (*AnnulmentEvent)(nil)
)
func (e *AnnulmentEvent) Type() string { return "annulment" }
func (e *AnnulmentEvent) ShortDescription() string { return e.abbrev("anul") }
func (e *AnnulmentEvent) What() string { return "had marriage anulled" }
type EventMatcher func(TimelineEvent) bool
// FilterEventList returns a new slice that includes only the events that match the
// supplied EventMatcher
func FilterEventList(evs []TimelineEvent, include EventMatcher) []TimelineEvent {
switch len(evs) {
case 0:
return []TimelineEvent{}
case 1:
if include(evs[0]) {
return []TimelineEvent{evs[0]}
}
return []TimelineEvent{}
default:
l := make([]TimelineEvent, 0, len(evs))
for _, ev := range evs {
if include(ev) {
l = append(l, ev)
}
}
return l
}
}
// CollapseEventList returns a new slice that includes only unique events
func CollapseEventList(evs []TimelineEvent) []TimelineEvent {
switch len(evs) {
case 0:
return []TimelineEvent{}
case 1:
return []TimelineEvent{evs[0]}
case 2:
if evs[0] == evs[1] {
return []TimelineEvent{evs[0]}
}
return []TimelineEvent{evs[0], evs[1]}
default:
seen := make(map[TimelineEvent]bool)
l := make([]TimelineEvent, 0, len(evs))
for _, ev := range evs {
if seen[ev] {
continue
}
seen[ev] = true
l = append(l, ev)
}
return l
}
}