]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - examples/project-work.lisp
docs(tmdm): coverage matrix, relationship readers, default name types; run report
[topic-maps.git] / examples / project-work.lisp
1 ;;;; Optional executable example; only public productive model/reader APIs.
2 (defpackage #:topic-maps.project-work.example
3 (:use #:cl #:topic-maps.tmdm)
4 (:export #:read-source #:load-map #:find-topic #:local-topic #:relations
5 #:identity-view #:model-view #:references-current-p #:show-topic
6 #:add-exercise-note #:demo #:*document-iri* #:*exercise-note*))
7 (in-package #:topic-maps.project-work.example)
8 (defparameter *document-iri* "https://topic-maps.example.invalid/examples/project-work.ctm")
9 (defparameter *exercise-note*
10 "Uebung: Frage fuer die Lektuere markiert; kein historischer Statuswechsel.")
11 (defun read-source (path)
12 ;; The example CTM is ASCII. No implementation-specific external format.
13 (with-open-file (in path :direction :input)
14 (with-output-to-string (out)
15 (loop for c = (read-char in nil nil) while c do (write-char c out)))))
16 (defun load-map (path &key (source-key "project-work-example"))
17 (let ((source (read-source path)))
18 (values (topic-maps.ctm:deserialize-string source :document-iri *document-iri*
19 :source-name source-key) source)))
20 (defun find-topic (map iri &key (kind :subject-identifier))
21 ;; A linear query over native Topics, not a second identity index.
22 (find-if (lambda (topic)
23 (member iri (ecase kind
24 (:subject-identifier (topic-subject-identifiers topic))
25 (:subject-locator (topic-subject-locators topic))
26 (:item-identifier (item-identifiers topic))) :test #'string=))
27 (topic-map-topics map)))
28 (defun local-topic (map id)
29 (find-topic map (concatenate 'string *document-iri* "#" id) :kind :item-identifier))
30 (defun ordered (items)
31 (sort (copy-list items) #'string< :key (lambda (x) (write-to-string x :readably t))))
32 (defun identity-view (topic)
33 (when topic
34 (list :ii (ordered (item-identifiers topic))
35 :si (ordered (topic-subject-identifiers topic))
36 :sl (ordered (topic-subject-locators topic)))))
37 (defun scope-view (topics) (ordered (mapcar #'identity-view topics)))
38 (defun relations (topic)
39 (remove-duplicates (mapcar #'association-role-parent (topic-roles-played topic)) :test #'eq))
40 (defun association-view (association)
41 (list :type (identity-view (association-type association))
42 :ii (ordered (item-identifiers association))
43 :scope (scope-view (association-scope association))
44 :roles (ordered (mapcar (lambda (role)
45 (list :type (identity-view (association-role-type role))
46 :player (identity-view (association-role-player role))
47 :ii (ordered (item-identifiers role)))) (association-roles association)))))
48 (defun model-view (map)
49 "A fresh read-only view of this example's native Items, never stored as a model."
50 (list :map-ii (ordered (item-identifiers map))
51 :topics (ordered (mapcar (lambda (topic)
52 (list :identity (identity-view topic)
53 :names (ordered (mapcar (lambda (name)
54 (list :value (topic-name-value name) :type (identity-view (topic-name-type name))
55 :scope (scope-view (topic-name-scope name)) :ii (ordered (item-identifiers name)))) (topic-names topic)))
56 :occurrences (ordered (mapcar (lambda (item)
57 (list :type (identity-view (occurrence-type item)) :value (occurrence-value item)
58 :datatype (occurrence-datatype item) :scope (scope-view (occurrence-scope item))
59 :ii (ordered (item-identifiers item)))) (topic-occurrences topic)))
60 :roles-played (ordered (mapcar (lambda (role)
61 (list :type (identity-view (association-role-type role))
62 :association (association-view (association-role-parent role)))) (topic-roles-played topic)))))
63 (topic-map-topics map)))
64 :associations (ordered (mapcar #'association-view (topic-map-associations map)))))
65 (defun references-current-p (map)
66 ;; Exact EQ within this map; no private slots or comparison between maps.
67 (labels ((current (topic)
68 (and (eq topic (canonical-topic topic)) (eq map (topic-parent topic))
69 (member topic (topic-map-topics map) :test #'eq))))
70 (and (null (item-reifier map))
71 (every (lambda (topic)
72 (and (current topic) (null (topic-reified topic))
73 (every (lambda (name)
74 (and (eq name (canonical-topic-name name)) (eq topic (topic-name-parent name))
75 (current (topic-name-type name)) (every #'current (topic-name-scope name))
76 (null (topic-name-variants name)) (null (item-reifier name)))) (topic-names topic))
77 (every (lambda (item)
78 (and (eq item (canonical-occurrence item)) (eq topic (occurrence-parent item))
79 (current (occurrence-type item)) (every #'current (occurrence-scope item))
80 (null (item-reifier item)))) (topic-occurrences topic)))) (topic-map-topics map))
81 (every (lambda (association)
82 (and (eq association (canonical-association association)) (eq map (association-parent association))
83 (current (association-type association)) (every #'current (association-scope association))
84 (null (item-reifier association))
85 (every (lambda (role)
86 (and (eq role (canonical-association-role role)) (eq association (association-role-parent role))
87 (current (association-role-type role)) (current (association-role-player role))
88 (member role (topic-roles-played (association-role-player role)) :test #'eq)
89 (null (item-reifier role)))) (association-roles association)))) (topic-map-associations map)))))
90 (defun label-of (topic)
91 (let ((name (first (topic-names topic))))
92 (if name (topic-name-value name) (identity-view topic))))
93 (defun show-topic (topic &optional (stream *standard-output*))
94 (format stream "~&Topic: ~A~%Identity: ~S~%" (label-of topic) (identity-view topic))
95 (dolist (item (topic-occurrences topic))
96 (format stream " ~A: ~S [~A] scope=~S~%" (label-of (occurrence-type item))
97 (occurrence-value item) (occurrence-datatype item) (mapcar #'label-of (occurrence-scope item))))
98 (dolist (association (relations topic))
99 (format stream " Relation ~A: ~S~%" (label-of (association-type association))
100 (mapcar (lambda (role) (list (label-of (association-role-type role))
101 (label-of (association-role-player role)))) (association-roles association))))
102 topic)
103 (defun add-exercise-note (map)
104 (let ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP"))
105 (type (local-topic map "note"))
106 (scope (find-topic map "urn:topic-maps:exercise:22u")))
107 (unless (and question type scope) (error "This example requires its Question, note Type and exercise Scope."))
108 (add-occurrence question type *exercise-note* :scope (list scope))))
109 (defun demo (path &optional (stream *standard-output*))
110 "Read the real CTM, inspect, add a scoped exercise note, return the actual map."
111 (multiple-value-bind (map source) (load-map path)
112 (let* ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP"))
113 (original-history (topic-occurrences question))
114 (before (model-view map))
115 (before-cxtm (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*)))
116 (format stream "~&BEFORE (native objects):~%") (show-topic question stream)
117 (let* ((note (add-exercise-note map)) (after (model-view map))
118 (after-cxtm (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*)))
119 (format stream "~&AFTER (exercise only):~%") (show-topic question stream)
120 (format stream "Same Topic EQ: ~S; native references current: ~S~%CXTM bytes ~D -> ~D; first difference (zero based) ~S~%"
121 (eq question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP"))
122 (not (null (references-current-p map))) (length before-cxtm) (length after-cxtm) (mismatch before-cxtm after-cxtm))
123 (values map before after before-cxtm after-cxtm note source question original-history)))))