103 lines
2.9 KiB
Markdown
103 lines
2.9 KiB
Markdown
# Graph with Reflection and Rewrite Nodes
|
||
|
||
This repository contains a minimal JavaScript implementation of a directed graph that supports custom node types, including the required **`Reflection`** and **`Rewrite`** nodes. The project is intentionally lightweight and does not rely on any external libraries or frameworks.
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
.
|
||
├── src
|
||
│ └── index.js # Graph implementation and demo
|
||
└── README.md # This file
|
||
```
|
||
|
||
## Purpose
|
||
|
||
The goal of this project is to provide a simple, testable graph structure that can be extended with additional node types. The `Reflection` node represents a point where the graph should introspect or analyze the current state, while the `Rewrite` node represents a transformation step that modifies data before passing it on.
|
||
|
||
## How It Works
|
||
|
||
- **Node**: Each node has a unique `id`, a `type` (e.g., `Start`, `Reflection`, `Rewrite`, `End`), optional `props`, and a list of outgoing edges.
|
||
- **Graph**: Maintains a map of nodes and provides methods to add nodes, connect them with directed edges, and serialize the graph to JSON.
|
||
|
||
## Usage
|
||
|
||
```bash
|
||
# Clone the repository
|
||
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-graf-s-refleksiey-i-do.git
|
||
cd povtornyy-ekzamen-graf-s-refleksiey-i-do
|
||
|
||
# Run the demo
|
||
node src/index.js
|
||
```
|
||
|
||
The demo will output a JSON representation of a simple graph that includes the required nodes:
|
||
|
||
```json
|
||
{
|
||
"n1": {
|
||
"id": "n1",
|
||
"type": "Start",
|
||
"props": { "description": "Entry point" },
|
||
"outgoing": ["n2"]
|
||
},
|
||
"n2": {
|
||
"id": "n2",
|
||
"type": "Reflection",
|
||
"props": { "description": "Reflect on the current state" },
|
||
"outgoing": ["n3"]
|
||
},
|
||
"n3": {
|
||
"id": "n3",
|
||
"type": "Rewrite",
|
||
"props": { "description": "Rewrite the data for the next step" },
|
||
"outgoing": ["n4"]
|
||
},
|
||
"n4": {
|
||
"id": "n4",
|
||
"type": "End",
|
||
"props": { "description": "Exit point" },
|
||
"outgoing": []
|
||
}
|
||
}
|
||
```
|
||
|
||
## Extending the Graph
|
||
|
||
You can import the `Graph` class in your own scripts:
|
||
|
||
```javascript
|
||
const { Graph } = require('./src/index');
|
||
|
||
const g = new Graph();
|
||
const a = g.addNode('Start');
|
||
const b = g.addNode('Reflection');
|
||
const c = g.addNode('Rewrite');
|
||
const d = g.addNode('End');
|
||
|
||
g.addEdge(a, b);
|
||
g.addEdge(b, c);
|
||
g.addEdge(c, d);
|
||
|
||
console.log(JSON.stringify(g.toJSON(), null, 2));
|
||
```
|
||
|
||
Feel free to add more node types or properties as needed.
|
||
|
||
## Running Tests
|
||
|
||
No automated tests are included in this repository. The demo in `src/index.js` serves as a basic sanity check. If you wish to add tests, you can use any testing framework (e.g., Jest, Mocha) and write tests against the `Graph` class.
|
||
|
||
## License
|
||
|
||
This project is released under the MIT License.
|
||
|
||
---
|
||
|
||
**Author:** Artur Kuzakhmetov
|
||
**Date:** 23.06.2026
|
||
**Version:** 13
|
||
**Deadline:** 31.08.2026
|
||
|
||
---
|
||
*This project was updated to include the required `Reflection` and `Rewrite` nodes as per the instructor’s feedback.* |