Changeset 337


Ignore:
Timestamp:
11/11/10 08:47:23 (14 years ago)
Author:
lgiessmann
Message:

fixed ticket #90 => added unit-tests for the trivial-query part of the datamodel; added some functionality and fixed some bugs in the trivial-query part.

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/base-tools/base-tools.lisp

    r334 r337  
    1212  (:nicknames :tools)
    1313  (:export :push-string
    14            :when-do))
     14           :when-do
     15           :remove-null
     16           :full-path))
    1517
    1618(in-package :base-tools)
     
    3234         nil)))
    3335
     36
     37(defun remove-null (lst)
     38  "Removes all null values from the passed list."
     39  (remove-if #'null lst))
     40
     41
     42(defun full-path (pathname)
     43  "Returns a string that represents the full path of the passed
     44   CL:Pathname construct."
     45  (declare (CL:Pathname pathname))
     46  (let ((segments
     47         (remove-if #'null
     48                    (map 'list #'(lambda(item)
     49                                   (when (stringp item)
     50                                     (concatenate 'string "/" item)))
     51                         (pathname-directory pathname))))
     52        (full-path-string ""))
     53    (dolist (segment segments)
     54      (push-string segment full-path-string))
     55    (concatenate 'string full-path-string "/" (pathname-name pathname))))
  • TabularUnified trunk/src/isidorus.asd

    r336 r337  
    4040                                     (:file "model_tools"
    4141                                            :depends-on ("exceptions")))
    42                         :depends-on ("constants"))
     42                        :depends-on ("constants" "base-tools"))
    4343               (:module "TM-SPARQL"
    4444                        :components ((:file "sparql"
     
    178178                                     "xml"
    179179                                     "json"
    180                                      "threading"))
     180                                     "threading"
     181                                     "base-tools"
     182                                     "TM-SPARQL"))
    181183               (:module "json"
    182184                        :components ((:file "json_exporter"
  • TabularUnified trunk/src/model/datamodel.lisp

    r335 r337  
    171171           :invoke-on
    172172           :names-by-type
    173            :occurrencs-by-type
     173           :occurrences-by-type
    174174           :characteristics-by-type
    175175           :occurrences-by-value
    176176           :names-by-value
    177            :characteristics-by-value))
     177           :characteristics-by-value
     178           :isa
     179           :aka))
    178180
    179181(in-package :datamodel)
  • TabularUnified trunk/src/model/trivial-queries.lisp

    r335 r337  
    2222           (type (or Null TopicC) role-type))
    2323  (if role-type
    24       (remove-if #'null
    25                 (map 'list #'(lambda(role)
    26                                 (when (eql (instance-of role :revision revision)
    27                                           role-type)
    28                                   role))
    29                       (player-in-roles construct :revision revision)))
     24      (remove-null
     25      (map 'list #'(lambda(role)
     26                      (when (eql (instance-of role :revision revision)
     27                                role-type)
     28                        role))
     29            (player-in-roles construct :revision revision)))
    3030      (player-in-roles construct :revision revision)))
    3131
     
    3636           (type (or Null TopicC) role-type))
    3737  (if role-type
    38       (remove-if #'null
    39                 (map 'list #'(lambda(role)
    40                                 (when (eql (instance-of role :revision revision)
    41                                           role-type)
    42                                   role))
    43                       (roles construct :revision revision)))
     38      (remove-null
     39      (map 'list #'(lambda(role)
     40                      (when (eql (instance-of role :revision revision)
     41                                role-type)
     42                        role))
     43            (roles construct :revision revision)))
    4444      (roles construct :revision revision)))
    4545
    4646
    47 (defgeneric roles-by-player (construct role-player &key revision)
     47(defgeneric roles-by-player (construct role-player
     48                                       &key role-player-is-type revision)
    4849  (:documentation "Returns all roles that contains the corresponding player.
    4950                   If the player is set to nil all roles are returned.")
    50   (:method ((construct AssociationC) role-player &key (revision *TM-REVISION*))
     51  (:method ((construct AssociationC) role-player
     52            &key role-player-is-type (revision *TM-REVISION*))
    5153    (declare (integer revision)
    52              (type (or Null TopicC) role-player))
     54             (type (or Null TopicC) role-player)
     55             (boolean role-player-is-type))
    5356    (if role-player
    54         (remove-if #'null
    55                    (map 'list #'(lambda(role)
    56                                   (when (eql (player role :revision revision)
    57                                              role-player)
    58                                     role))
    59                         (roles construct :revision revision)))
     57        (remove-null
     58         (map 'list #'(lambda(role)
     59                        (if role-player-is-type
     60                            (when (isa (player role :revision revision)
     61                                       role-player)
     62                              role)
     63                            (when (eql (player role :revision revision)
     64                                       role-player)
     65                              role)))
     66              (roles construct :revision revision)))
    6067        (roles construct :revision revision))))
    6168
     
    7885
    7986(defun filter-associations-by-role (associations role-type role-player
    80                                  &key (revision *TM-REVISION*))
     87                                 &key role-player-is-type (revision *TM-REVISION*))
    8188  "Returns associations that have a role corresponding to the passed
    8289   values. If any of the passed role-values is set to nil, it won't be used
    8390   for the evaluation of the result."
    8491  (declare (List associations)
    85            (type (or Null TopicC) role-type role-player))
    86   (remove-if #'null
    87              (intersection
    88               (map 'list #'(lambda(assoc)
    89                              (when (roles-by-type assoc role-type
    90                                                   :revision revision)
    91                                assoc))
    92                    associations)
    93               (map 'list #'(lambda(assoc)
    94                              (when (roles-by-player assoc role-player
    95                                                     :revision revision)
    96                                assoc))
    97                    associations))))
     92           (type (or Null TopicC) role-type role-player)
     93           (boolean role-player-is-type))
     94  (remove-null
     95   (intersection
     96    (map 'list #'(lambda(assoc)
     97                   (when (roles-by-type assoc role-type
     98                                        :revision revision)
     99                     assoc))
     100         associations)
     101    (map 'list #'(lambda(assoc)
     102                   (when (roles-by-player
     103                          assoc role-player
     104                          :role-player-is-type role-player-is-type
     105                          :revision revision)
     106                     assoc))
     107         associations))))
    98108
    99109
    100110(defgeneric associations-of (construct role-type association-type
    101111                                       other-role-type other-player
    102                                        &key revision)
     112                                       &key other-role-player-is-type
     113                                       revision)
    103114  (:documentation "Returns all associations of the passed topic (construct)
    104115                   that corresponds to the given values.
     
    106117                   used to evaluate the result.")
    107118  (:method ((construct TopicC) role-type association-type other-role-type
    108             other-player &key (revision *TM-REVISION*))
     119            other-player &key other-role-player-is-type
     120            (revision *TM-REVISION*))
    109121    (declare (integer revision)
    110122             (type (or Null TopicC) role-type association-type
    111                    other-role-type other-player))
     123                   other-role-type other-player)
     124             (boolean other-role-player-is-type))
    112125    (let ((assocs-by-role (map 'list #'(lambda(role)
    113126                                         (parent role :revision revision))
     
    117130             (filter-associations-by-type assocs-by-role association-type
    118131                                          :revision revision)))
    119         (filter-associations-by-role assocs-by-type other-role-type
    120                                      other-player :revision revision)))))
     132        (filter-associations-by-role
     133         assocs-by-type other-role-type other-player
     134         :role-player-is-type other-role-player-is-type
     135         :revision revision)))))
    121136
    122137
     
    173188    (declare (integer revision))
    174189    (let ((assocs (supertype-associations construct :revision revision)))
    175       (remove-if #'null
    176                  (map 'list #'(lambda(assoc)
    177                                 (find-if-not
    178                                  #'(lambda(role)
    179                                      (eql (player role :revision revision)
    180                                           construct))
    181                                  (roles assoc :revision revision)))
    182                       assocs)))))
     190      (let ((other-roles
     191             (remove-null
     192              (map 'list
     193                   #'(lambda(assoc)
     194                       (find-if-not #'(lambda(role)
     195                                        (eql (player role :revision revision)
     196                                             construct))
     197                                    (roles assoc :revision revision)))
     198                   assocs))))
     199        (remove-null (map 'list #'(lambda(role)
     200                                    (player role :revision revision))
     201                          other-roles))))))
    183202
    184203
     
    197216                          :valid-supertypes current-valid-super-types))))
    198217          (remove-duplicates
    199            (remove-if #'null recursive-super-types)))))))
     218           (remove-null (union recursive-super-types
     219                               current-valid-super-types))))))))
    200220
    201221
     
    205225    (declare (integer revision))
    206226    (let ((assocs (instance-of-associations construct :revision revision)))
    207       (remove-if #'null
    208                  (map 'list #'(lambda(assoc)
    209                                 (find-if-not
    210                                  #'(lambda(role)
    211                                      (eql (player role :revision revision)
    212                                           construct))
    213                                  (roles assoc :revision revision)))
    214                       assocs)))))
     227      (let ((other-roles
     228             (remove-null
     229              (map 'list #'(lambda(assoc)
     230                             (find-if-not #'(lambda(role)
     231                                              (eql (player role :revision revision)
     232                                                   construct))
     233                                          (roles assoc :revision revision)))
     234                   assocs))))
     235        (remove-null (map 'list #'(lambda(role)
     236                                    (player role :revision revision))
     237                          other-roles))))))
    215238
    216239
     
    218241  "Returns all type topics of the passed construct and their super-types."
    219242  (declare (integer revision))
    220   (let ((all-super-types (supertypes construct :revision revision)))
    221     (let ((all-types
    222            (loop for topic in (append (list construct) all-super-types)
    223                 append (direct-instance-of topic :revision revision))))
    224       (remove-duplicates
    225        (remove-if #'null all-types)))))
     243  (let ((direct-types (direct-instance-of construct :revision revision)))
     244    (let ((supertypes-of-types
     245           (loop for type in direct-types
     246              append (supertypes type :revision revision))))
     247      (union direct-types supertypes-of-types))))
    226248
    227249
     
    232254                   value is first casted by the cast-operation to another type
    233255                   and afterwords processed by main-opertion.")
    234   (:method ((construct TopicC) (operation Function))
     256  (:method ((construct CharacteristicC) (operation Function))
    235257    (funcall operation (charvalue construct))))
    236258
    237259
    238 (defgeneric names-by-type (construct type-identifier &key revision)
     260(defgeneric names-by-type (construct nametype &key revision)
    239261  (:documentation "Returns all names that are of the corresponding type.")
    240   (:method ((construct TopicC) (type-identifier IdentifierC)
     262  (:method ((construct TopicC) nametype &key (revision *TM-REVISION*))
     263    (declare (integer revision)
     264             (type (or Null TopicC) nametype))
     265    (remove-if-not #'(lambda(name)
     266                       (eql nametype (instance-of name :revision revision)))
     267                   (names construct :revision revision))))
     268
     269
     270(defgeneric occurrences-by-type (construct occurrencetype &key revision)
     271  (:documentation "Returns all names that are of the corresponding type.")
     272  (:method ((construct TopicC) (occurrencetype TopicC)
    241273            &key (revision *TM-REVISION*))
    242274    (declare (integer revision))
    243     (let ((type-topic (identified-construct type-identifier :revision revision)))
    244       (unless (typep type-topic 'TopicC)
    245         (error (make-bad-type-condition (format nil "from name-by-type(): expected a topic as instance-of but found ~a" (type-of type-topic)) 'TopicC type-topic)))
    246       (let ((results
    247              (map 'list #'(lambda(name)
    248                             (when (instance-of name :revision revision)
    249                               name))
    250                   (names construct :revision revision))))
    251         (remove-if #'null results)))))
    252 
    253 
    254 (defgeneric occurrences-by-type (construct type-identifier &key revision)
    255   (:documentation "Returns all names that are of the corresponding type.")
    256   (:method ((construct TopicC) (type-identifier IdentifierC)
    257             &key (revision *TM-REVISION*))
    258     (declare (integer revision))
    259     (let ((type-topic (identified-construct type-identifier :revision revision)))
    260       (unless (typep type-topic 'TopicC)
    261         (error (make-bad-type-condition (format nil "from occurrence-by-type(): expected a topic as instance-of but found ~a" (type-of type-topic)) 'TopicC type-topic)))
    262       (let ((results
    263              (map 'list #'(lambda(occ)
    264                             (when (instance-of occ :revision revision)
    265                               occ))
    266                   (occurrences construct :revision revision))))
    267         (remove-if #'null results)))))
    268 
    269 
    270 (defgeneric characteristic-by-type (construct type-identifier &key revision)
     275    (remove-if-not #'(lambda(occ)
     276                       (eql occurrencetype (instance-of occ :revision revision)))
     277                   (occurrences construct :revision revision))))
     278
     279
     280(defgeneric characteristics-by-type (construct chartype &key revision)
    271281  (:documentation "Returns all characteristics that are of the
    272282                   corresponding type.")
    273   (:method ((construct TopicC) (type-identifier IdentifierC)
    274             &key (revision *TM-REVISION*))
    275     (declare (integer revision))
    276     (union (names-by-type construct type-identifier :revision revision)
    277            (occurrences-by-type construct type-identifier :revision revision))))
     283  (:method ((construct TopicC) (chartype TopicC) &key (revision *TM-REVISION*))
     284    (declare (integer revision))
     285    (union (names-by-type construct chartype :revision revision)
     286           (occurrences-by-type construct chartype :revision revision))))
    278287
    279288
     
    288297                            occ))
    289298                (occurrences construct :revision revision))))
    290       (remove-if #'null results))))
     299      (remove-null results))))
    291300
    292301
     
    301310                            name))
    302311                (names construct :revision revision))))
    303       (remove-if #'null results))))
    304 
    305 
    306 (defgeneric characteristic-by-value (construct filter &key revision)
     312      (remove-null results))))
     313
     314
     315(defgeneric characteristics-by-value (construct filter &key revision)
    307316  (:documentation "Returns a list of all characteristics of the passed
    308317                   topic, that return a true value when calling filter.")
     
    311320    (union (names-by-value construct filter :revision revision)
    312321           (occurrences-by-value construct filter :revision revision))))
     322
     323
     324(defgeneric isa (construct type &key revision)
     325  (:documentation "Returns all types if the passed construct
     326                   is of the specified type.")
     327  (:method ((construct TopicC) (type TopicC) &key (revision *TM-REVISION*))
     328    (declare (integer revision))
     329    (let ((all-types (instance-of construct :revision revision)))
     330      (when (find type all-types)
     331        all-types))))
     332
     333
     334(defgeneric aka (construct supertype &key revision)
     335  (:documentation "Returns all types if the passed construct
     336                   is of the specified type.")
     337  (:method ((construct TopicC) (supertype TopicC) &key (revision *TM-REVISION*))
     338    (declare (integer revision))
     339    (let ((all-supertypes (supertypes construct :revision revision)))
     340      (when (find supertype all-supertypes)
     341        all-supertypes))))
  • TabularUnified trunk/src/unit_tests/fixtures.lisp

    r328 r337  
    1414   :datamodel
    1515   :it.bese.FiveAM
     16   :base-tools
    1617   :unittests-constants)
    1718  (:import-from :constants
     
    3940           :rdf-init-db
    4041           :rdf-test-db
    41            :with-empty-db))
     42           :with-empty-db
     43           :with-tm-filled-db))
    4244
    4345(in-package :fixtures)
     
    220222  (&body)
    221223  (tear-down-test-db))
     224
     225
     226(def-fixture with-tm-filled-db (dir xtm-path)
     227  (clean-out-db dir)
     228  (let ((tm-id "http://www.isidor.us/unittests/testtm")
     229        (xtm-id (full-path xtm-path)))
     230    (xml-importer:setup-repository xtm-path dir :tm-id tm-id :xtm-id xtm-id)
     231    (elephant:open-store (xml-importer:get-store-spec dir))
     232    (&body)
     233    (tear-down-test-db)))
  • TabularUnified trunk/src/unit_tests/poems.xtm

    r328 r337  
    26302630
    26312631  <tm:association>
    2632     <tm:itemIdentity href="wrrtten-by-erlkoenig-goethe"/>
     2632    <tm:itemIdentity href="written-by-erlkoenig-goethe"/>
    26332633    <tm:type><tm:topicRef href="#written-by"/></tm:type>
    26342634    <tm:role>
  • TabularUnified trunk/src/unit_tests/trivial_queries_test.lisp

    r336 r337  
    1111  (:use  :cl
    1212         :it.bese.FiveAM
    13          :datamodel)
     13         :datamodel
     14         :unittests-constants
     15         :fixtures
     16         :constants)
    1417  (:export :run-trivial-queries-tests
    15            :trivial-queries-tests))
     18           :trivial-queries-tests
     19           :test-aka
     20           :test-isa
     21           :test-x-by-value
     22           :test-x-by-type
     23           :test-invoke-on
     24           :test-instance-of
     25           :test-supertypes
     26           :test-direct-instance-of
     27           :test-direct-supertypes
     28           :test-supertype-associations
     29           :test-instance-of-associations
     30           :test-associations-of
     31           :test-roles-by-type
     32           :test-roles-by-player
     33           :test-filter-associations-by-type
     34           :test-filter-associations-by-role))
    1635
    1736
     
    1938
    2039
    21 (def-suite trivial-queries-test
     40(def-suite trivial-queries-tests
    2241     :description "tests various key functions of the trivial-query-test of
    2342                   the datamodel module")
    2443
    25 (in-suite trivial-queries-test)
     44(in-suite trivial-queries-tests)
     45
     46(test test-aka
     47  "Tests the function aka."
     48  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     49    (with-revision 0
     50      (let ((region (get-item-by-id "region"))
     51            (city (get-item-by-id "city"))
     52            (poem (get-item-by-id "poem"))
     53            (supertype (get-item-by-psi *supertype-psi*))
     54            (subtype (get-item-by-psi *subtype-psi*))
     55            (supertype-subtype (get-item-by-psi *supertype-subtype-psi*))
     56            (rev (get-revision)))
     57        (is-true region)
     58        (is-true city)
     59        (is-true poem)
     60        (is-true supertype)
     61        (is-true subtype)
     62        (is-true supertype-subtype)
     63        (is-true (aka city region))
     64        (is-false (aka city city))
     65        (make-construct 'AssociationC
     66                        :start-revision rev
     67                        :instance-of supertype-subtype
     68                        :roles (list (list :start-revision rev
     69                                           :player region
     70                                           :instance-of subtype)
     71                                     (list :start-revision rev
     72                                           :player poem
     73                                           :instance-of supertype)))
     74        (is-true (aka city region))
     75        (is-true (aka city poem))
     76        (is-true (aka region poem))))))
     77
     78
     79(test test-isa
     80  "Tests the function isa."
     81  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     82    (with-revision 0
     83      (let ((region (get-item-by-id "region"))
     84            (metropolis (get-item-by-id "metropolis"))
     85            (poem (get-item-by-id "poem"))
     86            (frankfurt (get-item-by-id "frankfurt_am_main")))
     87        (is-true region)
     88        (is-true frankfurt)
     89        (is-true metropolis)
     90        (is-true poem)
     91        (is-true (isa frankfurt metropolis))
     92        (is-true (isa frankfurt region))))))
     93
     94
     95(test test-x-by-value
     96  "Tests the functions names-by-value, occurrences-by-value
     97   and characteristics-by-value."
     98  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     99    (with-revision 0
     100      (let ((goethe (get-item-by-id "goethe"))
     101            (poem (get-item-by-id "poem"))
     102            (fn "Johann Wolfgang")
     103            (ln "von Goethe")
     104            (ai "http://de.wikipedia.org/wiki/Johann_Wolfgang_von_Goethe")
     105            (as "any string"))
     106        (let ((fun-fn (lambda(value)
     107                        (string= value fn)))
     108              (fun-ln (lambda(value)
     109                        (string= value ln)))
     110              (fun-ai (lambda(value)
     111                        (string= value ai)))
     112              (fun-as (lambda(value)
     113                        (string= value as))))
     114          (is-true goethe)
     115          (is-true poem)
     116          (is-false (names-by-value goethe fun-as))
     117          (is-false (occurrences-by-value goethe fun-as))
     118          (is-false (characteristics-by-value goethe fun-as))
     119          (is (= (length (names-by-value goethe fun-fn)) 1))
     120          (is (= (length (names-by-value goethe fun-ln)) 1))
     121          (is (= (length (occurrences-by-value goethe fun-ai)) 1))
     122          (is (string= (charvalue (first (names-by-value goethe fun-fn)))
     123                       fn))
     124          (is (string= (charvalue (first (names-by-value goethe fun-ln)))
     125                       ln))
     126          (is (string= (charvalue (first (occurrences-by-value goethe fun-ai)))
     127                       ai))
     128          (is (= (length (characteristics-by-value goethe fun-fn)) 1))
     129          (is (string=
     130               (charvalue (first (characteristics-by-value goethe fun-fn)))
     131               fn)))))))
     132
     133
     134(test test-x-by-type
     135  "Tests the functions names-by-type, occurrences-by-type
     136   and characteristics-by-type."
     137  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     138    (with-revision 0
     139      (let ((goethe (get-item-by-id "goethe"))
     140            (first-name (get-item-by-id "first-name"))
     141            (last-name (get-item-by-id "last-name"))
     142            (author-info (get-item-by-id "author-info"))
     143            (poem (get-item-by-id "poem")))
     144        (is-true goethe)
     145        (is-true first-name)
     146        (is-true last-name)
     147        (is-true author-info)
     148        (is-true poem)
     149        (is-false (names-by-type goethe poem))
     150        (is-false (occurrences-by-type goethe poem))
     151        (is-false (characteristics-by-type goethe poem))
     152        (is (= (length (names-by-type goethe first-name)) 1))
     153        (is (= (length (names-by-type goethe last-name)) 1))
     154        (is (= (length (occurrences-by-type goethe author-info)) 1))
     155        (is (string= (charvalue (first (names-by-type goethe first-name)))
     156                     "Johann Wolfgang"))
     157        (is (string= (charvalue (first (names-by-type goethe last-name)))
     158                     "von Goethe"))
     159        (is (string=
     160             (charvalue (first (occurrences-by-type goethe author-info)))
     161             "http://de.wikipedia.org/wiki/Johann_Wolfgang_von_Goethe"))
     162        (is (= (length (characteristics-by-type goethe first-name)) 1))
     163        (is (string=
     164             (charvalue (first (characteristics-by-type goethe first-name)))
     165             "Johann Wolfgang"))))))
     166
     167
     168(test test-invoke-on
     169  "Tests the function invoke-on."
     170  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     171    (with-revision 0
     172      (let ((frankfurt (get-item-by-id "frankfurt_am_main")))
     173        (is-true frankfurt)
     174        (is (= (length (occurrences frankfurt)) 1))
     175        (is (= (invoke-on (first (occurrences frankfurt))
     176                          #'(lambda(value)
     177                              (+ 1 (parse-integer value))))
     178               (+ 1 659021)))))))
     179
     180
     181
     182(test test-instance-of
     183  "Tests the function instance-of."
     184  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     185    (with-revision 0
     186      (let ((region (get-item-by-id "region"))
     187            (metropolis (get-item-by-id "metropolis"))
     188            (poem (get-item-by-id "poem"))
     189            (frankfurt (get-item-by-id "frankfurt_am_main")))
     190        (is-true region)
     191        (is-true frankfurt)
     192        (is-true metropolis)
     193        (is-true poem)
     194        (is (= (length (instance-of frankfurt)) 2))
     195        (is-false (set-exclusive-or (instance-of frankfurt)
     196                                    (list metropolis region)))))))
     197
     198
     199(test test-supertypes
     200  "Tests the function supertypes."
     201  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     202    (with-revision 0
     203      (let ((region (get-item-by-id "region"))
     204            (city (get-item-by-id "city"))
     205            (poem (get-item-by-id "poem"))
     206            (supertype (get-item-by-psi *supertype-psi*))
     207            (subtype (get-item-by-psi *subtype-psi*))
     208            (supertype-subtype (get-item-by-psi *supertype-subtype-psi*))
     209            (rev (get-revision)))
     210        (is-true region)
     211        (is-true city)
     212        (is-true poem)
     213        (is-true supertype)
     214        (is-true subtype)
     215        (is-true supertype-subtype)
     216        (is (= (length (supertypes city)) 1))
     217        (is (eql (first (supertypes city)) region))
     218        (is-false (supertypes region))
     219        (make-construct 'AssociationC
     220                        :start-revision rev
     221                        :instance-of supertype-subtype
     222                        :roles (list (list :start-revision rev
     223                                           :player region
     224                                           :instance-of subtype)
     225                                     (list :start-revision rev
     226                                           :player poem
     227                                           :instance-of supertype)))
     228        (is (= (length (supertypes city)) 2))))))
     229
     230
     231(test test-direct-instance-of
     232  "Tests the function direct-instance-of."
     233  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     234    (with-revision 0
     235      (let ((region (get-item-by-id "region"))
     236            (frankfurt (get-item-by-id "frankfurt_am_main"))
     237            (metropolis (get-item-by-id "metropolis")))
     238        (is-true region)
     239        (is-true metropolis)
     240        (is-true frankfurt)
     241        (is (= (length (direct-instance-of frankfurt)) 1))
     242        (is (eql (first (direct-instance-of frankfurt)) metropolis))
     243        (is-false (direct-instance-of metropolis))))))
     244
     245
     246(test test-direct-supertypes
     247  "Tests the function direct-supertypes."
     248  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     249    (with-revision 0
     250      (let ((region (get-item-by-id "region"))
     251            (city (get-item-by-id "city"))
     252            (poem (get-item-by-id "poem"))
     253            (supertype (get-item-by-psi *supertype-psi*))
     254            (subtype (get-item-by-psi *subtype-psi*))
     255            (supertype-subtype (get-item-by-psi *supertype-subtype-psi*))
     256            (rev (get-revision)))
     257        (is-true region)
     258        (is-true city)
     259        (is-true poem)
     260        (is-true supertype)
     261        (is-true subtype)
     262        (is-true supertype-subtype)
     263        (is (= (length (direct-supertypes city)) 1))
     264        (is (eql (first (direct-supertypes city)) region))
     265        (is-false (direct-supertypes region))
     266        (make-construct 'AssociationC
     267                        :start-revision rev
     268                        :instance-of supertype-subtype
     269                        :roles (list (list :start-revision rev
     270                                           :player region
     271                                           :instance-of subtype)
     272                                     (list :start-revision rev
     273                                           :player poem
     274                                           :instance-of supertype)))
     275        (is (= (length (direct-supertypes city)) 1))))))
     276
     277
     278(test test-supertype-associations
     279  "Tests the function supertype-associations."
     280  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     281    (with-revision 0
     282      (let ((region (get-item-by-id "region"))
     283            (city (get-item-by-id "city"))
     284            (metropolis (get-item-by-id "metropolis"))
     285            (assocs (get-all-associations))
     286            (supertype (get-item-by-psi *supertype-psi*))
     287            (subtype (get-item-by-psi *subtype-psi*))
     288            (supertype-subtype (get-item-by-psi *supertype-subtype-psi*)))
     289        (is-true region)
     290        (is-true city)
     291        (is-true metropolis)
     292        (is-true supertype)
     293        (is-true subtype)
     294        (is-true supertype-subtype)
     295        (let ((assoc-city
     296               (find-if
     297                #'(lambda(assoc)
     298                    (and (eql (instance-of assoc) supertype-subtype)
     299                         (find-if #'(lambda(role)
     300                                      (and (eql (player role) city)
     301                                           (eql (instance-of role) subtype)))
     302                                  (roles assoc))
     303                         (find-if #'(lambda(role)
     304                                      (and (eql (player role) region)
     305                                           (eql (instance-of role) supertype)))
     306                                  (roles assoc))))
     307                assocs))
     308              (assoc-metropolis
     309               (find-if
     310                #'(lambda(assoc)
     311                    (and (eql (instance-of assoc) supertype-subtype)
     312                         (find-if #'(lambda(role)
     313                                      (and (eql (player role) metropolis)
     314                                           (eql (instance-of role) subtype)))
     315                                  (roles assoc))
     316                         (find-if #'(lambda(role)
     317                                      (and (eql (player role) region)
     318                                           (eql (instance-of role) supertype)))
     319                                  (roles assoc))))
     320                assocs)))
     321          (is-true assoc-city)
     322          (is-true assoc-metropolis)
     323          (is (= (length (supertype-associations city)) 1))
     324          (is (= (length (supertype-associations metropolis)) 1))
     325          (is (eql (first (supertype-associations city)) assoc-city))
     326          (is (eql (first (supertype-associations metropolis)) assoc-metropolis))
     327          (is-false (supertype-associations region)))))))
     328
     329
     330(test test-instance-of-associations
     331  "Tests the function instance-of-associations."
     332  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     333    (with-revision 0
     334      (let ((goethe (get-item-by-id "goethe"))
     335            (instance (get-item-by-psi *instance-psi*))
     336            (type (get-item-by-psi *type-psi*))
     337            (type-instance (get-item-by-psi *type-instance-psi*))
     338            (author (get-item-by-id "author")))
     339        (is-true goethe)
     340        (is-true instance)
     341        (is-true type)
     342        (is-true type-instance)
     343        (is-true author)
     344        (is (= (length (instance-of-associations goethe)) 1))
     345        (is (eql type-instance
     346                 (instance-of (first (instance-of-associations goethe)))))
     347        (is-true (filter-associations-by-role (instance-of-associations goethe)
     348                                              instance goethe))
     349        (is-true (filter-associations-by-role (instance-of-associations goethe)
     350                                              type author))
     351        (is-true (filter-associations-by-type (instance-of-associations goethe)
     352                                              type-instance))))))
     353
     354
     355(test test-associations-of
     356  "Tests the function associations-of."
     357  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     358    (with-revision 0
     359      (let ((goethe (get-item-by-id "goethe"))
     360            (writer (get-item-by-id "writer"))
     361            (written-by (get-item-by-id "written-by"))
     362            (written (get-item-by-id "written"))
     363            (erlkoenig (get-item-by-id "erlkoenig"))
     364            (instance (get-item-by-psi *instance-psi*))
     365            (poem (get-item-by-id "poem")))
     366        (is-true goethe)
     367        (is-true writer)
     368        (is-true written-by)
     369        (is-true written)
     370        (is-true erlkoenig)
     371        (is-true instance)
     372        (is-true poem)
     373        (is (= (length (associations-of goethe nil nil nil nil)) 4))
     374        (is (= (length (associations-of goethe writer nil nil nil)) 3))
     375        (is (= (length (associations-of goethe writer written-by nil nil)) 2))
     376        (is (= (length (associations-of goethe writer written-by written nil)) 2))
     377        (is (= (length (associations-of goethe writer written-by written erlkoenig)) 1))
     378        (is-false (associations-of goethe writer written-by written instance))
     379        (is-false (associations-of goethe writer written-by instance erlkoenig))
     380        (is (= (length (associations-of goethe instance nil nil nil)) 1))
     381        (is-false (associations-of goethe writer written-by written erlkoenig
     382                                   :other-role-player-is-type t))
     383        (is (= (length (associations-of  goethe writer written-by written poem
     384                                         :other-role-player-is-type t)) 2))))))
     385
     386
     387(test test-roles-by-type
     388  "Tests the function roles-by-type bound to TopicC and AssociationC."
     389  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     390    (with-revision 0
     391      (let ((goethe (get-item-by-id "goethe"))
     392            (writer (get-item-by-id "writer"))
     393            (written (get-item-by-id "written"))
     394            (instance (get-item-by-psi *instance-psi*))
     395            (assoc (get-item-by-item-identifier "written-by-erlkoenig-goethe")))
     396        (is-true goethe)
     397        (is-true writer)
     398        (is-true written)
     399        (is-true instance)
     400        (is-true assoc)
     401        (is (= (length (roles-by-type goethe writer)) 3))
     402        (is (= (length (roles-by-type goethe nil)) 4))
     403        (is (= (length (roles-by-type goethe instance)) 1))
     404        (is-false (roles-by-type goethe written))
     405        (is (= (length (roles-by-type assoc writer)) 1))
     406        (is (eql writer (instance-of (first (roles-by-type assoc writer)))))
     407        (is (= (length (roles-by-type assoc nil)) 2))))))
     408
     409
     410(test test-roles-by-player
     411  "Tests the function roles-by-player."
     412  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     413    (with-revision 0
     414      (let ((goethe (get-item-by-id "goethe"))
     415            (writer (get-item-by-id "writer"))
     416            (written (get-item-by-id "written"))
     417            (instance (get-item-by-psi *instance-psi*))
     418            (assoc (get-item-by-item-identifier "written-by-erlkoenig-goethe"))
     419            (author (get-item-by-id "author")))
     420        (is-true goethe)
     421        (is-true author)
     422        (is-true writer)
     423        (is-true written)
     424        (is-true instance)
     425        (is-true assoc)
     426        (is (= (length (roles-by-player assoc goethe)) 1))
     427        (is (eql goethe (player (first (roles-by-player assoc goethe)))))
     428        (is (= (length (roles-by-player assoc written)) 0))
     429        (is (= (length (roles-by-player assoc nil)) 2))
     430        (is (= (length (roles-by-player assoc author :role-player-is-type t))
     431               1))
     432        (is-false (roles-by-player assoc goethe :role-player-is-type t))
     433        (is (eql goethe (player (first (roles-by-player
     434                                        assoc author
     435                                        :role-player-is-type t)))))))))
     436
     437
     438(test test-filter-associations-by-type
     439  "Tests the function roles-by-player."
     440  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     441    (with-revision 0
     442      (let ((written-by (get-item-by-id "written-by"))
     443            (born-in (get-item-by-id "born-in"))
     444            (assocs (get-all-associations)))
     445        (is-true written-by)
     446        (is-true assocs)
     447        (is-true born-in)
     448        (is (= (length (filter-associations-by-type assocs written-by)) 4))
     449        (is (> (length (filter-associations-by-type assocs nil)) (+ 4 2)))
     450        (is (= (length (filter-associations-by-type assocs born-in)) 2))))))
     451
     452
     453(test test-filter-associations-by-role
     454  "Tests the function roles-by-player."
     455  (with-fixture with-tm-filled-db ("data_base" *poems.xtm*)
     456    (with-revision 0
     457      (let ((written-by (get-item-by-id "written-by"))
     458            (born-in (get-item-by-id "born-in"))
     459            (written (get-item-by-id "written"))
     460            (writer (get-item-by-id "writer"))
     461            (place (get-item-by-id "place"))
     462            (goethe (get-item-by-id "goethe"))
     463            (frankfurt (get-item-by-id "frankfurt_am_main"))
     464            (assocs (get-all-associations))
     465            (author (get-item-by-id "author")))
     466        (is-true written-by)
     467        (is-true assocs)
     468        (is-true born-in)
     469        (is-true author)
     470        (is-true written)
     471        (is-true writer)
     472        (is-true place)
     473        (is-true frankfurt)
     474        (is (= (length (filter-associations-by-role assocs place frankfurt)) 1))
     475        (is (= (length (filter-associations-by-role assocs written nil)) 4))
     476        (is (= (length (filter-associations-by-role assocs written goethe)) 2))
     477        (is (= (length (filter-associations-by-role assocs writer nil)) 6))
     478        (is (= (length (filter-associations-by-role assocs nil goethe)) 4))
     479        (is (> (length (filter-associations-by-role assocs nil nil)) (+ 4 3)))
     480        (is-false (filter-associations-by-role assocs writer goethe
     481                                               :role-player-is-type t))
     482        (is (= (length (filter-associations-by-role assocs writer author
     483                                                    :role-player-is-type t))
     484               6))))))
     485       
     486
     487
    26488
    27489
  • TabularUnified trunk/src/unit_tests/unittests-constants.lisp

    r332 r337  
    3030           :*atom_test.xtm*
    3131           :*atom-conf.lisp*
     32           :*poems.xtm*
    3233           :*poems_light.rdf*
    3334           :*poems_light.xtm*
     
    101102   (asdf:find-component *unit-tests-component* "atom-conf")))
    102103
     104(defparameter *poems.xtm*
     105  (asdf:component-pathname
     106   (asdf:find-component *unit-tests-component* "poems.xtm")))
     107
    103108(defparameter *poems_light.rdf*
    104109  (asdf:component-pathname
Note: See TracChangeset for help on using the changeset viewer.