/
blank-node.ts
100 lines (87 loc) · 2.3 KB
/
blank-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import ClassOrder from './class-order'
import Node from './node-internal'
import IndexedFormula from './store'
import { BlankNodeTermType } from './types'
import { BlankNode as TFBlankNode } from './tf-types'
/**
* An RDF blank node is a Node without a URI
* @link https://rdf.js.org/data-model-spec/#blanknode-interface
*/
export default class BlankNode extends Node implements TFBlankNode {
termType: typeof BlankNodeTermType = BlankNodeTermType;
/**
* The next unique identifier for blank nodes
*/
static nextId: number = 0;
static NTAnonymousNodePrefix: '_:' = '_:'
private static getId (id: string | unknown): string {
if (id) {
if (typeof id !== 'string') {
console.log('Bad blank id:', id)
throw new Error('Bad id argument to new blank node: ' + id)
}
if (id.includes('#')) {
// Is a URI with hash fragment
let fragments = id.split('#')
return fragments[fragments.length - 1]
}
return id
}
return 'n' + BlankNode.nextId++
}
classOrder = ClassOrder.BlankNode
/** Whether this is a blank node */
isBlank: number = 1
/**
* This type of node is a variable.
*
* Note that the existence of this property already indicates that it is a variable.
*/
isVar = 1
/**
* Initializes this node
* @param [id] The identifier for the blank node
*/
constructor (id?: string | unknown) {
super(BlankNode.getId(id))
}
/**
* The identifier for the blank node
*/
public get id (): string {
return this.value
}
public set id (value: string) {
this.value = value
}
compareTerm (other: BlankNode): number {
if (this.classOrder < other.classOrder) {
return -1
}
if (this.classOrder > other.classOrder) {
return +1
}
if (this.id < other.id) {
return -1
}
if (this.id > other.id) {
return +1
}
return 0
}
/**
* Gets a copy of this blank node in the specified formula
* @param formula The formula
*/
copy (formula: IndexedFormula): BlankNode { // depends on the formula
var bnodeNew = new BlankNode()
formula.copyTo(this, bnodeNew)
return bnodeNew
}
toCanonical () {
return BlankNode.NTAnonymousNodePrefix + this.value
}
toString () {
return BlankNode.NTAnonymousNodePrefix + this.id
}
}