;;;; Optional executable example; only public productive model/reader APIs. (defpackage #:topic-maps.project-work.example (:use #:cl #:topic-maps.tmdm) (:export #:read-source #:load-map #:find-topic #:local-topic #:relations #:identity-view #:model-view #:references-current-p #:show-topic #:add-exercise-note #:demo #:*document-iri* #:*exercise-note*)) (in-package #:topic-maps.project-work.example) (defparameter *document-iri* "https://topic-maps.example.invalid/examples/project-work.ctm") (defparameter *exercise-note* "Uebung: Frage fuer die Lektuere markiert; kein historischer Statuswechsel.") (defun read-source (path) ;; The example CTM is ASCII. No implementation-specific external format. (with-open-file (in path :direction :input) (with-output-to-string (out) (loop for c = (read-char in nil nil) while c do (write-char c out))))) (defun load-map (path &key (source-key "project-work-example")) (let ((source (read-source path))) (values (topic-maps.ctm:deserialize-string source :document-iri *document-iri* :source-name source-key) source))) (defun find-topic (map iri &key (kind :subject-identifier)) ;; A linear query over native Topics, not a second identity index. (find-if (lambda (topic) (member iri (ecase kind (:subject-identifier (topic-subject-identifiers topic)) (:subject-locator (topic-subject-locators topic)) (:item-identifier (item-identifiers topic))) :test #'string=)) (topic-map-topics map))) (defun local-topic (map id) (find-topic map (concatenate 'string *document-iri* "#" id) :kind :item-identifier)) (defun ordered (items) (sort (copy-list items) #'string< :key (lambda (x) (write-to-string x :readably t)))) (defun identity-view (topic) (when topic (list :ii (ordered (item-identifiers topic)) :si (ordered (topic-subject-identifiers topic)) :sl (ordered (topic-subject-locators topic))))) (defun scope-view (topics) (ordered (mapcar #'identity-view topics))) (defun relations (topic) (remove-duplicates (mapcar #'association-role-parent (topic-roles-played topic)) :test #'eq)) (defun association-view (association) (list :type (identity-view (association-type association)) :ii (ordered (item-identifiers association)) :scope (scope-view (association-scope association)) :roles (ordered (mapcar (lambda (role) (list :type (identity-view (association-role-type role)) :player (identity-view (association-role-player role)) :ii (ordered (item-identifiers role)))) (association-roles association))))) (defun model-view (map) "A fresh read-only view of this example's native Items, never stored as a model." (list :map-ii (ordered (item-identifiers map)) :topics (ordered (mapcar (lambda (topic) (list :identity (identity-view topic) :names (ordered (mapcar (lambda (name) (list :value (topic-name-value name) :type (identity-view (topic-name-type name)) :scope (scope-view (topic-name-scope name)) :ii (ordered (item-identifiers name)))) (topic-names topic))) :occurrences (ordered (mapcar (lambda (item) (list :type (identity-view (occurrence-type item)) :value (occurrence-value item) :datatype (occurrence-datatype item) :scope (scope-view (occurrence-scope item)) :ii (ordered (item-identifiers item)))) (topic-occurrences topic))) :roles-played (ordered (mapcar (lambda (role) (list :type (identity-view (association-role-type role)) :association (association-view (association-role-parent role)))) (topic-roles-played topic))))) (topic-map-topics map))) :associations (ordered (mapcar #'association-view (topic-map-associations map))))) (defun references-current-p (map) ;; Exact EQ within this map; no private slots or comparison between maps. (labels ((current (topic) (and (eq topic (canonical-topic topic)) (eq map (topic-parent topic)) (member topic (topic-map-topics map) :test #'eq)))) (and (null (item-reifier map)) (every (lambda (topic) (and (current topic) (null (topic-reified topic)) (every (lambda (name) (and (eq name (canonical-topic-name name)) (eq topic (topic-name-parent name)) (current (topic-name-type name)) (every #'current (topic-name-scope name)) (null (topic-name-variants name)) (null (item-reifier name)))) (topic-names topic)) (every (lambda (item) (and (eq item (canonical-occurrence item)) (eq topic (occurrence-parent item)) (current (occurrence-type item)) (every #'current (occurrence-scope item)) (null (item-reifier item)))) (topic-occurrences topic)))) (topic-map-topics map)) (every (lambda (association) (and (eq association (canonical-association association)) (eq map (association-parent association)) (current (association-type association)) (every #'current (association-scope association)) (null (item-reifier association)) (every (lambda (role) (and (eq role (canonical-association-role role)) (eq association (association-role-parent role)) (current (association-role-type role)) (current (association-role-player role)) (member role (topic-roles-played (association-role-player role)) :test #'eq) (null (item-reifier role)))) (association-roles association)))) (topic-map-associations map))))) (defun label-of (topic) (let ((name (first (topic-names topic)))) (if name (topic-name-value name) (identity-view topic)))) (defun show-topic (topic &optional (stream *standard-output*)) (format stream "~&Topic: ~A~%Identity: ~S~%" (label-of topic) (identity-view topic)) (dolist (item (topic-occurrences topic)) (format stream " ~A: ~S [~A] scope=~S~%" (label-of (occurrence-type item)) (occurrence-value item) (occurrence-datatype item) (mapcar #'label-of (occurrence-scope item)))) (dolist (association (relations topic)) (format stream " Relation ~A: ~S~%" (label-of (association-type association)) (mapcar (lambda (role) (list (label-of (association-role-type role)) (label-of (association-role-player role)))) (association-roles association)))) topic) (defun add-exercise-note (map) (let ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP")) (type (local-topic map "note")) (scope (find-topic map "urn:topic-maps:exercise:22u"))) (unless (and question type scope) (error "This example requires its Question, note Type and exercise Scope.")) (add-occurrence question type *exercise-note* :scope (list scope)))) (defun demo (path &optional (stream *standard-output*)) "Read the real CTM, inspect, add a scoped exercise note, return the actual map." (multiple-value-bind (map source) (load-map path) (let* ((question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP")) (original-history (topic-occurrences question)) (before (model-view map)) (before-cxtm (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*))) (format stream "~&BEFORE (native objects):~%") (show-topic question stream) (let* ((note (add-exercise-note map)) (after (model-view map)) (after-cxtm (topic-maps.cxtm:canonicalize-octets map :base-locator *document-iri*))) (format stream "~&AFTER (exercise only):~%") (show-topic question stream) (format stream "Same Topic EQ: ~S; native references current: ~S~%CXTM bytes ~D -> ~D; first difference (zero based) ~S~%" (eq question (find-topic map "urn:topic-maps:question:Q-XTM-LOOP")) (not (null (references-current-p map))) (length before-cxtm) (length after-cxtm) (mismatch before-cxtm after-cxtm)) (values map before after before-cxtm after-cxtm note source question original-history)))))