74 lines
2.1 KiB
Markdown
74 lines
2.1 KiB
Markdown
# Graph with Reflection Capabilities
|
||
|
||
This project implements a simple directed graph data structure in JavaScript with built‑in reflection and introspection utilities.
|
||
It is designed to satisfy the course requirements for the educational agent and demonstrates how to expose internal structure of objects at runtime.
|
||
|
||
## Features
|
||
|
||
- **Nodes & Edges** – Add nodes with optional data, add directed edges with optional data.
|
||
- **Adjacency** – Retrieve neighbors, all nodes, all edges.
|
||
- **Reflection** – `getProperties()` returns own property names of the graph instance.
|
||
`getMethods()` returns all public method names defined on the prototype.
|
||
- **Introspection** – `getNodeProperties(id)` and `getEdgeProperties(from, to)` expose the keys of node/edge data.
|
||
- **Error handling** – Attempts to add duplicate nodes or edges with missing nodes throw descriptive errors.
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Clone the repository
|
||
git clone <repository-url>
|
||
cd <repository-directory>
|
||
|
||
# Install dependencies
|
||
npm install
|
||
```
|
||
|
||
## Usage
|
||
|
||
```js
|
||
const Graph = require('./src/index');
|
||
|
||
const g = new Graph();
|
||
g.addNode('A', { value: 10 });
|
||
g.addNode('B', { value: 20 });
|
||
g.addEdge('A', 'B', { weight: 5 });
|
||
|
||
console.log(g.getNeighbors('A')); // ['B']
|
||
console.log(g.getEdgeData('A', 'B')); // { weight: 5 }
|
||
|
||
console.log(g.getProperties()); // ['nodes', 'edges', 'edgeData']
|
||
console.log(g.getMethods()); // ['addNode', 'addEdge', ...]
|
||
```
|
||
|
||
## Running Tests
|
||
|
||
The project uses **Jest** as the test runner.
|
||
|
||
```bash
|
||
npm test
|
||
```
|
||
|
||
All tests are located in `src/index.test.js` and cover:
|
||
|
||
- Basic graph operations (add nodes/edges, retrieval).
|
||
- Error conditions.
|
||
- Reflection methods.
|
||
- Introspection utilities.
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
├── src
|
||
│ ├── index.js # Graph implementation
|
||
│ └── index.test.js # Jest test suite
|
||
├── package.json # npm configuration
|
||
└── README.md # Documentation
|
||
```
|
||
|
||
## Contributing
|
||
|
||
Feel free to open issues or pull requests. Please ensure that new features are accompanied by tests.
|
||
|
||
## License
|
||
|
||
MIT © Your Name |