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

trace functions #2134

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2133-show-functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: 1
name: Show functions
description: |
Press F2 to observe robot function execution
creative: false
robots:
- name: base
dir: east
devices:
- ADT calculator
- antenna
- branch predictor
- comparator
- compass
- dictionary
- grabber
- hourglass
- logger
- treads
- name: rat
dir: west
system: true
display:
invisible: false
devices:
- logger
program: |
run "data/scenarios/Testing/2133-robot-function-tracing/_2133-show-functions/rat.sw"
known: [bitcoin]
world:
dsl: |
{grass}
palette:
'B': [blank, null, base]
'r': [blank, null, rat]
'.': [blank]
upperleft: [-1, 0]
map: |
B.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def doWalk =
move;
move;
move;
end;

def doTurn =
turn left;
end;

def runSomeFunction = \f.
f;
end;

def go =
doWalk;
wait 2;
runSomeFunction doTurn;
wait 2;
go;
end;

go;
567 changes: 296 additions & 271 deletions src/swarm-engine/Swarm/Game/Step.hs

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion src/swarm-scenario/Swarm/Game/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module Swarm.Game.Robot (
systemRobot,
selfDestruct,
runningAtomic,
robotDebug,
currentFunction,
evalBuffer,

-- ** Creation & instantiation
mkRobot,
Expand Down Expand Up @@ -83,6 +86,7 @@ import Swarm.Language.JSON ()
import Swarm.Language.Syntax (Syntax, TSyntax)
import Swarm.Language.Text.Markdown (Document)
import Swarm.Util.Lens (makeLensesExcluding)
import Swarm.Util.RingBuffer
import Swarm.Util.Yaml
import System.Clock (TimeSpec)

Expand Down Expand Up @@ -121,6 +125,14 @@ type instance RobotLogMember 'TemplateRobot = ()
type family RobotLogUpdatedMember (phase :: RobotPhase) :: Data.Kind.Type
type instance RobotLogUpdatedMember 'TemplateRobot = ()

data RobotDebug = RobotDebug
{ _currentFunction :: Maybe Text
, _evalBuffer :: RingBuffer Text
}
deriving (Show, Eq)

makeLenses ''RobotDebug

-- | A value of type 'RobotR' is a record representing the state of a
-- single robot. The @f@ parameter is for tracking whether or not
-- the robot has been assigned a unique ID.
Expand All @@ -140,6 +152,7 @@ data RobotR (phase :: RobotPhase) = RobotR
, _systemRobot :: Bool
, _selfDestruct :: Bool
, _activityCounts :: RobotActivity phase
, _robotDebug :: RobotDebug
, _runningAtomic :: Bool
, _unwalkableEntities :: WalkabilityExceptions EntityName
, _robotCreatedAt :: TimeSpec
Expand All @@ -152,7 +165,15 @@ deriving instance (Eq (RobotLocation phase), Eq (RobotID phase), Eq (RobotMachin
-- See https://byorgey.wordpress.com/2021/09/17/automatically-updated-cached-views-with-lens/
-- for the approach used here with lenses.

makeLensesExcluding ['_robotCapabilities, '_equippedDevices, '_robotLog, '_robotLogUpdated, '_machine, '_activityCounts] ''RobotR
makeLensesExcluding
[ '_robotCapabilities
, '_equippedDevices
, '_robotLog
, '_robotLogUpdated
, '_machine
, '_activityCounts
]
''RobotR

-- | A template robot, i.e. a template robot record without a unique ID number,
-- and possibly without a location.
Expand Down Expand Up @@ -182,6 +203,7 @@ unwalkableEntities :: Lens' Robot (WalkabilityExceptions EntityName)

-- | The creation date of the robot.
robotCreatedAt :: Lens' Robot TimeSpec
robotDebug :: Lens' (RobotR phase) RobotDebug

-- robotName and trobotName could be generalized to
-- @robotName' :: Lens' (RobotR phase) Text@.
Expand Down Expand Up @@ -344,6 +366,11 @@ mkRobot pid name descr loc dir disp m devs inv sys heavy unwalkables ts =
, _systemRobot = sys
, _selfDestruct = False
, _activityCounts = ()
, _robotDebug =
RobotDebug
{ _currentFunction = Nothing
, _evalBuffer = mkRingBuffer $ Finite 16
}
, _runningAtomic = False
, _unwalkableEntities = unwalkables
}
Expand Down
5 changes: 5 additions & 0 deletions src/swarm-tui/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ robotsListWidget s = hCenter table
, "Pos"
, "Items"
, "Status"
, "Func"
, "Actns"
, "Cmds"
, "Cycles"
Expand All @@ -716,6 +717,7 @@ robotsListWidget s = hCenter table
]
headers = withAttr robotAttr . txt <$> applyWhen debugRID ("ID" :) headings
robotsTable = mkRobotRow <$> robots

mkRobotRow robot =
applyWhen debugRID (idWidget :) cells
where
Expand All @@ -725,6 +727,7 @@ robotsListWidget s = hCenter table
, locWidget
, padRight (Pad 1) (str $ show rInvCount)
, statusWidget
, activeFunction
, str $ show $ robot ^. activityCounts . tangibleCommandCount
, -- TODO(#1341): May want to expose the details of this histogram in
-- a per-robot pop-up
Expand All @@ -734,6 +737,8 @@ robotsListWidget s = hCenter table
, txt rLog
]

activeFunction = txt . fromMaybe "N/A" $ robot ^. robotDebug . currentFunction

idWidget = str $ show $ robot ^. robotID
nameWidget =
hBox
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-tui/Swarm/TUI/View/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ generateModal s mt = Modal mt (dialog (Just $ str title) buttons (maxModalWindow
(title, buttons, requiredWidth) =
case mt of
HelpModal -> (" Help ", Nothing, descriptionWidth)
RobotsModal -> ("Robots", Nothing, descriptionWidth)
RobotsModal -> ("Robots", Nothing, 160)
RecipesModal -> ("Available Recipes", Nothing, descriptionWidth)
CommandsModal -> ("Available Commands", Nothing, descriptionWidth)
MessagesModal -> ("Messages", Nothing, descriptionWidth)
Expand Down
7 changes: 7 additions & 0 deletions src/swarm-util/Swarm/Util/RingBuffer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Swarm.Util.RingBuffer (
) where

import Data.Aeson
import Data.Foldable (toList)
import Data.Sequence as S
import Servant.Docs (ToSample)
import Servant.Docs qualified as SD
Expand All @@ -30,6 +31,12 @@ instance (ToJSON a) => ToJSON (RingBuffer a) where
instance ToSample (RingBuffer a) where
toSamples _ = SD.noSamples

instance (Show a) => Show (RingBuffer a) where
show rb = show $ toList $ getValues rb

instance Eq (RingBuffer a) where
_ == _ = True

getValues :: RingBuffer a -> Seq a
getValues (RingBuffer xs _) = xs

Expand Down
Loading