Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GraphML.Writer use vertexID and edgeEndpoints #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions src/Pangraph/GraphML/Writer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,28 @@ writeGraphML _ = "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" \
writeGraphTag :: Int -> ByteString
writeGraphTag i = getIndentBS i `append` "<graph id=\"G\" edgedefault=\"undirected\">\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, "<node", nodesAtts, "/>\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, "<edge", edgeAtts, "/>\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, "\""]
Expand Down
28 changes: 27 additions & 1 deletion test/GraphML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Pangraph.GraphML.Writer
import Pangraph.Examples.SampleGraph

graphmlTests :: [Test]
graphmlTests = [case1, case2]
graphmlTests = [case1, case2, case3]

case1 :: Test
case1 =
Expand All @@ -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
[ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n",
" <graph id=\"G\" edgedefault=\"undirected\">\n",
" <node id=\"bar\"/>\n",
" <node id=\"buzz\"/>\n",
" <node id=\"foo\"/>\n",
" <edge source=\"foo\" target=\"bar\"/>\n",
" <edge source=\"bar\" target=\"buzz\"/>\n",
" <edge source=\"buzz\" target=\"foo\"/>\n",
" </graph>\n",
"</graphml>"
]