/
encoder.ts
156 lines (128 loc) · 3.83 KB
/
encoder.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
import { type Context, DataFactory, type Iri, type RDF, toRdf } from "./rdf.ts";
import type { Property, Schema } from "./schema/mod.ts";
import { rdf, xsd } from "./namespaces/mod.ts";
type DecodedNode = Record<string, unknown>;
type NodeId = RDF.NamedNode | RDF.BlankNode;
export const encode = (
node: DecodedNode,
schema: Schema,
context: Context,
variableInitCounter = 0,
) => {
return Encoder.encode(node, schema, context, variableInitCounter);
};
class Encoder {
private context: Context;
private df: DataFactory = new DataFactory({
blankNodePrefix: "b",
});
private variableCounter: number;
private output: RDF.Quad[] = [];
private constructor(context: Context, variableInitCounter: number) {
this.context = context;
this.variableCounter = variableInitCounter;
}
static encode(
node: DecodedNode,
schema: Schema,
context: Context,
variableInitCounter: number,
) {
return new Encoder(context, variableInitCounter).encode(node, schema);
}
encode(node: DecodedNode, schema: Schema) {
const nodeId = this.getNodeId(node);
this.encodeNode(node, schema, nodeId);
return this.output;
}
push(
s: RDF.Quad_Subject,
p: RDF.Quad_Predicate,
o: RDF.Quad_Object,
) {
this.output.push(this.df.quad(s, p, o));
}
getNodeId(node: DecodedNode) {
return node.$id
? this.df.namedNode(node.$id as string)
: this.df.blankNode();
}
getNodeTypes(node: DecodedNode) {
if (Array.isArray(node.$type)) {
return node.$type;
}
return node.$type ? [node.$type] : [];
}
encodeNode(node: DecodedNode, schema: Schema, nodeId: NodeId) {
this.encodeNodeType(node, schema["@type"], nodeId);
Object.keys(schema).forEach((key) => {
if (key === "@type") {
return;
}
this.encodeNodeProperty(node[key], schema[key] as Property, nodeId);
});
}
encodeNodeType(node: DecodedNode, requiredTypes: Iri[], nodeId: NodeId) {
const finalTypes = new Set([...this.getNodeTypes(node), ...requiredTypes]);
finalTypes.forEach((type) => {
this.push(nodeId, this.df.namedNode(rdf.type), this.df.namedNode(type));
});
}
encodeNodeProperty(value: any, property: Property, nodeId: NodeId) {
if (value === undefined) {
return;
}
const propertyId = this.df.namedNode(property["@id"]);
if (value === null) {
// TODO
this.push(
nodeId,
propertyId,
this.df.variable(`v${this.variableCounter++}`),
);
return;
}
if (property["@multilang"]) {
Object.keys(value).forEach((language) => {
const languageValue: string[] = Array.isArray(value[language])
? value[language]
: [value[language]];
languageValue.forEach((singleValue) => {
this.push(
nodeId,
propertyId,
this.df.literal(
singleValue,
language.length > 0 ? language : undefined,
),
);
});
});
return;
}
const values = Array.isArray(value) ? value : [value];
values.forEach((val) => {
if (property["@context"]) {
const subNodeId = this.getNodeId(val);
this.encodeNode(val, property["@context"], subNodeId);
this.push(nodeId, propertyId, subNodeId);
return;
}
const propertyType = property["@type"] ? property["@type"] : xsd.string;
if (typeof val === "string" && this.context.language) {
if (propertyType === xsd.string || propertyType === rdf.langString) {
this.push(
nodeId,
propertyId,
this.df.literal(val, this.context.language),
);
return;
}
}
const rdfValue = toRdf(val, {
datatype: this.df.namedNode(propertyType),
});
this.push(nodeId, propertyId, rdfValue);
});
}
}