Files
povtornyy-ekzamen-graf-s-re…/SOLUTION.md
T

2.3 KiB
Raw Blame History

What was implemented

  • Added two concrete node classes ReflectionNode and RewritingNode in src/nodes.js.
  • Updated the public API in src/index.js to export the new classes.
  • Wrote a comprehensive test suite (tests/graph.test.js) that checks:
    1. Nodes of all three types can be added.
    2. Duplicate IDs are rejected.
    3. Edges can be created between any node types.
    4. Removing a node cleans up its edges.
    5. Traversal works on disconnected subgraphs.

Why the main parts satisfy the requirements

  • The new node classes inherit from Node, so the existing Graph.addNode logic (instanceof Node) automatically accepts them.
  • Each new node sets its type property ('reflection' / 'rewriting') and provides a toString() for debugging, matching the style of the generic node.
  • Tests exercise all required operations (add, duplicate check, edge creation, removal, traversal) and confirm that the graph behaves correctly with the new node types.

Key code excerpts

src/nodes.js definition of the new node types

export class ReflectionNode extends Node {
  constructor(id, data = {}) {
    super(id, data);
    this.type = 'reflection';
  }
  toString() { return `ReflectionNode(${this.id})`; }
}

export class RewritingNode extends Node {
  constructor(id, data = {}) {
    super(id, data);
    this.type = 'rewriting';
  }
  toString() { return `RewritingNode(${this.id})`; }
}

tests/graph.test.js adding nodes and verifying presence

const n1 = new Node('n1');
const r1 = new ReflectionNode('r1');
const w1 = new RewritingNode('w1');

graph.addNode(n1);
graph.addNode(r1);
graph.addNode(w1);

expect(graph.getNode('n1')).toBe(n1);
expect(graph.getNode('r1')).toBe(r1);
expect(graph.getNode('w1')).toBe(w1);

src/graph.js node type check (unchanged, but still relevant)

addNode(node) {
  if (!(node instanceof Node)) {
    throw new Error('Only Node instances can be added');
  }
  ...
}

Honest limitations

  • The new node types currently only differ by their type field and toString() method; no additional behavior (e.g., special traversal rules) is implemented.
  • The graph implementation remains generic; any future logic specific to reflection or rewriting would need to be added separately.