UNPKG

meta-log-db

Version:

Native database package for Meta-Log (ProLog, DataLog, R5RS)

236 lines 8.25 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonlParser = void 0; const fs = __importStar(require("fs")); /** * JSONL Parser for Meta-Log Database * Supports both JSONL and CanvasL formats */ class JsonlParser { constructor() { this.facts = []; this.canvas = null; } /** * Parse JSONL file */ async parse(filePath) { const content = fs.readFileSync(filePath, 'utf-8'); const lines = content.split('\n').filter(line => line.trim()); const objects = []; for (const line of lines) { if (line.trim()) { try { const obj = JSON.parse(line); objects.push(obj); } catch (error) { console.warn(`Failed to parse line: ${line}`, error); } } } this.canvas = this.organizeCanvas(objects); return this.canvas; } /** * Parse CanvasL file (with extensions) */ async parseCanvasL(filePath) { const content = fs.readFileSync(filePath, 'utf-8'); const lines = content.split('\n'); const objects = []; let currentDirective = null; for (const line of lines) { const trimmed = line.trim(); // Handle directives if (trimmed.startsWith('@')) { const match = trimmed.match(/^@(\w+)\s*(.*)$/); if (match) { currentDirective = match[1]; if (match[2]) { objects.push({ type: `@${currentDirective}`, value: match[2] }); } } continue; } // Parse JSONL lines if (trimmed && trimmed.startsWith('{')) { try { const obj = JSON.parse(trimmed); if (currentDirective) { obj._directive = `@${currentDirective}`; } objects.push(obj); } catch (error) { console.warn(`Failed to parse CanvasL line: ${trimmed}`, error); } } } this.canvas = this.organizeCanvas(objects); return this.canvas; } /** * Organize parsed objects into canvas structure */ organizeCanvas(objects) { const canvas = { nodes: [], edges: [], }; for (const obj of objects) { if (obj.type === 'node' || obj.id) { canvas.nodes = canvas.nodes || []; canvas.nodes.push(obj); } else if (obj.type === 'edge' || obj.fromNode || obj.toNode) { canvas.edges = canvas.edges || []; canvas.edges.push(obj); } else { // Store other objects const key = obj.type || 'other'; if (!canvas[key]) { canvas[key] = []; } canvas[key].push(obj); } } return canvas; } /** * Extract facts from canvas */ extractFacts(canvas) { const targetCanvas = canvas || this.canvas; if (!targetCanvas) { return []; } this.facts = []; // Extract node facts if (targetCanvas.nodes) { for (const node of targetCanvas.nodes) { this.facts.push({ predicate: 'node', args: [node.id, node.type || 'unknown', node.x || 0, node.y || 0, node.text || ''] }); // Extract node properties as facts for (const [key, value] of Object.entries(node)) { if (!['id', 'type', 'x', 'y', 'text'].includes(key)) { this.facts.push({ predicate: `node_${key}`, args: [node.id, value] }); } } } } // Extract edge facts if (targetCanvas.edges) { for (const edge of targetCanvas.edges) { this.facts.push({ predicate: 'edge', args: [edge.id, edge.type || 'unknown', edge.fromNode, edge.toNode] }); // Extract vertical/horizontal relationships if (edge.type?.startsWith('v:')) { this.facts.push({ predicate: 'vertical', args: [edge.id, edge.fromNode, edge.toNode] }); } else if (edge.type?.startsWith('h:')) { this.facts.push({ predicate: 'horizontal', args: [edge.id, edge.fromNode, edge.toNode] }); } } } return this.facts; } /** * Convert facts to RDF triples */ toRdf(facts) { const targetFacts = facts || this.facts; const triples = []; for (const fact of targetFacts) { if (fact.predicate === 'node' && fact.args.length >= 2) { const [id, type] = fact.args; triples.push({ subject: `<http://example.org/node/${id}>`, predicate: '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', object: `<http://example.org/type/${type}>` }); } else if (fact.predicate === 'edge' && fact.args.length >= 4) { const [id, type, fromNode, toNode] = fact.args; triples.push({ subject: `<http://example.org/edge/${id}>`, predicate: '<http://example.org/predicate/fromNode>', object: `<http://example.org/node/${fromNode}>` }); triples.push({ subject: `<http://example.org/edge/${id}>`, predicate: '<http://example.org/predicate/toNode>', object: `<http://example.org/node/${toNode}>` }); triples.push({ subject: `<http://example.org/edge/${id}>`, predicate: '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', object: `<http://example.org/type/${type}>` }); } } return triples; } /** * Get extracted facts */ getFacts() { return [...this.facts]; } /** * Clear facts */ clear() { this.facts = []; this.canvas = null; } } exports.JsonlParser = JsonlParser; //# sourceMappingURL=parser.js.map