]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/associations.lisp
Implement Date Occurrences with lexical validation and CXTM canonicalization
[topic-maps.git] / src / tmdm / associations.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (defun same-role-signature-p (a b)
4 (and (eq (%association-role-type a) (%association-role-type b))
5 (eq (%association-role-player a) (%association-role-player b))))
6
7 (defun normalize-roles (association)
8 ;; TMDM 5.8/6.7: the compared Roles share this parent; other props are empty/null.
9 (loop for pair = (equal-pair (%association-roles association) #'same-role-signature-p)
10 while pair do
11 (let* ((a (first pair)) (b (second pair))
12 (c (%make-association-role association (%association-role-type a) (%association-role-player a))))
13 (setf (%association-roles association) (replace-pair (%association-roles association) a b c)
14 (%association-role-forward a) c (%association-role-forward b) c))))
15
16 (defun equal-associations-p (a b)
17 ;; TMDM 5.7: compare role sets structurally, independent of role ordering.
18 ;; Role parents are the associations being compared (the equality cycle).
19 ;; Scope is empty and reifier/II are absent in this subset.
20 (and (eq (%association-type a) (%association-type b))
21 (= (length (%association-roles a)) (length (%association-roles b)))
22 (every (lambda (role) (find role (%association-roles b) :test #'same-role-signature-p))
23 (%association-roles a))))
24
25 (defun merge-two-associations (map a b)
26 ;; TMDM 6.6: C receives A's equal role set. Retarget parent and discarded
27 ;; equivalent B-role handles, so clients cannot observe stale live references.
28 (let ((c (%make-association map (%association-type a))))
29 (setf (%association-roles c) (copy-list (%association-roles a))
30 (%topic-map-associations map) (replace-pair (%topic-map-associations map) a b c)
31 (%association-forward a) c (%association-forward b) c)
32 (dolist (role (%association-roles c)) (setf (%association-role-parent role) c))
33 (dolist (role (%association-roles b))
34 (setf (%association-role-forward role)
35 (find role (%association-roles c) :test #'same-role-signature-p)))
36 c))
37
38 (defun normalize-associations (map)
39 (dolist (association (%topic-map-associations map)) (normalize-roles association))
40 (loop for pair = (equal-pair (%topic-map-associations map) #'equal-associations-p)
41 while pair do (merge-two-associations map (first pair) (second pair)))
42 map)
43
44 (defun make-association (map type roles)
45 "Create a same-map unscoped Association. ROLES is a nonempty list of (TYPE PLAYER).
46 All references are validated before mutation and resolved to live Topics.
47 Return its current representative after duplicate normalization."
48 (check-type map topic-map)
49 (let ((type (live-topic type)))
50 (unless (eq map (%topic-parent type)) (error "Association type belongs to another map."))
51 (unless (and (listp roles) roles) (error "An Association requires at least one Role."))
52 (let ((references
53 (mapcar (lambda (pair)
54 (unless (and (listp pair) (= 2 (length pair)))
55 (error "Each Role requires a type and a player."))
56 (let ((type (live-topic (first pair))) (player (live-topic (second pair))))
57 (unless (and (eq map (%topic-parent type)) (eq map (%topic-parent player)))
58 (error "Role type and player must belong to the Association's map."))
59 (list type player))) roles)))
60 (let ((association (%make-association map type)))
61 (setf (%association-roles association)
62 (mapcar (lambda (pair) (%make-association-role association (first pair) (second pair))) references)
63 (%topic-map-associations map) (append (%topic-map-associations map) (list association)))
64 (normalize-associations map)
65 (canonical-association association)))))