Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (133 loc) · 4.63 KB
/
Copy pathindex.js
File metadata and controls
154 lines (133 loc) · 4.63 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
import jsonld from "jsonld";
import fs from "fs-extra";
const context = await fs.readJson(new URL("context.json", import.meta.url),);
/**
* This method extracts the definitions of enums.
* If the method doesn't find a definition for an enum,
* it will look for the properties that use this enum, and
* print the definitions of those properties.
* @param {string} pathToIntermediateJsonLdFile - Path to the JSON-LD file with the intermediate representation.
*/
export default async function extract(pathToIntermediateJsonLdFile) {
const inputObj = await fs.readJson(pathToIntermediateJsonLdFile);
const frame = {
"@context": context,
"inverseRange": {
"@explicit": true,
"apDefinition": {},
"vocDefinition": {},
"assignedURI": {}
}
};
// Frame frist with http URI of skos:Concept.
frame.assignedURI = "http://www.w3.org/2004/02/skos/core#Concept";
const framed = await jsonld.frame(inputObj, frame);
// Frame a second time with https URI of skos:Concept.
frame.assignedURI = "https://www.w3.org/2004/02/skos/core#Concept";
const framed2 = await jsonld.frame(inputObj, frame);
// Merge results of the two framing operations.
if (framed2["@graph"]) {
framed2["@graph"].forEach(el => {
console.error(`Enum "${el.diagramLabel}" uses incorrect URI for skos:Concept (https://www.w3.org/2004/02/skos/core#Concept)`);
});
framed["@graph"] = framed["@graph"].concat(framed2["@graph"]);
}
fs.writeJson("enums.jsonld", framed, {
spaces: 2
});
const testSubjects = [];
framed["@graph"].forEach(el => {
let text = `Enum "${el.diagramLabel}". `;
if (el.apDefinition) {
const defs = el.apDefinition.map(d => `"${d["@value"]}"`).join(", ");
text += defs;
testSubjects.push({
"@type": "TestSubject",
"@id": el["@id"],
"dcterms:title": `${el.diagramLabel}`,
"assertion": {
"@type": "Assertion",
"test": "_:definition-test",
"result": {
"@type": "TestResult",
"outcome": "passed"
}
}
});
} else {
text += "\n No AP definition provided.\n";
const testSubject = {
"@type": "TestSubject",
"@id": el["@id"],
"dcterms:title": `${el.diagramLabel}`,
"assertion": {
"@type": "Assertion",
"test": "_:definition-test",
"result": {
"@type": "TestResult",
"dcterms:description": "No AP definition provided.",
"outcome": "failed"
}
}
};
if (el.inverseRange) {
if (!Array.isArray(el.inverseRange)) {
el.inverseRange = [el.inverseRange];
}
let potentialFix = "";
for (let i = 0; i < el.inverseRange.length; i++) {
const p = el.inverseRange[i];
if (i > 0) {
text += "\n";
potentialFix += "\n";
}
text += " ";
const prop = p.assignedURI || p.diagramLabel[0]["@value"];
text += `${prop} uses this enum`;
potentialFix += `${prop} uses this enum`;
if (p.apDefinition && p.apDefinition.length > 0) {
const propDefs = p.apDefinition.map(d => `"${d["@value"]}"`).join(", ");
text += ": " + propDefs + " ";
potentialFix += ": " + propDefs + " ";
} else if (p.vocDefinition && p.vocDefinition.length > 0) {
const propDefs = p.vocDefinition.map(d => `"${d["@value"]}"`).join(", ");
text += ": " + propDefs + " ";
potentialFix += ": " + propDefs + " ";
} else {
text += ", but no AP or VOC definition provided for it.";
potentialFix += ", but no AP or VOC definition provided for it.";
}
}
testSubject.assertion.potentialFix = potentialFix;
} else {
const temp = "No property found that uses this enum.";
text += temp;
testSubject.assertion.potentialFix = temp;
}
testSubjects.push(testSubject);
}
console.log(text);
});
testSubjects.push({
"@type": ["TestCriterion", "TestRequirement"],
"@id": "_:definition-test",
"dcterms:title": "An enum should have an AP definition."
});
const assertionsJsonLD = {
"@context": {
"@vocab": "http://www.w3.org/ns/earl#",
"dcterms": "http://purl.org/dc/terms/",
"ex": "http://example.com#",
"potentialFix": "ex:potentialFix",
"assertion": {"@reverse": "subject"},
"test": {"@type": "@id"},
"outcome": {
"@type": "@id"
}
},
"@graph": testSubjects
};
fs.writeJson("assertions.jsonld", assertionsJsonLD, {
spaces: 2
});
}