Quellcode durchsuchen

Added example (and test) for GraphState class.

Joeri Exelmans vor 2 Jahren
Ursprung
Commit
63ddf6e91e
2 geänderte Dateien mit 90 neuen und 30 gelöschten Zeilen
  1. 89 29
      src/onion/graph_state.test.ts
  2. 1 1
      src/onion/graph_state.ts

+ 89 - 29
src/onion/graph_state.test.ts

@@ -1,4 +1,4 @@
-import {GraphState, GraphStateListener} from "./graph_state";
+import {GraphState, GraphStateListener, IValueState, INodeState} from "./graph_state";
 // import {GraphState} from "./graph_state";
 import {mockUuid} from "./test_helpers";
 
@@ -10,46 +10,106 @@ import {
   PrimitiveRegistry,
 } from "./primitive_delta";
 
+import {CompositeLevel} from "./composite_delta";
+
 import {
   assert,
 } from "../util/assert";
 
-// describe("GraphState", () => {
-//   // need to test more scenarios!
+class MyGraphStateListener implements GraphStateListener {
+  createNode(ns: INodeState) {
+    console.log("created node", ns);
+  }
+  createValue(vs: IValueState) {
+    console.log("created value", vs);
+  }
+  deleteNode(id) {
+    console.log("deleted node", id);
+  }
+  deleteValue(value) {
+    console.log("deleted value", value);
+  }
+  createLinkToNode(sourceId, label: string, targetId) {
+    console.log("created link to node", sourceId, label, targetId);
+  }
+  createLinkToValue(sourceId, label: string, targetValue) {
+    console.log("created link to value", sourceId, label, targetValue);
+  }
+  deleteLink(sourceId, label: string) {
+    console.log("deleted link", sourceId, label);
+  }
+}
+
+describe("GraphState", () => {
+  // need to test more scenarios!
+
+  // taken from primitive delta test:
+  it("Delete node with self-edge", () => {
+    const graphState = new GraphState();
+    const registry = new PrimitiveRegistry();
+    const compositeLvl = new CompositeLevel();
+    const getId = mockUuid();
+
+    const myListener = new MyGraphStateListener();
+
+    // Better not to create these primitive deltas by hand (is hard!). Instead, use 'getDeltasForSetEdge' and 'getDeltasForDelete' (see below).
+    
+    // const edgeCreation = registry.newEdgeCreation(nodeCreation, "label", nodeCreation);
+    // const edgeUpdate = registry.newEdgeUpdate(edgeCreation, null);
+    // const nodeDeletion = registry.newNodeDeletion(nodeCreation, [edgeUpdate], [edgeUpdate]);
+
+    assert(graphState.nodes.size === 0, "Expected no nodes initially");
+
+    const nodeId = getId();
+    const nodeCreation = registry.newNodeCreation(nodeId);
+    const comp0 = compositeLvl.createComposite([nodeCreation], "node creation");
+
+    graphState.exec(comp0, myListener);
+
+    assert(graphState.nodes.size === 1, "Expected one node after NodeCreation");
+
+    const nodeState = graphState.nodes.get(nodeId.value);
+    const deltasForSetEdge = nodeState!.getDeltasForSetEdge(registry, "x", 42);
+    console.log({deltasForSetEdge});
+    const comp1 = compositeLvl.createComposite(deltasForSetEdge, "set edge x == 42");
+
+    graphState.exec(comp1, myListener);
+
+    assert(graphState.nodes.get(nodeId.value)!.outgoing.get("x") !== undefined, "Expected outgoing edge 'x' for node " + nodeId);
+
+    assert((graphState.nodes.get(nodeId.value)!.getOutgoingEdges().get("x") as IValueState).value === 42, "Expected value of outgoing edge 'x' to be 42");
+
+    const deltasForDelete = nodeState!.getDeltasForDelete(registry);
+    console.log({deltasForDelete})
+    const comp2 = compositeLvl.createComposite(deltasForDelete, "delete node (and edge)");
+
+    graphState.exec(comp2, myListener);
+
+    console.log(nodeState!.getOutgoingEdges().get("x"))
+    // assert(nodeState!.getOutgoingEdges().get("x") === null, "Exepected no outgoing edge 'x'");
+
 
-//   // taken from primitive delta test:
-//   it("Delete node with self-edge", () => {
-//     const graphState = new GraphState();
-//     const registry = new PrimitiveRegistry();
-//     // const executor = new GraphState();
-//     const getId = mockUuid();
+    graphState.unexec(comp2, myListener);
 
-//     const nodeCreation = registry.newNodeCreation(getId());
-//     const edgeCreation = registry.newEdgeCreation(nodeCreation, "label", nodeCreation);
-//     const edgeUpdate = registry.newEdgeUpdate(edgeCreation, null);
-//     const nodeDeletion = registry.newNodeDeletion(nodeCreation, [edgeUpdate], [edgeUpdate]);
+    // assert((graphState.nodes.get(nodeId.value)!.getOutgoingEdges().get("x") as IValueState).value === 42, "Expected value of outgoing edge 'x' to be 42");
 
-//     assert(graphState.nodes.size === 0, "Expected no nodes initially");
-//     // assert(graphState.edges.length === 0, "Expected no edges initially");
 
-//     graphState.exec(nodeCreation);
+    // assert(graphState.nodes.size === 0, "Expected no more nodes in graphState");
 
-//     assert(graphState.nodes.size === 1, "Expected one node after NodeCreation");
-//     // assert(graphState.edges.length === 0, "Expected no edges after NodeCreation");
 
-//     graphState.exec(edgeCreation);
+    // // graphState.exec(edgeCreation);
 
-//     assert(graphState.nodes.size === 1, "Expected one node after EdgeCreation");
-//     // assert(graphState.edges.length === 1, "Expected one edge after EdgeCreation");
+    // assert(graphState.nodes.size === 1, "Expected one node after EdgeCreation");
+    // // assert(graphState.edges.length === 1, "Expected one edge after EdgeCreation");
 
-//     graphState.exec(edgeUpdate);
+    // graphState.exec(edgeUpdate);
 
-//     assert(graphState.nodes.size === 1, "Expected one node after EdgeUpdate");
-//     // assert(graphState.edges.length === 0, "Expected no edges after EdgeUpdate");
+    // assert(graphState.nodes.size === 1, "Expected one node after EdgeUpdate");
+    // // assert(graphState.edges.length === 0, "Expected no edges after EdgeUpdate");
 
-//     graphState.exec(nodeDeletion);
+    // graphState.exec(nodeDeletion);
 
-//     assert(graphState.nodes.size === 0, "Expected no nodes after NodeDeletion");
-//     // assert(graphState.edges.length === 0, "Expected no edges after NodeDeletion");
-//   });
-// });
+    // assert(graphState.nodes.size === 0, "Expected no nodes after NodeDeletion");
+    // // assert(graphState.edges.length === 0, "Expected no edges after NodeDeletion");
+  });
+});

+ 1 - 1
src/onion/graph_state.ts

@@ -403,7 +403,7 @@ export class GraphState {
         listener.deleteLink(sourceId, label);
       }
     }
-    listener.deleteNode(id);    
+    listener.deleteNode(id);
   }
   unexecNodeDeletion(delta: NodeDeletion, listener: GraphStateListener) {
     // restore outgoing links