-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
63 lines (55 loc) · 1.9 KB
/
index.js
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
const parseTTL = require('@frogcat/ttl2jsonld').parse;
const lut = require('./lookup-tables/');
const { handleContract, handleMCODeonticExpression } = require('./handlers/');
const { getType } = require('./handlers/lib/Utils');
const formatIntoMediaContractualObjects = (mediaContract) => {
const finalMCObjects = { contracts: [] };
// Search for all contract objects
Object.values(mediaContract).forEach((element) => {
if (element.class === 'Contract') {
Object.keys(element).forEach((contractKey) => {
if (
element[contractKey] instanceof Array &&
element[contractKey].length > 0
) {
const temp = {};
element[contractKey].forEach((arrayElement) => {
temp[arrayElement] = mediaContract[arrayElement];
});
element[contractKey] = temp;
}
});
finalMCObjects.contracts.push(element);
}
});
return finalMCObjects;
};
const getContractFromMCO = (ttl) => {
const jsonLDGraph = {};
const mediaContractualObjects = {};
const jsonld = parseTTL(ttl);
jsonld['@graph'].forEach((element) => {
jsonLDGraph[element['@id']] = element;
});
// Search for all contract objects
Object.values(jsonLDGraph).forEach((element) => {
const classData = lut.AllClasses[getType(element).toLowerCase()];
if (classData[0] === 'Contract') {
handleContract(jsonLDGraph, mediaContractualObjects, classData, element);
}
});
// Search for all deontic expression objects
Object.values(jsonLDGraph).forEach((element) => {
const classData = lut.AllClasses[getType(element).toLowerCase()];
if (classData[0] === 'MCODeonticExpression') {
handleMCODeonticExpression(
jsonLDGraph,
mediaContractualObjects,
classData,
element
);
}
});
return formatIntoMediaContractualObjects(mediaContractualObjects);
};
module.exports = { getContractFromMCO };