Skip to content

Commit 384f9c1

Browse files
Deduplicate some functions
1 parent 91fb4f7 commit 384f9c1

File tree

2 files changed

+39
-82
lines changed

2 files changed

+39
-82
lines changed

gap/dot.gd

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#! @Chapter
22
#! @ChapterTitle An introduction to the DOT language and Graphviz.
3-
#! This chapter explains what the DOT and graphviz are,
3+
#! This chapter explains what the DOT and graphviz are,
44
#! key basic concepts relating to them, and how this package interacts with them.
55

66
#! @Section A Brief Introduction
7-
#! DOT is a language for descrbing to a computer how to display a visualization
8-
#! for a graph or digraph. Graphviz is a graph vizualization software which can
7+
#! DOT is a language for descrbing to a computer how to display a visualization
8+
#! for a graph or digraph. Graphviz is a graph vizualization software which can
99
#! consume DOT and produce vizual outputs. This package is designed to allow
10-
#! users to programmatically construct objects in GAP which can then be
10+
#! users to programmatically construct objects in GAP which can then be
1111
#! converted into DOT. That DOT can then be inputted into the graphviz software
12-
#! to produce a visual output. As DOT is central to the design of this package
12+
#! to produce a visual output. As DOT is central to the design of this package
1313
#! it will likely be helpful to have a basic understanding of the language.
14-
#! For more information about DOT see
14+
#! For more information about DOT see
1515
#! <URL>https://graphviz.org/doc/info/lang.html</URL>.
1616

1717
#! @Chapter
@@ -20,13 +20,15 @@
2020
#! @Section Graphviz Categories
2121

2222
#! @BeginGroup Filters
23-
#! @Description Every object in graphviz belongs to the IsGVObject category.
24-
#! The categories following it are for further specificity on the type of
23+
#! @Description Every object in graphviz belongs to the IsGVObject category.
24+
#! The categories following it are for further specificity on the type of
2525
#! objects. These are graphs, digraphs, nodes and edges respectively.
2626
#! All are direct subcategories of IsGVObject excluding IsGVDigraph which is a
27-
#! subcategory of is GVGraph.
27+
#! subcategory of is GVGraph.
2828
DeclareCategory("IsGVObject", IsObject);
2929
DeclareCategory("IsGVGraph", IsGVObject);
30+
# TODO change to IsGVObject below, since digraphs aren't a special kind of
31+
# graph, unless I'm (JDM) mistaken?
3032
DeclareCategory("IsGVDigraph", IsGVGraph);
3133
DeclareCategory("IsGVContext", IsGVGraph);
3234
DeclareCategory("IsGVNode", IsGVObject);
@@ -54,10 +56,10 @@ DeclareOperation("GraphvizDigraph", []);
5456
#! @EndGroup
5557

5658
#! @Section Get Operations
57-
#! This section covers the operations for getting information about graphviz
59+
#! This section covers the operations for getting information about graphviz
5860
#! objects.
5961

60-
#! @Subsection For all graphviz objects.
62+
#! @Subsection For all graphviz objects.
6163

6264
#! @Arguments obj
6365
#! @Returns the name of the provided graphviz object
@@ -85,10 +87,10 @@ DeclareOperation("GraphvizGetSubgraph", [IsGVGraph, IsObject]);
8587

8688
#! @Arguments graph, name
8789
#! @Returns a graph with the provided name.
88-
#! @Description
90+
#! @Description
8991
#! Searches through the tree of subgraphs connected to this subgraph for a graph
90-
#! with the provided name.
91-
#! It returns the graph if it exists.
92+
#! with the provided name.
93+
#! It returns the graph if it exists.
9294
#! If no such graph exists then it will return fail.
9395
DeclareOperation("GraphvizFindGraph", [IsGVGraph, IsObject]);
9496

@@ -157,15 +159,15 @@ DeclareOperation("GraphvizFilterEdges", [IsGVGraph, IsFunction]);
157159

158160
#! @Arguments graph, head_name, tail_name
159161
#! @Returns the modified graph.
160-
#! @Description Filters the graph's edges, removing edges between nodes with
162+
#! @Description Filters the graph's edges, removing edges between nodes with
161163
#! the specified names.
162164
DeclareOperation("GraphvizFilterEnds", [IsGVGraph, IsObject, IsObject]);
163165

164166
#! @Subsection For modifying object attributes.
165167

166168
#! @Arguments obj, attrs
167169
#! @Returns the modified object.
168-
#! @Description
170+
#! @Description
169171
#! Updates the attribtues of the object.
170172
#! All current attributes remain.
171173
#! If an attribute already exists and a new value is provided, the old value
@@ -176,15 +178,15 @@ DeclareOperation("GraphvizSetAttr", [IsGVObject, IsObject]);
176178

177179
#! @Arguments obj, label
178180
#! @Returns the modified object.
179-
#! @Description
181+
#! @Description
180182
#! Updates the label of the object.
181-
#! If a label already exists and a new value is provided, the old value will
183+
#! If a label already exists and a new value is provided, the old value will
182184
#! be overwritten.
183185
DeclareOperation("GraphvizSetLabel", [IsGVObject, IsObject]);
184186

185187
#! @Arguments obj, color
186188
#! @Returns the modified object.
187-
#! @Description
189+
#! @Description
188190
#! Updates the color of the object.
189191
#! If a color already exists and a new value is provided, the old value will
190192
#! be overwritten.

gap/dot.gi

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ DeclareOperation("GV_StringifyGraphHead", [IsGVGraph]);
1010
DeclareOperation("GV_StringifyDigraphHead", [IsGVGraph]);
1111
DeclareOperation("GV_StringifySubgraphHead", [IsGVGraph]);
1212
DeclareOperation("GV_StringifyContextHead", [IsGVGraph]);
13-
DeclareOperation("GV_StringifyGraphEdge", [IsGVEdge]);
14-
DeclareOperation("GV_StringifyDigraphEdge", [IsGVEdge]);
1513
DeclareOperation("GV_StringifyNode", [IsGVNode]);
1614
DeclareOperation("GV_StringifyGraphAttrs", [IsGVGraph]);
1715
DeclareOperation("GV_StringifyNodeEdgeAttrs", [IsGV_Map]);
@@ -375,50 +373,21 @@ end);
375373

376374
InstallMethod(ViewString, "for a graphviz graph", [IsGVGraph],
377375
function(g)
378-
local result, edges, nodes;
379-
result := "";
380-
edges := Length(GraphvizEdges(g));
381-
nodes := Length(GV_MapNames(GraphvizNodes(g)));
382-
383-
Append(result, StringFormatted("<graphviz graph ", GraphvizName(g)));
384-
385-
if GraphvizName(g) <> "" then
386-
Append(result, StringFormatted("{} ", GraphvizName(g)));
387-
fi;
388-
389-
Append(result, StringFormatted("with {} ", GV_Pluralize(nodes, "node")));
390-
Append(result, StringFormatted("and {}>", GV_Pluralize(edges, "edge")));
376+
local result, edges, nodes, kind;
391377

392-
return result;
393-
end);
394-
395-
InstallMethod(ViewString, "for a graphviz digraph", [IsGVDigraph],
396-
function(g)
397-
local result, edges, nodes;
398378
result := "";
399379
edges := Length(GraphvizEdges(g));
400380
nodes := Length(GV_MapNames(GraphvizNodes(g)));
401381

402-
Append(result, StringFormatted("<graphviz digraph ", GraphvizName(g)));
403-
404-
if GraphvizName(g) <> "" then
405-
Append(result, StringFormatted("{} ", GraphvizName(g)));
382+
if IsGVDigraph(g) then
383+
kind := "digraph";
384+
elif IsGVContext(g) then
385+
kind := "context";
386+
else
387+
kind := "graph";
406388
fi;
407389

408-
Append(result, StringFormatted("with {} ", GV_Pluralize(nodes, "node")));
409-
Append(result, StringFormatted("and {}>", GV_Pluralize(edges, "edge")));
410-
411-
return result;
412-
end);
413-
414-
InstallMethod(ViewString, "for a graphviz context", [IsGVContext],
415-
function(g)
416-
local result, edges, nodes;
417-
result := "";
418-
edges := Length(GraphvizEdges(g));
419-
nodes := Length(GV_MapNames(GraphvizNodes(g)));
420-
421-
Append(result, StringFormatted("<graphviz context ", GraphvizName(g)));
390+
Append(result, StringFormatted("<graphviz {} ", kind));
422391

423392
if GraphvizName(g) <> "" then
424393
Append(result, StringFormatted("{} ", GraphvizName(g)));
@@ -430,9 +399,9 @@ function(g)
430399
return result;
431400
end);
432401

433-
# ###########################################################
402+
############################################################
434403
# Getters
435-
# ###########################################################
404+
############################################################
436405
InstallMethod(GraphvizName,
437406
"for a graphviz object",
438407
[IsGVObject],
@@ -1059,33 +1028,19 @@ function(node)
10591028
end);
10601029

10611030
# @ Return DOT graph edge statement line.
1062-
InstallMethod(GV_StringifyGraphEdge, "for a graphviz edge",
1063-
[IsGVEdge],
1064-
function(edge)
1065-
local head, tail, attrs;
1066-
head := GraphvizName(GraphvizHead(edge));
1067-
tail := GraphvizName(GraphvizTail(edge));
1068-
attrs := GraphvizAttrs(edge);
1069-
1070-
# handle : syntax
1071-
return StringFormatted("\t{} -- {}{}\n",
1072-
head,
1073-
tail,
1074-
GV_StringifyNodeEdgeAttrs(attrs));
1075-
end);
1076-
1077-
# @ Return DOT digraph edge statement line.
1078-
InstallMethod(GV_StringifyDigraphEdge, "for a graphviz edge",
1079-
[IsGVEdge],
1080-
function(edge)
1031+
BindGlobal("GV_StringifyEdge",
1032+
function(edge, edge_str)
10811033
local head, tail, attrs;
1034+
Assert(0, IsGVEdge(edge));
1035+
Assert(0, IsString(edge_str));
10821036
head := GraphvizName(GraphvizHead(edge));
10831037
tail := GraphvizName(GraphvizTail(edge));
10841038
attrs := GraphvizAttrs(edge);
10851039

10861040
# handle : syntax
1087-
return StringFormatted("\t{} -> {}{}\n",
1041+
return StringFormatted("\t{} {} {}{}\n",
10881042
head,
1043+
edge_str,
10891044
tail,
10901045
GV_StringifyNodeEdgeAttrs(attrs));
10911046
end);
@@ -1233,9 +1188,9 @@ function(graph, is_subgraph)
12331188
Append(result, GV_StringifyNode(obj));
12341189
elif IsGVEdge(obj) then
12351190
if IsGVDigraph(GV_GetRoot(graph)) then
1236-
Append(result, GV_StringifyDigraphEdge(obj));
1191+
Append(result, GV_StringifyEdge(obj, "->"));
12371192
else
1238-
Append(result, GV_StringifyGraphEdge(obj));
1193+
Append(result, GV_StringifyEdge(obj, "--"));
12391194
fi;
12401195
else
12411196
return ErrorNoReturn("Invalid graphviz object type.");

0 commit comments

Comments
 (0)