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

This commit is contained in:
2026-07-01 17:04:31 +03:00
parent ab3d08e839
commit c2b451d767
3 changed files with 183 additions and 224 deletions
+40 -55
View File
@@ -1,74 +1,59 @@
# Graph with Reflection Capabilities
# Graph Answer Generation with Retry Logic
This project implements a simple directed graph data structure in JavaScript with builtin 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.
This repository contains a minimal example of how to replace a
special "reflect" node in a graph-based answer generation system
with a simple `try/except` retry mechanism.
## 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
```
- **Retry Logic**: Attempts to generate an answer up to a configurable
number of times (`max_retries`). If all attempts fail, a
`GenerationError` is raised.
- **Backoff**: Optional exponential backoff between retries.
- **Simulation**: The example uses a simulated generator that
randomly fails to demonstrate the retry behavior.
## 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
# Run the example
python -m src.index
```
All tests are located in `src/index.test.js` and cover:
You should see output similar to:
- Basic graph operations (add nodes/edges, retrieval).
- Error conditions.
- Reflection methods.
- Introspection utilities.
```
Answer generated successfully:
Generated answer content
```
If the generation fails after all retries, you will see:
```
Error: Answer generation failed after 3 attempts
```
## Customization
- **Changing the number of retries**:
```python
answer = get_answer_with_retry(max_retries=5)
```
- **Using a real generator**:
Replace `_simulate_answer_generation` with your own function
that performs the actual answer generation logic.
## Project Structure
```
├── src
├── index.js # Graph implementation
│ └── index.test.js # Jest test suite
├── package.json # npm configuration
└── README.md # Documentation
src/
├── index.py # Main implementation
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
This project is released under the MIT License.