Files
scaenae/src/ontology.js

70 lines
1.6 KiB
JavaScript

/**
* @module Ontology
*/
/**
* @todo Temporarily returns domains and ontology labels only
* Traverse an ontology from its JSON description
* @param {String} jsonPath The path (URI) of the ontology JSON file
* @returns {Object}
*/
export async function traverseOntology(jsonPath) {
const ontology = await loadOntology(jsonPath);
const modules = ontology.modules;
let tree = [];
for (const module of modules) {
if (module.id === 'Architecture') {
console.debug(module.classTrees);
for (const classTree of module.classTrees) {
traverseClasses(classTree, tree);
}
}
}
return {
ontology: "Architecture",
domains: tree
};
}
/**
*
* @param {Object} ontologyClass
* @param {Array<Object>} tree
* @param {Number} depth
*/
function traverseClasses(ontologyClass, tree, depth = 1) {
const ontologyNode = {
id: ontologyClass.id,
label: ontologyClass.label,
depth,
children: [],
};
tree.push(ontologyNode);
if (ontologyClass.children && Array.isArray(ontologyClass.children)) {
for(const child of ontologyClass.children) {
ontologyNode.children.push(
traverseClasses(child, tree, depth + 1)
);
}
}
}
/**
* Load an ontology from its JSON description
* @param {String} jsonPath The path (URI) of the ontology JSON file
* @returns {Object}
*/
export async function loadOntology(jsonPath) {
const ontology = await fetch(jsonPath)
.then(res => res.json())
.catch(err => console.error(err));
return ontology;
}