Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Nov 4, 2024
1 parent bbc8df7 commit da920e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import Data.Hashable (Hashable)
import Data.Int (Int32)
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NE
import Data.List.Split (wordsBy)
import Data.Maybe (catMaybes, mapMaybe)
import Data.Semigroup (sconcat)
import Data.Tuple (swap)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Type
import Text.AhoCorasick (makeStateMachine)
import Data.List.Split (wordsBy)

-- | Given all candidate structures, explode them into annotated rows.
-- These annotations entail both the row index with the original structure
Expand Down Expand Up @@ -77,7 +77,6 @@ mkEntityLookup ::
mkEntityLookup grids =
HM.map mkRowAutomatons rowsByEntityParticipation
where

-- Produces an automaton to evaluate whenever a given entity
-- is encountered.
mkRowAutomatons neList =
Expand All @@ -86,7 +85,6 @@ mkEntityLookup grids =
where
searchPatternsAndSubAutomatons = NE.map (\(a, b) -> (a, mkSmValue a b)) groupedByUniqueRow
where

groupedByUniqueRow =
binTuplesHMasListNE $
NE.map (rowContent . myRow &&& id) neList
Expand All @@ -102,18 +100,23 @@ mkEntityLookup grids =
bounds = sconcat $ NE.map expandedOffsets neList
sm = makeStateMachine $ NE.toList searchPatternsAndSubAutomatons

extractedChunksForStateMachine = HS.fromList . concat . NE.toList $
NE.map (map chunkContents . contiguousChunks) neList
extractedChunksForStateMachine =
HS.fromList . concat . NE.toList $
NE.map (map chunkContents . contiguousChunks) neList

extractedChunksForLookup = HM.fromList $ NE.toList $ NE.map
(HS.fromList . map chunkContents . contiguousChunks &&& mkRightMap)
neList
where
mkRightMap sreo = binTuplesHM $ map (chunkContents &&& chunkStartPos) $ contiguousChunks sreo

smPiecewise = makeStateMachine $ map (NE.toList . fmap Just &&& id) $
HS.toList extractedChunksForStateMachine
extractedChunksForLookup =
HM.fromList $
NE.toList $
NE.map
(HS.fromList . map chunkContents . contiguousChunks &&& mkRightMap)
neList
where
mkRightMap sreo = binTuplesHM $ map (chunkContents &&& chunkStartPos) $ contiguousChunks sreo

smPiecewise =
makeStateMachine $
map (NE.toList . fmap Just &&& id) $
HS.toList extractedChunksForStateMachine

-- The values of this map are guaranteed to contain only one
-- entry per row of each structure, even if some of those
Expand All @@ -127,11 +130,13 @@ mkEntityLookup grids =
$ allStructureRows grids

getContiguousChunks :: [Maybe a] -> [PositionedChunk a]
getContiguousChunks rowMembers = map mkChunk .
mapMaybe (NE.nonEmpty . mapMaybe sequenceA) .
wordsBy (null . snd) $ zip [0::Int ..] rowMembers
where
mkChunk xs = PositionedChunk (fst $ NE.head xs) (NE.map snd xs)
getContiguousChunks rowMembers =
map mkChunk
. mapMaybe (NE.nonEmpty . mapMaybe sequenceA)
. wordsBy (null . snd)
$ zip [0 :: Int ..] rowMembers
where
mkChunk xs = PositionedChunk (fst $ NE.head xs) (NE.map snd xs)

-- | All of the occurrences of each unique entity within a row
-- are consolidated into one record, in which the repetitions are noted.
Expand All @@ -145,7 +150,6 @@ explodeRowEntities ::
explodeRowEntities annotatedRow@(StructureRow _ _ rowMembers) =
map f $ HM.toList $ binTuplesHM unconsolidatedEntityOccurrences
where

chunks = getContiguousChunks rowMembers

f (e, occurrences) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ registerRowMatches ::
RecognitionState b a ->
s (RecognitionState b a)
registerRowMatches entLoader cLoc (AutomatonNewInfo horizontalOffsets sm _ pwMatcher) rState = do

entitiesRow <-
getWorldRow
entLoader
Expand All @@ -186,7 +185,6 @@ registerRowMatches entLoader cLoc (AutomatonNewInfo horizontalOffsets sm _ pwMat
rState2 = rState & recognitionLog %~ (logEntry :)
candidates = snd maskChoices


let PiecewiseRecognition pwSM pwMap = pwMatcher

Check warning on line 188 in src/swarm-topography/Swarm/Game/Scenario/Topography/Structure/Recognition/Tracking.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Defined but not used: ‘pwMap’
let candidatesChunked = findAll pwSM entitiesRow

Check warning on line 189 in src/swarm-topography/Swarm/Game/Scenario/Topography/Structure/Recognition/Tracking.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Defined but not used: ‘candidatesChunked’

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Control.Lens (makeLenses)
import Data.Aeson (ToJSON)
import Data.Function (on)
import Data.HashMap.Strict (HashMap)
import Data.HashSet (HashSet)
import Data.Int (Int32)
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
Expand All @@ -37,7 +38,6 @@ import Swarm.Game.Scenario.Topography.Area
import Swarm.Game.Scenario.Topography.Structure.Named (NamedGrid)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Static
import Swarm.Game.Universe (Cosmic, offsetBy)
import Data.HashSet (HashSet)
import Swarm.Language.Syntax.Direction (AbsoluteDir)
import Text.AhoCorasick (StateMachine)

Expand Down Expand Up @@ -94,14 +94,13 @@ data PositionWithinRow b a = PositionWithinRow
, structureRow :: StructureRow b a
}


data PiecewiseRecognition a = PiecewiseRecognition {
piecewiseSM :: StateMachine (AtomicKeySymbol a) (NonEmpty a)
data PiecewiseRecognition a = PiecewiseRecognition
{ piecewiseSM :: StateMachine (AtomicKeySymbol a) (NonEmpty a)
, picewiseLookup :: HashMap (HashSet (NonEmpty a)) (HashMap (NonEmpty a) (NonEmpty Int))
}

data PositionedChunk a = PositionedChunk {
chunkStartPos :: Int
data PositionedChunk a = PositionedChunk
{ chunkStartPos :: Int
, chunkContents :: NonEmpty a
}

Expand Down

0 comments on commit da920e3

Please sign in to comment.