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

Add a flag to generate a graphviz graph of fragments forbidding each other #21

Open
wants to merge 2 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
22 changes: 16 additions & 6 deletions syncon-parser/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ composeCommand = Opt.command "compose" (Opt.info composeCmd $ Opt.progDesc "Crea
<> Opt.metavar "N"
<> Opt.help "The minimum number of language fragments present in the composition, including the base fragment."
<> Opt.value 10
forbidGraph <- Opt.switch
$ Opt.long "forbid-graph"
<> Opt.help "If supplied, output a graphviz dot graph where fragments are connected by an edge if they forbid each other."
base <- Opt.argument Opt.str
$ Opt.metavar "FILE"
<> Opt.help "The '.syncon' file that defines the base fragment that is to always be present."
Expand All @@ -619,12 +622,19 @@ composeCommand = Opt.command "compose" (Opt.info composeCmd $ Opt.progDesc "Crea
pure $ do
let baseID = RC.mkID base
info <- RC.computeInfo (base : others) >>= dataOrError mempty ()
case dirPath of
Nothing -> RC.generateComposition count baseID info
>>= dataOrError mempty ()
>>= putStrLn
Just path -> RC.writeAllFragmentsToDir path baseID info
>>= dataOrError mempty ()
let mkSingle = isNothing dirPath && not forbidGraph

forM_ dirPath $ \path ->
RC.writeAllFragmentsToDir path baseID info
>>= dataOrError mempty ()

when forbidGraph $
putStr $ RC.graphvizForbidGraph info

when mkSingle $
RC.generateComposition count baseID info
>>= dataOrError mempty ()
>>= putStrLn

main :: IO ()
main = join $ Opt.execParser $ Opt.info (Opt.hsubparser (compileCommand <> parseCommand <> devCommand <> pbtCommand <> composeCommand) <**> Opt.helper)
Expand Down
16 changes: 16 additions & 0 deletions syncon-parser/exe/RandomCompose.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module RandomCompose
, computeInfo
, writeAllFragmentsToDir
, generateComposition
, graphvizForbidGraph
) where

import Pre hiding (state, (<.>))
Expand Down Expand Up @@ -106,6 +107,21 @@ data ComposeState = ComposeState
, pickable :: !(HashSet ID)
}

graphvizForbidGraph :: HashMap ID FileInfo -> Text
graphvizForbidGraph infos =
"graph {\n"
<> (toList edges <&> mkEdge <&> (<> ";\n") & Text.concat)
<> "}\n"
where
mkNode (ID id) = show id
mkEdge (id1, id2) = mkNode id1 <> " -- " <> mkNode id2
edges = toList infos
>>= (\FileInfo{id, forbidden} -> toList forbidden <&> (id,) <&> sortTuple)
& S.fromList
sortTuple (a, b)
| a < b = (a, b)
| otherwise = (b, a)

addID :: HashSet ID -> ID -> StateT ComposeState (Result (HashSet Error)) (HashSet ID)
addID picked id
| id `S.member` picked = return picked
Expand Down