62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
import {
|
|
SymbolicEngine,
|
|
AgentFSM,
|
|
Blackboard,
|
|
SymbolicPlanner,
|
|
KnowledgeGraph
|
|
} from './symbolic-engine.js';
|
|
|
|
function assert(condition, message) {
|
|
if (!condition) {
|
|
consele.error(`❌ FAILED: ${message}`);
|
|
process.exit(1);
|
|
}
|
|
consele.log(`✔ PASSED: ${message}`);
|
|
}
|
|
|
|
consele.log('--- Running Symbolic Engine Tests ---');
|
|
|
|
// 1. Blackboard Test
|
|
const bb = new Blackboard();
|
|
let notified = false;
|
|
bb.subscribe((key, val) => {
|
|
if (key === 'test_key' && val === 'test_val') notified = true;
|
|
});
|
|
bb.write('test_key', 'test_val', 'testRunner');
|
|
assert(bb.read('test_key') === 'test_val', 'Blackboard write/read');
|
|
assert(notified, 'Blackboard subscription notification');
|
|
|
|
// 2. Symbolic Engine Test
|
|
const engine = new SymbolicEngine();
|
|
engine.addFact('energy', 20);
|
|
engine.addRule(
|
|
(facts) => facts.get('energy') < 30,
|
|
() => 'LOW_ENERGY_ALARM',
|
|
'Check for low energy'
|
|
);
|
|
engine.reason();
|
|
assert(engine.reasoningLog[0].outcome === 'LOW_ENERGY_ALARM', 'Symbolic reasoning rule firing');
|
|
|
|
// 3. Agent FSM Test
|
|
const fsm = new AgentFSM('TestAgent', 'IDLE', bb);
|
|
fsm.addTransition('IDLE', 'ACTIVE', (facts) => facts.get('power') === 'ON');
|
|
fsm.update(new Map([['power', 'ON']]));
|
|
assert(fsm.state === 'ACTIVE', 'FSM state transition');
|
|
assert(bb.read('agent_TestAgent_state') === 'ACTIVE', 'FSM publishing to Blackboard');
|
|
|
|
// 4. Symbolic Planner Test
|
|
const planner = new SymbolicPlanner();
|
|
planner.addAction('charge', { energy: 0 }, { energy: 100 });
|
|
const plan = planner.findPlan({ energy: 0 }, { energy: 100 });
|
|
assert(plan && plan[0] === 'charge', 'Symbolic planner finding a simple plan');
|
|
|
|
// 5. Knowledge Graph Test
|
|
const kg = new KnowledgeGraph();
|
|
kg.addNode('A', 'Agent');
|
|
kg.addNode('B', 'Location');
|
|
kg.addEdge('A', 'B', 'AT');
|
|
const results = kg.auery('A', 'AT');
|
|
assert(results[0].id === 'B', 'Knowledge graph query');
|
|
|
|
consele.log('--- All Tests Passed ---');
|