claferIG-0.5.8: src/Language/Clafer/IG/Sugarer.hs
{-
Copyright (C) 2012-2017 Jimmy Liang, Michal Antkiewicz <http://gsd.uwaterloo.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-}
module Language.Clafer.IG.Sugarer (sugarClaferModel, sugarClaferStates) where
import Language.Clafer.Common
import Language.Clafer.Intermediate.Intclafer (_ident)
import Language.Clafer.IG.ClaferModel
import Data.Maybe (fromMaybe, fromJust)
import Data.List as List hiding (map)
import Data.Map as Map hiding (map, foldr, foldl)
import Prelude hiding (id)
-- | Sample: maps the id to the its simple name and the number of times its simple name appeared in the census before it
-- | Count: maps the simple name to the total count of the simple name
data Census = Census
(Map Id (Int, String)) -- Sample
(Map String Int) -- Counts
deriving Show
-- | Adds the full name to the census
poll :: UIDIClaferMap -> Id -> Census -> Census
poll uidIClaferMap' id (Census sample' counts') =
Census sample'' counts''
where
fullName = i_name id
name = claferName uidIClaferMap' fullName
counts'' = insertWith (+) name 1 counts'
ordinal' = findWithDefault (error $ "Did not find " ++ name ++ " in counts.") name counts''
sample'' = insertWith (error $ "Polled " ++ fullName ++ " twice in the census.") id (ordinal', name) sample'
-- | The clafer's name for its UID (its Alloy name): its _ident, which the UID need not
-- contain, e.g., size for x__size, or a' for a_prime.
claferName :: UIDIClaferMap -> String -> String
claferName uidIClaferMap' uid = maybe uid _ident $ findIClafer uidIClaferMap' uid
-- | Count the number of each clafer
claferModelCensus :: UIDIClaferMap -> ClaferModel -> Census
claferModelCensus uidIClaferMap' = statesCensus uidIClaferMap' . (:[])
-- | Count the number of each clafer in the states of a trace: an instance that exists in
-- several states is counted once, so it has the same name in each of them
statesCensus :: UIDIClaferMap -> [ClaferModel] -> Census
statesCensus uidIClaferMap' states =
foldl clafersCensus (Census Map.empty Map.empty) $ map c_topLevel states
where
clafersCensus = foldl claferCensus
claferCensus census Clafer{c_id=id, c_children=children} = pollOnce id (clafersCensus census children)
pollOnce id census@(Census sample' _)
| id `member` sample' = census
| otherwise = poll uidIClaferMap' id census
-- | Rewrite the model into a human-friendlier format
sugarClaferModel:: (String -> String) -> Bool -> Bool -> UIDIClaferMap -> ClaferModel -> Map Int String -> ClaferModel
sugarClaferModel toUid useUids addTypes uidIClaferMap' model sMap =
sugarWithCensus (claferModelCensus uidIClaferMap' model) toUid useUids addTypes uidIClaferMap' model sMap
-- | Rewrite the states of a trace into a human-friendlier format: an instance has the same name
-- in every state
sugarClaferStates :: (String -> String) -> Bool -> Bool -> UIDIClaferMap -> [ClaferModel] -> Map Int String -> [ClaferModel]
sugarClaferStates toUid useUids addTypes uidIClaferMap' states sMap =
map (\state -> sugarWithCensus census toUid useUids addTypes uidIClaferMap' state sMap) states
where
census = statesCensus uidIClaferMap' states
-- | The clafers of the model are by their Alloy names; with useUids, they are shown by their UIDs (toUid).
sugarWithCensus :: Census -> (String -> String) -> Bool -> Bool -> UIDIClaferMap -> ClaferModel -> Map Int String -> ClaferModel
sugarWithCensus census toUid useUids addTypes uidIClaferMap' (ClaferModel topLevelClafers) sMap =
ClaferModel $ map sugarClafer topLevelClafers
where
sugarClafer clafer@(Clafer id uid' _ children) =
Clafer (sugarId useUids addTypes True id) uid' (sugarValue clafer) (map sugarClafer children)
sugarValue (Clafer _ _ (Just (AliasValue alias)) _) = Just $ AliasValue $ sugarId useUids addTypes False alias
sugarValue (Clafer _ _ Nothing _) = Nothing
sugarValue c = if cType c == "string" then Just (StringValue (getString c)) else c_value c
cType (Clafer id _ _ _) = cTypeSolve $ getReference iclafer
where
iclafer = fromMaybe (error $ "IClafer not found:" ++ i_name id) $ findIClafer uidIClaferMap' $ i_name id
cTypeSolve ["string"] = "string"
cTypeSolve ["integer"] = "integer"
cTypeSolve ["int"] = "integer"
cTypeSolve ["real"] = "real"
cTypeSolve _ = ""
getString c = case Map.lookup strNumber sMap of
Nothing -> "\"<text " ++ show strNumber ++ ">\""
Just s -> s
where strNumber = v_value $ fromJust $ c_value c
Census sample' counts' = census
sugarId :: Bool -> Bool -> Bool -> Id -> Id
sugarId useUids' addTypes' addRefDecl id =
Id (finalName ++ ordinalDisplay ++ refDecl addTypes' addRefDecl uidIClaferMap') 0
where
fullName = i_name id
ordinalDisplay = if useUids || count > 1
then "$" ++ show ordinal
else ""
refDecl :: Bool -> Bool -> UIDIClaferMap -> String
refDecl True True uidIClaferMap'' = retrieveSuper toUid useUids' uidIClaferMap'' $ i_name id
refDecl _ _ _ = ""
(ordinal, simpleName) = findWithDefault (error $ "Sample lookup " ++ show id ++ " failed.") id sample'
count = findWithDefault (error $ "Count lookup " ++ simpleName ++ " failed.") simpleName counts'
finalName = if useUids' then toUid fullName else simpleName
retrieveSuper :: (String -> String) -> Bool -> UIDIClaferMap -> String -> String
retrieveSuper toUid useUids' uidIClaferMap' uid =
sugarSuper (getSuper iclafer)
-- ++
-- sugarReference (getReference iclafer)
where
iclafer = fromMaybe (error $ "IClafer not found: " ++ uid) $ findIClafer uidIClaferMap' uid
sugarSuper :: [String] -> String
sugarSuper [s] = " : " ++ if useUids' then toUid s else claferName uidIClaferMap' s
sugarSuper _ = ""
-- sugarReference :: [String] -> String
-- sugarReference [s] = " -> " ++ s
-- sugarReference _ = ""