feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'

This commit is contained in:
2026-07-01 16:14:52 +03:00
parent 1c6bbc04af
commit 5801947c0d
8 changed files with 376 additions and 279 deletions
+90 -47
View File
@@ -1,60 +1,103 @@
# Graph with Reflection and Rewriting
# Graph with Reflection and Rewrite Nodes
This project implements a simple graph data structure in JavaScript that supports **reflection** and **rewriting** operations through dedicated node types.
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.
## Features
## Project Structure
- **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**: Depthfirst traversal of the graph.
```
.
├── src
│ └── index.js # Graph implementation and demo
└── README.md # This file
```
## Installation
## 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
npm install
# 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
```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'));
```
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
MIT
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 instructors feedback.*