rdflib 2.2.20 → 2.2.21-3a98cfb3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lists.ts ADDED
@@ -0,0 +1,121 @@
1
+ /* Lists form conversion
2
+ */
3
+
4
+
5
+ // import DataFactory from './factories/extended-term-factory'
6
+ // import jsonldParser from './jsonldparser'
7
+ // @ts-ignore is this injected?
8
+ import { Parser as N3jsParser } from 'n3' // @@ Goal: remove this dependency
9
+ // import N3Parser from './n3parser'
10
+ // import { parseRDFaDOM } from './rdfaparser'
11
+ // import RDFParser from './rdfxmlparser'
12
+ // import sparqlUpdateParser from './patch-parser'
13
+ // import * as Util from './utils-js'
14
+ import Node from './node-internal'
15
+ // import BlankNode from './blank-node'
16
+ // import NamedNode from './named-node'
17
+ import Collection from './collection'
18
+ import Statement from './statement'
19
+ // import Formula from './formula'
20
+ import Store from './store'
21
+ // import { ContentType, TurtleContentType, N3ContentType, RDFXMLContentType, XHTMLContentType, HTMLContentType, SPARQLUpdateContentType, SPARQLUpdateSingleMatchContentType, JSONLDContentType, NQuadsContentType, NQuadsAltContentType } from './types'
22
+
23
+ // import { Quad } from './tf-types'
24
+
25
+ import {BlankNode, NamedNode, Quad, Term,} from './tf-types'
26
+ import Namespace from './namespace'
27
+
28
+ const RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
29
+
30
+ /* Replace a given node with another node throughout a given document
31
+ *
32
+ * we do the predicate as well for complenesss though we don't expect Collections to use it
33
+ */
34
+ export function substituteInDoc (store:Store, x:Term, y:Term, doc?: NamedNode ) {
35
+ // console.log(`substituteInDoc put ${x} for ${y} in ${doc}}`)
36
+ for (const quad of store.statementsMatching(y as any, null, null, doc as any)) {
37
+ const newStatement = new Statement(x as any, quad.predicate, quad.object, doc as any)
38
+ store.remove(quad)
39
+ store.add(newStatement)
40
+ }
41
+ for (const quad of store.statementsMatching(null, y as any, null, doc) as any) {
42
+ store.remove(quad)
43
+ // console.log(` substituteInDoc predicate ${x} in ${quad}}`)
44
+ store.add(new Statement(quad.subject, x as any, quad.object, doc as any))
45
+ }
46
+ for (const quad of store.statementsMatching(null, null, y as any, doc) as any) {
47
+ store.remove(quad)
48
+ store.add(new Statement(quad.subject, quad.predicate, x as any, doc as any))
49
+ }
50
+ }
51
+
52
+ /* Change all lone rdf:nil nodes into empty Collections
53
+ */
54
+ export function substituteNillsInDoc (store:Store, doc?: NamedNode) {
55
+ const x = RDF('nil')
56
+ for (const quad of store.statementsMatching(x as any, null, null, doc as any)) {
57
+ store.remove(quad)
58
+ const y = new Collection()
59
+ store.add(new Statement(y as any, quad.predicate, quad.object, doc as any))
60
+ }
61
+ for (const quad of store.statementsMatching(null, null, x as any, doc) as any) {
62
+ if (!quad.predicate.sameTerm(RDF('rest'))) { // If not a tail
63
+ store.remove(quad)
64
+ const y = new Collection()
65
+ store.add(new Statement(quad.subject, quad.predicate, y as any, doc as any))
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * Convert lists reified as rdf:first, rest
71
+ * Normal method is sync.
72
+ * Unfortunately jsdonld is currently written to need to be called async.
73
+ * Hence the mess below with executeCallback.
74
+ * @param store - The quadstore
75
+ * @param doc - The document in which the conversion is done
76
+ */
77
+
78
+
79
+ export function convertFirstRestNil (
80
+ store: Store,
81
+ doc: NamedNode | undefined, // Do whole store?
82
+ ) {
83
+
84
+ function preceding (ele:BlankNode, listSoFar: Node[], trash: Quad[]): undefined {
85
+
86
+ const rests = store.statementsMatching(ele, RDF('rest'), null, doc)
87
+ if (rests.length !== 1) throw new Error(`Bad list structure: no rest at ${ele}`)
88
+
89
+ const firsts = store.statementsMatching(ele, RDF('first'), null, doc)
90
+ if (firsts.length !== 1) throw new Error(`Bad list structure: rest but ${firsts.length} firsts at ${ele}`)
91
+ const value = firsts[0].object
92
+ const total = [value].concat(listSoFar as any)
93
+ // console.log(' List now is: ', total)
94
+ const totalTrash = trash.concat(rests).concat(firsts)
95
+
96
+ const pres = store.statementsMatching(null, RDF('rest'), ele, doc)
97
+ if (pres.length === 0) { // Head of the list
98
+ const newList = new Collection(total)
99
+ store.remove(totalTrash)
100
+ // Replace old list with new list:
101
+ substituteInDoc(store, newList, ele, doc)
102
+ return
103
+ }
104
+ if (pres.length !== 1) throw new Error(`Bad list structure: ${pres.length} pres at ${ele}`)
105
+ const pre = pres[0].subject
106
+ if (pre.termType !== 'BlankNode') throw new Error(`Bad list element node ${pre} type: ${pre.termType} `)
107
+
108
+ preceding(pre, total, totalTrash)
109
+ return
110
+ }
111
+
112
+ substituteNillsInDoc(store, doc) // lone ones only
113
+
114
+ const tails = store.statementsMatching(null, RDF('rest'), RDF('nil'), doc)
115
+ tails.forEach(tail => {
116
+ if (tail.subject.termType !== 'BlankNode')
117
+ throw new Error(`Bad list element node ${tail.subject} type: ${tail.subject.termType} `)
118
+ preceding(tail.subject, [], [])
119
+ })
120
+
121
+ }
package/src/statement.ts CHANGED
@@ -137,6 +137,13 @@ export default class Statement<
137
137
 
138
138
  /** Creates a string representation of this statement */
139
139
  toString (): string {
140
+ /*
141
+ return [
142
+ this.subject.toString(),
143
+ this.predicate.toString(),
144
+ this.object.toString(),
145
+ ].join(' ') + ' .'
146
+ */
140
147
  return this.toNT()
141
148
  }
142
149
  }
package/src/store.ts CHANGED
@@ -23,12 +23,14 @@ import Formula, { FormulaOpts } from './formula'
23
23
  import { ArrayIndexOf } from './utils'
24
24
  import { RDFArrayRemove } from './utils-js'
25
25
  import {
26
+ isRDFlibSubject,
27
+ isRDFlibPredicate,
26
28
  isRDFlibObject,
27
29
  isStore,
28
30
  isGraph,
29
- isPredicate,
31
+ // isPredicate,
30
32
  isQuad,
31
- isSubject
33
+ // isSubject
32
34
  } from './utils/terms'
33
35
  import Node from './node'
34
36
  import Variable from './variable'
@@ -429,10 +431,10 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
429
431
  pred = Node.fromValue(pred)
430
432
  const objNode = Node.fromValue(obj) as Term
431
433
  why = Node.fromValue(why)
432
- if (!isSubject(subj)) {
434
+ if (!isRDFlibSubject(subj)) {
433
435
  throw new Error('Subject is not a subject type')
434
436
  }
435
- if (!isPredicate(pred)) {
437
+ if (!isRDFlibPredicate(pred)) {
436
438
  throw new Error(`Predicate ${pred} is not a predicate type`)
437
439
  }
438
440
  if (!isRDFlibObject(objNode)) {
@@ -920,9 +922,7 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
920
922
  object?: Quad_Object | null,
921
923
  graph?: Quad_Graph | null
922
924
  ): IndexedFormula {
923
- this.removeStatements(
924
- this.statementsMatching(subject, predicate, object, graph)
925
- )
925
+ this.removeMany(subject, predicate, object, graph)
926
926
  return this
927
927
  }
928
928
 
@@ -689,7 +689,7 @@ export default class UpdateManager {
689
689
  thisUpdater.update(deletions.filter(st => st.why.equals(doc)),
690
690
  insertions.filter(st => st.why.equals(doc))))
691
691
  if (updates.length > 1) {
692
- console.log(`@@ updateMany to ${updates.length}: ${uniqueDocs}`)
692
+ // console.log(`@@ updateMany to ${updates.length}: ${uniqueDocs}`)
693
693
  }
694
694
  return Promise.all(updates)
695
695
  }
@@ -840,9 +840,10 @@ export default class UpdateManager {
840
840
 
841
841
  this.fire(doc.value, query, (uri, success, body, response) => {
842
842
  (response as any).elapsedTimeMs = Date.now() - startTime
843
- console.log(' UpdateManager: Return ' +
843
+ /* console.log(' UpdateManager: Return ' +
844
844
  (success ? 'success ' : 'FAILURE ') + (response as Response).status +
845
845
  ' elapsed ' + (response as any).elapsedTimeMs + 'ms')
846
+ */
846
847
  if (success) {
847
848
  try {
848
849
  kb.remove(ds)
@@ -33,7 +33,7 @@ export function isCollection(obj: any): obj is Collection<any> {
33
33
  && (obj as Term).termType === CollectionTermType
34
34
  }
35
35
 
36
- /** TypeGuard for valid RDFlib Object types, also allows Collections */
36
+ /** TypeGuard for valid RDFlib Object types, also allows Collections, Graphs */
37
37
  export function isRDFlibObject(obj: any): obj is ObjectType {
38
38
  return obj && Object.prototype.hasOwnProperty.call(obj, 'termType') && (
39
39
  obj.termType === NamedNodeTermType ||
@@ -45,6 +45,28 @@ export function isRDFlibObject(obj: any): obj is ObjectType {
45
45
  )
46
46
  }
47
47
 
48
+ /** TypeGuard for valid RDFlib Subject types, same as Object as RDFLib symmetrical.
49
+ */
50
+ export function isRDFlibSubject(obj: any): obj is ObjectType {
51
+ return obj && Object.prototype.hasOwnProperty.call(obj, 'termType') && (
52
+ obj.termType === NamedNodeTermType ||
53
+ obj.termType === VariableTermType ||
54
+ obj.termType === BlankNodeTermType ||
55
+ obj.termType === CollectionTermType ||
56
+ obj.termType === LiteralTermType ||
57
+ obj.termType === GraphTermType
58
+ )
59
+ }
60
+
61
+ /** TypeGuard for valid RDF/JS spec Predicate types */
62
+ export function isRDFlibPredicate(obj: any): obj is Quad_Predicate {
63
+ return isTerm(obj) && (
64
+ obj.termType === NamedNodeTermType ||
65
+ obj.termType === BlankNodeTermType ||
66
+ obj.termType === VariableTermType
67
+ )
68
+ }
69
+
48
70
  /** TypeGuard for RDFLib Variables */
49
71
  export function isVariable(obj: any): obj is Variable {
50
72
  return isTerm(obj)