diff --git a/gap/dot.gd b/gap/dot.gd index d36b448..4b611d1 100644 --- a/gap/dot.gd +++ b/gap/dot.gd @@ -312,6 +312,8 @@ DeclareOperation("GraphvizAttrs", [IsGraphvizObject]); #! @EndExampleSession DeclareOperation("GraphvizNodes", [IsGraphvizGraphDigraphOrContext]); +DeclareOperation("GraphvizNode", [IsGraphvizGraphDigraphOrContext, IsObject]); + #! @Arguments graph #! @Returns the number of nodes in a &GAPGraphviz; object. @@ -629,14 +631,6 @@ DeclareOperation("GraphvizSetNodeLabels", #! @EndExampleSession DeclareGlobalFunction("ErrorIfNotValidColor"); -#! @Arguments graph, node_name -#! @Returns a &GAPGraphviz; node object or fail -#! @Description -#! Gets a node from a (di)graph by id. -#! @BeginExampleSession -#! @EndExampleSession -DeclareOperation("\[\]", [IsGraphvizGraphDigraphOrContext, IsObject]); - #! @Section Debugging #! @Description #! The info class of this package; its info level is 0 by default. diff --git a/gap/dot.gi b/gap/dot.gi index bdc53cf..0997734 100644 --- a/gap/dot.gi +++ b/gap/dot.gi @@ -111,6 +111,19 @@ x -> x!.Attrs); InstallMethod(GraphvizNodes, "for a graphviz (di)graph or context", [IsGraphvizGraphDigraphOrContext], x -> x!.Nodes); +InstallMethod(GraphvizNode, "for a graphviz (di)graph or context and object", +[IsGraphvizGraphDigraphOrContext, IsObject], +function(gv, val) + local graph; + val := String(val); + graph := GV_FindGraphWithNode(gv, val); + if graph = fail then + return fail; + fi; + return GraphvizNodes(graph).(val); +end); + +# FIXME the below only counts nodes in the root graph, not in its children InstallMethod(GraphvizNumberOfNodes, "for a graphviz (di)graph or context", [IsGraphvizGraphDigraphOrContext], x -> Length(RecNames(GraphvizNodes(x)))); @@ -164,21 +177,6 @@ InstallMethod(\=, "for graphviz edges", # Accessor for graphs and digraphs -# TODO Remove this, and use GraphvizAttrs(graph) instead? -InstallMethod(\[\], "for a graphviz (di)graph or context and string", -[IsGraphvizGraphDigraphOrContext, IsString], -function(graph, node) - if IsBound(GraphvizNodes(graph).(node)) then - return GraphvizNodes(graph).(node); - fi; - return fail; -end); - -# TODO Remove this, and use GraphvizAttrs(graph) instead? -InstallMethod(\[\], "for a graphviz (di)graph or context and object", -[IsGraphvizGraphDigraphOrContext, IsObject], -{g, o} -> g[String(o)]); - InstallMethod(GraphvizFindSubgraphRecursive, "for a graphviz (di)graph or context and a string", [IsGraphvizGraphDigraphOrContext, IsString], @@ -298,31 +296,31 @@ InstallMethod(GraphvizGetAttr, "for a graphviz (di)graph or context and string", InstallMethod(GraphvizAddNode, "for a graphviz (di)graph or context and string", [IsGraphvizGraphDigraphOrContext, IsString], -function(x, name) +function(gv, val) local node; # Reuse existing node if available - node := GV_FindNode(x, name); + node := GraphvizNode(gv, val); if node <> fail then - if not IsIdenticalObj(GV_GetParent(node), x) then + if not IsIdenticalObj(GV_GetParent(node), gv) then ErrorFormatted("The 2nd argument \"{}\" is the name of a node in a ", "(di)graph or context named \"{}\" which is an ", "ancestor, sibling or descendant of the 1st argument ", "(a graphviz (di)graph / context) named \"{}\" (", "adding the node \"{}\" to the 1st argument would ", "silently change the enclosing context of \"{}\")", - name, + val, GraphvizName(GV_GetParent(node)), - GraphvizName(x), - name, - GraphvizName(x), - name); + GraphvizName(gv), + val, + GraphvizName(gv), + val); fi; return node; fi; - node := GV_Node(x, name); - GV_AddNode(x, node); + node := GV_Node(gv, val); + GV_AddNode(gv, node); return node; end); @@ -382,7 +380,7 @@ InstallMethod(GraphvizAddEdge, function(x, head, tail) local head_node, tail_node; - head_node := GV_FindNode(x, head); + head_node := GraphvizNode(x, head); if head_node = fail then Info(InfoGraphviz, 3, "the head of the edge \"", head, "\" is not an existing node, adding node \"", head, "\""); @@ -390,7 +388,7 @@ function(x, head, tail) GV_AddNode(x, head_node); fi; - tail_node := GV_FindNode(x, tail); + tail_node := GraphvizNode(x, tail); if tail_node = fail then Info(InfoGraphviz, 3, "the tail of the edge \"", tail, "\" is not an existing node, adding node \"", tail, "\""); @@ -553,8 +551,8 @@ function(g, hn, tn) local lh, lt, len; # if no such nodes exist -> error out - lh := GV_FindNode(g, hn) = fail; - lt := GV_FindNode(g, tn) = fail; + lh := GraphvizNode(g, hn) = fail; + lt := GraphvizNode(g, tn) = fail; if lh and lt then ErrorFormatted("no nodes with names \"{}\" or \"{}\"", hn, tn); elif lh then diff --git a/gap/gv.gd b/gap/gv.gd index a0a81c9..ae874f4 100644 --- a/gap/gv.gd +++ b/gap/gv.gd @@ -24,8 +24,6 @@ DeclareOperation("GV_StringifyNodeEdgeAttrs", [IsRecord]); DeclareOperation("GV_StringifyGraph", [IsGraphvizGraphDigraphOrContext, IsBool]); -DeclareOperation("GV_FindNode", [IsGraphvizGraphDigraphOrContext, IsObject]); - DeclareOperation("GV_Node", [IsGraphvizGraphDigraphOrContext, IsString]); DeclareOperation("GV_Edge", [IsGraphvizGraphDigraphOrContext, IsGraphvizNode, IsGraphvizNode]); diff --git a/gap/gv.gi b/gap/gv.gi index a7617fb..9db9dd4 100644 --- a/gap/gv.gi +++ b/gap/gv.gi @@ -229,7 +229,7 @@ end); InstallMethod(GV_FindGraphWithNode, "for a graphviz graph and a node", [IsGraphvizGraphDigraphOrContext, IsString], -{g, n} -> GV_GraphTreeSearch(g, v -> v[n] <> fail)); +{g, n} -> GV_GraphTreeSearch(g, v -> IsBound(GraphvizNodes(v).(n)))); InstallMethod(GV_GetRoot, "for a graphviz graph", @@ -253,18 +253,6 @@ function(graph) return parent; end); -InstallMethod(GV_FindNode, -"for a graphviz graph and a string", -[IsGraphvizGraphDigraphOrContext, IsString], -function(g, n) - local graph; - graph := GV_FindGraphWithNode(g, n); - if graph = fail then - return fail; - fi; - return graph[n]; -end); - InstallMethod(GV_AddNode, "for a graphviz graph and node", [IsGraphvizGraphDigraphOrContext, IsGraphvizNode], diff --git a/tst/examples/cluster.tst b/tst/examples/cluster.tst index c3ee49f..0214c8b 100644 --- a/tst/examples/cluster.tst +++ b/tst/examples/cluster.tst @@ -68,9 +68,9 @@ gap> GraphvizAddEdge(graph, "b3", "end"); # -gap> GraphvizSetAttr(graph["start"], "shape", "Mdiamond"); +gap> GraphvizSetAttr(GraphvizNode(graph, "start"), "shape", "Mdiamond"); -gap> GraphvizSetAttr(graph["end"], "shape", "Msquare"); +gap> GraphvizSetAttr(GraphvizNode(graph, "end"), "shape", "Msquare"); # diff --git a/tst/graph.tst b/tst/graph.tst index 5abc4ee..02ec5cf 100644 --- a/tst/graph.tst +++ b/tst/graph.tst @@ -8,7 +8,7 @@ ############################################################################# ## -#@local a, ab, b, c, cd, d, g, n, n1, n2, x +#@local a, ab, b, c, cd, d, g, n, n1, n2, x, gv gap> START_TEST("graphviz package: graph.tst"); gap> LoadPackage("GraphvizForGAP", false);; @@ -204,21 +204,21 @@ gap> AsString(g); # [ ] # Test getting a node using bracket notation -gap> g := GraphvizGraph();; -gap> n1 := GraphvizAddNode(g, "test");; -gap> n2 := GraphvizAddNode(g, "abc");; -gap> g["test"]; +gap> gv := GraphvizGraph();; +gap> n1 := GraphvizAddNode(gv, "test");; +gap> n2 := GraphvizAddNode(gv, "abc");; +gap> GraphvizNode(gv, "test"); -gap> g["abc"]; +gap> GraphvizNode(gv, "abc"); # Test getting a node with a non-string name using bracket notation gap> g := GraphvizGraph();; gap> n1 := GraphvizAddNode(g, 1);; gap> n2 := GraphvizAddNode(g, ["a"]);; -gap> g[1]; +gap> GraphvizNode(g, 1); -gap> g[["a"]]; +gap> GraphvizNode(g, ["a"]); gap> g := GraphvizGraph();; gap> GraphvizSetAttr(g, "label", "test");; diff --git a/tst/subgraph.tst b/tst/subgraph.tst index 70e340d..fa74c7d 100644 --- a/tst/subgraph.tst +++ b/tst/subgraph.tst @@ -197,27 +197,27 @@ gap> g := GraphvizDigraph();; gap> s1 := GraphvizAddSubgraph(g);; gap> GraphvizAddNode(s1, "a");; gap> s2 := GraphvizAddSubgraph(g);; -gap> GV_FindNode(s2, "a"); +gap> GraphvizNode(s2, "a"); -gap> GV_FindNode(s2, "b"); +gap> GraphvizNode(s2, "b"); fail # finding a node in a child graph gap> g := GraphvizDigraph();; gap> s1 := GraphvizAddSubgraph(g);; gap> GraphvizAddNode(s1, "a");; -gap> GV_FindNode(g, "a"); +gap> GraphvizNode(g, "a"); -gap> GV_FindNode(g, "b"); +gap> GraphvizNode(g, "b"); fail # finding a node in a parent graph gap> g := GraphvizDigraph();; gap> s1 := GraphvizAddSubgraph(g);; gap> GraphvizAddNode(g, "a");; -gap> GV_FindNode(s1, "a"); +gap> GraphvizNode(s1, "a"); -gap> GV_FindNode(s1, "b"); +gap> GraphvizNode(s1, "b"); fail # finding a node in a parent's sibling graph @@ -226,9 +226,9 @@ gap> s1 := GraphvizAddSubgraph(g);; gap> s2 := GraphvizAddSubgraph(g);; gap> s11 := GraphvizAddSubgraph(s1);; gap> GraphvizAddNode(s2, "a");; -gap> GV_FindNode(s11, "a"); +gap> GraphvizNode(s11, "a"); -gap> GV_FindNode(s11, "b"); +gap> GraphvizNode(s11, "b"); fail # Test removing a node from a graph