1 (in-package #:topic-maps.tmdm)
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))))
7 (defun normalize-roles (association)
8 ;; TMDM 5.8/6.7: shared parent; identifiers are metadata, not equality keys.
9 (loop for pair = (equal-pair (%association-roles association) #'same-role-signature-p)
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-role-item-identifiers c)
14 (set-union (%association-role-item-identifiers a) (%association-role-item-identifiers b) #'string=)
15 (%association-roles association) (replace-pair (%association-roles association) a b c)
16 (%association-role-forward a) c (%association-role-forward b) c))))
18 (defun equal-associations-p (a b)
19 ;; TMDM 5.7: compare role sets structurally, independent of role ordering.
20 ;; Role parents are the associations being compared (the equality cycle).
21 ;; Scope sets are significant; identifiers do not participate in equality.
22 (and (eq (%association-type a) (%association-type b))
23 (same-scope-p (%association-scope a) (%association-scope b))
24 (= (length (%association-roles a)) (length (%association-roles b)))
25 (every (lambda (role) (find role (%association-roles b) :test #'same-role-signature-p))
26 (%association-roles a))))
28 (defun merge-two-associations (map a b)
29 ;; TMDM 6.6/6.7: retarget both equal role sets to C, then merge corresponding
30 ;; Roles. This preserves their identifiers AND every retained role handle.
31 (let ((c (%make-association map (%association-type a))))
32 (setf (%association-scope c) (copy-list (%association-scope a))
33 (%association-item-identifiers c)
34 (set-union (%association-item-identifiers a) (%association-item-identifiers b) #'string=)
35 (%association-roles c) (set-union (%association-roles a) (%association-roles b) #'eq)
36 (%topic-map-associations map) (replace-pair (%topic-map-associations map) a b c)
37 (%association-forward a) c (%association-forward b) c)
38 (dolist (role (%association-roles c)) (setf (%association-role-parent role) c))
42 (defun normalize-associations (map)
43 (dolist (association (%topic-map-associations map)) (normalize-roles association))
44 (loop for pair = (equal-pair (%topic-map-associations map) #'equal-associations-p)
45 while pair do (merge-two-associations map (first pair) (second pair)))
48 (defun make-association (map type roles &key scope)
49 "Create a same-map Association with a Scope Topic set. ROLES is a nonempty list of (TYPE PLAYER).
50 All references are validated before mutation and resolved to live Topics.
51 Return its current representative after duplicate normalization."
52 (check-type map topic-map)
53 (let ((type (live-topic type)) (scope (canonical-scope map scope)))
54 (unless (eq map (%topic-parent type)) (error "Association type belongs to another map."))
55 (unless (and (proper-list-p roles) roles) (error "An Association requires at least one Role."))
57 (mapcar (lambda (pair)
58 (unless (and (proper-list-p pair) (= 2 (length pair)))
59 (error "Each Role requires a type and a player."))
60 (let ((type (live-topic (first pair))) (player (live-topic (second pair))))
61 (unless (and (eq map (%topic-parent type)) (eq map (%topic-parent player)))
62 (error "Role type and player must belong to the Association's map."))
63 (list type player))) roles)))
64 (let ((association (%make-association map type)))
65 (setf (%association-scope association) scope
66 (%association-roles association)
67 (mapcar (lambda (pair) (%make-association-role association (first pair) (second pair))) references)
68 (%topic-map-associations map) (append (%topic-map-associations map) (list association)))
69 (normalize-associations map)
70 (canonical-association association)))))