Morphism.hs revision 332dadf617c7fdc3353346f6b8e1c4d918214f3c
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{- |
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceModule : $Header$
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceDescription : RDF Morphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceCopyright : (c) Francisc-Nicolae Bungiu, Felix Gabriel Mance, 2011
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceLicense : GPLv2 or higher, see LICENSE.txt
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceMaintainer : f.bungiu@jacobs-university.de
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceStability : provisional
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancePortability : portable
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceMorphisms for RDF
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mancemodule RDF.Morphism where
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Common.DocUtils
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Manceimport Common.Doc
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{-
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Common.Lib.State
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Common.Lib.MapSet (setToMap)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Common.Result
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport OWL2.AS
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport RDF.AS
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport RDF.Sign
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport RDF.Function
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance--import RDF.StaticAnalysis
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance--import RDF.Symbols
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport RDF.Print ()
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Control.Monad
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Data.Maybe
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport qualified Data.Map as Map
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport qualified Data.Set as Set
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mancedata RDFMorphism = RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance { osource :: Sign
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , otarget :: Sign
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , mmaps :: MorphMap
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance } deriving (Show, Eq, Ord)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinclRDFMorphism :: Sign -> Sign -> RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinclRDFMorphism s t = RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance { osource = s
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , otarget = t
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , mmaps = Map.empty }
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesymMap :: MorphMap -> Map.Map RDFEntity RDFEntity
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancesymMap = Map.mapWithKey (\ (RDFEntity ty _) -> RDFEntity ty)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedElems :: MorphMap -> [RDFEntity]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedElems = Map.elems . symMap
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedSign :: MorphMap -> Sign -> Sign
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedSign m = execState (do
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance mapM_ (modEntity Set.delete) $ Map.keys m
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance mapM_ (modEntity Set.insert) $ inducedElems m)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedFromMor :: Map.Map RawSymb RawSymb -> Sign -> Result RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceinducedFromMor rm sig = do
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let syms = symOf sig
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance mm <- foldM (\ m p -> case p of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance (ASymbol s@(RDFEntity _ v), ASymbol (RDFEntity _ u)) ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance if Set.member s syms
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance then return $ if u == v then m else Map.insert s u m
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance else fail $ "unknown symbol: " ++ showDoc s "\n" ++ shows sig ""
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance (AnUri v, AnUri u) -> case filter (`Set.member` syms)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance $ map (`RDFEntity` v) rdfEntityTypes of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance [] -> fail $ "unknown symbol: " ++ showDoc v "\n" ++ shows sig ""
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance l -> return $ if u == v then m else foldr (`Map.insert` u) m l
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance _ -> error "RDF.Morphism.inducedFromMor") Map.empty $ Map.toList rm
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance return RDFMorphism
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance { osource = sig
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance , otarget = inducedSign mm sig
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance , mmaps = mm }
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancesymMapOf :: RDFMorphism -> Map.Map RDFEntity RDFEntity
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancesymMapOf mor = Map.union (symMap $ mmaps mor) $ setToMap $ symOf $ osource mor
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance-}
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceisRDFInclusion :: RDFMorphism -> Bool
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceisRDFInclusion m = Map.null (mmaps m) && isSubSign (osource m) (otarget m)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Manceinstance Pretty RDFMorphism where
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance pretty m = let
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Mance s = osource m
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance srcD = specBraces $ space <> pretty s
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance t = otarget m
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance in if isRDFInclusion m then
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance if isSubSign t s then
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance fsep [text "identity morphism over", srcD]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance else fsep
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance [ text "inclusion morphism of"
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance , srcD
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , text "extended with"
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , pretty $ Set.difference (symOf t) $ symOf s ]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance else fsep
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance [ pretty $ mmaps m
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , colon <+> srcD, mapsto <+> specBraces (space <> pretty t) ]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{-
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancelegalMor :: RDFMorphism -> Result ()
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancelegalMor m = let mm = mmaps m in unless
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance (Set.isSubsetOf (Map.keysSet mm) (symOf $ osource m)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance && Set.isSubsetOf (Set.fromList $ inducedElems mm) (symOf $ otarget m))
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance $ fail "illegal RDF morphism"
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancecomposeMor :: RDFMorphism -> RDFMorphism -> Result RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancecomposeMor m1 m2 =
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let nm = Set.fold (\ s@(RDFEntity ty u) -> let
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance t = getIri ty u $ mmaps m1
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance r = getIri ty t $ mmaps m2
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance in if r == u then id else Map.insert s r) Map.empty
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance . symOf $ osource m1
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance in return m1
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance { otarget = otarget m2
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance , mmaps = nm }
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancecogeneratedSign :: Set.Set RDFEntity -> Sign -> Result RDFMorphism
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancecogeneratedSign s sign =
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let sig2 = execState (mapM_ (modEntity Set.delete) $ Set.toList s) sign
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance in if isSubSign sig2 sign then return $ inclRDFMorphism sig2 sign else
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance fail "non RDF subsignatures for (co)generatedSign"
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancegeneratedSign :: Set.Set RDFEntity -> Sign -> Result RDFMorphism
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancegeneratedSign s sign = cogeneratedSign (Set.difference (symOf sign) s) sign
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel MancematchesSym :: RDFEntity -> RawSymb -> Bool
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancematchesSym e@(RDFEntity _ u) r = case r of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance ASymbol s -> s == e
a921ae1da1302f673204e7b63cdce01439a9bd5eFelix Gabriel Mance AnUri s -> s == u || namePrefix u == localPart s && null (namePrefix s)
a921ae1da1302f673204e7b63cdce01439a9bd5eFelix Gabriel Mance
a921ae1da1302f673204e7b63cdce01439a9bd5eFelix Gabriel MancestatSymbItems :: [SymbItems] -> [RawSymb]
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancestatSymbItems = concatMap
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance $ \ (SymbItems m us) -> case m of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Nothing -> map AnUri us
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Just ty -> map (ASymbol . RDFEntity ty) us
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancestatSymbMapItems :: [SymbMapItems] -> Result (Map.Map RawSymb RawSymb)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel MancestatSymbMapItems =
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance foldM (\ m (s, t) -> case Map.lookup s m of
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance Nothing -> return $ Map.insert s t m
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Just u -> case (u, t) of
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Mance (AnUri su, ASymbol (RDFEntity _ tu)) | su == tu ->
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Mance return $ Map.insert s t m
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Mance (ASymbol (RDFEntity _ su), AnUri tu) | su == tu -> return m
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance _ -> if u == t then return m else
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance fail $ "differently mapped symbol: " ++ showDoc s "\nmapped to "
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance ++ showDoc u " and " ++ showDoc t "")
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Map.empty
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance . concatMap (\ (SymbMapItems m us) ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let ps = map (\ (u, v) -> (u, fromMaybe u v)) us in
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance case m of
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Nothing -> map (\ (s, t) -> (AnUri s, AnUri t)) ps
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance Just ty ->
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let mS = ASymbol . RDFEntity ty
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance in map (\ (s, t) -> (mS s, mS t)) ps)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancemapSen :: RDFMorphism -> Axiom -> Result Axiom
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancemapSen m a = return $ function Rename (MorphMap $ mmaps m) a
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance