From 9e10caf6047203eab3e729e4abdf75dd447a5081 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Wed, 4 Jun 2025 10:01:03 -0700 Subject: [PATCH] feat: add examples for read-only mode, sequential pipeline, and configuration tests Added illustrative example scripts to demonstrate read-only mode functionality, sequential pipeline processing, and automatic configuration detection. --- .idea/material_theme_project_new.xml | 4 +- examples/configurationTest.js | 74 ++++++++ examples/readOnlyTest.js | 72 ++++++++ examples/sequentialPipelineExample.js | 235 ++++++++++++++++++++++++++ 4 files changed, 382 insertions(+), 3 deletions(-) create mode 100644 examples/configurationTest.js create mode 100644 examples/readOnlyTest.js create mode 100644 examples/sequentialPipelineExample.js diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml index 4bc91b71..9f8a89bb 100644 --- a/.idea/material_theme_project_new.xml +++ b/.idea/material_theme_project_new.xml @@ -9,9 +9,7 @@ \ No newline at end of file diff --git a/examples/configurationTest.js b/examples/configurationTest.js new file mode 100644 index 00000000..c998e74f --- /dev/null +++ b/examples/configurationTest.js @@ -0,0 +1,74 @@ +// Configuration Test Script +// This script tests the automatic configuration detection features of the library + +const { BrainyData } = require('../dist/index.js'); + +async function testDefaultConfiguration() { + console.log('Testing default configuration...'); + + // Create a database with no configuration + const db = new BrainyData(); + await db.init(); + + // Add a test vector + const id = await db.add('This is a test vector', { test: true }); + console.log(`Added test vector with ID: ${id}`); + + // Get the vector back + const vector = await db.get(id); + console.log('Retrieved vector:', vector.metadata); + + // Get storage status + const status = await db.status(); + console.log('Storage status:', status); + + // Clean up + await db.clear(); + console.log('Database cleared'); + + console.log('Default configuration test completed successfully!'); +} + +async function testStorageConfiguration() { + console.log('\nTesting storage configuration...'); + + // Create a database with storage configuration + const db = new BrainyData({ + storage: { + // Force in-memory storage for testing + forceMemoryStorage: true + } + }); + await db.init(); + + // Add a test vector + const id = await db.add('This is a test vector with storage config', { test: true }); + console.log(`Added test vector with ID: ${id}`); + + // Get the vector back + const vector = await db.get(id); + console.log('Retrieved vector:', vector.metadata); + + // Get storage status + const status = await db.status(); + console.log('Storage status:', status); + + // Clean up + await db.clear(); + console.log('Database cleared'); + + console.log('Storage configuration test completed successfully!'); +} + +async function runTests() { + try { + await testDefaultConfiguration(); + await testStorageConfiguration(); + console.log('\nAll tests completed successfully!'); + } catch (error) { + console.error('Error running tests:', error); + } +} + +// Run the tests +runTests(); diff --git a/examples/readOnlyTest.js b/examples/readOnlyTest.js new file mode 100644 index 00000000..d5541bbd --- /dev/null +++ b/examples/readOnlyTest.js @@ -0,0 +1,72 @@ +/** + * Read-Only Mode Test + * + * This example demonstrates how to use the read-only mode feature of BrainyData. + */ + +import { BrainyData } from '../dist/index.js'; + +async function testReadOnlyMode() { + console.log('Testing read-only mode...'); + + // Test 1: Create a database in read-only mode + console.log('\nTest 1: Create a database in read-only mode'); + const readOnlyDb = new BrainyData({ readOnly: true }); + await readOnlyDb.init(); + + console.log('Database initialized in read-only mode'); + console.log('Is read-only:', readOnlyDb.isReadOnly()); + + // Try to add data (should throw an error) + try { + console.log('Attempting to add data to read-only database...'); + await readOnlyDb.add([0.1, 0.2, 0.3], { name: 'test' }); + console.log('ERROR: Add operation succeeded but should have failed!'); + } catch (error) { + console.log('Expected error caught:', error.message); + } + + // Test 2: Toggle read-only mode at runtime + console.log('\nTest 2: Toggle read-only mode at runtime'); + const db = new BrainyData(); + await db.init(); + + console.log('Database initialized in writable mode'); + console.log('Is read-only:', db.isReadOnly()); + + // Add data while writable + console.log('Adding data while writable...'); + const itemId = await db.add([0.1, 0.2, 0.3], { name: 'test' }); + console.log('Added item with ID:', itemId); + + // Set to read-only mode + console.log('Setting database to read-only mode'); + db.setReadOnly(true); + console.log('Is read-only:', db.isReadOnly()); + + // Try to delete data (should throw an error) + try { + console.log('Attempting to delete data from read-only database...'); + await db.delete(itemId); + console.log('ERROR: Delete operation succeeded but should have failed!'); + } catch (error) { + console.log('Expected error caught:', error.message); + } + + // Set back to writable mode + console.log('Setting database back to writable mode'); + db.setReadOnly(false); + console.log('Is read-only:', db.isReadOnly()); + + // Delete data (should succeed) + console.log('Deleting data while writable...'); + const deleteResult = await db.delete(itemId); + console.log('Delete result:', deleteResult); + + console.log('\nAll tests completed successfully!'); +} + +// Run the tests +testReadOnlyMode().catch(error => { + console.error('Test failed:', error); +}); diff --git a/examples/sequentialPipelineExample.js b/examples/sequentialPipelineExample.js new file mode 100644 index 00000000..a08d2e80 --- /dev/null +++ b/examples/sequentialPipelineExample.js @@ -0,0 +1,235 @@ +/** + * Sequential Pipeline Example + * + * This example demonstrates how to use the sequential pipeline to process data + * through a sequence of augmentations: ISense -> IMemory -> ICognition -> IConduit -> IActivation -> IPerception + */ + +import { + sequentialPipeline, + registerAugmentation, + initializeAugmentationPipeline, + createMemoryAugmentation +} from '../dist/index.js'; + +// Create a simple ISense augmentation +const senseAugmentation = { + name: 'SimpleSense', + description: 'A simple sense augmentation for testing', + enabled: true, + + async initialize() {}, + async shutDown() {}, + async getStatus() { return 'active'; }, + + processRawData(rawData, dataType) { + console.log(`[SimpleSense] Processing ${dataType} data: ${rawData}`); + return { + success: true, + data: { + nouns: ['example', 'test', 'data'], + verbs: ['process', 'analyze', 'test'] + } + }; + }, + + async listenToFeed(feedUrl, callback) { + console.log(`[SimpleSense] Listening to feed: ${feedUrl}`); + } +}; + +// Create a simple ICognition augmentation +const cognitionAugmentation = { + name: 'SimpleCognition', + description: 'A simple cognition augmentation for testing', + enabled: true, + + async initialize() {}, + async shutDown() {}, + async getStatus() { return 'active'; }, + + reason(query, context) { + console.log(`[SimpleCognition] Reasoning about: ${query}`); + console.log(`[SimpleCognition] Context:`, context); + return { + success: true, + data: { + inference: 'This is test data that needs to be processed', + confidence: 0.85 + } + }; + }, + + infer(dataSubset) { + return { + success: true, + data: { result: 'inferred data' } + }; + }, + + executeLogic(ruleId, input) { + return { + success: true, + data: true + }; + } +}; + +// Create a simple IConduit augmentation +const conduitAugmentation = { + name: 'SimpleConduit', + description: 'A simple conduit augmentation for testing', + enabled: true, + + async initialize() {}, + async shutDown() {}, + async getStatus() { return 'active'; }, + + establishConnection(targetSystemId, config) { + console.log(`[SimpleConduit] Establishing connection to: ${targetSystemId}`); + return { + success: true, + data: { connectionId: 'test-connection' } + }; + }, + + readData(query, options) { + return { + success: true, + data: { result: 'read data' } + }; + }, + + writeData(data, options) { + console.log(`[SimpleConduit] Writing data:`, data); + return { + success: true, + data: { written: true } + }; + }, + + async monitorStream(streamId, callback) { + console.log(`[SimpleConduit] Monitoring stream: ${streamId}`); + } +}; + +// Create a simple IActivation augmentation +const activationAugmentation = { + name: 'SimpleActivation', + description: 'A simple activation augmentation for testing', + enabled: true, + + async initialize() {}, + async shutDown() {}, + async getStatus() { return 'active'; }, + + triggerAction(actionName, parameters) { + console.log(`[SimpleActivation] Triggering action: ${actionName}`); + console.log(`[SimpleActivation] Parameters:`, parameters); + return { + success: true, + data: { triggered: true } + }; + }, + + generateOutput(knowledgeId, format) { + return { + success: true, + data: 'Generated output' + }; + }, + + interactExternal(systemId, payload) { + return { + success: true, + data: { result: 'external interaction' } + }; + } +}; + +// Create a simple IPerception augmentation +const perceptionAugmentation = { + name: 'SimplePerception', + description: 'A simple perception augmentation for testing', + enabled: true, + + async initialize() {}, + async shutDown() {}, + async getStatus() { return 'active'; }, + + interpret(nouns, verbs, context) { + console.log(`[SimplePerception] Interpreting nouns:`, nouns); + console.log(`[SimplePerception] Interpreting verbs:`, verbs); + console.log(`[SimplePerception] Context:`, context); + return { + success: true, + data: { + interpretation: 'This is a test data sample that needs processing and analysis', + confidence: 0.9 + } + }; + }, + + organize(data, criteria) { + return { + success: true, + data: { organized: true } + }; + }, + + generateVisualization(data, visualizationType) { + return { + success: true, + data: 'Visualization data' + }; + } +}; + +async function runExample() { + try { + // Register augmentations + registerAugmentation(senseAugmentation); + registerAugmentation(cognitionAugmentation); + registerAugmentation(conduitAugmentation); + registerAugmentation(activationAugmentation); + registerAugmentation(perceptionAugmentation); + + // Create and register a memory augmentation + const memoryAugmentation = await createMemoryAugmentation('SimpleMemory', { storageType: 'memory' }); + registerAugmentation(memoryAugmentation); + + // Initialize the augmentation pipeline + initializeAugmentationPipeline(); + + // Initialize the sequential pipeline + await sequentialPipeline.initialize(); + + console.log('Processing data through the sequential pipeline...'); + + // Process data through the sequential pipeline + const result = await sequentialPipeline.processData( + 'This is a test message', + 'text' + ); + + console.log('\nPipeline execution result:'); + console.log('Success:', result.success); + console.log('Data:', result.data); + + if (result.error) { + console.log('Error:', result.error); + } + + console.log('\nStage results:'); + for (const stage in result.stageResults) { + console.log(`${stage}:`, result.stageResults[stage].success); + } + + console.log('\nExample completed successfully!'); + } catch (error) { + console.error('Error running example:', error); + } +} + +// Run the example +runExample();