AS.hs revision d850dba73b02f345f64a3546d0f0299c292f88d6
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder{- |
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederModule : $Header$
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederCopyright : (c) C. Maeder, Felix Gabriel Mance
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederLicense : GPLv2 or higher, see LICENSE.txt
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder
b4fbc96e05117839ca409f5f20f97b3ac872d1edTill MossakowskiMaintainer : Christian.Maeder@dfki.de
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederStability : provisional
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederPortability : portable
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederOWL 2 Functional Syntax constructs
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaederReferences:
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder <http://www.w3.org/TR/2009/REC-owl2-syntax-20091027/#Functional-Style_Syntax>
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder <http://www.w3.org/TR/owl2-manchester-syntax/>
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder-}
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedermodule OWL2.AS where
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport Common.Id
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport OWL2.ColonKeywords
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport OWL2.Keywords
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maeder
04dada28736b4a237745e92063d8bdd49a362debChristian Maederimport Data.Char (intToDigit)
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maederimport Data.List
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maederimport Data.Maybe
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport qualified Data.Map as Map
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maederdata IRIType = Full | Abbreviated | NodeID
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder deriving (Show, Eq, Ord)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder{- | full or abbreviated IRIs with a possible uri for the prefix
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder or a local part following a hash sign -}
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maederdata QName = QN
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder { namePrefix :: String
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder -- ^ the name prefix part of a qualified name \"namePrefix:localPart\"
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder , localPart :: String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder -- ^ the local part of a qualified name \"namePrefix:localPart\"
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder , iriType :: IRIType
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder , expandedIRI :: String
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder -- ^ the associated namespace uri (not printed)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder , iriPos :: Range
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maeder } deriving Show
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maederinstance Eq QName where
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder p == q = compare p q == EQ
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederinstance Ord QName where
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder compare (QN p1 l1 b1 n1 _) (QN p2 l2 b2 n2 _) =
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder if null n1 || null n2 then compare (b1, p1, l1) (b2, p2, l2) else
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder compare n1 n2 -- compare fully expanded names only
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederinstance GetRange QName where
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder getRange = iriPos
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowQN :: QName -> String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowQN q = (if iriType q /= Abbreviated then showQI else showQU) q
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder-- | show QName as abbreviated iri
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaedershowQU :: QName -> String
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaedershowQU (QN pre local _ _ _) =
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder if null pre then local else pre ++ ":" ++ local
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder-- | show QName in ankle brackets as full iris
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowQI :: QName -> String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowQI = ('<' :) . (++ ">") . showQU
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaedernullQName :: QName
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedernullQName = QN "" "" Abbreviated "" nullRange
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaederisNullQName :: QName -> Bool
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisNullQName qn = case qn of
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder QN "" "" _ "" _ -> True
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder _ -> False
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian MaederdummyQName :: QName
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederdummyQName =
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder QN "http" "//www.dfki.de/sks/hets/ontology/unamed" Full "" nullRange
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian MaedermkQName :: String -> QName
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaedermkQName s = nullQName { localPart = s }
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedersetQRange :: Range -> QName -> QName
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedersetQRange r q = q { iriPos = r }
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedersetPrefix :: String -> QName -> QName
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedersetPrefix s q = q { namePrefix = s }
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedersetFull :: QName -> QName
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedersetFull q = q {iriType = Full}
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype IRI = QName
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder-- | checks if an IRI is an anonymous individual
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisAnonymous :: IRI -> Bool
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisAnonymous iri = iriType iri == NodeID
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder-- | checks if a string (bound to be localPart of an IRI) contains \":\/\/\"
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedercssIRI :: String -> IRIType
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedercssIRI iri = if isInfixOf "://" iri then Full else Abbreviated
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder-- | prefix -> localname
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maedertype PrefixMap = Map.Map String String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederpredefPrefixes :: PrefixMap
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaederpredefPrefixes = Map.fromList
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maeder [ ("owl", "http://www.w3.org/2002/07/owl#")
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder , ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder , ("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder , ("xsd", "http://www.w3.org/2001/XMLSchema#")
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder , ("", showQU dummyQName ++ "#") ]
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype LexicalForm = String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype LanguageTag = String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype ImportIRI = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype OntologyIRI = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype Class = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype Datatype = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype ObjectProperty = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype DataProperty = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype AnnotationProperty = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype NamedIndividual = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype Individual = IRI
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
04dada28736b4a237745e92063d8bdd49a362debChristian Maederdata EquivOrDisjoint = Equivalent | Disjoint
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder deriving (Show, Eq, Ord)
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowEquivOrDisjoint :: EquivOrDisjoint -> String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowEquivOrDisjoint ed = case ed of
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder Equivalent -> equivalentToC
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder Disjoint -> disjointWithC
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maederdata DomainOrRange = ADomain | ARange
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder deriving (Show, Eq, Ord)
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian MaedershowDomainOrRange :: DomainOrRange -> String
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian MaedershowDomainOrRange dr = case dr of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder ADomain -> domainC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder ARange -> rangeC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maederdata SameOrDifferent = Same | Different
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder deriving (Show, Eq, Ord)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedershowSameOrDifferent :: SameOrDifferent -> String
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedershowSameOrDifferent sd = case sd of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder Same -> sameAsC
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder Different -> differentFromC
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata Relation =
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder EDRelation EquivOrDisjoint
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder | SubPropertyOf
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder | InverseOf
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | SubClass
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Types
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder | DRRelation DomainOrRange
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder | SDRelation SameOrDifferent
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder deriving (Show, Eq, Ord)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowRelation :: Relation -> String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowRelation r = case r of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder EDRelation ed -> showEquivOrDisjoint ed
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SubPropertyOf -> subPropertyOfC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder InverseOf -> inverseOfC
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder SubClass -> subClassOfC
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder Types -> typesC
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder DRRelation dr -> showDomainOrRange dr
e76e6a43f51438215737d6fc176c89da05bb86daChristian Maeder SDRelation sd -> showSameOrDifferent sd
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedergetED :: Relation -> EquivOrDisjoint
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedergetED r = case r of
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder EDRelation ed -> ed
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder _ -> error "not domain or range"
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedergetDR :: Relation -> DomainOrRange
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedergetDR r = case r of
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder DRRelation dr -> dr
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder _ -> error "not domain or range"
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaedergetSD :: Relation -> SameOrDifferent
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaedergetSD s = case s of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SDRelation sd -> sd
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder _ -> error "not same or different"
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maederdata Character =
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder Functional
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder | InverseFunctional
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Reflexive
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Irreflexive
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder | Symmetric
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder | Asymmetric
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder | Antisymmetric
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Transitive
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder deriving (Enum, Bounded, Show, Eq, Ord)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata PositiveOrNegative = Positive | Negative
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder deriving (Show, Eq, Ord)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata QuantifierType = AllValuesFrom | SomeValuesFrom
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder deriving (Show, Eq, Ord)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowQuantifierType :: QuantifierType -> String
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaedershowQuantifierType ty = case ty of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder AllValuesFrom -> onlyS
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder SomeValuesFrom -> someS
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder-- * Predefined IRI checkings
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder
15bb922b665fcd44c6230a1202785d0c7890e90cChristian MaederisThing :: IRI -> Bool
15bb922b665fcd44c6230a1202785d0c7890e90cChristian MaederisThing = isOWLPredef predefClass
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maeder
isPredefObjProp :: IRI -> Bool
isPredefObjProp = isOWLPredef predefObjProp
isPredefDataProp :: IRI -> Bool
isPredefDataProp = isOWLPredef predefDataProp
isPredefRDFSAnnoProp :: IRI -> Bool
isPredefRDFSAnnoProp iri = checkPredef predefRDFSAnnoProps "rdfs" iri
isPredefOWLAnnoProp :: IRI -> Bool
isPredefOWLAnnoProp iri = isOWLPredef predefOWLAnnoProps iri
isPredefAnnoProp :: IRI -> Bool
isPredefAnnoProp iri = isPredefOWLAnnoProp iri || isPredefRDFSAnnoProp iri
isPredefPropOrClass :: IRI -> Bool
isPredefPropOrClass iri = isPredefAnnoProp iri || isPredefDataProp iri
|| isPredefObjProp iri || isThing iri
predefIRIs :: [IRI]
predefIRIs = map (setPrefix "xsd" . mkQName) xsdKeys
++ map (setPrefix "owl" . mkQName) owlNumbers
++ [setPrefix "rdf" (mkQName rdfsLiteral),
setPrefix "rdfs" $ mkQName xmlLiteral]
isDatatypeKey :: IRI -> Bool
isDatatypeKey iri = any (\ (l, p) -> checkPredef l p iri) [(xsdKeys, "xsd"),
(owlNumbers, "owl"), ([xmlLiteral], "rdf"), ([rdfsLiteral], "rdfs")]
checkPredef :: [String] -> String -> IRI -> Bool
checkPredef sl pref u =
localPart u `elem` sl && elem (namePrefix u) ["", pref]
|| showQU u `elem` map (Map.findWithDefault
(error $ "not predefined prefix: " ++ show pref)
pref (predefPrefixes `Map.difference`
Map.fromList [("", showQU dummyQName ++ "#")]) ++) sl
isOWLPredef :: [String] -> IRI -> Bool
isOWLPredef sl = checkPredef sl "owl"
-- | sets the correct prefix for the predefined datatypes
setDatatypePrefix :: IRI -> IRI
setDatatypePrefix iri = let lp = localPart iri in
if lp `elem` xsdKeys
then setPrefix "xsd" iri
else if lp `elem` owlNumbers
then setPrefix "owl" iri
else case lp of
"XMLLiteral" -> setPrefix "rdf" iri
"Literal" -> setPrefix "rdfs" iri
_ -> error $ showQU iri ++ " is not a predefined datatype"
-- | checks if the IRI is part of the built-in ones and puts the correct prefix
setReservedPrefix :: IRI -> IRI
setReservedPrefix iri
| isDatatypeKey iri && null (namePrefix iri) = setDatatypePrefix iri
| (isThing iri || isPredefDataProp iri || isPredefOWLAnnoProp iri
|| isPredefObjProp iri) && null (namePrefix iri) = setPrefix "owl" iri
| isPredefRDFSAnnoProp iri = setPrefix "rdfs" iri
| otherwise = iri
stripReservedPrefix :: IRI -> IRI
stripReservedPrefix = mkQName . getPredefName
{- | returns the name of the predefined IRI (e.g <xsd:string> returns "string"
or <http://www.w3.org/2002/07/owl#real> returns "real") -}
getPredefName :: IRI -> String
getPredefName iri =
if namePrefix iri `elem` ["", "xsd", "rdf", "rdfs", "owl"]
then localPart iri
else case mapMaybe (flip stripPrefix $ showQU iri)
$ Map.elems predefPrefixes of
[s] -> s
_ -> error $ showQU iri ++ " is not a predefined IRI"
data DatatypeCat = OWL2Number | OWL2String | OWL2Bool | Other
deriving (Show, Eq, Ord)
getDatatypeCat :: IRI -> DatatypeCat
getDatatypeCat iri = case isDatatypeKey iri of
True
| hasPrefXSD [booleanS] iri -> OWL2Bool
| hasPrefXSD xsdNumbers iri || checkPredef owlNumbers "owl" iri
-> OWL2Number
| hasPrefXSD xsdStrings iri -> OWL2String
| otherwise -> Other
False -> Other
hasPrefXSD :: [String] -> IRI -> Bool
hasPrefXSD sl = checkPredef sl "xsd"
facetToIRI :: DatatypeFacet -> ConstrainingFacet
facetToIRI = setPrefix "xsd" . mkQName . showFacet
-- * Cardinalities
data CardinalityType = MinCardinality | MaxCardinality | ExactCardinality
deriving (Show, Eq, Ord)
showCardinalityType :: CardinalityType -> String
showCardinalityType ty = case ty of
MinCardinality -> minS
MaxCardinality -> maxS
ExactCardinality -> exactlyS
data Cardinality a b = Cardinality CardinalityType Int a (Maybe b)
deriving (Show, Eq, Ord)
data JunctionType = UnionOf | IntersectionOf
deriving (Show, Eq, Ord)
type ConstrainingFacet = IRI
type RestrictionValue = Literal
-- * ENTITIES
data Entity = Entity EntityType IRI deriving (Show, Eq, Ord)
instance GetRange Entity where
getRange (Entity _ iri) = iriPos iri
data EntityType =
Datatype
| Class
| ObjectProperty
| DataProperty
| AnnotationProperty
| NamedIndividual
deriving (Enum, Bounded, Show, Read, Eq, Ord)
showEntityType :: EntityType -> String
showEntityType e = case e of
Datatype -> datatypeC
Class -> classC
ObjectProperty -> objectPropertyC
DataProperty -> dataPropertyC
AnnotationProperty -> annotationPropertyC
NamedIndividual -> individualC
entityTypes :: [EntityType]
entityTypes = [minBound .. maxBound]
cutIRI :: Entity -> IRI
cutIRI (Entity _ iri) = iri
-- * LITERALS
data TypedOrUntyped = Typed Datatype | Untyped (Maybe LanguageTag)
deriving (Show, Eq, Ord)
data Literal = Literal LexicalForm TypedOrUntyped | NumberLit FloatLit
deriving (Show, Eq, Ord)
-- | non-negative integers given by the sequence of digits
data NNInt = NNInt [Int] deriving (Eq, Ord)
instance Show NNInt where
show (NNInt l) = map intToDigit l
zeroNNInt :: NNInt
zeroNNInt = NNInt []
isZeroNNInt :: NNInt -> Bool
isZeroNNInt (NNInt l) = null l
data IntLit = IntLit
{ absInt :: NNInt
, isNegInt :: Bool }
deriving (Eq, Ord)
instance Show IntLit where
show (IntLit n b) = (if b then ('-' :) else id) $ show n
zeroInt :: IntLit
zeroInt = IntLit zeroNNInt False
isZeroInt :: IntLit -> Bool
isZeroInt (IntLit n _) = isZeroNNInt n
negNNInt :: Bool -> NNInt -> IntLit
negNNInt b n = IntLit n b
negInt :: IntLit -> IntLit
negInt (IntLit n b) = IntLit n $ not b
data DecLit = DecLit
{ truncDec :: IntLit
, fracDec :: NNInt }
deriving (Eq, Ord)
instance Show DecLit where
show (DecLit t f) = show t
++ if isZeroNNInt f then "" else
'.' : show f
isDecInt :: DecLit -> Bool
isDecInt = isZeroNNInt . fracDec
negDec :: Bool -> DecLit -> DecLit
negDec b (DecLit t f) = DecLit (if b then negInt t else t) f
data FloatLit = FloatLit
{ floatBase :: DecLit
, floatExp :: IntLit }
deriving (Eq, Ord)
instance Show FloatLit where
show (FloatLit b e) = show b
++ if isZeroInt e then "" else
'E' : show e ++ "F"
isFloatDec :: FloatLit -> Bool
isFloatDec = isZeroInt . floatExp
isFloatInt :: FloatLit -> Bool
isFloatInt f = isFloatDec f && isDecInt (floatBase f)
floatToInt :: FloatLit -> IntLit
floatToInt = truncDec . floatBase
intToDec :: IntLit -> DecLit
intToDec i = DecLit i zeroNNInt
decToFloat :: DecLit -> FloatLit
decToFloat d = FloatLit d zeroInt
intToFloat :: IntLit -> FloatLit
intToFloat = decToFloat . intToDec
abInt :: IntLit -> IntLit
abInt int = int {isNegInt = False}
abDec :: DecLit -> DecLit
abDec dec = dec {truncDec = abInt $ truncDec dec}
abFloat :: FloatLit -> FloatLit
abFloat f = f {floatBase = abDec $ floatBase f}
isNegDec :: DecLit -> Bool
isNegDec d = isNegInt $ truncDec d
numberName :: FloatLit -> String
numberName f
| isFloatInt f = integerS
| isFloatDec f = decimalS
| otherwise = floatS
cTypeS :: String
cTypeS = "^^"
-- * PROPERTY EXPRESSIONS
type InverseObjectProperty = ObjectPropertyExpression
data ObjectPropertyExpression = ObjectProp ObjectProperty
| ObjectInverseOf InverseObjectProperty
deriving (Show, Eq, Ord)
type DataPropertyExpression = DataProperty
-- * DATA RANGES
data DataRange =
DataType Datatype [(ConstrainingFacet, RestrictionValue)]
| DataJunction JunctionType [DataRange]
| DataComplementOf DataRange
| DataOneOf [Literal]
deriving (Show, Eq, Ord)
-- * CLASS EXPERSSIONS
data ClassExpression =
Expression Class
| ObjectJunction JunctionType [ClassExpression]
| ObjectComplementOf ClassExpression
| ObjectOneOf [Individual]
| ObjectValuesFrom QuantifierType ObjectPropertyExpression ClassExpression
| ObjectHasValue ObjectPropertyExpression Individual
| ObjectHasSelf ObjectPropertyExpression
| ObjectCardinality (Cardinality ObjectPropertyExpression ClassExpression)
| DataValuesFrom QuantifierType DataPropertyExpression DataRange
| DataHasValue DataPropertyExpression Literal
| DataCardinality (Cardinality DataPropertyExpression DataRange)
deriving (Show, Eq, Ord)
-- * ANNOTATIONS
data Annotation = Annotation [Annotation] AnnotationProperty AnnotationValue
deriving (Show, Eq, Ord)
data AnnotationValue =
AnnValue IRI
| AnnValLit Literal
deriving (Show, Eq, Ord)