This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Term.ts
408 lines (344 loc) · 11.7 KB
/
Term.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import type * as RDF from '@rdfjs/types';
import { DataFactory } from 'rdf-data-factory';
import { TermTransformer } from '../transformers/TermTransformer';
import * as C from '../util/Consts';
import { TypeAlias, TypeURL } from '../util/Consts';
import type {
IDateRepresentation,
IDateTimeRepresentation,
IDurationRepresentation,
ITimeRepresentation, IYearMonthDurationRepresentation,
} from '../util/DateTimeHelpers';
import * as Err from '../util/Errors';
import { serializeDateTime, serializeDuration, serializeTime, serializeDate } from '../util/Serialization';
import type { ISuperTypeProvider } from '../util/TypeHandling';
import { isSubTypeOf } from '../util/TypeHandling';
import type { TermExpression, TermType } from './Expressions';
import { ExpressionType } from './Expressions';
const DF = new DataFactory();
export abstract class Term implements TermExpression {
public expressionType: ExpressionType.Term = ExpressionType.Term;
abstract termType: TermType;
abstract toRDF(): RDF.Term;
public str(): string {
throw new Err.InvalidArgumentTypes([ this ], C.RegularOperator.STR);
}
public coerceEBV(): boolean {
throw new Err.EBVCoercionError(this);
}
}
// NamedNodes -----------------------------------------------------------------
export class NamedNode extends Term {
public termType: TermType = 'namedNode';
public constructor(public value: string) {
super();
}
public toRDF(): RDF.Term {
return DF.namedNode(this.value);
}
public str(): string {
return this.value;
}
}
// BlankNodes -----------------------------------------------------------------
export class BlankNode extends Term {
public value: RDF.BlankNode;
public termType: TermType = 'blankNode';
public constructor(value: RDF.BlankNode | string) {
super();
this.value = typeof value === 'string' ? DF.blankNode(value) : value;
}
public toRDF(): RDF.Term {
return this.value;
}
}
// Quads -----------------------------------------------------------------
export class Quad extends Term {
public termType: TermType = 'quad';
private readonly transformer: TermTransformer;
private readonly valueTerm: RDF.BaseQuad;
public constructor(input: RDF.BaseQuad, superTypeProvider: ISuperTypeProvider) {
super();
this.transformer = new TermTransformer(superTypeProvider);
this.valueTerm = input;
}
public toRDF(): RDF.BaseQuad {
return this.valueTerm;
}
public get subject(): Term {
return this.transformer.transformRDFTermUnsafe(this.RDFsubject);
}
public get predicate(): Term {
return this.transformer.transformRDFTermUnsafe(this.RDFpredicate);
}
public get object(): Term {
return this.transformer.transformRDFTermUnsafe(this.RDFobject);
}
public get RDFsubject(): RDF.Term {
return this.toRDF().subject;
}
public get RDFpredicate(): RDF.Term {
return this.toRDF().predicate;
}
public get RDFobject(): RDF.Term {
return this.toRDF().object;
}
}
// Literals-- -----------------------------------------------------------------
export function isLiteralTermExpression(expr: TermExpression): Literal<any> | undefined {
if (expr.termType === 'literal') {
return <Literal<any>> expr;
}
return undefined;
}
export class Literal<T extends { toString: () => string }> extends Term {
public termType: 'literal' = 'literal';
/**
* @param typedValue internal representation of this literal's value
* @param dataType a string representing the datatype. Can be of type @see LiteralTypes or any URI
* @param strValue the string value of this literal. In other words, the string representing the RDF.literal value.
* @param language the language, mainly for language enabled strings like RDF_LANG_STRING
*/
public constructor(
public typedValue: T,
public dataType: string,
public strValue?: string,
public language?: string,
) {
super();
}
public toRDF(): RDF.Literal {
return DF.literal(
this.strValue || this.str(),
this.language || DF.namedNode(this.dataType),
);
}
public str(): string {
return this.strValue || this.typedValue.toString();
}
}
export abstract class NumericLiteral extends Literal<number> {
protected constructor(
public typedValue: number,
dataType: string,
public strValue?: string,
public language?: string,
) {
super(typedValue, dataType, strValue, language);
}
protected abstract specificFormatter(val: number): string;
public coerceEBV(): boolean {
return !!this.typedValue;
}
public toRDF(): RDF.Literal {
const term = super.toRDF();
if (!Number.isFinite(this.typedValue)) {
term.value = term.value.replace('Infinity', 'INF');
}
return term;
}
public str(): string {
return this.strValue ||
this.specificFormatter(this.typedValue);
}
}
export class IntegerLiteral extends NumericLiteral {
public constructor(
public typedValue: number,
dataType?: string,
public strValue?: string,
public language?: string,
) {
super(typedValue, dataType || TypeURL.XSD_INTEGER, strValue, language);
}
protected specificFormatter(val: number): string {
return val.toFixed(0);
}
}
export class DecimalLiteral extends NumericLiteral {
public constructor(
public typedValue: number,
dataType?: string,
public strValue?: string,
public language?: string,
) {
super(typedValue, dataType || TypeURL.XSD_DECIMAL, strValue, language);
}
protected specificFormatter(val: number): string {
return val.toString();
}
}
export class FloatLiteral extends NumericLiteral {
public constructor(
public typedValue: number,
dataType?: string,
public strValue?: string,
public language?: string,
) {
super(typedValue, dataType || TypeURL.XSD_FLOAT, strValue, language);
}
protected specificFormatter(val: number): string {
return val.toString();
}
}
export class DoubleLiteral extends NumericLiteral {
public constructor(
public typedValue: number,
dataType?: string,
public strValue?: string,
public language?: string,
) {
super(typedValue, dataType || TypeURL.XSD_DOUBLE, strValue, language);
}
protected specificFormatter(val: number): string {
if (!Number.isFinite(val)) {
if (val > 0) {
return 'INF';
}
if (val < 0) {
return '-INF';
}
return 'NaN';
}
const jsExponential = val.toExponential();
const [ jsMantisse, jsExponent ] = jsExponential.split('e');
// Leading + must be removed for integer
// https://www.w3.org/TR/xmlschema-2/#integer
const exponent = jsExponent.replace(/\+/u, '');
// SPARQL test suite prefers trailing zero's
const mantisse = jsMantisse.includes('.') ?
jsMantisse :
`${jsMantisse}.0`;
return `${mantisse}E${exponent}`;
}
}
export class BooleanLiteral extends Literal<boolean> {
public constructor(public typedValue: boolean, public strValue?: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_BOOLEAN, strValue);
}
public coerceEBV(): boolean {
return this.typedValue;
}
}
export class LangStringLiteral extends Literal<string> {
public constructor(public typedValue: string, public language: string, dataType?: string) {
super(typedValue, dataType || TypeURL.RDF_LANG_STRING, typedValue, language);
}
public coerceEBV(): boolean {
return this.str().length > 0;
}
}
// https://www.w3.org/TR/2004/REC-rdf-concepts-20040210/#dfn-plain-literal
// https://www.w3.org/TR/sparql11-query/#defn_SimpleLiteral
// https://www.w3.org/TR/sparql11-query/#func-strings
// This does not include language tagged literals
export class StringLiteral extends Literal<string> {
/**
* @param typedValue
* @param dataType Should be type that implements XSD_STRING
*/
public constructor(public typedValue: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_STRING, typedValue);
}
public coerceEBV(): boolean {
return this.str().length > 0;
}
}
export class DateTimeLiteral extends Literal<IDateTimeRepresentation> {
public constructor(public typedValue: IDateTimeRepresentation, public strValue?: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_DATE_TIME, strValue);
}
public str(): string {
return serializeDateTime(this.typedValue);
}
}
export class TimeLiteral extends Literal<ITimeRepresentation> {
public constructor(public typedValue: ITimeRepresentation, public strValue?: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_TIME, strValue);
}
public str(): string {
return serializeTime(this.typedValue);
}
}
export class DateLiteral extends Literal<IDateRepresentation> {
public constructor(public typedValue: IDateRepresentation, public strValue?: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_DATE, strValue);
}
public str(): string {
return serializeDate(this.typedValue);
}
}
export class DurationLiteral extends Literal<Partial<IDurationRepresentation>> {
public constructor(public typedValue: Partial<IDurationRepresentation>, public strValue?: string, dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_DURATION, strValue);
}
public str(): string {
return serializeDuration(this.typedValue);
}
}
export class DayTimeDurationLiteral extends DurationLiteral {
public constructor(public typedValue: Partial<IDurationRepresentation>, public strValue?: string, dataType?: string) {
super(typedValue, strValue, dataType || TypeURL.XSD_DAY_TIME_DURATION);
}
}
export class YearMonthDurationLiteral extends Literal<Partial<IYearMonthDurationRepresentation>> {
public constructor(public typedValue: Partial<IYearMonthDurationRepresentation>, public strValue?: string,
dataType?: string) {
super(typedValue, dataType || TypeURL.XSD_YEAR_MONTH_DURATION, strValue);
}
public str(): string {
return serializeDuration(this.typedValue, 'P0M');
}
}
/**
* This class is used when a literal is parsed, and it's value is
* an invalid lexical form for it's datatype. The spec defines value with
* invalid lexical form are still valid terms, and as such we can not error
* immediately. This class makes sure that the typedValue will remain undefined,
* and the category 'nonlexical'. This way, only when operators apply to the
* 'nonlexical' category, they will keep working, otherwise they will throw a
* type error.
* This seems to match the spec, except maybe for functions that accept
* non-lexical values for their datatype.
*
* See:
* - https://www.w3.org/TR/xquery/#dt-type-error
* - https://www.w3.org/TR/rdf-concepts/#section-Literal-Value
* - https://www.w3.org/TR/xquery/#dt-ebv
* - ... some other more precise thing i can't find...
*/
export class NonLexicalLiteral extends Literal<{ toString: () => 'undefined' }> {
public constructor(
typedValue: undefined,
dataType: string,
private readonly openWorldType: ISuperTypeProvider,
strValue?: string,
language?: string,
) {
super({ toString: () => 'undefined' }, dataType, strValue, language);
}
public coerceEBV(): boolean {
const isNumericOrBool =
isSubTypeOf(this.dataType, TypeURL.XSD_BOOLEAN, this.openWorldType) ||
isSubTypeOf(this.dataType, TypeAlias.SPARQL_NUMERIC, this.openWorldType);
if (isNumericOrBool) {
return false;
}
throw new Err.EBVCoercionError(this);
}
public toRDF(): RDF.Literal {
return DF.literal(
this.str(),
this.language || DF.namedNode(this.dataType),
);
}
public str(): string {
return this.strValue || '';
}
}
export function isNonLexicalLiteral(lit: Literal<any>): NonLexicalLiteral | undefined {
if (lit instanceof NonLexicalLiteral) {
return lit;
}
return undefined;
}