UNPKG

meta-log-db

Version:

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

513 lines 16.7 kB
"use strict"; /** * CanvasL Metaverse Browser - Unified Browser API for CanvasL Operations * * Provides a unified interface for CanvasL metaverse browser functionality, * consolidating implementations from template-projector and ui packages. * * Features: * - CanvasL/JSONL file loading and parsing * - ProLog, DataLog, SPARQL query execution * - R5RS function execution * - SHACL validation * - CanvasL object execution (rdf-triple, r5rs-call, sparql-construct, etc.) * - Browser-native with IndexedDB caching */ 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.CanvasLMetaverseBrowser = void 0; /** * Unified CanvasL Metaverse Browser */ class CanvasLMetaverseBrowser { constructor(config = {}) { this.db = null; this.initialized = false; this.initPromise = null; this.config = { enableProlog: true, enableDatalog: true, enableRdf: true, enableShacl: true, enableEncryption: false, cacheStrategy: 'both', indexedDBName: 'meta-log-db', ...config }; } /** * Initialize browser database * Uses lazy initialization with promise-based pattern to prevent race conditions */ async init() { if (this.initialized) { return; } // If initialization is already in progress, wait for it if (this.initPromise) { return this.initPromise; } this.initPromise = this._doInit(); return this.initPromise; } async _doInit() { try { // Dynamic import to avoid bundling issues const { MetaLogDbBrowser } = await Promise.resolve().then(() => __importStar(require('./database.js'))); // Create browser-native database instance this.db = new MetaLogDbBrowser(this.config); // Initialize (sets up IndexedDB, file I/O, etc.) await this.db.init(); this.initialized = true; } catch (error) { console.error('Failed to initialize CanvasLMetaverseBrowser:', error); throw new Error(`CanvasL Metaverse Browser initialization failed: ${error instanceof Error ? error.message : String(error)}`); } finally { this.initPromise = null; } } /** * Load CanvasL/JSONL file from URL or path * Standardized parameter order: path (identifier), url (optional fetch location) */ async loadCanvas(path, url) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } try { // Use path as identifier, url as the actual URL to fetch // If url is not provided, use path as both const fileUrl = url || path; await this.db.loadCanvas(path, fileUrl); } catch (error) { throw new Error(`Failed to load canvas from ${url || path}: ${error instanceof Error ? error.message : String(error)}`); } } /** * Parse JSONL canvas from URL without loading into database */ async parseJsonlCanvas(path, url) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.parseJsonlCanvas(path, url); } /** * Parse CanvasL file from URL without loading into database */ async parseCanvasL(path, url) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.parseCanvasL(path, url); } /** * Extract facts from loaded canvas */ extractFacts(canvasFile) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return this.db.extractFacts(); } /** * Convert facts to RDF triples */ jsonlToRdf(facts) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return this.db.jsonlToRdf(facts); } /** * Execute ProLog query */ async prologQuery(query, options) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } // Add facts if provided if (options?.facts && options.facts.length > 0) { this.db.buildPrologDb(options.facts); } return await this.db.prologQuery(query); } /** * Execute DataLog query */ async datalogQuery(goal, program, options) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.datalogQuery(goal, program); } /** * Execute SPARQL query */ async sparqlQuery(query, options) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.sparqlQuery(query); } /** * Validate with SHACL */ async validateShacl(shapes, triples) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.validateShacl(shapes, triples); } /** * Execute R5RS function */ async executeR5RS(functionName, args = []) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } return await this.db.executeR5RS(functionName, args); } /** * Get R5RS function (if available) */ async getR5RSFunction(name) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } // Try to access R5RS registry through the database instance const db = this.db; if (db.r5rs) { const fn = db.r5rs.getFunction(name); if (fn) { return { name, function: fn, available: true }; } } return null; } /** * List R5RS functions */ async listR5RSFunctions(pattern) { await this.init(); if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } // Try to access R5RS registry through the database instance const db = this.db; if (db.r5rs) { let functions = db.r5rs.getFunctionNames(); // Filter by pattern if provided if (pattern) { const regex = new RegExp(pattern, 'i'); functions = functions.filter((name) => regex.test(name)); } return functions; } return []; } /** * Invoke R5RS function (alias for executeR5RS) */ async invokeR5RSFunction(name, args, context) { // MetaLogDbBrowser doesn't support context parameter directly // Context would need to be handled at a higher level return await this.executeR5RS(name, args); } /** * Add ProLog rule */ addPrologRule(rule) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } this.db.addPrologRule(rule); } /** * Add DataLog rule */ addDatalogRule(rule) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } // MetaLogDbBrowser uses buildDatalogProgram instead of addDatalogRule const db = this.db; if (db.buildDatalogProgram) { db.buildDatalogProgram([rule]); } } /** * Store RDF triples */ storeTriples(triples) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } this.db.storeTriples(triples); } /** * Add ProLog facts */ addPrologFacts(facts) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } this.db.buildPrologDb(facts); } /** * Add DataLog facts */ addDatalogFacts(facts) { if (!this.db) { throw new Error('MetaLogDbBrowser not initialized'); } // Facts are added when loading canvas files // For direct addition, we'd need to build a DataLog program const db = this.db; if (db.datalog) { db.datalog.addFacts(facts); } } /** * Clear all data */ async clear() { if (this.db) { await this.db.clearCache(); } } /** * Check if browser is initialized */ isInitialized() { return this.initialized && this.db !== null; } /** * Get the underlying database instance (for advanced usage) */ getDb() { return this.db; } /** * Execute a CanvasL object * Supports: rdf-triple, r5rs-call, sparql-construct, prolog-query, datalog-query, shacl-validate, slide */ async executeCanvasLObject(obj) { await this.init(); try { switch (obj.type) { case 'rdf-triple': return await this.executeRdfTriple(obj); case 'r5rs-call': return await this.executeR5RSCall(obj); case 'sparql-construct': return await this.executeSparqlConstruct(obj); case 'prolog-query': return await this.executePrologQuery(obj); case 'datalog-query': return await this.executeDatalogQuery(obj); case 'shacl-validate': return await this.executeShaclValidate(obj); case 'slide': return { type: 'slide', result: obj }; default: console.warn(`Unknown CanvasL object type: ${obj.type}`); return { type: 'unknown', result: obj }; } } catch (error) { return { type: obj.type || 'error', error: error instanceof Error ? error.message : String(error), object: obj }; } } /** * Execute RDF triple */ async executeRdfTriple(obj) { const triple = { subject: obj.subject, predicate: obj.predicate, object: obj.object }; this.storeTriples([triple]); return { type: 'rdf-triple', result: triple }; } /** * Execute R5RS function call */ async executeR5RSCall(obj) { // Handle different R5RS call formats let functionName; let args = []; if (obj.function) { functionName = obj.function; args = obj.args || []; } else if (obj.expression) { // Parse expression like "(r5rs:church-add 2 3)" const match = obj.expression.match(/^\(([^\s]+)\s*(.*)\)$/); if (match) { functionName = match[1]; // Simple argument parsing (could be enhanced) const argStr = match[2].trim(); if (argStr) { args = argStr.split(/\s+/).map((a) => { // Try to parse as number const num = Number(a); return isNaN(num) ? a : num; }); } } else { throw new Error(`Invalid R5RS expression format: ${obj.expression}`); } } else { throw new Error('R5RS call object missing function or expression'); } const result = await this.executeR5RS(functionName, args); return { type: 'r5rs-result', result }; } /** * Execute SPARQL CONSTRUCT query */ async executeSparqlConstruct(obj) { const query = typeof obj.query === 'string' ? obj.query : obj.query?.template || obj.query; const result = await this.sparqlQuery(query); // If CONSTRUCT query, triples are already added to store return { type: 'sparql-result', result }; } /** * Execute ProLog query */ async executePrologQuery(obj) { const query = obj.query || obj.goal; const facts = obj.facts || []; const result = await this.prologQuery(query, { facts }); return { type: 'prolog-result', result }; } /** * Execute DataLog query */ async executeDatalogQuery(obj) { const goal = obj.goal || obj.query; const program = obj.program || null; const result = await this.datalogQuery(goal, program); return { type: 'datalog-result', result }; } /** * Execute SHACL validation */ async executeShaclValidate(obj) { const shapes = obj.shapes || obj.shape; const triples = obj.triples || obj.focus || null; const result = await this.validateShacl(shapes, triples); return { type: 'shacl-result', result }; } /** * Execute multiple CanvasL objects */ async executeCanvasLObjects(objects) { const results = { triples: [], slides: [], objects: new Map(), errors: [] }; for (const obj of objects) { try { // Skip directives and macros if (obj['@include'] || obj.type === '@include' || obj['@version'] || obj.type === 'macro') { continue; } // Handle slide objects directly if (obj.type === 'slide') { if (!obj.id) { console.warn('Slide object missing id:', obj); } if (!obj.dimension) { obj.dimension = '0D'; } results.slides.push(obj); if (obj.id) { results.objects.set(obj.id, obj); } continue; } const result = await this.executeCanvasLObject(obj); // Store object by ID if it has one if (obj.id) { results.objects.set(obj.id, result); } if (result.type === 'rdf-triple') { results.triples.push(result.result); } else if (result.type === 'slide') { results.slides.push(result.result); } else if (result.error) { results.errors.push({ object: obj, error: result.error }); } } catch (error) { results.errors.push({ object: obj, error: error instanceof Error ? error.message : String(error) }); } } return results; } } exports.CanvasLMetaverseBrowser = CanvasLMetaverseBrowser; //# sourceMappingURL=canvasl-browser.js.map