+;; Slice 21A's twenty successful, previously unregistered originals. Keep this
+;; explicit: similar filenames and .sub helpers are not additional contracts.
+(defparameter *additional-originals*
+ '("itemid-duplicate" "itemid-fragment" "itemid-relative" "itemid"
+ "merge-itemid-with-types" "merge-itemid" "merge-subjid" "merge-subjloc"
+ "subjid-duplicate" "subjid-escaping" "subjid-escaping2" "subjid-fragment"
+ "subjid-relative" "subjid-sameas-itemid" "subjid"
+ "subjloc-duplicate" "subjloc-fragment" "subjloc-multiple" "subjloc-relative" "subjloc"))
+
+(defun references-current-p (map)
+ (let ((topics (topic-map-topics map)) (associations (topic-map-associations map)))
+ (flet ((live (topic) (and (member topic topics :test #'eq) (eq topic (canonical-topic topic)))))
+ (and
+ (every (lambda (topic) (and (live topic) (eq map (topic-parent topic))
+ (every (lambda (role) (and (eq topic (association-role-player role))
+ (member (association-role-parent role) associations :test #'eq)
+ (member role (association-roles (association-role-parent role)) :test #'eq)))
+ (topic-roles-played topic)))) topics)
+ (every (lambda (a) (and (eq a (canonical-association a)) (eq map (association-parent a))
+ (live (association-type a)) (every #'live (association-scope a))
+ (every (lambda (role) (and (eq role (canonical-association-role role))
+ (eq a (association-role-parent role)) (live (association-role-type role))
+ (live (association-role-player role))
+ (member role (topic-roles-played (association-role-player role)) :test #'eq)))
+ (association-roles a)))) associations)))))
+
+(defun same-identifiers-p (a b)
+ (and (= (length a) (length b)) (null (set-exclusive-or a b :test #'equal))))
+
+(defun additional-model-checks (name map)
+ (let* ((topic (by-id map "topic"))
+ (ii (list (concatenate 'string *document* "#topic"))) (si nil) (sl nil)
+ (directory (subseq *document* 0 (1+ (position #\/ *document* :from-end t))))
+ (shared "http://example.org/#topic"))
+ (check (concatenate 'string name ":declared-topic-live-eq")
+ (and topic (eq topic (canonical-topic topic)) (member topic (topic-map-topics map) :test #'eq)))
+ (cond
+ ((equal name "itemid-fragment") (push (concatenate 'string *document* "#topic2") ii))
+ ((equal name "itemid-relative") (push (concatenate 'string directory "more-tests/test.xtm#topic") ii))
+ ((equal name "itemid") (push shared ii))
+ ((member name '("merge-itemid" "merge-itemid-with-types" "merge-subjid" "merge-subjloc") :test #'equal)
+ (push (concatenate 'string *document* "#topic2") ii)
+ (cond ((member name '("merge-itemid" "merge-itemid-with-types") :test #'equal) (push shared ii))
+ ((equal name "merge-subjid") (push shared si))
+ (t (push shared sl)))
+ (check (concatenate 'string name ":merged-declarations-eq") (eq topic (by-id map "topic2"))))
+ ((member name '("subjid-duplicate" "subjid-relative") :test #'equal)
+ (push (concatenate 'string directory "photo.jpg") si))
+ ((equal name "subjid-escaping") (push "http://example.org/test+folder/#topic" si))
+ ((equal name "subjid-escaping2") (push "http://example.org/test folder/#topic" si))
+ ((equal name "subjid-fragment") (push (concatenate 'string *document* "#topic") si))
+ ((equal name "subjid-sameas-itemid")
+ (push "http://psi.example.org/test" ii) (push "http://psi.example.org/test" si))
+ ((equal name "subjid") (push shared si))
+ ((member name '("subjloc-duplicate" "subjloc-relative") :test #'equal)
+ (push (concatenate 'string directory "photo.jpg") sl))
+ ((equal name "subjloc-fragment") (push (concatenate 'string *document* "#topic") sl))
+ ((equal name "subjloc-multiple") (setf sl (list shared "http://example.org/#subject")))
+ ((equal name "subjloc") (push shared sl)))
+ (check (concatenate 'string name ":exact-ii-set") (same-identifiers-p ii (topic-item-identifiers topic)))
+ (check (concatenate 'string name ":exact-si-set") (same-identifiers-p si (topic-subject-identifiers topic)))
+ (check (concatenate 'string name ":exact-sl-set") (same-identifiers-p sl (topic-subject-locators topic)))
+ (check (concatenate 'string name ":no-unrequested-statements-or-reification")
+ (and (null (item-identifiers map)) (null (item-reifier map))
+ (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic))
+ (null (topic-reified topic)))) (topic-map-topics map))))
+ (when (equal name "merge-itemid-with-types")
+ (dolist (type-id '("topictype1" "topictype2"))
+ (let* ((type (by-id map type-id))
+ (role (find type (loop for a in (topic-map-associations map) append (association-roles a))
+ :key #'association-role-player :test #'eq))
+ (a (and role (association-role-parent role))))
+ (check (concatenate 'string name ":forward-reference:" type-id)
+ (and type role (eq type (canonical-topic type))
+ (equal (list (concatenate 'string *document* "#" type-id)) (topic-item-identifiers type))
+ (eq (association-role-type role) (core map "type"))
+ (eq (association-type a) (core map "type-instance"))
+ (= 2 (length (association-roles a)))
+ (find-if (lambda (r) (and (eq topic (association-role-player r))
+ (eq (core map "instance") (association-role-type r)))) (association-roles a))))))
+ (dolist (core-id '("type" "instance" "type-instance"))
+ (check (concatenate 'string name ":core-no-extra-ii:" core-id)
+ (null (topic-item-identifiers (core map core-id))))))))
+
+(defun original-condition-data (condition)
+ (when condition
+ (let ((code (cond ((typep condition 'topic-maps.xtm:xtm-error) (error-code condition))
+ ((typep condition 'item-identifier-error) (item-identifier-error-code condition)))))
+ (topic-maps.test-foundation::fields
+ "class" (string (type-of condition)) "code" (when code (string-downcase (string code)))
+ "message" (princ-to-string condition)))))
+
+(defun record-original-result (case actual expected condition)
+ ;; No rejection (XML, XTM or TMDM) can satisfy a positive original contract.
+ (topic-maps.test-foundation:record-result case
+ (cond ((typep condition 'xtm-unsupported) :unsupported) (condition :execution-error)
+ ((equalp actual expected) :cxtm-match) (t :cxtm-mismatch))
+ :diagnostics (list (if condition (format nil "~A: ~A" (type-of condition) condition)
+ (format nil "Actual ~D bytes; original baseline ~D bytes." (length actual) (length expected))))))
+
+(defun original-model-data (map)
+ (when map
+ (flet ((array (items) (coerce items 'vector))
+ (fields (&rest args) (apply #'topic-maps.test-foundation::fields args)))
+ (let ((topics (topic-map-topics map)))
+ (labels ((ref (topic) (1+ (position topic topics :test #'eq))))
+ (fields "topicCount" (length topics) "associationCount" (length (topic-map-associations map))
+ "canonicalParentsAndInverseReferences" (if (references-current-p map) :true :false)
+ "topics" (array (loop for topic in topics collect
+ (fields "ordinal" (ref topic) "itemIdentifiers" (array (topic-item-identifiers topic))
+ "subjectIdentifiers" (array (topic-subject-identifiers topic)) "subjectLocators" (array (topic-subject-locators topic)))))
+ "associations" (array (loop for a in (topic-map-associations map) collect
+ (fields "type" (ref (association-type a)) "roles" (array (loop for role in (association-roles a) collect
+ (fields "type" (ref (association-role-type role)) "player" (ref (association-role-player role))))))))))))))
+