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

KindCoveredAll%
expression0243 0.0
branch012 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
 ;;; adapt application/turtle-star parser from turtle
6
 ;;; this add the group-Constructor, which extracts formula, computes
7
 ;;; canonical dientifiers and rearranges the triple lists into quads
8
 
9
 ;;; see 
10
 ;;;  http://www.w3.org/TR/2014/REC-turtle-20140225/#sec-grammar
11
 ;;;
12
 
13
 (defparameter *turtle-star-bnf* "
14
 [[1]] turtleDoc ::= statement*
15
 [[2]] statement ::= directive | ( triples '.' )
16
 [[3]] directive ::= prefixID | base | BaseDecl | PrefixDecl
17
 [[4]] prefixID ::= '@prefix' PNAME_NS NAMESPACE_REF '.'
18
 [[5]] base ::= '@base' NAMESPACE_REF '.'
19
 [[5s]] BaseDecl ::= 'BASE' NAMESPACE_REF
20
 [[6s]] PrefixDecl ::= 'PREFIX' PNAME_NS NAMESPACE_REF
21
 [[6]] triples ::= ( subject PropertyListNotEmpty ) | ( BlankNodePropertyList PropertyListNotEmpty? )
22
 //[[7]] PropertyListNotEmpty ::= verb objectList (';' (verb objectList)?)*
23
 [[8]] objectList ::= object (',' object)*
24
 [[9]] verb ::= predicate | 'a'
25
 //[[10]] subject ::= iri | BlankNode | collection
26
 [[10]] subject ::= iri | BlankNode | collection | group
27
 [[10a]] group ::= ( '{' triplesList '}' ) | ( '<<' subject verb object '>>' )
28
 [[10b]] triplesList ::= triples ( '.' triplesList )?  // final '.' is present in [[2]]
29
 [[11]] predicate ::= iri
30
 [[12]] object ::= iri | BlankNode | collection | BlankNodePropertyList | literal
31
 [[13]] literal ::= RDFLiteral | NumericLiteral | BooleanLiteral
32
 [[14]] BlankNodePropertyList ::= '[' PropertyListNotEmpty ']'
33
 [[15]] collection ::= '(' object* ')'
34
 //[16]] NumericLiteral ::= INTEGER | DECIMAL | DOUBLE
35
 [[128s]] RDFLiteral ::= String (LANGTAG | ( '^^' iri ))?
36
 [[133s]] BooleanLiteral ::= 'true' | 'false'
37
 //[17]] String ::= STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | STRING_LITERAL_LONG_SINGLE_QUOTE | STRING_LITERAL_LONG_QUOTE
38
 [[135s]] iri ::= IRI_REF | PrefixedName
39
 [[136s]] PrefixedName ::= PNAME_LN | PNAME_NS
40
 [[137s]] BlankNode ::= ( '_:' BLANK_NODE_LABEL) | ANON
41
 
42
 // from sparql
43
 [[72]] PropertyListNotEmpty ::= VerbObjectList ( ';' VerbObjectList? )* 
44
 [[72a]] VerbObjectList ::= verb objectList
45
 [[119]] NumericLiteral ::= NumericLiteralUnsigned | NumericLiteralPositive | NumericLiteralNegative
46
 [[120]] NumericLiteralUnsigned ::= INTEGER | DECIMAL | DOUBLE
47
 [[121]] NumericLiteralPositive ::= INTEGER_POSITIVE | DECIMAL_POSITIVE | DOUBLE_POSITIVE
48
 [[122]] NumericLiteralNegative ::= INTEGER_NEGATIVE | DECIMAL_NEGATIVE | DOUBLE_NEGATIVE
49
 [[124]] String ::= STRING_LITERAL1 | STRING_LITERAL2 | STRING_LITERAL_LONG1 | STRING_LITERAL_LONG2
50
 [[128]] IRI_REF ::= '<' IRI_NAMESTRING? '>'
51
 [[128a]] NAMESPACE_REF ::= '<' IRI_NAMESTRING? '>'
52
 [[152]] ANON ::= '['  ']'
53
 "
54
   "A turtle grammar which re-uses several productions from sparql in order to reuse the respectve constructor logic" )
55
 
56
 (defpackage :org.datagraph.turtle-star
57
   (:use :common-lisp
58
         :org.datagraph.spocq.implementation
59
         :de.setf.utility)
60
   (:nicknames :odts)
61
   (:import-from :sparql-1-0-4
62
                 "ANON-Constructor"
63
                 "BaseDecl-Constructor"
64
                 "BlankNode-Constructor"
65
                 "BlankNodePropertyList-Constructor"
66
                 "BLANK_NODE_LABEL-Constructor"
67
                 "BooleanLiteral-Constructor"
68
                 "LANGTAG-Constructor"
69
                 "NumericLiteral-Constructor"
70
                 "PrefixDecl-Constructor"
71
                 "IRI_NAMESTRING-Constructor"
72
                 "IRI_REF-Constructor"
73
                 "NAMESPACE_REF-Constructor"
74
                 "PNAME_NS-Constructor"
75
                 "PrefixedName-Constructor"
76
                 "PropertyListNotEmpty-Constructor"
77
                 "RDFLiteral-Constructor"
78
                 "String-Constructor"
79
                 "VerbObjectList-Constructor")
80
   (:import-from :spocq.i
81
                 :*turtle-star-bnf*
82
                 :IS-IRI_NAMESTRING))
83
 
84
 (defun odts::|turtleDoc-Constructor| (statement*)
85
   (reduce #'append statement* :from-end t))
86
 
87
 (defun odts::|base-Constructor| (IRI_REF)
88
   (sparql-1-0-4::|BaseDecl-Constructor| IRI_REF))
89
 
90
 (defun odts::|collection-Constructor| (object*)
91
   (if object* ; can be empty
92
       (let* ((tail '|rdf|:|nil|)
93
              (triples nil))
94
         (dolist (node object*)
95
           (let ((cell (ecase *nondistinguished-marker-type*
96
                         (:blank-node (cons-blank-node "b"))
97
                         (:variable (cons-variable "TTL")))))
98
             (when (consp node)
99
               (setf triples (append node triples))
100
               (setf node (second (first node))))
101
             (push (list 'spocq.a:|triple| cell '|rdf|:|rest| tail) triples)
102
             (push (list 'spocq.a:|triple| cell '|rdf|:|first| node) triples)
103
             (setf tail cell)))
104
         triples)
105
       '|rdf|:|nil|))
106
 ;;; (parse-turtle "[ <http://host> () ] .")
107
 ;;; (parse-turtle "[ a _:Class ; <http://consequent> [ a _:OtherClass  ; <http://steps> () ] ]."))
108
 
109
 (defun odts::|directive-Constructor| (prefixID base sparqlPrefix sparqlBase)
110
   (or prefixID base sparqlPrefix sparqlBase))
111
 
112
 (defun odts::|group-Constructor| (object subject triplesList verb)
113
   "Given a term triple, check the subject and object to distinguish
114
   atomic terms from formula graphs. If the reduction argument is a triple list,
115
   that list is already flattened with references to any  formula contained.
116
   isolate the formula graph as its triples, compute their canonical signature and
117
   augment them to quads with thie graph name. Then recombine combine with accompanying
118
   formula and return the full flattened quad list.
119
   Otherwise, it is a (subject predicate object), of which the subject and/or predicate
120
   may be to be replaced by the respective graph identifier."
121
   (if triplesList
122
       ;; given triples, canonicalize and transform into quads
123
       (let* ((triples (remove-if-not #'triple-form-p triplesList))
124
              (quads (remove-if #'triple-form-p triplesList))
125
              (iri (compute-formula-identifier `(spocq.a:|bgp| ,@triples))))
126
         (append (loop for triple in triples
127
                   collect `(spocq.a:|quad| ,@(rest triple) ,iri))
128
                 quads))
129
       (if (or (consp subject) (consp object))
130
           (let ((graphs ()))
131
             (flet ((normalize-term (term-or-graph)
132
                      (cond ((consp term-or-graph)
133
                             (setf graphs (append graphs term-or-graph))
134
                             (statement-context (first term-or-graph)))
135
                            (t
136
                             term-or-graph))))
137
               (let* ((triple (list (normalize-term subject) verb (normalize-term object)))
138
                      (iri (compute-formula-identifier `(spocq.a:|bgp| ,triple))))
139
                 (cons `(spocq.a:|quad| ,@triple ,iri)
140
                       graphs))))
141
           (let* ((triple (list subject verb object))
142
                  (iri (compute-formula-identifier `(spocq.a:|bgp| ,triple))))
143
             (list `(spocq.a:|quad| ,@triple ,iri))))))
144
 
145
 (defun odts::|iri-Constructor| (IRI_REF PrefixedName)
146
   (or IRI_REF PrefixedName))
147
 
148
 (defun odts::|literal-Constructor| (BooleanLiteral NumericLiteral RDFLiteral)
149
   (or BooleanLiteral NumericLiteral RDFLiteral))
150
 
151
 (defun odts::|object-Constructor| (BlankNode BlankNodePropertyList collection iri literal)
152
   (or BlankNode BlankNodePropertyList collection iri literal))
153
 
154
 (defun odts::|objectList-Constructor| (object*)
155
   (reverse object*))
156
 
157
 (defun odts::|predicate-Constructor| (iri)
158
   iri)
159
 
160
 (defun odts::|prefixID-Constructor| (IRI_REF_NAMESTRING PNAME_NS)
161
   (sparql-1-0-4::|PrefixDecl-Constructor| IRI_REF_NAMESTRING PNAME_NS))
162
 
163
 (defun odts::|subject-Constructor| (BlankNode collection group iri)
164
   (or BlankNode collection group iri))
165
 
166
 (defun odts::|statement-Constructor| (directive triples)
167
   (declare (ignore directive))
168
   triples)
169
 
170
 (defun odts::|triplesList-Constructor| (triples triplesList)
171
   (append triples triplesList))
172
 
173
 (defun odts::|triples-Constructor| (BlankNodePropertyList PropertyListNotEmpty subject)
174
   (typecase subject
175
     (null
176
      (let ((subject-term (second (first BlankNodePropertyList))))
177
        (append BlankNodePropertyList
178
                (loop for plne-element in PropertyListNotEmpty
179
                  collect (list* 'spocq.a:|triple| subject-term plne-element)))))
180
     (cons
181
      ;; given a collection as subject, use the first statements susbject
182
      (let ((statement (first subject)))
183
        (if (triple-form-p statement)
184
            (let ((subject-term (second statement)))
185
              (append subject
186
                      (loop for plne-element in PropertyListNotEmpty
187
                        collect (list* 'spocq.a:|triple| subject-term plne-element))))
188
            (let ((subject-term (statement-context statement)))
189
              (append subject
190
                      (loop for plne-element in PropertyListNotEmpty
191
                        collect (list* 'spocq.a:|triple| subject-term plne-element)))))))
192
     (t
193
       (loop for plne-element in PropertyListNotEmpty
194
                  collect (list* 'spocq.a:|triple| subject plne-element)))))
195
 
196
 (defun odts::|verb-Constructor| (predicate)
197
   (or predicate |rdf|:|type|))
198
 
199
 
200
 (defun parse-turtle-star (string &key (start 0) (end (length string)) (start-name 'odts::|turtleDoc|))
201
   (let ((*max-input-index* 0)
202
         (atnp:*atn-term* nil)
203
         (*namespace-bindings* *namespace-bindings*))
204
     (multiple-value-bind (tokens byte-offsets line-offsets)
205
                          (tokenize-turtle string :start start :end end)
206
       (when tokens
207
         (multiple-value-bind (result index success)
208
                              (funcall 'odts::|turtleDoc-Parser| tokens :start-name start-name)
209
           (if success
210
               (values result tokens index)
211
               (flet ((_aref (array index)
212
                        (when (and (integerp index(< index (length array))) (aref array index))))
213
                 (spocq.e::message-syntax-error :expression string
214
                                                :token (_aref tokens *max-input-index*)
215
                                                :byte-offset (_aref byte-offsets *max-input-index*)
216
                                                :line-offset (_aref line-offsets *max-input-index*)))))))))
217
 
218
 
219
 (in-package :odts)
220
 (bnfp:compile-atn-system  spocq.i::*turtle-star-bnf*
221
                           :execute nil ;; compile in build :  t
222
                           :compile nil
223
                           :token-package (find-package :spocq.s)
224
                           :source-package (find-package :odts)
225
                           :source-pathname #p"/development/source/library/org/datagraph/spocq-dev/src/core/encoding/turtle-star-grammar.lisp"
226
                           :input-function 'sparql-1-0-4::input-reference
227
                           :input-eof-function 'sparql-1-0-4::input-eof?
228
                           :ambiguous t
229
                           :trace nil)
230
 
231
 #|
232
 (load (compile-file #p"/development/source/library/org/datagraph/spocq/src/core/encoding/turtle-star-grammar.lisp"
233
                     :output-file "turtle-star-grammar.fasl"))
234
 
235
 ;;; turtle:
236
 (spocq.i::parse-turtle-star "
237
 prefix : <http://example.org#>
238
 [ a :Query ;
239
   :name 'Simple Query';
240
   :steps ( [ a :Decode; :location _:query] 
241
            [ a :Bind ]
242
            [ a :Project ] 
243
            [ a :Encode ] 
244
            ) ] .
245
 ")
246
 
247
 ;; turtle-star
248
 
249
 (spocq.i::parse-turtle-star "
250
 prefix : <http://example.org#>
251
 {_:q a :Query }
252
   :name 'Simple Query' .
253
 ")
254
 
255
 (spocq.i::parse-turtle-star "
256
 prefix : <http://example.org#>
257
 {_:q a :Query }
258
   :name 'Simple Query';
259
   :steps ( [ a :Decode; :location _:query] 
260
            ) .
261
 ")
262
 
263
 |#