|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fs = require('fs'); |
|
const path = require('path'); |
|
|
|
|
|
const sourcePath = path.join(__dirname, 'graph.json'); |
|
const outputPath = path.join(__dirname, 'sigma_graph.json'); |
|
|
|
|
|
let config; |
|
try { |
|
config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8')); |
|
} catch (error) { |
|
console.error('Error loading config.json:', error); |
|
config = { |
|
nodeTypes: { |
|
paper: { color: '#FF9A00', size: 3 }, |
|
author: { color: '#62D600', size: 5 }, |
|
organization: { color: '#020AA7', size: 4 }, |
|
unknown: { color: '#ff7f0e', size: 3 } |
|
} |
|
}; |
|
} |
|
|
|
|
|
function convertGraph() { |
|
try { |
|
|
|
const graphData = JSON.parse(fs.readFileSync(sourcePath, 'utf8')); |
|
|
|
|
|
const sigmaData = { |
|
nodes: [], |
|
edges: [] |
|
}; |
|
|
|
|
|
graphData.nodes.forEach(node => { |
|
|
|
const nodeType = node.attributes.type || 'unknown'; |
|
const nodeColor = (nodeType && config.nodeTypes && config.nodeTypes[nodeType]) |
|
? config.nodeTypes[nodeType].color |
|
: node.attributes.color || '#666'; |
|
|
|
|
|
const sigmaNode = { |
|
id: node.key, |
|
label: node.attributes.label || node.key, |
|
x: node.attributes.x || 0, |
|
y: node.attributes.y || 0, |
|
size: node.attributes.size || 1, |
|
color: nodeColor, |
|
type: nodeType, |
|
attr: { colors: {} } |
|
}; |
|
|
|
|
|
if (nodeType) { |
|
sigmaNode.attr.colors.type = nodeType; |
|
} |
|
|
|
|
|
for (const [key, value] of Object.entries(node.attributes)) { |
|
if (!['label', 'x', 'y', 'size', 'color', 'type'].includes(key)) { |
|
sigmaNode[key] = value; |
|
} |
|
} |
|
|
|
sigmaData.nodes.push(sigmaNode); |
|
}); |
|
|
|
|
|
let edgeCount = 0; |
|
graphData.edges.forEach(edge => { |
|
|
|
const sigmaEdge = { |
|
id: edge.key || `e${edgeCount++}`, |
|
source: edge.source, |
|
target: edge.target |
|
}; |
|
|
|
|
|
if (edge.attributes && edge.attributes.weight) { |
|
sigmaEdge.weight = edge.attributes.weight; |
|
} |
|
|
|
|
|
if (edge.attributes && edge.attributes.label) { |
|
sigmaEdge.label = edge.attributes.label; |
|
} |
|
|
|
sigmaData.edges.push(sigmaEdge); |
|
}); |
|
|
|
|
|
fs.writeFileSync(outputPath, JSON.stringify(sigmaData, null, 2)); |
|
|
|
return true; |
|
} catch (error) { |
|
console.error('Error during conversion:', error); |
|
return false; |
|
} |
|
} |
|
|
|
|
|
convertGraph(); |
|
|
|
|
|
module.exports = { convertGraph }; |