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
36 changes: 10 additions & 26 deletions gap/dot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,16 @@ DeclareOperation("GraphvizSetAttr", [IsGraphvizObject, IsObject, IsObject]);
DeclareOperation("GraphvizSetAttr", [IsGraphvizObject, IsObject]);
#! @EndGroup

#! @Arguments gv, obj
#! @Returns the value of an attribute or <K>fail</K>.
#! @Description
#! This operation returns the current value of the attribute obtained from
#! <C>String(obj)</C>, or <K>fail</K> if the value of the attribute has not
#! been set.
#! @BeginExampleSession
#! @EndExampleSession
DeclareOperation("GraphvizGetAttr", [IsGraphvizObject, IsObject]);

#! @Arguments obj, attr
#! @Returns a &GAPGraphviz; object
#! @Description Removes an attribute from the object provided.
Expand Down Expand Up @@ -619,32 +629,6 @@ DeclareOperation("GraphvizSetNodeLabels",
#! @EndExampleSession
DeclareGlobalFunction("ErrorIfNotValidColor");

#! @BeginGroup
#! @GroupTitle Getting attributes
#! @Arguments edge, attr
#! @Returns an object
#! @Description
#! Gets the value associated with the attribute <K>attr</K>.
#! @BeginExampleSession
#! @EndExampleSession
DeclareOperation("\[\]", [IsGraphvizEdge, IsObject]);
#! @Arguments node, attr
DeclareOperation("\[\]", [IsGraphvizNode, IsObject]);
#! @EndGroup

#! @BeginGroup
#! @GroupTitle Setting attributes
#! @Arguments node, attr
#! @Returns an object
#! @Description
#! Sets the value associated with the attribute <K>attr</K>.
#! @BeginExampleSession
#! @EndExampleSession
DeclareOperation("\[\]\:\=", [IsGraphvizNode, IsObject, IsObject]);
#! @Arguments edge, attr
DeclareOperation("\[\]\:\=", [IsGraphvizEdge, IsObject, IsObject]);
#! @EndGroup

#! @Arguments graph, node_name
#! @Returns a &GAPGraphviz; node object or fail
#! @Description
Expand Down
81 changes: 15 additions & 66 deletions gap/dot.gi
Original file line number Diff line number Diff line change
Expand Up @@ -159,70 +159,6 @@ x -> x!.Head);
InstallMethod(\=, "for graphviz nodes",
[IsGraphvizNode, IsGraphvizNode], IsIdenticalObj);

# Accessing node attributes

# TODO Remove this, and just use GraphvizAttrs(node) directly?
InstallMethod(\[\], "for a graphviz node and a string",
[IsGraphvizNode, IsString],
function(node, key)
if IsBound(GraphvizAttrs(node).(key)) then
return GraphvizAttrs(node).(key);
fi;
return fail;
end);

# TODO Remove this, and just use GraphvizAttrs(node) directly?
InstallMethod(\[\], "for a graphviz node and an object",
[IsGraphvizNode, IsObject],
{node, key} -> node[String(key)]);

# Setting node attributes

# TODO Remove this, and just use GraphvizAttrs(node) directly?
InstallMethod(\[\]\:\=, "for a graphviz node and two strings",
[IsGraphvizNode, IsString, IsString],
function(node, key, val)
GraphvizAttrs(node).(key) := val;
end);

# TODO Remove this, and just use SetGraphvizAttr(node, key, val) directly?
InstallMethod(\[\]\:\=, "for a graphviz node and two strings",
[IsGraphvizNode, IsObject, IsObject],
function(node, key, val)
node[String(key)] := String(val);
end);

# Accessing edge attributes

# TODO Remove this, and just use GraphvizAttr(edge) directly?
InstallMethod(\[\], "for a graphviz edge and a string",
[IsGraphvizEdge, IsString],
function(edge, key)
if IsBound(GraphvizAttrs(edge).(key)) then
return GraphvizAttrs(edge).(key);
fi;
return fail;
end);

# TODO Remove this, and just use GraphvizAttr(edge) directly?
InstallMethod(\[\], "for a graphviz edge and an object",
[IsGraphvizEdge, IsObject],
{edge, key} -> edge[String(key)]);

# TODO Remove this, and just use GraphvizAttr(edge) directly?
InstallMethod(\[\]\:\=, "for a graphviz edge and a string",
[IsGraphvizEdge, IsString, IsString],
function(edge, key, val)
GraphvizAttrs(edge).(key) := val;
end);

# TODO Remove this, and use GraphvizSetAttr(edge) instead?
InstallMethod(\[\]\:\=, "for a graphviz edge and an object",
[IsGraphvizEdge, IsObject, IsObject],
function(edge, key, val)
edge[String(key)] := String(val);
end);

InstallMethod(\=, "for graphviz edges",
[IsGraphvizEdge, IsGraphvizEdge], IsIdenticalObj);

Expand Down Expand Up @@ -286,8 +222,8 @@ end);
InstallMethod(GraphvizSetAttr,
"for a graphviz node or edge, object, and object",
[IsGraphvizNodeOrEdge, IsObject, IsObject],
function(x, name, value)
GraphvizAttrs(x).(String(name)) := String(value);
function(x, key, value)
GraphvizAttrs(x).(String(key)) := String(value);
return x;
end);

Expand Down Expand Up @@ -343,6 +279,19 @@ function(x, value)
return x;
end);

InstallMethod(GraphvizGetAttr, "for a graphviz (di)graph or context and string",
[IsGraphvizObject, IsString],
function(x, key)
if not IsBound(GraphvizAttrs(x).(key)) then
return fail;
fi;
return GraphvizAttrs(x).(key);
end);

InstallMethod(GraphvizGetAttr, "for a graphviz (di)graph or context and string",
[IsGraphvizObject, IsObject],
{x, key} -> GraphvizGetAttr(x, String(key)));

#############################################################################
# GraphvizAddNode
#############################################################################
Expand Down
26 changes: 13 additions & 13 deletions tst/edge.tst
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,26 @@ gap> GraphvizRemoveEdges(g, 1, 1);;
gap> GraphvizEdges(g);
[ ]

# Test setting attributes using the []:= syntax
# Test setting attributes
gap> g := GraphvizGraph();;
gap> n := GraphvizAddEdge(g, "a", "b");;
gap> n["color"] := "red";;
gap> GraphvizAttrs(n);
gap> e := GraphvizAddEdge(g, "a", "b");;
gap> GraphvizSetAttr(e, "color", "red");;
gap> GraphvizAttrs(e);
rec( color := "red" )
gap> n["label"] := 1;;
gap> GraphvizAttrs(n);
gap> GraphvizSetAttr(e, "label", 1);;
gap> GraphvizAttrs(e);
rec( color := "red", label := "1" )
gap> n["color"] := "blue";;
gap> GraphvizAttrs(n);
gap> GraphvizSetAttr(e, "color", "blue");;
gap> GraphvizAttrs(e);
rec( color := "blue", label := "1" )
gap> n[1];
gap> GraphvizGetAttr(e, 1);
fail

# Test getting attributes using the [] syntax
# Test getting attributes
gap> g := GraphvizGraph();;
gap> n := GraphvizAddEdge(g, "a", "b");;
gap> n["color"] := "red";;
gap> n["color"];
gap> e := GraphvizAddEdge(g, "a", "b");;
gap> GraphvizSetAttr(e, "color", "red");;
gap> GraphvizGetAttr(e, "color");
"red"

# Test set label (edge)
Expand Down
24 changes: 12 additions & 12 deletions tst/node.tst
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ gap> n := GraphvizAddNode(g, 111);
<graphviz node "111">
gap> AsString(g);
"//dot\ngraph {\n\t111\n}\n"
gap> n[1];
gap> GraphvizGetAttr(n, 1);
fail
gap> n[1] := 2;
2
gap> n[1];
gap> GraphvizSetAttr(n, 1, 2);
<graphviz node "111">
gap> GraphvizGetAttr(n, 1);
"2"
gap> GraphvizRemoveAttr(n, 1);
<graphviz node "111">
gap> n[1];
gap> GraphvizGetAttr(n, 1);
fail

# Test removing a node with a non-string name
Expand All @@ -81,24 +81,24 @@ gap> GraphvizRemoveNode(g, 111);;
gap> GraphvizNodes(g);
rec( )

# Test setting attributes using the []:= syntax
# Test setting attributes
gap> g := GraphvizGraph();;
gap> n := GraphvizAddNode(g, "a");;
gap> n["color"] := "red";;
gap> GraphvizSetAttr(n, "color", "red");;
gap> GraphvizAttrs(n);
rec( color := "red" )
gap> n["label"] := 1;;
gap> GraphvizSetAttr(n, "label", 1);;
gap> GraphvizAttrs(n);
rec( color := "red", label := "1" )
gap> n["color"] := "blue";;
gap> GraphvizSetAttr(n, "color", "blue");;
gap> GraphvizAttrs(n);
rec( color := "blue", label := "1" )

# Test getting attributes using the [] syntax
# Test getting attributes
gap> g := GraphvizGraph();;
gap> n := GraphvizAddNode(g, "a");;
gap> n["color"] := "red";;
gap> n["color"];
gap> GraphvizSetAttr(n, "color", "red");;
gap> GraphvizGetAttr(n, "color");
"red"

# Test set label (node)
Expand Down
Loading