Changeset 381 for trunk/src/TM-SPARQL


Ignore:
Timestamp:
12/20/10 20:47:48 (14 years ago)
Author:
lgiessmann
Message:

TM-SPARQL: fixed the type-handling in FILTERs when there is given something like 'xyz'anyType

Location:
trunk/src/TM-SPARQL
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/TM-SPARQL/filter_wrappers.lisp

    r379 r381  
    1414
    1515
     16(defun filter-functions::normalize-value (value)
     17  "Returns the normalized value, i.e. if a literal
     18   is passed as '12'^^xsd:integer 12 is returned."
     19  (cond ((not (stringp value))
     20         value)
     21        ((or (base-tools:string-starts-with value "'")
     22             (base-tools:string-starts-with value "\""))
     23         (let* ((literal-result (tm-sparql::get-literal value))
     24                (literal-value
     25                 (cond ((or (base-tools:string-starts-with
     26                             (getf literal-result :literal) "\"\"\"")
     27                            (base-tools:string-starts-with
     28                             (getf literal-result :literal) "'''"))
     29                        (subseq (getf literal-result :literal) 3
     30                                (- (length (getf literal-result :literal)) 3)))
     31                       (t
     32                        (subseq (getf literal-result :literal) 1
     33                                (- (length (getf literal-result :literal)) 1)))))
     34                (given-datatype
     35                 (when (base-tools:string-starts-with
     36                        (getf literal-result :next-string) "^^")
     37                   (subseq (getf literal-result :next-string) 2))))
     38           (tm-sparql::cast-literal literal-value given-datatype)))
     39        (t
     40         value)))
     41
     42
    1643(defun filter-functions::not(x)
    17   (not x))
     44  (not (filter-functions::normalize-value x)))
    1845
    1946
    2047(defun filter-functions::one+(x)
    21   (1+ x))
     48  (1+ (filter-functions::normalize-value x)))
    2249
    2350
    2451(defun filter-functions::one-(x)
    25   (1- x))
     52  (1- (filter-functions::normalize-value x)))
    2653
    2754
    2855(defun filter-functions::+(x y)
    29   (+ x y))
     56  (+ (filter-functions::normalize-value x)
     57     (filter-functions::normalize-value y)))
    3058
    3159
    3260(defun filter-functions::-(x y)
    33   (- x y))
     61  (- (filter-functions::normalize-value x)
     62     (filter-functions::normalize-value y)))
    3463
    3564
    3665(defun filter-functions::*(x y)
    37   (* x y))
     66  (* (filter-functions::normalize-value x)
     67     (filter-functions::normalize-value y)))
    3868
    3969
    4070(defun filter-functions::/(x y)
    41   (/ x y))
     71  (/ (filter-functions::normalize-value x)
     72     (filter-functions::normalize-value y)))
    4273
    4374
    4475(defun filter-functions::or(x y)
    45   (or x y))
     76  (or (filter-functions::normalize-value x)
     77      (filter-functions::normalize-value y)))
    4678
    4779
    4880(defun filter-functions::and(x y)
    49   (and x y))
     81  (and (filter-functions::normalize-value x)
     82       (filter-functions::normalize-value y)))
    5083
    5184
    5285(defun filter-functions::=(x y)
    53   (cond ((and (stringp x) (stringp y))
    54          (string= x y))
    55         ((and (numberp x)( numberp y))
    56          (= x y))
    57         (t
    58          (eql x y))))
     86  (let ((local-x (filter-functions::normalize-value x))
     87        (local-y (filter-functions::normalize-value y)))
     88    (cond ((and (stringp local-x) (stringp local-y))
     89           (string= local-x local-y))
     90          ((and (numberp local-x)( numberp local-y))
     91           (= local-x local-y))
     92          (t
     93           (eql local-x local-y)))))
    5994
    6095
     
    65100
    66101(defun filter-functions::<(x y)
    67   (cond ((and (numberp x) (numberp y))
    68          (< x y))
    69         ((and (stringp x) (stringp y))
    70          (string< x y))
    71         ((and (typep x 'Boolean) (typep y 'Boolean))
    72          (and (not x) y))
    73         (t
    74          nil)))
     102  (let ((local-x (filter-functions::normalize-value x))
     103        (local-y (filter-functions::normalize-value y)))
     104    (cond ((and (numberp local-x) (numberp local-y))
     105           (< local-x local-y))
     106          ((and (stringp local-x) (stringp local-y))
     107           (string< local-x local-y))
     108          ((and (typep local-x 'Boolean) (typep local-y 'Boolean))
     109           (and (not local-x) local-y))
     110          (t
     111           nil))))
    75112
    76113
     
    93130
    94131(defun filter-functions::regex(str pattern &optional flags)
    95   (declare (Ignorable flags))
    96   (let* ((case-insensitive (when (find #\i flags) t))
    97          (multi-line (when (find #\m flags) t))
    98          (single-line (when (find #\s flags) t))
     132  (let* ((local-flags (filter-functions::normalize-value flags))
     133         (case-insensitive (when (find #\i local-flags) t))
     134         (multi-line (when (find #\m local-flags) t))
     135         (single-line (when (find #\s local-flags) t))
    99136         (local-pattern
    100           (if (find #\x flags)
     137          (if (find #\x local-flags)
    101138              (base-tools:string-replace
    102139               (base-tools:string-replace
    103140                (base-tools:string-replace
    104                  (base-tools:string-replace pattern (string #\newline) "")
     141                 (base-tools:string-replace
     142                  (filter-functions::normalize-value pattern)
     143                  (string #\newline) "")
    105144                 (string #\tab) "") (string #\cr) "") " " "")
    106               pattern))
     145              (filter-functions::normalize-value pattern)))
    107146         (scanner
    108147          (ppcre:create-scanner local-pattern
  • TabularUnified trunk/src/TM-SPARQL/sparql.lisp

    r379 r381  
    10111011
    10121012
     1013(defun cast-literal (literal-value literal-type)
     1014  "A helper function that casts the passed string value of the literal
     1015   corresponding to the passed literal-type."
     1016  (declare (String literal-value literal-type))
     1017  (cond ((string= literal-type *xml-string*)
     1018         literal-value)
     1019        ((string= literal-type *xml-boolean*)
     1020         (when (and (string/= literal-value "false")
     1021                    (string/= literal-value "true"))
     1022           (error (make-condition
     1023                   'sparql-parser-error
     1024                   :message (format nil "Could not cast from ~a to ~a"
     1025                                    literal-value literal-type))))
     1026         (if (string= literal-value "false")
     1027             nil
     1028             t))
     1029        ((string= literal-type *xml-integer*)
     1030         (handler-case (parse-integer literal-value)
     1031           (condition ()
     1032             (error (make-condition
     1033                   'sparql-parser-error
     1034                   :message (format nil "Could not cast from ~a to ~a"
     1035                                    literal-value literal-type))))))
     1036        ((or (string= literal-type *xml-decimal*) ;;both types are
     1037             (string= literal-type *xml-double*)) ;;handled the same way
     1038         (let ((value (read-from-string literal-value)))
     1039           (unless (numberp value)
     1040             (error (make-condition
     1041                   'sparql-parser-error
     1042                   :message (format nil "Could not cast from ~a to ~a"
     1043                                    literal-value literal-type))))
     1044           value))
     1045        (t ; return the value as a string
     1046         literal-value)))
     1047
     1048
    10131049(defmethod initialize-instance :after ((construct SPARQL-Query) &rest args)
    10141050  (declare (ignorable args))
  • TabularUnified trunk/src/TM-SPARQL/sparql_filter.lisp

    r380 r381  
    122122                   construct filter-string-functions original-filter-string))
    123123      (parse-group construct next-query))))
    124   ;;TODO: implement
    125   ;; *add ^^datatype to the object-literal-results
    126   ;; *implement to-literal => CharacteristicC => \"...\"^^datatype => use for tm-sparql
    127   ;; *implement str correctly
    128124
    129125
  • TabularUnified trunk/src/TM-SPARQL/sparql_parser.lisp

    r379 r381  
    218218
    219219
    220 (defun cast-literal (literal-value literal-type)
    221   "A helper function that casts the passed string value of the literal
    222    corresponding to the passed literal-type."
    223   (declare (String literal-value literal-type))
    224   (cond ((string= literal-type *xml-string*)
    225          literal-value)
    226         ((string= literal-type *xml-boolean*)
    227          (when (and (string/= literal-value "false")
    228                     (string/= literal-value "true"))
    229            (error (make-condition
    230                    'sparql-parser-error
    231                    :message (format nil "Could not cast from ~a to ~a"
    232                                     literal-value literal-type))))
    233          (if (string= literal-value "false")
    234              nil
    235              t))
    236         ((string= literal-type *xml-integer*)
    237          (handler-case (parse-integer literal-value)
    238            (condition ()
    239              (error (make-condition
    240                    'sparql-parser-error
    241                    :message (format nil "Could not cast from ~a to ~a"
    242                                     literal-value literal-type))))))
    243         ((or (string= literal-type *xml-decimal*) ;;both types are
    244              (string= literal-type *xml-double*)) ;;handled the same way
    245          (let ((value (read-from-string literal-value)))
    246            (unless (numberp value)
    247              (error (make-condition
    248                    'sparql-parser-error
    249                    :message (format nil "Could not cast from ~a to ~a"
    250                                     literal-value literal-type))))
    251            value))
    252         (t ; return the value as a string
    253          literal-value)))
    254          
    255 
    256220(defgeneric separate-literal-lang-or-type (construct query-string)
    257221  (:documentation "A helper function that returns (:next-query string
Note: See TracChangeset for help on using the changeset viewer.