-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.ts
222 lines (208 loc) · 5.91 KB
/
generate.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
import path = require("path");
import fs = require("fs");
import ts = require("typescript");
import prettier = require("prettier");
import * as schema from "./schema.json";
const headerFile = path.join(path.dirname(__filename), "header.ts");
const pathFile = "path.ts";
const code = fs.readFileSync(headerFile, "utf-8");
const sourceFile = ts.createSourceFile(
pathFile,
code,
ts.ScriptTarget.Latest,
true
);
const pathClass = sourceFile.statements[sourceFile.statements.length - 1];
if (
!(ts.isClassDeclaration(pathClass) && pathClass.name.escapedText === "Path")
) {
throw new Error(`Unexpected statement: ${pathClass}`);
}
type SchemaObject = {
"@id": string;
};
type Restriction = SchemaObject & {
"@type": "owl:Restriction";
"owl:cardinality"?: number;
"owl:maxCardinality"?: number;
"owl:onProperty"?: SchemaObject;
};
type BaseStep = SchemaObject & {
"@type": "rdfs:Class";
"rdfs:subClassOf": Array<SchemaObject | Restriction>;
"rdfs:comment": string;
};
type BaseProperty = SchemaObject & {
"@type": "owl:ObjectProperty" | "owl:DatatypeProperty";
/** @todo this is invalid domain should receive { @id: string } */
"rdfs:domain":
| string
| {
"@id": string;
"@type": "owl:Class";
"owl:unionOf": {
"@id": string;
"@list": BaseStep[];
};
};
/** @todo this is invalid range should receive { @id: string } */
"rdfs:range": string;
};
const rangeToType = (range: string) => {
const rangeID = range;
if (rangeID === "linkedql:PathStep") {
return ts.createTypeReferenceNode("Path", []);
}
if (rangeID == "xsd:string") {
return ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
}
if (rangeID == "xsd:int" || rangeID == "xsd:float") {
return ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
}
if (rangeID == "xsd:boolean") {
return ts.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
}
if (rangeID == "linkedql:Operator") {
return ts.createTypeReferenceNode("Operator", []);
}
if (rangeID == "rdfs:Resource") {
return ts.createTypeReferenceNode("Identifier", []);
}
throw Error(`Unexpected range: ${range}`);
};
function createMethodFromStep(
step: BaseStep,
pathClassName: ts.Identifier,
properties: BaseProperty[]
): ts.MethodDeclaration {
const stepTypeID = step["@id"];
const stepTypeName = stepTypeID.replace("linkedql:", "");
const stepName = stepTypeName[0].toLowerCase() + stepTypeName.slice(1);
const restrictions: Restriction[] = step["rdfs:subClassOf"].filter(
(subClass): subClass is Restriction =>
subClass["@type"] === "owl:Restriction"
);
const stepProperties = properties.filter(property => {
if (property["@id"] === "linkedql:from") {
return false;
}
const domain = property["rdfs:domain"];
if (typeof domain === "string") {
return domain === stepTypeID;
}
return domain["owl:unionOf"]["@list"].some(
step => step["@id"] === stepTypeID
);
});
const parameterNames = stepProperties.map(property =>
property["@id"].replace("linkedql:", "")
);
const parameters = parameterNames.map((name, i) => {
const property = stepProperties[i];
const propertyRestrictions = restrictions.filter(restriction => {
return restriction["owl:onProperty"]["@id"] === property["@id"];
});
const cardinality =
propertyRestrictions[0] && propertyRestrictions[0]["owl:cardinality"];
const maxCardinality =
propertyRestrictions[0] && propertyRestrictions[0]["owl:maxCardinality"];
const baseType = rangeToType(property["rdfs:range"]);
let type: ts.TypeNode = baseType;
if (maxCardinality === 1) {
type = ts.createUnionTypeNode([
type,
ts.createKeywordTypeNode(ts.SyntaxKind.NullKeyword)
]);
} else if (cardinality !== 1) {
type = ts.createArrayTypeNode(type);
}
return ts.createParameter(
[],
[],
undefined,
name,
undefined,
type,
undefined
);
});
const stepPropertyAssignments = [
ts.createPropertyAssignment(
ts.createStringLiteral("@type"),
ts.createStringLiteral(stepTypeID)
),
...stepProperties.map((property, i) => {
const propertyName = parameterNames[i];
return ts.createPropertyAssignment(
ts.createStringLiteral(property["@id"]),
ts.createIdentifier(propertyName)
);
})
];
return ts.createMethod(
[],
[],
undefined,
stepName,
undefined,
[],
parameters,
ts.createTypeReferenceNode(pathClassName, []),
ts.createBlock([
ts.createStatement(
ts.createCall(
ts.createPropertyAccess(ts.createThis(), "addStep"),
[],
[ts.createObjectLiteral(stepPropertyAssignments)]
)
),
ts.createReturn(ts.createThis())
])
);
}
// @ts-ignore
const steps: BaseStep[] = schema.filter(
object => object["@type"] === "rdfs:Class"
);
// @ts-ignore
const properties: BaseProperty[] = schema.filter(
object =>
object["@type"] === "owl:ObjectProperty" ||
object["@type"] === "owl:DatatypeProperty"
);
const newMembers = [
...pathClass.members,
...steps.map((step: BaseStep) =>
createMethodFromStep(step, pathClass.name, properties)
)
];
const newPathClass = ts.createClassDeclaration(
pathClass.decorators,
pathClass.modifiers,
pathClass.name,
pathClass.typeParameters,
pathClass.heritageClauses,
newMembers
);
const newSourceFile = ts.createSourceFile(
sourceFile.fileName,
sourceFile.text,
ts.ScriptTarget.Latest,
/*setParentNodes*/ false,
ts.ScriptKind.TS
);
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed
});
const result = printer.printList(
ts.ListFormat.SourceFileStatements,
ts.createNodeArray(
[
...sourceFile.statements.slice(0, sourceFile.statements.length - 1),
newPathClass
],
false
),
newSourceFile
);
fs.writeFileSync(pathFile, prettier.format(result, { parser: "typescript" }));