meta-log-db
Version:
Native database package for Meta-Log (ProLog, DataLog, R5RS)
358 lines • 10.7 kB
JavaScript
"use strict";
/**
* Browser-Native Meta-Log Database
*
* Browser-specific implementation of MetaLogDb using browser file I/O and IndexedDB
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetaLogDbBrowser = void 0;
const engine_js_1 = require("../prolog/engine.js");
const engine_js_2 = require("../datalog/engine.js");
const triple_store_js_1 = require("../rdf/triple-store.js");
const validator_js_1 = require("../shacl/validator.js");
const browser_parser_js_1 = require("./jsonl/browser-parser.js");
const browser_registry_js_1 = require("./r5rs/browser-registry.js");
const io_js_1 = require("./io.js");
const indexeddb_storage_js_1 = require("./indexeddb-storage.js");
const index_js_1 = require("../extensions/homology/index.js");
/**
* Browser-Native Meta-Log Database
*/
class MetaLogDbBrowser {
constructor(config = {}) {
this.initialized = false;
this.config = {
enableProlog: true,
enableDatalog: true,
enableRdf: true,
enableShacl: true,
enableEncryption: false,
cacheStrategy: 'both',
indexedDBName: 'meta-log-db',
...config
};
// Initialize file I/O
this.fileIO = new io_js_1.BrowserFileIO({
enableCache: true,
cacheStrategy: this.config.cacheStrategy,
indexedDBName: this.config.indexedDBName
});
// Initialize IndexedDB storage
this.storage = new indexeddb_storage_js_1.IndexedDBStorage({
dbName: this.config.indexedDBName
});
// Initialize JSONL parser
this.jsonl = new browser_parser_js_1.BrowserJsonlParser({
enableEncryption: this.config.enableEncryption,
mnemonic: this.config.mnemonic,
encryptionPurpose: 'local',
fileIO: this.fileIO
});
// Initialize engines
if (this.config.enableProlog) {
this.prolog = new engine_js_1.PrologEngine();
}
if (this.config.enableDatalog) {
this.datalog = new engine_js_2.DatalogEngine();
}
if (this.config.enableRdf) {
this.rdf = new triple_store_js_1.TripleStore();
}
if (this.config.enableShacl) {
this.shacl = new validator_js_1.ShaclValidator();
}
if (this.config.r5rsEnginePath || this.config.r5rsEngineURL) {
this.loadR5RSEngine(this.config.r5rsEnginePath || this.config.r5rsEngineURL);
}
else {
// Initialize R5RS registry without file
this.r5rs = new browser_registry_js_1.BrowserR5RSRegistry({
enableEncryption: this.config.enableEncryption,
mnemonic: this.config.mnemonic,
encryptionPurpose: 'local',
fileIO: this.fileIO
});
}
}
/**
* Initialize browser database (async initialization)
*/
async init() {
if (this.initialized) {
return;
}
await this.fileIO.init();
await this.storage.init();
await this.jsonl.init();
if (this.r5rs) {
await this.r5rs.init();
}
this.initialized = true;
}
/**
* Load R5RS engine from URL or IndexedDB
*/
async loadR5RSEngine(pathOrURL) {
this.r5rs = new browser_registry_js_1.BrowserR5RSRegistry({
enableEncryption: this.config.enableEncryption,
mnemonic: this.config.mnemonic,
encryptionPurpose: 'local',
fileIO: this.fileIO
});
await this.r5rs.init();
// Determine if it's a URL or file path
const isURL = pathOrURL.startsWith('http://') || pathOrURL.startsWith('https://') || pathOrURL.startsWith('/');
if (isURL) {
await this.r5rs.load(pathOrURL, pathOrURL);
}
else {
await this.r5rs.load(pathOrURL);
}
}
/**
* Load canvas file (JSONL or CanvasL) from URL or IndexedDB
*/
async loadCanvas(path, url) {
if (!this.initialized) {
await this.init();
}
let canvas;
if (path.endsWith('.canvasl')) {
canvas = await this.jsonl.parseCanvasL(path, url);
}
else {
canvas = await this.jsonl.parse(path, url);
}
const facts = this.jsonl.extractFacts(canvas);
if (this.prolog) {
this.prolog.addFacts(facts);
}
if (this.datalog) {
this.datalog.addFacts(facts);
}
if (this.rdf) {
const triples = this.jsonl.toRdf(facts);
this.rdf.addTriples(triples);
}
}
/**
* Parse JSONL canvas
*/
async parseJsonlCanvas(path, url) {
if (!this.initialized) {
await this.init();
}
return await this.jsonl.parse(path, url);
}
/**
* Parse CanvasL file
*/
async parseCanvasL(path, url) {
if (!this.initialized) {
await this.init();
}
return await this.jsonl.parseCanvasL(path, url);
}
/**
* Extract facts from canvas
*/
extractFactsFromCanvas(canvas) {
return this.jsonl.extractFacts(canvas);
}
/**
* Extract facts
*/
extractFacts() {
return this.jsonl.getFacts();
}
/**
* Convert JSONL facts to RDF
*/
jsonlToRdf(facts) {
return this.jsonl.toRdf(facts);
}
/**
* Execute ProLog query
*/
async prologQuery(query) {
if (!this.prolog) {
throw new Error('ProLog engine not enabled');
}
return await this.prolog.query(query);
}
/**
* Build ProLog database from facts
*/
buildPrologDb(facts) {
if (!this.prolog) {
throw new Error('ProLog engine not enabled');
}
this.prolog.buildDb(facts);
}
/**
* Add ProLog rule
*/
addPrologRule(rule) {
if (!this.prolog) {
throw new Error('ProLog engine not enabled');
}
// Parse rule string into PrologRule format
const match = rule.match(/^(.+?)\s*:-\s*(.+)$/);
if (match) {
const head = match[1].trim();
const body = match[2].split(',').map(b => b.trim());
this.prolog.addRule({ head, body });
}
}
/**
* Execute DataLog query
*/
async datalogQuery(query, program) {
if (!this.datalog) {
throw new Error('DataLog engine not enabled');
}
return await this.datalog.query(query, program);
}
/**
* Build DataLog program
*/
buildDatalogProgram(rules) {
if (!this.datalog) {
throw new Error('DataLog engine not enabled');
}
const parsedRules = rules.map(rule => {
const match = rule.match(/^(.+?)\s*:-\s*(.+)$/);
if (match) {
return {
head: match[1].trim(),
body: match[2].split(',').map(b => b.trim())
};
}
throw new Error(`Invalid rule format: ${rule}`);
});
return this.datalog.buildProgram(parsedRules);
}
/**
* Execute SPARQL query
*/
async sparqlQuery(query) {
if (!this.rdf) {
throw new Error('RDF engine not enabled');
}
return await this.rdf.sparql(query);
}
/**
* Store RDF triples
*/
storeTriples(triples) {
if (!this.rdf) {
throw new Error('RDF engine not enabled');
}
this.rdf.addTriples(triples);
}
/**
* RDFS entailment
*/
rdfsEntailment(triples) {
if (!this.rdf) {
throw new Error('RDF engine not enabled');
}
return this.rdf.rdfsEntailment(triples);
}
/**
* Load SHACL shapes from URL or IndexedDB
*/
async loadShaclShapes(path, url) {
if (!this.shacl) {
throw new Error('SHACL validator not enabled');
}
if (!this.initialized) {
await this.init();
}
// Load shapes file
const content = await this.fileIO.loadFile(path, url);
// Parse and load shapes (simplified - full implementation would parse Turtle)
return await this.shacl.loadShapes(path);
}
/**
* Validate SHACL
*/
async validateShacl(shapes, triples) {
if (!this.shacl) {
throw new Error('SHACL validator not enabled');
}
const targetTriples = triples || (this.rdf ? this.rdf.getTriples() : []);
return await this.shacl.validate(shapes || {}, targetTriples);
}
/**
* Execute R5RS function
*/
async executeR5RS(functionName, args) {
if (!this.r5rs) {
throw new Error('R5RS engine not loaded');
}
return await this.r5rs.execute(functionName, args);
}
/**
* Register R5RS function
*/
registerR5RSFunction(name, fn) {
if (!this.r5rs) {
this.r5rs = new browser_registry_js_1.BrowserR5RSRegistry({
enableEncryption: this.config.enableEncryption,
mnemonic: this.config.mnemonic,
encryptionPurpose: 'local',
fileIO: this.fileIO
});
}
this.r5rs.register(name, fn);
}
/**
* Get configuration
*/
getConfig() {
return { ...this.config };
}
/**
* Get file I/O instance
*/
getFileIO() {
return this.fileIO;
}
/**
* Get IndexedDB storage instance
*/
getStorage() {
return this.storage;
}
/**
* Clear cache
*/
async clearCache() {
await this.fileIO.clearCache();
}
/**
* Validate homology of chain complex
* Requires enableHomology: true in config
*/
validateHomology(complex) {
if (!this.config.enableHomology) {
throw new Error('Homology extension not enabled. Set enableHomology: true in config');
}
const validator = new index_js_1.HomologyValidator(complex);
return validator.validate();
}
/**
* Compute Betti number for dimension n
* Requires enableHomology: true in config
*/
computeBetti(complex, n) {
if (!this.config.enableHomology) {
throw new Error('Homology extension not enabled. Set enableHomology: true in config');
}
const validator = new index_js_1.HomologyValidator(complex);
return validator.computeBetti(n);
}
}
exports.MetaLogDbBrowser = MetaLogDbBrowser;
//# sourceMappingURL=database.js.map