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: the compared Roles share this parent; other props are empty/null.
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-roles association) (replace-pair (%association-roles association) a b c)
14 (%association-role-forward a) c (%association-role-forward b) c))))
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 sets are significant; reifier/II are absent in this subset.
20 (and (eq (%association-type a) (%association-type b))
21 (same-scope-p (%association-scope a) (%association-scope b))
22 (= (length (%association-roles a)) (length (%association-roles b)))
23 (every (lambda (role) (find role (%association-roles b) :test #'same-role-signature-p))
24 (%association-roles a))))
26 (defun merge-two-associations (map a b)
27 ;; TMDM 6.6: C receives A's equal role set. Retarget parent and discarded
28 ;; equivalent B-role handles, so clients cannot observe stale live references.
29 (let ((c (%make-association map (%association-type a))))
30 (setf (%association-scope c) (copy-list (%association-scope a))
31 (%association-roles c) (copy-list (%association-roles a))
32 (%topic-map-associations map) (replace-pair (%topic-map-associations map) a b c)
33 (%association-forward a) c (%association-forward b) c)
34 (dolist (role (%association-roles c)) (setf (%association-role-parent role) c))
35 (dolist (role (%association-roles b))
36 (setf (%association-role-forward role)
37 (find role (%association-roles c) :test #'same-role-signature-p)))
40 (defun normalize-associations (map)
41 (dolist (association (%topic-map-associations map)) (normalize-roles association))
42 (loop for pair = (equal-pair (%topic-map-associations map) #'equal-associations-p)
43 while pair do (merge-two-associations map (first pair) (second pair)))
46 (defun make-association (map type roles &key scope)
47 "Create a same-map Association with a Scope Topic set. ROLES is a nonempty list of (TYPE PLAYER).
48 All references are validated before mutation and resolved to live Topics.
49 Return its current representative after duplicate normalization."
50 (check-type map topic-map)
51 (let ((type (live-topic type)) (scope (canonical-scope map scope)))
52 (unless (eq map (%topic-parent type)) (error "Association type belongs to another map."))
53 (unless (and (proper-list-p roles) roles) (error "An Association requires at least one Role."))
55 (mapcar (lambda (pair)
56 (unless (and (proper-list-p pair) (= 2 (length pair)))
57 (error "Each Role requires a type and a player."))
58 (let ((type (live-topic (first pair))) (player (live-topic (second pair))))
59 (unless (and (eq map (%topic-parent type)) (eq map (%topic-parent player)))
60 (error "Role type and player must belong to the Association's map."))
61 (list type player))) roles)))
62 (let ((association (%make-association map type)))
63 (setf (%association-scope association) scope
64 (%association-roles association)
65 (mapcar (lambda (pair) (%make-association-role association (first pair) (second pair))) references)
66 (%topic-map-associations map) (append (%topic-map-associations map) (list association)))
67 (normalize-associations map)
68 (canonical-association association)))))