Morphism.hs revision 38733ccc23cd5e522a1ed721eed36139e402d49c
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaederModule : $Header$
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederDescription : OWL Morphisms
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederCopyright : (c) Dominik Luecke, 2008
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian MaederLicense : GPLv2 or higher, see LICENSE.txt
ffd01020a4f35f434b912844ad6e0d6918fadffdChristian MaederMaintainer : luecke@informatik.uni-bremen.de
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederStability : provisional
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederPortability : portable
fb69cd512eab767747f109e40322df7cae2f7bdfChristian MaederMorphisms for OWL
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder ( OWLMorphism (..)
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder , isOWLInclusion
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , inclOWLMorphism
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , cogeneratedSign
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder , generatedSign
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder , statSymbItems
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder , statSymbMapItems
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , inducedFromMor
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederimport Common.Lib.State (execState)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maederimport Common.Lib.Rel (setToMap)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederimport qualified Data.Map as Map
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maederimport qualified Data.Set as Set
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maederdata OWLMorphism = OWLMorphism
8c81b727b788d90ff3b8cbda7b0900c9009243bbChristian Maeder { osource :: Sign
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , otarget :: Sign
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , mmaps :: Map.Map Entity IRI
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder } deriving (Show, Eq, Ord)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinclOWLMorphism :: Sign -> Sign -> OWLMorphism
07b1bf56f3a486f26d69514d05b73100abb25a0eChristian MaederinclOWLMorphism s t = OWLMorphism
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maeder { osource = s
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , otarget = t
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisOWLInclusion :: OWLMorphism -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisOWLInclusion m = Map.null (mmaps m) && isSubSign (osource m) (otarget m)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersymMap :: Map.Map Entity IRI -> Map.Map Entity Entity
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedersymMap = Map.mapWithKey (\ (Entity ty _) -> Entity ty)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedElems :: Map.Map Entity IRI -> [Entity]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedElems = Map.elems . symMap
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedSign :: Map.Map Entity IRI -> Sign -> Sign
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedSign m = execState $ do
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder mapM_ (modEntity Set.insert) $ inducedElems m
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedFromMor :: Map.Map RawSymb RawSymb -> Sign -> Result OWLMorphism
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinducedFromMor rm sig = do
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let syms = symOf sig
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder mm <- foldM (\ m p -> case p of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (ASymbol s@(Entity _ v), ASymbol (Entity _ u)) ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder then return $ if u == v then m else Map.insert s u m
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else fail $ "unknown symbol: " ++ showDoc s "\n" ++ shows sig ""
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder (AnUri v, AnUri u) -> case filter (`Set.member` syms)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder $ map (`Entity` v) entityTypes of
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder [] -> fail $ "unknown symbol: " ++ showDoc v "\n" ++ shows sig ""
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder l -> return $ if u == v then m else foldr (`Map.insert` u) m l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> error "OWL2.Morphism.inducedFromMor") Map.empty $ Map.toList rm
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder return OWLMorphism
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { osource = sig
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , otarget = inducedSign mm sig
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , mmaps = mm }
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersymMapOf :: OWLMorphism -> Map.Map Entity Entity
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersymMapOf mor = Map.union (symMap $ mmaps mor) $ setToMap $ symOf $ osource mor
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Pretty OWLMorphism where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder pretty m = let
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder s = osource m
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder srcD = specBraces $ space <> pretty s
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder t = otarget m
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in if isOWLInclusion m then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if isSubSign t s then
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder fsep [text "identity morphism over", srcD]
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder [ text "inclusion morphism of"
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , text "extended with"
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , pretty $ Set.difference (symOf t) $ symOf s ]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder [ pretty $ mmaps m
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , colon <+> srcD, mapsto <+> specBraces (space <> pretty t) ]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederlegalMor :: OWLMorphism -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederlegalMor m = let mm = mmaps m in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Set.isSubsetOf (Map.keysSet mm) (symOf $ osource m)
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder && Set.isSubsetOf (Set.fromList $ inducedElems mm) (symOf $ otarget m)
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaedergetIri :: EntityType -> IRI -> Map.Map Entity IRI -> IRI
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaedergetIri ty u = fromMaybe u . Map.lookup (Entity ty u)
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedercomposeMor :: OWLMorphism -> OWLMorphism -> Result OWLMorphism
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaedercomposeMor m1 m2 =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let nm = Set.fold (\ s@(Entity ty u) -> let
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder t = getIri ty u $ mmaps m1
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder r = getIri ty t $ mmaps m2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in if r == u then id else Map.insert s r) Map.empty
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder . symOf $ osource m1
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder { otarget = otarget m2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , mmaps = nm }
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedercogeneratedSign :: Set.Set Entity -> Sign -> Result OWLMorphism
61091743da1a9ed6dfd5e077fdcc972553358962Christian MaedercogeneratedSign s sign =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let sig2 = execState (mapM_ (modEntity Set.delete) $ Set.toList s) sign
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in if isSubSign sig2 sign then return $ inclOWLMorphism sig2 sign else
08b724e8dcbba5820d80f0974b9a5385140815baChristian Maeder fail "non OWL2 subsignatures for (co)generatedSign"
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian MaedergeneratedSign :: Set.Set Entity -> Sign -> Result OWLMorphism
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedergeneratedSign s sign = cogeneratedSign (Set.difference (symOf sign) s) sign
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian MaedermatchesSym :: Entity -> RawSymb -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermatchesSym e@(Entity _ u) r = case r of
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder ASymbol s -> s == e
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder AnUri s -> s == u
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstatSymbItems :: [SymbItems] -> [RawSymb]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstatSymbItems = concatMap
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder $ \ (SymbItems m us) -> case m of
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder Nothing -> map AnUri us
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Just ty -> map (ASymbol . Entity ty) us
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstatSymbMapItems :: [SymbMapItems] -> Result (Map.Map RawSymb RawSymb)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstatSymbMapItems =
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder foldM (\ m (s, t) -> case Map.lookup s m of
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder Nothing -> return $ Map.insert s t m
405b95208385572f491e1e0207d8d14e31022fa6Christian Maeder Just u -> case (u, t) of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (AnUri su, ASymbol (Entity _ tu)) | su == tu ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (ASymbol (Entity _ su), AnUri tu) | su == tu ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> if u == t then return m else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder fail $ "differently mapped symbol: " ++ showDoc s "\nmapped to "
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ++ showDoc u " and " ++ showDoc t "")
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder . concatMap (\ (SymbMapItems m us) ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let ps = map (\ (u, v) -> (u, fromMaybe u v)) us in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Nothing -> map (\ (s, t) -> (AnUri s, AnUri t)) ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Just ty -> let mS = ASymbol . Entity ty in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (\ (s, t) -> (mS s, mS t)) ps)
05e2a3161e4589a717c6fe5c7306820273a473c5Christian MaedermapAnno :: Map.Map Entity IRI -> Annotation -> Annotation
76647324ed70f33b95a881b536d883daccf9568dChristian MaedermapAnno m ann = case ann of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Annotation l a e -> Annotation l (getIri AnnotationProperty a m) e
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapSen :: OWLMorphism -> Axiom -> Result Axiom
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapSen m = return . mapAxiom (mmaps m)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapAxiom :: Map.Map Entity IRI -> Axiom -> Axiom
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian MaedermapAxiom m axm = case axm of
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian Maeder PlainAxiom as a -> PlainAxiom (map (mapAnno m) as) $ mapPlainAxiom m a
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapObjExpr :: Map.Map Entity IRI -> ObjectPropertyExpression
mapDRange :: Map.Map Entity IRI -> DataRange -> DataRange
mapDataExpr :: Map.Map Entity IRI -> DataPropertyExpression
getClassIri :: IRI -> Map.Map Entity IRI -> IRI
getIndIri :: IRI -> Map.Map Entity IRI -> IRI
mapDescr :: Map.Map Entity IRI -> ClassExpression -> ClassExpression
mapSubObjExpr :: Map.Map Entity IRI -> SubObjectPropertyExpression
mapDataDomOrRange :: Map.Map Entity IRI -> DataDomainOrRange
mapAssertion :: Map.Map Entity IRI -> (a -> b) -> (c -> d)
mapPlainAxiom :: Map.Map Entity IRI -> PlainAxiom -> PlainAxiom