Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions gap/dot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
56 changes: 27 additions & 29 deletions gap/dot.gi
Original file line number Diff line number Diff line change
Expand Up @@ -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))));

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -382,15 +380,15 @@ 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, "\"");
head_node := GV_Node(x, head);
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, "\"");
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions gap/gv.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
14 changes: 1 addition & 13 deletions gap/gv.gi
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions tst/examples/cluster.tst
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ gap> GraphvizAddEdge(graph, "b3", "end");
<graphviz edge (b3, end)>

#
gap> GraphvizSetAttr(graph["start"], "shape", "Mdiamond");
gap> GraphvizSetAttr(GraphvizNode(graph, "start"), "shape", "Mdiamond");
<graphviz node "start">
gap> GraphvizSetAttr(graph["end"], "shape", "Msquare");
gap> GraphvizSetAttr(GraphvizNode(graph, "end"), "shape", "Msquare");
<graphviz node "end">

#
Expand Down
16 changes: 8 additions & 8 deletions tst/graph.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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);;

Expand Down Expand Up @@ -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");
<graphviz node "test">
gap> g["abc"];
gap> GraphvizNode(gv, "abc");
<graphviz node "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);
<graphviz node "1">
gap> g[["a"]];
gap> GraphvizNode(g, ["a"]);
<graphviz node "[ "a" ]">
gap> g := GraphvizGraph();;
gap> GraphvizSetAttr(g, "label", "test");;
Expand Down
16 changes: 8 additions & 8 deletions tst/subgraph.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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");
<graphviz node "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");
<graphviz node "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");
<graphviz node "a">
gap> GV_FindNode(s1, "b");
gap> GraphvizNode(s1, "b");
fail

# finding a node in a parent's sibling graph
Expand All @@ -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");
<graphviz node "a">
gap> GV_FindNode(s11, "b");
gap> GraphvizNode(s11, "b");
fail

# Test removing a node from a graph
Expand Down
Loading