function queryResponseToHierarchy(
json_query_results,
prefixes,
hierarchySteps,
valueKeyName
) {
const rootNode = {
name: "root",
node_id: 1,
node_depth: 0,
node_count: 0,
children: []
};
let nextId = 2;
let children, foundChild;
for (let seq of json_query_results.results.bindings) {
let currentNode = rootNode;
for (let i = 0; i < hierarchySteps.length - 1; i++) {
if (
typeof seq[hierarchySteps[i]].value !== 'undefined' &&
seq[hierarchySteps[i]].value
) {
children = currentNode.children;
let node_type = nodeType(seq[hierarchySteps[i]]);
//Step 1-2: format name to SPARQL standard if URI (with/without prefix) or literal
let node_name = nodeName(seq[hierarchySteps[i]], node_type);
// Step 1-3: add current step into the hierarchy
foundChild = false;
let childNode = null;
// Step 1-3a: check if current event already exists as a child node
for (let j = 0; j < children.length; j++) {
if (children[j].name == node_name) {
childNode = children[j];
childNode.node_count += 1;
foundChild = true;
break;
}
}
// Step 1-3b: if current event doesn't exist as a child node, create it
if (!foundChild) {
childNode = {
name: node_name,
type: node_type,
node_id: nextId,
node_depth: currentNode.node_depth + 1,
node_count: 1,
children: []
};
children.push(childNode);
nextId += 1;
}
currentNode = childNode;
}
}
// Step 2: at the end of a sequence close off with the end value and node count
// Step 2-1: First ignore all empty/undefined/zero fields
if (
typeof seq[hierarchySteps[hierarchySteps.length - 1]].value !==
'undefined' &&
seq[hierarchySteps[hierarchySteps.length - 1]].value
) {
children = currentNode.children;
// Step 2-2: extract the node type
let node_type = nodeType(seq[hierarchySteps[hierarchySteps.length - 1]]);
//Step 2-3: format name to SPARQL standard if URI (with/without prefix) or literal
let node_name = nodeName(
seq[hierarchySteps[hierarchySteps.length - 1]],
node_type
);
// Step 2-4: if end element already exists as a child node, increment count by 1 and add value
foundChild = false;
for (let j = 0; j < children.length; j++) {
if (children[j].name == node_name) {
children[j].value += +seq[valueKeyName];
children[j].node_count += 1;
foundChild = true;
break;
}
}
// Step 2-5: if end doesn't exist as a child node, create it and set count to 1
if (!foundChild) {
children.push({
name: node_name,
type: node_type,
node_id: nextId,
node_depth: currentNode.node_depth + 1,
node_count: 1,
value: +seq[valueKeyName]
});
}
nextId += 1;
}
}
// Step 3: count nodes in children nodes
for (let j = 0; j < rootNode.children.length; j++) {
rootNode.node_count += rootNode.children[j].node_count;
}
return rootNode;
function nodeType(node) {
let node_type;
if (node.datatype !== 'undefined' && node.datatype) {
node_type = replaceMap(node.datatype, prefixes);
} else {
if (node.value.substring(0, 4) === 'http') {
node_type = 'URI';
} else {
node_type = 'Label';
}
}
return node_type;
}
function nodeName(node, node_type) {
let node_name;
node_name = replaceMap(node.value, prefixes);
if (node.type === 'uri') {
if (node_name.substring(0, 4) === 'http') {
node_name = '<' + node_name + '>';
}
} else if (node_type === 'Label') {
} else {
node_name = '"' + node_name + '"^^' + node_type;
}
return node_name;
}
}