]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/core-relationships.lisp
feat(tmdm): readers for the TMDM 7.2/7.3 relationships; tests for both default name...
[topic-maps.git] / src / tmdm / core-relationships.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; Readers for the two relationships of TMDM 2006, 7.2 and 7.3.
4 ;;; They recognize the structure those clauses define and report it; they are added API, not a repair.
5 ;;; They read stored items only: no Topic is interned, nothing is merged, cached or inferred, and Scope
6 ;;; is not evaluated. An Association that uses the Core Subject Identifiers without conforming to the
7 ;;; structural rules is, in the words of both clauses, not considered to represent the relationship.
8
9 (defun core-locator (local)
10 (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))
11
12 (defun has-subject-identifier-p (topic iri)
13 ;; The clauses name the [subject identifiers] property; an equal Item Identifier or Subject Locator
14 ;; does not count.
15 (member iri (%topic-subject-identifiers (canonical-topic topic)) :test #'string=))
16
17 (defun core-relationship-players (association type first-role second-role)
18 "The players of FIRST-ROLE and SECOND-ROLE when ASSOCIATION conforms to the structural rules shared by
19 TMDM 7.2 and 7.3 for the Core Subject locals TYPE, FIRST-ROLE and SECOND-ROLE; otherwise NIL and NIL."
20 (check-type association association)
21 (let* ((association (canonical-association association)) (roles (%association-roles association)))
22 (if (and (has-subject-identifier-p (%association-type association) (core-locator type))
23 (= 2 (length roles)))
24 (flet ((role-of (local)
25 (find-if (lambda (role) (has-subject-identifier-p (%association-role-type role) (core-locator local)))
26 roles)))
27 (let ((first (role-of first-role)) (second (role-of second-role)))
28 (if (and first second (not (eq first second)))
29 (values (canonical-topic (%association-role-player first))
30 (canonical-topic (%association-role-player second)))
31 (values nil nil))))
32 (values nil nil))))
33
34 (defun type-instance-relationship (association)
35 "TMDM 7.2. Return (values TYPE INSTANCE), the two player Topics, when ASSOCIATION represents a
36 type-instance relationship: its type has the Subject Identifier .../type-instance, it has exactly two
37 Roles, one whose type has .../type and one whose type has .../instance. Otherwise (values NIL NIL).
38 The Association keeps its Scope; this reader does not interpret it."
39 (core-relationship-players association "type-instance" "type" "instance"))
40
41 (defun supertype-subtype-relationship (association)
42 "TMDM 7.3. Return (values SUPERTYPE SUBTYPE), the two player Topics, when ASSOCIATION represents a
43 supertype-subtype relationship: its type has the Subject Identifier .../supertype-subtype, it has exactly
44 two Roles, one whose type has .../supertype and one whose type has .../subtype. Otherwise (values NIL NIL).
45 Only this stated relationship is reported; transitive consequences are not computed."
46 (core-relationship-players association "supertype-subtype" "supertype" "subtype"))
47
48 (defun relationships-played (topic reader position)
49 ;; Computed from the Roles the Topic plays (TMDM 5.3.5); no index is kept.
50 (let ((topic (canonical-topic topic)) (found nil))
51 (dolist (role (topic-roles-played topic) (nreverse found))
52 (let ((association (canonical-association (%association-role-parent role))))
53 (unless (member association found :test #'eq)
54 (multiple-value-bind (first second) (funcall reader association)
55 (when (eq topic (ecase position (:first first) (:second second)))
56 (push association found))))))))
57
58 (defun topic-type-associations (topic)
59 "The Associations that state a type of TOPIC: TMDM 7.2 relationships in which TOPIC is the instance."
60 (relationships-played topic #'type-instance-relationship :second))
61 (defun topic-instance-associations (topic)
62 "The Associations that state an instance of TOPIC: TMDM 7.2 relationships in which TOPIC is the type."
63 (relationships-played topic #'type-instance-relationship :first))
64 (defun topic-supertype-associations (topic)
65 "The Associations that state a supertype of TOPIC: TMDM 7.3 relationships in which TOPIC is the subtype."
66 (relationships-played topic #'supertype-subtype-relationship :second))
67 (defun topic-subtype-associations (topic)
68 "The Associations that state a subtype of TOPIC: TMDM 7.3 relationships in which TOPIC is the supertype."
69 (relationships-played topic #'supertype-subtype-relationship :first))