Morphism.hs revision be00381168b3f10192afabbba136fb06d3a9f358
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederModule : $Header$
e47d29b522739fbf08aac80c6faa447dde113fbcChristian MaederDescription : OWL Morphisms
11d6ec73ee5550e00cb56b221bdbeb709142e779Christian MaederCopyright : (c) Dominik Luecke, 2008
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederLicense : GPLv2 or higher, see LICENSE.txt
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : luecke@informatik.uni-bremen.de
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederStability : provisional
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian MaederPortability : portable
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederMorphisms for OWL
ac142c1b088711f911018d8108a64be80b2f2a58Christian Maeder ( OWLMorphism (..)
ac142c1b088711f911018d8108a64be80b2f2a58Christian Maeder , isOWLInclusion
ac142c1b088711f911018d8108a64be80b2f2a58Christian Maeder , inclOWLMorphism
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian Maeder , cogeneratedSign
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder , generatedSign
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder , statSymbItems
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian Maeder , statSymbMapItems
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder , inducedFromMor
1eb10c0c30323eed3cc21082fd242cd09a612dc5Christian Maederimport Common.Utils (composeMap)
975642b989852fc24119c59cf40bc1af653608ffChristian Maederimport Common.Lib.State (execState)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maederimport qualified Data.Map as Map
975642b989852fc24119c59cf40bc1af653608ffChristian Maederimport qualified Data.Set as Set
836e72a3c413366ba9801726f3b249c7791cb9caChristian Maederdata OWLMorphism = OWLMorphism
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder { osource :: Sign
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder , otarget :: Sign
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder , mmaps :: Map.Map Entity IRI
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder , pmap :: TranslationMap
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder } deriving (Show, Eq, Ord)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaederinclOWLMorphism :: Sign -> Sign -> OWLMorphism
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaederinclOWLMorphism s t = OWLMorphism
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder { osource = s
0216a1580abf46ed8981f25e89d6fd99b2944ac2Christian Maeder , otarget = t
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaederisOWLInclusion :: OWLMorphism -> Bool
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaederisOWLInclusion m = Map.null (pmap m)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder && Map.null (mmaps m) && isSubSign (osource m) (otarget m)
fa45d098e1c9d468f128be9505eb7e5b2705b304Christian MaedersymMap :: Map.Map Entity IRI -> Map.Map Entity Entity
25612a7b3ce708909298d5426406592473880a20Christian MaedersymMap = Map.mapWithKey (\ (Entity ty _) -> Entity ty)
35cd0c10843c2cdbbe29f00a2a5d7e5e4f2d0064Christian MaederinducedElems :: Map.Map Entity IRI -> [Entity]
c1db3d36c29a6324745a86dbcba18b8e4cd9f338Christian MaederinducedElems = Map.elems . symMap
36c6cc568751e4235502cfee00ba7b597dae78dcChristian MaederinducedSign :: Map.Map Entity IRI -> TranslationMap -> Sign -> Sign
6e2c88c65d50b2e44f7afa165e6a5fac0724f08cChristian MaederinducedSign m t s =
1eb10c0c30323eed3cc21082fd242cd09a612dc5Christian Maeder let new = execState (do
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder mapM_ (modEntity Set.insert) $ inducedElems m) s
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian MaederinducedPref :: String -> String -> Sign -> (Map.Map Entity IRI, TranslationMap)
18b709ce961d68328da768318dcc70067f066d86Christian Maeder -> (Map.Map Entity IRI, TranslationMap)
ac142c1b088711f911018d8108a64be80b2f2a58Christian MaederinducedPref v u sig (m, t) =
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder let pm = prefixMap sig
0216a1580abf46ed8981f25e89d6fd99b2944ac2Christian Maeder then if u == v then (m, t) else (m, Map.insert v u t)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder else error $ "unknown symbol: " ++ showDoc v "\n" ++ shows sig ""
9c5b1136299d9052e4e995614a3a36a051a2682fChristian MaederinducedFromMor :: Map.Map RawSymb RawSymb -> Sign -> Result OWLMorphism
9c5b1136299d9052e4e995614a3a36a051a2682fChristian MaederinducedFromMor rm sig = do
6e2c88c65d50b2e44f7afa165e6a5fac0724f08cChristian Maeder let syms = symOf sig
0216a1580abf46ed8981f25e89d6fd99b2944ac2Christian Maeder (mm, tm) <- foldM (\ (m, t) p -> case p of
797f811e57952d59e73b8cd03b667eef276db972Christian Maeder (ASymbol s@(Entity _ v), ASymbol (Entity _ u)) ->
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder then return $ if u == v then (m, t) else (Map.insert s u m, t)
797f811e57952d59e73b8cd03b667eef276db972Christian Maeder else fail $ "unknown symbol: " ++ showDoc s "\n" ++ shows sig ""
797f811e57952d59e73b8cd03b667eef276db972Christian Maeder (AnUri v, AnUri u) -> case filter (`Set.member` syms)
18b709ce961d68328da768318dcc70067f066d86Christian Maeder $ map (`Entity` v) entityTypes of
42c01284bba8d7c8d995c8dfb96ace57d28ed1bcTill Mossakowski [] -> let v2 = showQU v
369454f9b2dbea113cbb40544a9b0f31425b2c69Christian Maeder u2 = showQU u
b52ad1aed6b1eb8b8416aaf100695f54ea59aea0Christian Maeder in return $ inducedPref v2 u2 sig (m, t)
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder l -> return $ if u == v then (m, t) else
18b709ce961d68328da768318dcc70067f066d86Christian Maeder (foldr (`Map.insert` u) m l, t)
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder (APrefix v, APrefix u) -> return $ inducedPref v u sig (m, t)
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder _ -> error "OWL2.Morphism.inducedFromMor") (Map.empty, Map.empty)
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder return OWLMorphism
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder { osource = sig
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder , otarget = inducedSign mm tm sig
e9490701e16d1e8abd995ef876d6f937da93b412Christian Maeder , mmaps = mm }
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian MaedersymMapOf :: OWLMorphism -> Map.Map Entity Entity
d5c415f6373274fed04d83b9322891f3b82e9c26Christian MaedersymMapOf mor = Map.union (symMap $ mmaps mor) $ setToMap $ symOf $ osource mor
22fc8a1bd14dc53c5c7f482d2e0c04eb5ee4beb4Christian Maederinstance Pretty OWLMorphism where
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder pretty m = let
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder s = osource m
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder srcD = specBraces $ space <> pretty s
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder t = otarget m
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder in if isOWLInclusion m then
6e2c88c65d50b2e44f7afa165e6a5fac0724f08cChristian Maeder if isSubSign t s then
adfdcfa67b7f12df6df7292e238c3f9a4b637980Christian Maeder fsep [text "identity morphism over", srcD]
e9490701e16d1e8abd995ef876d6f937da93b412Christian Maeder [ text "inclusion morphism of"
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder , text "extended with"
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder , pretty $ Set.difference (symOf t) $ symOf s ]
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder [ pretty $ mmaps m
9e0472be46104307b974fe5079bf5cc9e94a1a96Christian Maeder , colon <+> srcD, mapsto <+> specBraces (space <> pretty t) ]
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian MaederlegalMor :: OWLMorphism -> Bool
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian MaederlegalMor m = let mm = mmaps m in
6e2c88c65d50b2e44f7afa165e6a5fac0724f08cChristian Maeder Set.isSubsetOf (Map.keysSet mm) (symOf $ osource m)
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder && Set.isSubsetOf (Set.fromList $ inducedElems mm) (symOf $ otarget m)
11d6ec73ee5550e00cb56b221bdbeb709142e779Christian MaedergetIri :: EntityType -> IRI -> Map.Map Entity IRI -> IRI
9c5b1136299d9052e4e995614a3a36a051a2682fChristian MaedergetIri ty u = fromMaybe u . Map.lookup (Entity ty u)
ac142c1b088711f911018d8108a64be80b2f2a58Christian MaedercomposeMor :: OWLMorphism -> OWLMorphism -> Result OWLMorphism
fcec1ffa4a95dbc47cf23f75e6843ceff93a925eChristian MaedercomposeMor m1 m2 =
fcec1ffa4a95dbc47cf23f75e6843ceff93a925eChristian Maeder let nm = Set.fold (\ s@(Entity ty u) -> let
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder t = getIri ty u $ mmaps m1
b52ad1aed6b1eb8b8416aaf100695f54ea59aea0Christian Maeder r = getIri ty t $ mmaps m2
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder in if r == u then id else Map.insert s r) Map.empty
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder . symOf $ osource m1
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder { otarget = otarget m2
fcec1ffa4a95dbc47cf23f75e6843ceff93a925eChristian Maeder , pmap = composeMap (prefixMap $ osource m1) (pmap m1) $ pmap m2
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder , mmaps = nm }
b52ad1aed6b1eb8b8416aaf100695f54ea59aea0Christian MaedercogeneratedSign :: Set.Set Entity -> Sign -> Result OWLMorphism
65835942d66905c377fa503e0d577df5aade58feChristian MaedercogeneratedSign s sign =
65835942d66905c377fa503e0d577df5aade58feChristian Maeder let sig2 = execState (mapM_ (modEntity Set.delete) $ Set.toList s) sign
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder in if isSubSign sig2 sign then return $ inclOWLMorphism sig2 sign else
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian Maeder fail "non OWL2 subsignatures for (co)generatedSign"
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaedergeneratedSign :: Set.Set Entity -> Sign -> Result OWLMorphism
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaedergeneratedSign s sign = cogeneratedSign (Set.difference (symOf sign) s) sign
b603f34b79bc0992e5d74f484e5bdc9f9c2346c6Christian MaedermatchesSym :: Entity -> RawSymb -> Bool
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaedermatchesSym e@(Entity _ u) r = case r of
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder ASymbol s -> s == e
fcec1ffa4a95dbc47cf23f75e6843ceff93a925eChristian Maeder AnUri s -> s == u || namePrefix u == localPart s && null (namePrefix s)
5581c4644d91dcb9b7e2e7f6052f7cbf5f97b6deChristian Maeder APrefix p -> p == namePrefix u
statSymbMapItems :: [SymbMapItems] -> Result (Map.Map RawSymb RawSymb)
foldM (\ m (s, t) -> case Map.lookup s m of
Nothing -> return $ Map.insert s t m
return $ Map.insert s t m
return $ Map.insert s t m
mapAnno :: Map.Map Entity IRI -> Annotation -> Annotation
mapAnnoList :: Map.Map Entity IRI -> Annotations -> Annotations
mapObjExpr :: 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
mapFact :: Map.Map Entity IRI -> Fact -> Fact
mapAnnList :: Map.Map Entity IRI -> (a -> a) ->
mapLFB :: Map.Map Entity IRI -> ListFrameBit -> ListFrameBit
mapAFB :: Map.Map Entity IRI -> AnnFrameBit -> AnnFrameBit
mapFB :: Map.Map Entity IRI -> FrameBit -> FrameBit
mapAxiom :: Map.Map Entity IRI -> Axiom -> Axiom