60 lines
1.3 KiB
Markdown
60 lines
1.3 KiB
Markdown
# Graph with Reflection and Rewriting
|
||
|
||
This project implements a simple graph data structure in JavaScript that supports **reflection** and **rewriting** operations through dedicated node types.
|
||
|
||
## Features
|
||
|
||
- **Graph**: Stores nodes and directed edges.
|
||
- **Node**: Base class for all nodes.
|
||
- **ReflectionNode**: Creates copies of its target nodes and their outgoing edges.
|
||
- **RewritingNode**: Replaces a target node with a new node while preserving graph connectivity.
|
||
- **Traversal**: Depth‑first traversal of the graph.
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
npm install
|
||
```
|
||
|
||
## Running Tests
|
||
|
||
```bash
|
||
npm test
|
||
```
|
||
|
||
The test suite verifies:
|
||
|
||
- Reflection node correctly duplicates target nodes.
|
||
- Rewriting node correctly replaces target nodes.
|
||
- Circular references are handled safely.
|
||
- Graph traversal works after modifications.
|
||
|
||
## Usage Example
|
||
|
||
```js
|
||
const { Graph, Node, ReflectionNode, RewritingNode } = require('./src/index');
|
||
|
||
const graph = new Graph();
|
||
graph.addNode(new Node('A'));
|
||
graph.addNode(new Node('B'));
|
||
graph.addNode(new Node('C'));
|
||
graph.addEdge('A', 'B');
|
||
graph.addEdge('B', 'C');
|
||
|
||
const r = new ReflectionNode('R');
|
||
graph.addNode(r);
|
||
graph.addEdge('R', 'B');
|
||
r.reflect(graph);
|
||
|
||
const w = new RewritingNode('W');
|
||
graph.addNode(w);
|
||
graph.addEdge('W', 'C');
|
||
const d = new Node('D');
|
||
w.rewrite(graph, 'C', d);
|
||
|
||
console.log(graph.traverse('A'));
|
||
```
|
||
|
||
## License
|
||
|
||
MIT |