Skip to content

Commit

Permalink
render goals graph (#2289)
Browse files Browse the repository at this point in the history
# Demo

1. In one console:
    ```
    scripts/play.sh -i data/scenarios/Challenges/bridge-building.yaml
   ```
2. In another console:
    ```
    scripts/gen/goals-graph.sh
    ```
3. View `goals.svg`:
    ![goals](https://github.com/user-attachments/assets/d6114c27-fde2-41ce-8c65-c52d3a9707de)
  • Loading branch information
kostmo authored Jan 12, 2025
1 parent 3d465b4 commit 809e8ff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/gen/goals-graph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -ex

cd $(git rev-parse --show-toplevel)

curl -s http://localhost:5357/goals/render | dot -Tsvg -o goals.svg
10 changes: 10 additions & 0 deletions src/swarm-web/Swarm/Web.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import Swarm.TUI.Model.UI
import Swarm.TUI.Model.UI.Gameplay
import Swarm.Util (applyJust)
import Swarm.Util.RingBuffer
import Swarm.Web.GraphRender
import Swarm.Web.Worldview
import System.Timeout (timeout)
import Text.Read (readEither)
Expand All @@ -107,6 +108,7 @@ type SwarmAPI =
:<|> "goals" :> "prereqs" :> Get '[JSON] [PrereqSatisfaction]
:<|> "goals" :> "active" :> Get '[JSON] [Objective]
:<|> "goals" :> "graph" :> Get '[JSON] (Maybe GraphInfo)
:<|> "goals" :> "render" :> Get '[PlainText] T.Text
:<|> "goals" :> "uigoal" :> Get '[JSON] GoalTracking
:<|> "goals" :> Get '[JSON] WinCondition
:<|> "navigation" :> Get '[JSON] (Navigation (M.Map SubworldName) Location)
Expand Down Expand Up @@ -164,6 +166,7 @@ mkApp state events =
:<|> prereqsHandler state
:<|> activeGoalsHandler state
:<|> goalsGraphHandler state
:<|> goalsRenderHandler state
:<|> uiGoalHandler state
:<|> goalsHandler state
:<|> navigationHandler state
Expand Down Expand Up @@ -206,6 +209,13 @@ goalsGraphHandler appStateRef = do
WinConditions _winState oc -> Just $ makeGraphInfo oc
_ -> Nothing

goalsRenderHandler :: IO AppState -> Handler T.Text
goalsRenderHandler appStateRef = do
appState <- liftIO appStateRef
return $ case appState ^. gameState . winCondition of
WinConditions _winState oc -> T.pack $ renderGoalsGraph oc
_ -> mempty

uiGoalHandler :: IO AppState -> Handler GoalTracking
uiGoalHandler appStateRef = do
appState <- liftIO appStateRef
Expand Down
27 changes: 27 additions & 0 deletions src/swarm-web/Swarm/Web/GraphRender.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
module Swarm.Web.GraphRender where

import Control.Lens ((^.), (^..))
import Data.Map qualified as M
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Swarm.Game.Scenario.Objective
import Swarm.Game.Scenario.Objective.Graph
import Text.Dot

renderGoalsGraph :: ObjectiveCompletion -> String
renderGoalsGraph oc =
showDot nlg
where
edgeLookup = M.fromList $ map (\x@(_, b, _) -> (b, x)) edges
nlg =
netlistGraph
(\k -> maybe mempty (\(a, _, _) -> [("label", T.unpack $ fromMaybe "<?>" $ a ^. objectiveId)]) $ M.lookup k edgeLookup)
(\k -> maybe mempty (\(_, _, c) -> c) $ M.lookup k edgeLookup)
([(a, a) | (_, a, _) <- edges])

edges = makeGraphEdges objs
objs = oc ^.. allObjectives
2 changes: 2 additions & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,14 @@ library swarm-web
wai,
wai-app-static,
warp,
dotgen,
witch,

visibility: public
-- cabal-gild: discover src/swarm-web
exposed-modules:
Swarm.Web
Swarm.Web.GraphRender
Swarm.Web.Worldview

other-modules: Paths_swarm
Expand Down

0 comments on commit 809e8ff

Please sign in to comment.