-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleMetadata.ts
More file actions
280 lines (259 loc) · 9.18 KB
/
ExampleMetadata.ts
File metadata and controls
280 lines (259 loc) · 9.18 KB
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
import * as Reactodia from '@reactodia/workspace';
const owl = vocabulary('http://www.w3.org/2002/07/owl#', [
'Class',
'AnnotationProperty',
'DatatypeProperty',
'ObjectProperty',
]);
export const rdfs = vocabulary('http://www.w3.org/2000/01/rdf-schema#', [
'comment',
'domain',
'range',
'seeAlso',
'subClassOf',
'subPropertyOf',
]);
export const example = vocabulary('http://www.example.com/', [
'workflowStatus',
]);
const SIMULATED_DELAY: number = 50; /* ms */
export class ExampleMetadataProvider extends Reactodia.BaseMetadataProvider {
private readonly propertyTypes = [
owl.AnnotationProperty,
owl.DatatypeProperty,
owl.ObjectProperty,
];
private readonly editableTypes = new Set([owl.Class, ...this.propertyTypes]);
private readonly editableRelations = new Set<Reactodia.LinkTypeIri>([
rdfs.domain,
rdfs.range,
]);
private readonly literalLanguages: ReadonlyArray<string> =
['de', 'en', 'es', 'ru', 'zh'];
constructor() {
super({
getLiteralLanguages: () => this.literalLanguages,
createEntity: async (type, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
const random32BitDigits = Math.floor((1 + Math.random()) * 0x100000000)
.toString(16).substring(1);
const typeLabel = Reactodia.Rdf.getLocalName(type) ?? 'Entity';
return {
data: {
id: `${type}_${random32BitDigits}`,
types: [type],
properties: {
[Reactodia.rdfs.label]: [
Reactodia.Rdf.DefaultDataFactory.literal(`New ${typeLabel}`)
]
},
},
elementState: type === owl.Class
? Reactodia.TemplateState.empty.set(
Reactodia.TemplateProperties.Expanded, true
)
: undefined,
};
},
createRelation: async (source, target, linkType, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
return {
data: {
sourceId: source.id,
targetId: target.id,
linkTypeId: linkType,
properties: {},
},
};
},
canConnect: async (source, target, linkType, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
const connections: Reactodia.MetadataCanConnect[] = [];
const addConnections = (
types: readonly Reactodia.ElementTypeIri[],
allOutLinks: readonly Reactodia.LinkTypeIri[],
allInLinks: readonly Reactodia.LinkTypeIri[]
) => {
const outLinks = linkType
? allOutLinks.filter(type => type === linkType)
: allOutLinks;
const inLinks = linkType
? allInLinks.filter(type => type === linkType)
: allInLinks;
if (types.length > 0 && (outLinks.length > 0 || inLinks.length > 0)) {
connections.push({ targetTypes: new Set(types), outLinks, inLinks });
}
};
if (hasType(source, owl.Class)) {
if (hasType(target, owl.Class)) {
addConnections([owl.Class], [rdfs.subClassOf], [rdfs.subClassOf]);
}
const targetPropertyTypes = this.propertyTypes.filter(type => hasType(target, type));
if (targetPropertyTypes.length > 0) {
addConnections(targetPropertyTypes, [], [rdfs.domain, rdfs.range]);
}
}
const sourcePropertyTypes = this.propertyTypes.filter(type => hasType(source, type));
if (sourcePropertyTypes.length > 0) {
for (const type of sourcePropertyTypes) {
if (hasType(target, type)) {
addConnections([type], [rdfs.subPropertyOf], [rdfs.subPropertyOf]);
}
}
if (hasType(target, owl.Class)) {
addConnections([owl.Class], [rdfs.domain, rdfs.range], []);
}
}
return connections;
},
canModifyEntity: async (entity, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
const editable = entity.types.some(type => this.editableTypes.has(type));
return {
canChangeIri: entity.types.includes(owl.Class),
canEdit: editable,
canDelete: editable,
};
},
canModifyRelation: async (link, source, target, options) => {
await Reactodia.delay(SIMULATED_DELAY, {signal: options.signal});
switch (link.linkTypeId) {
case rdfs.domain:
case rdfs.range:
case rdfs.subClassOf:
case rdfs.subPropertyOf: {
return {
canChangeType: true,
canEdit: this.editableRelations.has(link.linkTypeId),
canDelete: true,
};
}
default: {
return {};
}
}
},
getEntityShape: async (types, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
const properties = new Map<Reactodia.PropertyTypeIri, Reactodia.MetadataPropertyShape>();
if (types.some(type => this.editableTypes.has(type))) {
properties.set(Reactodia.rdfs.label, {
valueShape: {termType: 'Literal'},
order: 1,
});
properties.set(example.workflowStatus, {
valueShape: {termType: 'Literal'},
order: 2,
});
properties.set(rdfs.comment, {
valueShape: {termType: 'Literal'},
order: 3,
});
properties.set(Reactodia.schema.thumbnailUrl, {
valueShape: {termType: 'NamedNode'},
maxCount: 1,
order: 4,
});
properties.set(rdfs.seeAlso, {
valueShape: {termType: 'NamedNode'},
order: 5,
});
}
return {properties};
},
getRelationShape: async (linkType, source, target, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
const properties = new Map<Reactodia.PropertyTypeIri, Reactodia.MetadataPropertyShape>();
if (this.editableRelations.has(linkType)) {
properties.set(example.workflowStatus, {
valueShape: {termType: 'Literal'},
order: 1,
});
properties.set(rdfs.comment, {
valueShape: {termType: 'Literal'},
order: 2,
});
}
return {properties};
},
filterConstructibleTypes: async (types, {signal}) => {
await Reactodia.delay(SIMULATED_DELAY, {signal});
return new Set(Array.from(types).filter(type => this.editableTypes.has(type)));
},
});
}
}
export class ExampleValidationProvider implements Reactodia.ValidationProvider {
async validate(
event: Reactodia.ValidationEvent
): Promise<Reactodia.ValidationResult> {
const {target, outboundLinks, graph, state, translation, language, signal} = event;
const items: Array<Reactodia.ValidatedElement | Reactodia.ValidatedLink> = [];
if (target.types.includes(owl.Class)) {
for (const e of state.links.values()) {
if (e.type === 'relationAdd' && e.data.sourceId === target.id) {
const linkType = graph.getLinkType(e.data.linkTypeId);
const linkLabel = translation.formatLabel(
linkType?.data?.label, e.data.linkTypeId, language
);
items.push({
type: 'link',
target: e.data,
severity: 'error',
message: 'Cannot add any new link from a Class',
});
items.push({
type: 'element',
target: target.id,
severity: 'warning',
message: `Cannot create "${linkLabel}" relation from a Class`,
});
}
}
}
if (
state.elements.has(target.id) &&
target.types.includes(owl.ObjectProperty)
) {
if (!outboundLinks.some(link => link.linkTypeId === rdfs.subPropertyOf)) {
items.push({
type: 'element',
target: target.id,
severity: 'info',
message: 'It might be a good idea to make the property a sub-property of another',
});
}
}
for (const link of outboundLinks) {
const {[rdfs.comment]: comments} = link.properties;
if (comments && !comments.every(comment => comment.termType === 'Literal' && comment.language)) {
items.push({
type: 'link',
target: link,
severity: 'error',
message: 'value should have a language',
propertyType: rdfs.comment,
});
}
}
await Reactodia.delay(SIMULATED_DELAY, {signal});
return {items};
}
}
type VocabularyKeyType<K extends string> =
K extends Capitalize<K>
? Reactodia.ElementTypeIri
: Reactodia.LinkTypeIri & Reactodia.PropertyTypeIri;
type Vocabulary<Keys extends string[]> = {
readonly [K in Keys[number]]: VocabularyKeyType<K>;
};
function vocabulary<const Keys extends string[]>(prefix: string, keys: Keys): Vocabulary<Keys> {
const result: { [key: string]: string } = Object.create(null);
for (const key of keys) {
result[key] = prefix + key;
}
return result as Vocabulary<Keys>;
}
function hasType(model: Reactodia.ElementModel | undefined, type: Reactodia.ElementTypeIri) {
return Boolean(!model || model.types.includes(type));
}