110 likes | 218 Views
Graph Class Diagram. Graph Invariant. ReadOnlyGraph: nodes().empty() implies edges().empty(). Graph:. ReadOnlyGraphImplementation:. ListSetGraph: nodes().empty() implies edges().empty() && the_nodes != null && the_edges != null.
E N D
Graph Invariant ReadOnlyGraph: nodes().empty() implies edges().empty() Graph: ReadOnlyGraphImplementation: ListSetGraph: nodes().empty() implies edges().empty() && the_nodes != null && the_edges != null HashTableSetGraph: nodes().empty() implies edges().empty() && the_nodes != null && the_edges != null
@post nodes().empty() @post edges().empty() public HashtableSetGraph() { the_nodes = new HashtableSet(); the_edges = new HashtableSet(); } @post nodes().empty() @post edges().empty() public ListSetGraph() { the_nodes = new ListSet(); the_edges = new ListSet(); } HashTable/Set ListGraph
ReadOnlyGraph @post $ret != null public ReadOnlySet nodes(); @post $ret != null public ReadOnlySet edges(); @pre node != null @post $ret != null @post !nodes().has(node) implies $ret.empty() public ReadOnlySet outgoing_edges(Node node);
ReadOnlyGraph @pre label != null @post $ret != null implies nodes().has($ret) && label.equals($ret.label()) public Node find_node(Object label); @post label != null @post $ret != null implies edges().has($ret) && label.equals($ret.label()) public Edge find_edge(Object label); @pre node != null @pre label != null @post !nodes().has(node) implies $ret == null @post $ret != null implies edges().has($ret) && node.equals($ret.from()) && label.equals($ret.label()) public Edge find_outgoing(Node node, Object label);
Graph:add_edge @pre edge != null @post nodes().has(edge.from()) @post nodes().has(edge.to()) @post !nodes().empty() @post edges().has(edge) @post !edges().empty() @post outgoing_edges(edge.from()).has(edge) //@post $prev(! edges().has(edge)) implies edges().count() == $prev(edges().count())+ 1 //@post $prev(edges().has(edge)) implies edges().count() == $prev(edges().count()) public void add_edge(Edge edge);
Graph:add_node @pre node != null @post nodes().has(node) @post !nodes().empty() //@post $prev(!nodes().has(node)) implies nodes().count() == $prev(nodes().count()) + 1 //@post $prev(nodes().has(node)) implies nodes().count() == $prev(nodes().count()) public void add_node(Node node);
ReadOnlyGraphImplementation /** Find an edge with a given label in the given sequence. * @pre edges != null * @pre label != null */ protected Edge edge_search(ReadOnlySet edges, Object label)
For any g : GRAPH[NODE-LABEL, EDGE-LABEL], n, n1, n2 : NODE[NODE-LABEL], e : EDGE[NODE-LABEL, EDGE-LABEL]: A5 nodes (make-graph) = emptyset A6 edges (make-graph) = emptyset A7 nodes (add-node (g, n)) = extend (nodes (g), n) A8 edges (add-node (g, n)) = edges (g) A9 nodes (add-edge (g, e)) = extend (extend (nodes (g), edge-from (e)), edge-to (e)) A10 edges (add-edge (g, e)) = extend (edges (g), e) A11 outgoing-edges (make-graph, n) = emptyset A12 not has (nodes (g), n) outgoing-edges (add-node (g, n), n) = emptyset A13 outgoing-edges (add-node (g, n1), n2) = outgoing-edges (g, n2) A14 n = edge-from (e) outgoing-edges (add-edge (g, e), n) = extend (outgoing-edges (g, n), e) A15 n ≠ edge-from (e) outgoing-edges (add-edge (g, e), n) = outgoing-edges (g, n)