/** * Bridge state machine states * * **Poka-Yoke**: Enum prevents invalid states * Valid transitions: * - Uninitialized => Ready (after constructor with valid store) * - Ready => Operating => Ready (during operations) * - Any => Error (on failures) * - Any => Destroyed (terminal state) * * @typedef {'Uninitialized' | 'Ready' | 'Operating' | 'Error' | 'Destroyed'} BridgeState */ /** * OxigraphBridge - High-level API for BEAM to Oxigraph communication * * Provides methods for adding, querying, and removing RDF triples. * Designed for integration with AtomVM BEAM runtime. * * **Poka-Yoke Design**: * - State machine prevents invalid operations (cannot operate without store) * - Validation prevents invalid inputs (triples must have required properties) * - OTEL instrumentation for observability * * @example * ```javascript * import { createStore, dataFactory } from '@unrdf/oxigraph'; * import { OxigraphBridge } from '@unrdf/atomvm'; * * const store = createStore(); * const bridge = new OxigraphBridge(store); * * // Add triples * const subject = dataFactory.namedNode('http://example.org/s'); * const predicate = dataFactory.namedNode('http://example.org/p'); * const object = dataFactory.literal('value'); * * await bridge.addTriples([{ subject, predicate, object }]); * * // Query pattern * const results = await bridge.queryPattern(subject, null, null); * ``` */ export class OxigraphBridge { /** * Create a new OxigraphBridge instance * * **Poka-Yoke**: Validates store at construction time * * @param {import('@unrdf/oxigraph').OxigraphStore} store - Oxigraph store instance from @unrdf/oxigraph * @throws {Error} If store is null, undefined, or invalid */ constructor(store: import("@unrdf/oxigraph").OxigraphStore); /** * Get the current bridge state * @returns {BridgeState} Current state */ get state(): BridgeState; /** * Get operation statistics * @returns {{addCount: number, queryCount: number, removeCount: number, errorCount: number}} Stats */ get stats(): { addCount: number; queryCount: number; removeCount: number; errorCount: number; }; /** * Check if bridge is ready for operations * * **Poka-Yoke**: Prevents operations in invalid states * * @returns {boolean} True if bridge is ready */ isReady(): boolean; /** * Add multiple triples to the store in batch * * Accepts triples from BEAM messages and adds them to Oxigraph. * Supports both { subject, predicate, object } and { s, p, o } formats. * * **Poka-Yoke**: Validates all triples before adding any * * @param {Array<{subject?: object, predicate?: object, object?: object, s?: object, p?: object, o?: object}>} triples - Array of triple objects * @returns {Promise<{success: boolean, count: number, errors: Array}>} Result with success status and count * @throws {Error} If bridge is not ready or triples array is invalid * * @example * ```javascript * const result = await bridge.addTriples([ * { subject: s1, predicate: p1, object: o1 }, * { s: s2, p: p2, o: o2 }, * ]); * console.log(result.count); // 2 * ``` */ addTriples(triples: Array<{ subject?: object; predicate?: object; object?: object; s?: object; p?: object; o?: object; }>): Promise<{ success: boolean; count: number; errors: Array; }>; /** * Query triples matching a pattern * * Returns all triples matching the given subject, predicate, and object pattern. * Use null for wildcard matching on any component. * * @param {object|null} subject - Subject to match, or null for wildcard * @param {object|null} predicate - Predicate to match, or null for wildcard * @param {object|null} object - Object to match, or null for wildcard * @returns {Promise>} Matching triples * @throws {Error} If bridge is not ready * * @example * ```javascript * // Get all triples with a specific subject * const results = await bridge.queryPattern(mySubject, null, null); * * // Get all triples (full pattern match) * const allTriples = await bridge.queryPattern(null, null, null); * ``` */ queryPattern(subject: object | null, predicate: object | null, object: object | null): Promise>; /** * Remove multiple triples from the store in batch * * Removes triples matching the given patterns. * Supports both { subject, predicate, object } and { s, p, o } formats. * * **Poka-Yoke**: Validates all triples before removing any * * @param {Array<{subject?: object, predicate?: object, object?: object, s?: object, p?: object, o?: object}>} triples - Array of triple objects to remove * @returns {Promise<{success: boolean, count: number, errors: Array}>} Result with success status and count * @throws {Error} If bridge is not ready or triples array is invalid * * @example * ```javascript * const result = await bridge.removeTriples([ * { subject: s1, predicate: p1, object: o1 }, * ]); * console.log(result.count); // 1 * ``` */ removeTriples(triples: Array<{ subject?: object; predicate?: object; object?: object; s?: object; p?: object; o?: object; }>): Promise<{ success: boolean; count: number; errors: Array; }>; /** * Get all triples from the store * * Returns an async generator that yields triples for memory-efficient streaming. * For small stores, collect all results with Array.from() or for-await-of. * * @returns {Promise>} All triples in the store * @throws {Error} If bridge is not ready * * @example * ```javascript * const allTriples = await bridge.getAllTriples(); * console.log(`Store contains ${allTriples.length} triples`); * ``` */ getAllTriples(): Promise>; /** * Execute a SPARQL query on the store * * Supports SELECT, ASK, CONSTRUCT, and DESCRIBE queries. * This is a placeholder that delegates to the underlying store's query method. * Full SPARQL implementation is handled by Agent 2. * * @param {string} query - SPARQL query string * @returns {Promise|boolean>} Query results (array for SELECT/CONSTRUCT, boolean for ASK) * @throws {Error} If bridge is not ready or query is invalid * * @example * ```javascript * const results = await bridge.sparqlQuery('SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10'); * console.log(`Found ${results.length} results`); * * const exists = await bridge.sparqlQuery('ASK { ?p ?o }'); * console.log(`Triple exists: ${exists}`); * ``` */ sparqlQuery(query: string): Promise | boolean>; /** * Clean up resources and destroy the bridge * * **Poka-Yoke**: Terminal state prevents further operations */ destroy(): void; #private; } /** * Bridge state machine states * * **Poka-Yoke**: Enum prevents invalid states * Valid transitions: * - Uninitialized => Ready (after constructor with valid store) * - Ready => Operating => Ready (during operations) * - Any => Error (on failures) * - Any => Destroyed (terminal state) */ export type BridgeState = "Uninitialized" | "Ready" | "Operating" | "Error" | "Destroyed"; /** * Operation types for OTEL tracking */ export type BRIDGE_OPERATIONS = string; export namespace BRIDGE_OPERATIONS { let ADD_TRIPLES: string; let QUERY_PATTERN: string; let REMOVE_TRIPLES: string; let GET_ALL_TRIPLES: string; let SPARQL_QUERY: string; }