Coverage report: /development/source/library/org/datagraph/spocq-shard/src/core/encoding/rdf-json.lisp

KindCoveredAll%
expression0119 0.0
branch04 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: org.datagraph.spocq.implementation; -*-
2
 
3
 (in-package :org.datagraph.spocq.implementation)
4
 
5
 ;;; json serializer
6
 ;;; http://www.w3.org/TR/rdf-sparql-XMLres/
7
 
8
 ;;; reading in in json.lsip
9
 
10
 ;;; solution field writing when as json, require just s-p-o
11
 
12
 (defparameter *encode-json-term.type-literals* t)
13
 
14
 (defgeneric write-rdf+json (results stream)
15
   (:documentation "Encode the result field to the stream as rdf+json (or pplication/json.
16
  If results are a list of lists, then the first element is a header, which specifies the variable names and
17
  the remaining entries aer solutions sets. if a variable is unbound, the solution element is etf:nil.
18
  Otherwise, the results can be a matrix, in which case the dimensionality is explicit.")
19
   
20
   (:method ((result symbol) (stream t))
21
     (log-warn "json result is not a graph: ~s." result)
22
     0)
23
   
24
   (:method ((results list-solution-field) (stream t))
25
     (write-rdf+json (cons (list-solution-field-dimensions results)
26
                          (list-solution-field-solutions results))
27
                    stream))
28
   
29
   (:method ((results cons) (stream t))
30
     (let* ((dimensions (first results))
31
            (solutions (rest results)))
32
 
33
       (cond ((equal dimensions *construct-dimensions*)
34
              )
35
             ((= (length dimensions) (length *construct-dimensions*))
36
              ;; (log-warn "json dimensions assumed: dimensions ~s." dimensions)
37
              ;;(setf dimensions *construct-dimensions*)
38
              )
39
             (t
40
              (log-warn "json result is not a graph: dimensions ~s." dimensions)
41
              (return-from write-rdf+json 0)))
42
 
43
       (let* ((cache (make-hash-table :test #'equalp))
44
             (subjects (remove-duplicates (mapcar #'first solutions) :test #'equalp))
45
             ;(root-subjects (set-difference subjects (remove-duplicates (mapcar #'third solutions) :test #'equalp)))
46
             (count 0)
47
             (*encode-json-term.type-literals* nil))   ; suppress type wrapper
48
         ;; no offset limit handling
49
         (loop for (subject predicate object) in solutions
50
               for entry = (gethash subject cache)
51
               do (incf count)
52
               if entry
53
               do (push object (getf (rest entry) predicate))
54
               else do (setf (gethash subject cache)
55
                             `(,subject ,predicate (,object))))
56
         (labels ((emit-objects (objects)
57
                    (cond ((rest objects)
58
                           (write-char #\[ stream)
59
                           (loop for first = t then nil
60
                                 for object in objects
61
                                 do (progn (unless first (write-string ", " stream))
62
                                           (emit-object object)))
63
                           (write-char #\] stream))
64
                          (t
65
                           (emit-object (first objects)))))
66
                  (emit-object (object)
67
                    (multiple-value-bind (cache-entry present-p)
68
                                         (gethash object cache)
69
                      (if present-p
70
                          (cond (cache-entry
71
                                 (setf (gethash object cache) nil)
72
                                 (write-char #\{ stream)
73
                                 (write-string "\"ID\": " stream)
74
                                 (encode-json-term object stream)
75
                                 (loop for (property objects) on (rest cache-entry) by #'cddr
76
                                   do (progn (format stream ", \"~a\": "
77
                                                     (if (symbolp property)
78
                                                         (symbol-name property)
79
                                                         (iri-lexical-form property)))
80
                                        (emit-objects objects)))
81
                                 (write-char #\} stream))
82
                                (t ; already emitted
83
                                 (encode-json-term object stream)))
84
                          (encode-json-term object stream)))))
85
           ;; now emit all roots
86
           ;(emit-objects root-subjects)
87
           (emit-objects subjects)
88
           )
89
       (incf-stat *statements-returned* count))))
90
 
91
   (:method ((results boolean-generator) (stream t))
92
     (log-warn "attempt to encode a boolean result as json")
93
     0)
94
   
95
   (:method ((results solution-generator) (stream t))
96
     (log-warn "attempt to encode a solution generator as rdf+json")
97
     0)
98
 
99
   (:method ((result-field true-matrix-field) (stream t))
100
     (log-warn "attempt to encode a boolean result as rdf+json")
101
     0)
102
 
103
   (:method ((result-field false-matrix-field) (stream t))
104
     (log-warn "attempt to encode a boolean result as rdf+json")
105
     0)
106
 
107
   (:method ((result-field matrix-field) (stream t))
108
     (log-warn "attempt to encode a matrix field as rdf+json")))
109
 
110
 
111
 
112
 
113
 
114
 
115
 
116
 
117
 (defmethod send-response-message (operation (message-body t) (stream t) (content-type mime:rdf/json))
118
   "Given a MESSAGE, and a STREAM with the application/rdf+xml CONTENT-TYPE, encode as an xml rdf document"
119
   (when *encoding-trace-output*
120
     (setf stream (make-broadcast-stream *encoding-trace-output* stream)))
121
   (let ((*package* *spocq-reader-package*))
122
     (write-rdf+json message-body stream)))
123
 
124
 
125
 #|
126
 (write-rdf+json *rdf+xml=test=field* *trace-output*)
127
 |#