Rename.hs revision 31e8c991a19170655c5aeaad33e3c684459535b1
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance{- |
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel ManceModule : $Header$
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel ManceCopyright : (c) Felix Gabriel Mance
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel ManceLicense : GPLv2 or higher, see LICENSE.txt
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel ManceMaintainer : f.mance@jacobs-university.de
c0949afe66b701d624196bea79094dbfbeefa4abFelix Gabriel ManceStability : provisional
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel MancePortability : portable
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel ManceRenames prefixes in OntologyDocuments, so that there are
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceno prefix clashes
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance-}
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancemodule OWL2.Rename where
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport OWL2.AS
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport OWL2.MS
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport OWL2.Sign
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Manceimport OWL2.Function
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport Data.Maybe
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport Data.Char (isDigit)
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Manceimport Data.List (find, nub)
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Manceimport qualified Data.Map as Map
9a46e7194fce1b73383479ba16f81e21c292722cmcodescuimport qualified Data.Set as Set
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport Common.Result
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancetestAndInteg :: (String, String)
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance -> (PrefixMap, StringMap) -> (PrefixMap, StringMap)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancetestAndInteg (pre, oiri) (old, tm) = case Map.lookup pre old of
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance Just iri ->
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if oiri == iri then (old, tm)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance else let pre' = disambiguateName pre old
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance in (Map.insert pre' oiri old, Map.insert pre pre' tm)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance Nothing -> (Map.insert pre oiri old, tm)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancedisambiguateName :: String -> PrefixMap -> String
bed4f1f668ac5d014595c17199fa786aacda4e4cChristian MaederdisambiguateName n nameMap =
bed4f1f668ac5d014595c17199fa786aacda4e4cChristian Maeder let nm = if null n then "n" else n -- change other empty prefixes to "n..."
bed4f1f668ac5d014595c17199fa786aacda4e4cChristian Maeder newname = reverse . dropWhile isDigit $ reverse nm
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance in fromJust $ find (not . flip Map.member nameMap)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance [newname ++ show (i :: Int) | i <- [1 ..]]
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceuniteSign :: Sign -> Sign -> Result Sign
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceuniteSign s1 s2 = do
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance let (pm, tm) = integPref (prefixMap s1) (prefixMap s2)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance if Map.null tm then return (addSign s1 s2) {prefixMap = pm}
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance else fail "Static analysis could not unite signatures"
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
9a46e7194fce1b73383479ba16f81e21c292722cmcodescuintersectSign :: Sign -> Sign -> Result Sign
9a46e7194fce1b73383479ba16f81e21c292722cmcodescuintersectSign s1 s2 = do
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu let (pm, tm) = integPref (prefixMap s1) $ prefixMap s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu if Map.null tm then
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu return emptySign{
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu concepts = Set.intersection (concepts s1) $ concepts s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu , datatypes = Set.intersection (datatypes s1) $ datatypes s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu , objectProperties = Set.intersection (objectProperties s1) $ objectProperties s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu , dataProperties = Set.intersection (dataProperties s1) $ dataProperties s2
31e8c991a19170655c5aeaad33e3c684459535b1mcodescu , annotationRoles = Set.intersection (annotationRoles s1) $ annotationRoles s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu , individuals = Set.intersection (individuals s1) $ individuals s2
31e8c991a19170655c5aeaad33e3c684459535b1mcodescu , labelMap = Map.intersection (labelMap s1) $ labelMap s2
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu , prefixMap = pm
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu }
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu else fail "Static analysis could not intersect signatures"
9a46e7194fce1b73383479ba16f81e21c292722cmcodescu
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceintegPref :: PrefixMap -> PrefixMap
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance -> (PrefixMap, StringMap)
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceintegPref oldMap testMap =
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance foldr testAndInteg (oldMap, Map.empty) (Map.toList testMap)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel MancenewOid :: OntologyIRI -> OntologyIRI -> OntologyIRI
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel MancenewOid id1 id2 =
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance let lid1 = localPart id1
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance lid2 = localPart id2
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance in if null lid1 then id2
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance else if null lid2 || id1 == id2 then id1
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance else id1 { localPart = uriToName lid1 ++ "_" ++ uriToName lid2 }
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel MancecombineDoc :: OntologyDocument -> OntologyDocument
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance -> OntologyDocument
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel MancecombineDoc od1@( OntologyDocument ns1
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance ( Ontology oid1 imp1 anno1 frames1))
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance od2@( OntologyDocument ns2
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance ( Ontology oid2 imp2 anno2 frames2)) =
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance if od1 == od2 then od1
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance else
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance let (newPref, tm) = integPref ns1 ns2
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance in OntologyDocument newPref
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance (Ontology (newOid oid1 oid2) (nub $ imp1 ++ map
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance (function Rename $ StringMap tm) imp2)
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance (nub $ anno1 ++ map (function Rename $ StringMap tm) anno2)
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance (nub $ frames1 ++ map (function Rename $ StringMap tm) frames2))
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceuriToName :: String -> String
137edd3944aacd150d60af8977de962113ead859Felix Gabriel ManceuriToName str = let
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance str' = case str of
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance '"' : _ -> read str
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance _ -> str
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance in takeWhile (/= '.') $ reverse $ case takeWhile (/= '/') $ reverse str' of
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance '#' : r -> r
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance r -> r
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceunifyWith1 :: OntologyDocument -> [OntologyDocument] -> [OntologyDocument]
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceunifyWith1 d odl = case odl of
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance [] -> []
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance [doc] -> [snd $ unifyTwo d doc]
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance doc1 : docs ->
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance let (merged, newDoc1) = unifyTwo d doc1
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance in newDoc1 : unifyWith1 merged docs
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance
41ddc1ba03dadd983308781ae8fd638c3faab453Felix Gabriel Mance{- | takes 2 docs and returns as snd the corrected first one
41ddc1ba03dadd983308781ae8fd638c3faab453Felix Gabriel Mance and as fst the merge of the two -}
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel ManceunifyTwo :: OntologyDocument -> OntologyDocument ->
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance (OntologyDocument, OntologyDocument)
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceunifyTwo od1 od2 =
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance let (_, tm) = integPref (prefixDeclaration od1) (prefixDeclaration od2)
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance newod2 = function Rename (StringMap tm) od2
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel Mance alld = combineDoc od1 od2
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance in (alld, newod2)
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceunifyDocs :: [OntologyDocument] -> [OntologyDocument]
81ec673ac5ab1493568d9ef7798b752ab8ee0e61Felix Gabriel ManceunifyDocs = unifyWith1 emptyOntologyDoc