From d057b550bb1360999366d7c985c98cbfb12c9fbc Mon Sep 17 00:00:00 2001 From: Toshio Ito Date: Thu, 20 Jun 2019 08:08:47 +0900 Subject: [PATCH 1/3] test/GraphML: add test case without explicit id attributes. --- test/GraphML.hs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/GraphML.hs b/test/GraphML.hs index 2463bcb..c9004f5 100644 --- a/test/GraphML.hs +++ b/test/GraphML.hs @@ -12,7 +12,7 @@ import Pangraph.GraphML.Writer import Pangraph.Examples.SampleGraph graphmlTests :: [Test] -graphmlTests = [case1, case2] +graphmlTests = [case1, case2, case3] case1 :: Test case1 = @@ -34,3 +34,29 @@ case1 = case2 :: Test case2 = TestCase $ assertEqual "GraphML Write case 1" (Just smallGraph) (parse . write $ smallGraph) + +case3 :: Test +case3 = TestCase $ assertEqual "GraphML Write case: without explicit id attributes" expected (write input) + where + (Just input) = makePangraph vertices edges + vertices = [ makeVertex "foo" [], + makeVertex "bar" [], + makeVertex "buzz" [] + ] + edges = [ makeEdge ("foo", "bar") [], + makeEdge ("bar", "buzz") [], + makeEdge ("buzz", "foo") [] + ] + expected = mconcat + [ "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n" + ] From 4554db84dfa214a8c67e19f5ffb1289417a01d75 Mon Sep 17 00:00:00 2001 From: Toshio Ito Date: Thu, 20 Jun 2019 08:21:30 +0900 Subject: [PATCH 2/3] GraphML.Writer: now it uses vertexID and edgeEndpoints to generate "id", "source" and "target" attributes. If they are already defined in vertexAttributes or edgeAttributes, those are ignored. --- src/Pangraph/GraphML/Writer.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Pangraph/GraphML/Writer.hs b/src/Pangraph/GraphML/Writer.hs index f756323..e23d5b2 100644 --- a/src/Pangraph/GraphML/Writer.hs +++ b/src/Pangraph/GraphML/Writer.hs @@ -37,17 +37,28 @@ writeGraphML _ = " ByteString writeGraphTag i = getIndentBS i `append` "\n" +mergedVertexAttributes :: Vertex -> [Attribute] +mergedVertexAttributes v = ("id", vertexID v) : (filter notID $ vertexAttributes v) + where + notID (key, _) = key /= "id" + writeNode :: Int -> Vertex -> ByteString writeNode i v = concat [getIndentBS i, "\n"] where nodesAtts :: ByteString - nodesAtts = concat $ map writeAttribute (vertexAttributes v) + nodesAtts = concat $ map writeAttribute (mergedVertexAttributes v) + +mergedEdgeAttributes :: Edge -> [Attribute] +mergedEdgeAttributes e = ("source", source) : ("target", target) : (filter notEndpoint $ edgeAttributes e) + where + (source, target) = edgeEndpoints e + notEndpoint (key, _) = key /= "source" && key /= "target" writeEdge :: Int -> Edge -> ByteString writeEdge i e = concat [getIndentBS i, "\n"] where edgeAtts :: ByteString - edgeAtts = concat $ map writeAttribute (edgeAttributes e) + edgeAtts = concat $ map writeAttribute (mergedEdgeAttributes e) writeAttribute :: Attribute -> ByteString writeAttribute (a,b) = concat [" ", a, "=\"", b, "\""] From fa0c93f8acb884e338abadf6ba419cf5fa57f0af Mon Sep 17 00:00:00 2001 From: Toshio Ito Date: Thu, 20 Jun 2019 13:36:02 +0900 Subject: [PATCH 3/3] test/GraphML: fix expectation. nodes are ordered by the ID. --- test/GraphML.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/GraphML.hs b/test/GraphML.hs index c9004f5..cd6e6ec 100644 --- a/test/GraphML.hs +++ b/test/GraphML.hs @@ -51,12 +51,12 @@ case3 = TestCase $ assertEqual "GraphML Write case: without explicit id attribut [ "\n", "\n", " \n", - " \n", " \n", " \n", + " \n", " \n", " \n", " \n", " \n", - "\n" + "" ]