Coverage report: /development/source/library/org/datagraph/spocq-shard/src/spocq-server/streams/model.lisp
| Kind | Covered | All | % |
| expression | 0 | 506 | 0.0 |
| branch | 0 | 30 | 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.server.implementation; -*-
2
;;; (load #p"patches/model.lisp")
4
(in-package :org.datagraph.spocq.server.implementation)
6
(:documentation "RDF stream semantics.
8
This file implements rdf stream processing model as a specialized repository
9
and operators which manage the relation between the assertion content and
12
the model and operations evolved from the original rdf stream processing definitions
13
(http://streamreasoning.github.io/RSP-QL/RSP_Requirements_Design_Document/)
14
through a draft of a version extended for transactional stores.
17
the core model is the 'element' which associates graph data with an
18
'application' and/or 'transaction' time. the element is modeled as a
19
sequence of triple with associated time and transaction identifiers.
23
(defpackage :rdf-stream-processing
28
:associative-structure
33
:multivalent-structure
37
:element-temporal-predicate
38
:element-temporal-identifier
39
:element-transactional-predicate
40
:element-transactional-identifier
51
:structure-associative
72
:is-persistent-structure
115
:store-transformation
133
(defun rsp:format-term (stream object &optional colon at)
134
(declare (ignore colon at))
135
(spocq.i::encode-turtle-object object stream))
139
(defgeneric apply-map (map name)
140
(:method ((map function) name) (funcall map name))
141
(:method ((map list) name) (rest (assoc name map :test #'equalp)))
142
(:method ((map hash-table) name) (gethash name map)))
144
(defgeneric map-content (function context) )
146
(defgeneric exclude (map name)
147
(:method ((map list) name) (remove name map :test #'equalp :key #'first)))
149
(defgeneric augment (map name value)
150
(:method ((map list) name value) (acons name value map)))
152
(defgeneric restrict (map names)
153
(:method ((map list) names)
154
(flet ((test-name (name) (find name names :test #'equalp)))
155
(declare (dynamic-extent #'test-name))
156
(remove-if #'test-name map :key #'first))))
158
(defgeneric rsp:equal (object1 object2)
159
(:method ((o1 t) (o2 t))
162
(defun set-equal (m1 m2 &key (test #'equal))
163
(= (length (intersection m1 m2 :test test))
169
(defun rsp:triple (s p o)
170
`(spocq.a:|triple| ,s ,p ,o))
172
(defclass rsp:structure ()
174
(:documentation "A structure is the abstract root for all combinations of
175
graphs - that is, statements, and names. It comprises associative and
176
sequential combinations, in variations and order hierarchies:"))
178
(defclass rsp:associative-structure (rsp:structure)
180
(:documentation "An associative stricture is an arrangement of names and graphs.
181
It begins at order-0, which is a simple graph, progresses to order-1, which is a simple
182
dataset and evolves through higher orders to represent repositories and stores."))
184
(defclass rsp:container ()
185
((content :initform nil :initarg :content
186
:accessor rsp:content))
187
(:documentation "A container encapsulates unspecified content."))
189
(defclass rsp:graph (rsp:associative-structure rsp:container)
190
((content :accessor rsp:statements))
191
(:documentation "A graph comprises a set of RDF statements (triples)."))
193
(defclass rsp:associative (rsp:associative-structure)
194
((names :initform nil
197
(name-map :initform nil
199
:accessor rsp:name-map)))
201
(defclass rsp:associative-tree (rsp:associative)
204
(defclass rsp:dataset (rsp:associative-tree)
207
(defclass rsp:sequence (rdf-stream-container)
210
(defclass rsp:multivalent-structure (rsp:structure)
211
((associative :initform nil
212
:initarg :associative
213
:accessor rsp:structure-associative)
214
(sequential :initform nil
216
:accessor rsp:structure-sequential)))
218
(defclass rsp:time-series-graph (rsp:dataset)
221
(defclass rsp:persistent-structure (rsp:structure)
222
((static-part :initform nil
223
:accessor rsp:static-part)
224
(dynamic-part :initform nil
225
:accessor rsp:dynamic-part))
226
(:documentation "A parsistent structure combines a static and a dynamic part
227
as the working representation of versioned streaming data.
228
There are two specialized forms, a repository, which adds a time stamp predicate
229
and a store, which comprises repositories."))
231
(defclass rsp:repository (rsp:persistent-structure)
232
((timestamp-predicate
233
:initform |prov|:|atTime|
234
:accessor rsp:timestamp-predicate))
235
(:documentation "A repository adds a timestamp predicate to a persistent
236
structure to associate temporal values with its versioned content."))
238
(defclass rsp:store (rsp:persistent-structure)
240
(:documentation "A repository is a versioned collection of repositories."))
242
(defclass rsp:stream (rsp:graph)
248
(defmethod rsp:equal ((s1 rsp:structure) (s2 rsp:structure))
254
(defun rsp:graph (content)
255
(make-instance 'rsp:graph :content content))
257
(defmethod print-object ((object rsp:graph) stream)
258
(print-unreadable-object (object stream :identity t :type t)
259
(format stream " x~d" (length (rsp:content object)))))
261
(defmethod rsp:equal ((s1 rsp:graph) (s2 rsp:graph))
262
(and (call-next-method)
263
(= (length (intersection (rsp:content s1) (rsp:content s2)
265
(length (rsp:content s1))
266
(length (rsp:content s2)))))
271
(defun rsp:associative (names name-map)
272
(make-instance 'rsp:associative
273
:names names :name-map name-map))
275
(defmethod rsp:equal ((s1 rsp:associative) (s2 rsp:associative))
276
(and (call-next-method)
277
(set-equal (rsp:names s1) (rsp:names s2))
278
(set-equal (rsp:name-map s1) (rsp:name-map s2))))
280
(defgeneric rsp:is-associative (object)
281
(:method ((object rsp:associative)) t)
282
(:method ((object t)) nil))
284
(defmethod apply-map ((structure rsp:associative) name)
285
(apply-map (rsp:name-map structure) name))
287
(defgeneric rsp:named-content (name associative)
288
(:method ((name t) (object rsp:associative))
289
(apply-map (rsp:name-map object) name)))
291
(defgeneric rsp:default-part (structure)
292
(:method ((structure t))
294
(:method ((structure rsp:associative))
295
(apply-map (rsp:name-map structure) <urn:dydra:default>)))
297
(defgeneric rsp:named-part (structure)
298
(:method ((structure t))
300
(:method ((structure rsp:associative))
301
(rsp:associative (rsp:names structure)
302
(exclude (rsp:name-map structure) <urn:dydra:default>))))
304
(defgeneric rsp:cardinality (structure)
305
(:method ((object t))
307
(:method ((structure rsp:associative))
308
(length (rsp:names structure))))
310
(defun rsp:solitary (name structure)
311
(rsp:associative (list name) (acons name structure nil)))
313
(defgeneric rsp:is-solitary (object)
314
(:method ((object t))
316
(:method ((structure rsp:associative))
317
(when (rsp:is-associative structure)
318
(and (= (rsp:cardinality structure) 1)
319
(null (rsp:default-part structure))))))
324
(defun rsp:associative-tree (names name-map)
325
(make-instance 'rsp:associative-tree
326
:names names :name-map name-map))
328
(defgeneric rsp:is-associative-tree (object)
329
(:method ((object rsp:associative-tree)) t)
330
(:method ((object t)) nil))
335
(defun rsp:dataset (names name-map)
336
(make-instance 'rsp:dataset
337
:names names :name-map name-map))
339
(defgeneric rsp:is-dataset (object)
340
(:method ((object rsp:dataset)) t)
341
(:method ((object t)) nil))
344
;;; time-series-graph
346
(defun rsp:tree-union (graph triple)
349
(defun rsp:time-series-graph (default-graph timestamp-triple named-graph)
350
(destructuring-bind (s p o) (spocq.i::triple-terms timestamp-triple)
352
(let ((name (cons-blank-node)))
353
(rsp:multivalent-structure (rsp:associative (list name)
354
(rsp:multivalent-structure
355
(rsp:associative (list s)
356
(acons name named-graph nil))
357
(rsp:tree-union default-graph timestamp-triple)))
358
(rsp:graph (list (rsp:triple name <http://www.w3.org/ns/rsp#usesTimestampPredicate> p)))))))
360
(defgeneric rsp:is-time-series-graph (object)
361
(:method ((object rsp:time-series-graph)) t)
362
(:method ((object t)) nil))
366
(defun rsp:sequence (content)
367
(make-instance 'rsp:sequence :content content))
369
(defgeneric rsp:is-sequence (object)
370
(:method ((object rsp:sequence))
372
(:method ((object t))
376
(defgeneric rsp:is-member-of (element sequence)
377
(:method (element (sequence rsp:sequence))
378
(find element (rsp:content sequence))))
380
(defgeneric rsp:has-greatest (sequence)
381
(:method ((sequence rsp:sequence))
382
(not (null (rsp:content sequence)))))
384
(defgeneric rsp:has-least (sequence)
385
(:method ((sequence rsp:sequence))
386
(not (null (rsp:content sequence)))))
388
(defgeneric rsp:precedes (element1 element2 sequence)
389
(:method (element1 element2 (sequence rsp:sequence))
390
(rsp:precedes element1 element2 (rsp:content sequence)))
391
(:method (element1 element2 (sequence t))
392
(equal element1 (rsp:predecessor element2 sequence))))
394
(defgeneric rsp:follows (element1 element2 sequence)
395
(:method (element1 element2 (sequence rsp:sequence))
396
(rsp:follows element1 element2 (rsp:content sequence)))
397
(:method (element1 element2 (sequence t))
398
(equal element1 (rsp:successor element2 sequence))))
400
(defgeneric rsp:least (sequence)
401
(:method ((sequence rsp:sequence))
402
(rsp:least (rsp:content sequence)))
403
(:method ((sequence vector))
404
(when (plusp (length sequence))
406
(:method ((sequence list))
409
(defgeneric rsp:greatest (sequence)
410
(:method ((sequence rsp:sequence))
411
(rsp:greatest (rsp:content sequence)))
412
(:method ((sequence vector))
413
(when (plusp (length sequence))
414
(aref sequence (1- (length sequence)))))
415
(:method ((sequence list))
416
(first (last sequence))))
418
(defgeneric rsp:is-least-of (element sequence)
419
(:method (element (sequence rsp:sequence))
420
(rsp:is-least-of element (rsp:content sequence)))
421
(:method (element (sequence t))
422
(eql element (rsp:least sequence))))
424
(defgeneric rsp:is-greatest-of (element sequence)
425
(:method (element (sequence rsp:sequence))
426
(rsp:is-greatest-of element (rsp:content sequence)))
427
(:method (element (sequence t))
428
(eql element (rsp:greatest sequence))))
430
(defgeneric rsp:predecessor (element sequence)
431
(:method (element (sequence rsp:sequence))
432
(rsp:predecessor element (rsp:content sequence)))
433
(:method (element (sequence vector))
434
(let ((position (position element sequence)))
435
(when (and position (> position 0))
436
(elt sequence (1- position)))))
437
(:method (element (sequence list))
438
(loop with predecessor = nil
439
for member in sequence
440
if (equal element member)
442
else do (setf predecessor member))))
444
(defgeneric rsp:successor (element sequence)
445
(:method (element (sequence rsp:sequence))
446
(rsp:successor element (rsp:content sequence)))
447
(:method (element (sequence vector))
448
(let ((position (position element sequence)))
449
(when (and position (< position (1- (length sequence))))
450
(aref sequence (1+ position)))))
451
(:method (element (sequence list))
452
(loop for sequence on sequence
453
when (equal element (first sequence))
454
return (second sequence))))
459
(defun rsp:multivalent-structure (associative sequential)
460
(make-instance 'rsp:multivalent-structure
461
:associative associative
462
:sequential sequential))
466
(defgeneric rsp:cons (element sequence)
467
(:method ((element t) (sequence rsp:sequence))
468
(clone-instance sequence :content (rsp:cons element (rsp:content sequence))))
469
(:method ((element t) (sequence list))
470
(cons element sequence))
471
(:method ((element t) (sequence vector))
472
(setf sequence (adjust-array sequence (list (1+ (length sequence))))
473
(aref sequence (1- (length sequence))) element)
476
(defgeneric rsp:revise (operator sequence)
477
(:method ((operator function) (sequence rsp:sequence))
478
(rsp:cons (funcall operator (rsp:greatest sequence))
482
;;; base update operators
484
(defmethod repository-add-graph (transaction
485
(from-graph sequence-property-path)
486
(to-graph sequence-property-path)
487
&key if-does-not-exist)
488
(declare (ignore if-does-not-exist))
491
(defmethod repository-clear-graph (transaction (graph-designator sequence-property-path)
492
&key if-does-not-exist)
493
(declare (ignore if-does-not-exist))
496
(defmethod repository-copy-graph (transaction*
497
(from-graph sequence-property-path)
498
(to-graph sequence-property-path)
499
&key if-does-not-exist)
500
(declare (ignore if-does-not-exist))
503
(defmethod repository-create-graph (transaction (graph-designator sequence-property-path)
505
(declare (ignore if-exists))
509
(defmethod repository-delete-graph (transaction (graph-designator sequence-property-path)
510
&key if-does-not-exist)
511
(declare (ignore if-does-not-exist))
515
(defmethod spocq.e:delete-data ((structure rsp:structure))
516
(repository-delete-field (task-repository *query*) structure)
519
(defmethod repository-delete-field (transaction (structure rsp:structure))
522
;; delete where is not extended
525
;;; specialize insert data for the respective structure forms
526
;;; each isolates the respective component and perform the operation oon that
527
(defmethod spocq.e:insert-data ((data rsp:container))
528
(repository-insert-field (task-repository *query*) data)
531
(defmethod repository-insert-field (transaction (data rsp:container))
532
"Insert the container-content into the dataset corresponding to the transaction"
533
(repository-insert-field transaction (rsp:content data)))
536
(defmethod spocq.e:load-graph (location (graph-designator sequence-property-path) &key (verbose t))
537
(declare (ignore verbose))
541
;;; devolves to compute-modify-operators
542
;;; which computes functions which operate on the solution field sets
543
;;; that operator must be extended for structures
545
(defmethod repository-move-graph (transaction*
546
(from-graph sequence-property-path)
547
(to-graph sequence-property-path)
548
&key if-does-not-exist)
549
(declare (ignore if-does-not-exist))
552
;;; persistent-structure
554
(defgeneric rsp:content-union (dataset )
558
(defclass rsp::element (rsp:graph) ;!!! could delegate to the graph element rather than inheriting
561
:accessor rsp::element-identifier)))
563
(defclass rsp::temporal-element (rsp::element)
564
((temporal-identifier
565
:initform (error "temporal-identifier is required")
566
:accessor rsp::element-temporal-identifier)
568
:initform (error "temporal-predicate is required")
569
:accessor rsp::element-temporal-predicate)))
570
(defclass rsp::transactional-element (rsp::element)
571
((transactional-identifier
572
:initform (error "transactional-identifier is required")
573
:accessor rsp::element-transactional-identifier)
574
(transactional-predicate
575
:initform (error "transactional-predicate is required")
576
:accessor rsp::element-transactional-predicate)))
577
(defclass rsp::stream-element (rsp::temporal-element rsp::transactional-element)
580
(defgeneric rsp::encode-element (element media-type stream)
581
(:documentation "encode the element graph, including temporal and transactional identifiers")
582
(:method ((element rsp::temporal-element) (media-type mime:rdf) (stream t))
583
(rsp:encode-element `(spocq:quad ,(rsp:element-identifier element)
584
,(rsp:element-temporal-predicate element)
585
,(rsp:element-temporal-identifier element))
589
(:method ((element rsp::temporal-element) (media-type mime:rdf) (stream t))
590
(rsp:encode-element `(spocq:quad ,(rsp:element-identifier element)
591
,(rsp:element-transactional-predicate element)
592
,(rsp:element-transactional-identifier element))
596
(:method ((element rsp::element) (media-type mime:application/n-quads) stream)
597
(loop for statement in (rsp:content element)
598
do (rsp::encode-element statement media-type stream)))
599
(:method ((element rsp::element) (media-type mime:application/trig) stream)
600
(format stream "~/rsp:format-term/ {" (rsp::element-identifier element))
601
(loop for statement in (rsp:content element) ;
602
do (rsp::encode-element statement media-type stream))
603
(write-string "}" stream))
607
(defgeneric rsp:is-persistent-structure (object)
608
(:method ((instance rsp:persistent-structure))
610
(:method ((instance t))
613
(defgeneric rsp:has-static (structure)
614
(:method ((structure rsp:persistent-structure))
615
(spocq.i::test-rlmdb-pattern-match (rsp:static-part structure) 0 0 0 0)))
617
(defgeneric rsp:has-dynamic (structure)
618
(:method ((structure rsp:persistent-structure))
619
(spocq.i::test-rlmdb-pattern-match (rsp:dynamic-part structure) 0 0 0 0)))
621
(defgeneric rsp:is-element-of (element structure)
622
(:method-combination and)
623
(:method and ((element rsp::temporal-element) (structure rsp:persistent-structure))
625
(:method and ((element rsp::transactional-element) (structure rsp:persistent-structure))
626
(equalp (run-sparql `(spocq.a:|ask|
627
(spocq.a:|bgp| ,(rsp:statements element)))
628
:agent spocq.i::*agent*
629
:repository (rsp:dynamic-part structure)
630
:revision-identifier (rsp::element-transactional-identifier element))
632
(:method and ((element rsp::temporal-element) (structure rsp:persistent-structure))
633
(equalp (run-sparql `(spocq.a:|ask|
634
(spocq.a:|bgp| ,(rsp:statements element)))
635
:agent spocq.i::*agent*
636
:repository (rsp:dynamic-part structure)
637
:temporal-identifier (rsp::element-temporal-identifier element))
639
(:method and ((element rsp::stream-element) (structure rsp:persistent-structure))
640
(equalp (run-sparql `(spocq.a:|ask|
641
(spocq.a:|bgp| ,(rsp:statements element)))
642
:agent spocq.i::*agent*
643
:repository (rsp:dynamic-part structure)
644
:temporal-identifier (rsp::element-temporal-identifier element)
645
:revision-identifier (rsp::element-transactional-identifier element))