diff --git a/gap/dot.gd b/gap/dot.gd index 62b2968..d36b448 100644 --- a/gap/dot.gd +++ b/gap/dot.gd @@ -557,6 +557,16 @@ DeclareOperation("GraphvizSetAttr", [IsGraphvizObject, IsObject, IsObject]); DeclareOperation("GraphvizSetAttr", [IsGraphvizObject, IsObject]); #! @EndGroup +#! @Arguments gv, obj +#! @Returns the value of an attribute or fail. +#! @Description +#! This operation returns the current value of the attribute obtained from +#! String(obj), or fail 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. @@ -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 attr. -#! @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 attr. -#! @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 diff --git a/gap/dot.gi b/gap/dot.gi index c133c69..bdc53cf 100644 --- a/gap/dot.gi +++ b/gap/dot.gi @@ -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); @@ -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); @@ -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 ############################################################################# diff --git a/tst/edge.tst b/tst/edge.tst index a095ee3..26da424 100644 --- a/tst/edge.tst +++ b/tst/edge.tst @@ -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) diff --git a/tst/node.tst b/tst/node.tst index 49bb8be..0df86ab 100644 --- a/tst/node.tst +++ b/tst/node.tst @@ -63,15 +63,15 @@ gap> n := GraphvizAddNode(g, 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); + +gap> GraphvizGetAttr(n, 1); "2" gap> GraphvizRemoveAttr(n, 1); -gap> n[1]; +gap> GraphvizGetAttr(n, 1); fail # Test removing a node with a non-string name @@ -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)