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