AS.hs revision 80875f917d741946a39d0ec0b5721e46ba609823
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder{-# LANGUAGE DeriveDataTypeable #-}
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder{- |
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederModule : ./OWL2/AS.hs
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederCopyright : (c) C. Maeder, Felix Gabriel Mance
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederLicense : GPLv2 or higher, see LICENSE.txt
b4fbc96e05117839ca409f5f20f97b3ac872d1edTill Mossakowski
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederMaintainer : Alexander.Koslowski@st.ovgu.de
c797f343be2f3619bb1f5569753166ec49d27bdbChristian MaederStability : provisional
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederPortability : portable
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian 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/>
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder-}
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedermodule OWL2.AS where
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport Common.Id
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport Common.Keywords (stringS)
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maederimport Common.IRI
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maederimport Common.Result
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederimport OWL2.ColonKeywords
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maederimport OWL2.Keywords
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maederimport Data.Char (intToDigit)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maederimport Data.Data
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maederimport Data.List
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maederimport Data.Maybe
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maederimport qualified Data.Map as Map
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maederimport qualified Data.Set as Set
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder-- | checks if an IRI is an anonymous individual
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisAnonymous :: IRI -> Bool
04dada28736b4a237745e92063d8bdd49a362debChristian MaederisAnonymous i = prefixName i == "_" && isBlankNode i
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian Maeder-- | prefix -> localname
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype PrefixMap = Map.Map String String
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederpredefPrefixes :: PrefixMap
b190f5c7cf3ddda73724efe5ce82b9585ed76be1Christian MaederpredefPrefixes = Map.fromList
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder [ ("owl", "http://www.w3.org/2002/07/owl#")
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder , ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder , ("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder , ("xsd", "http://www.w3.org/2001/XMLSchema#")
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder , ("", showIRICompact dummyIRI ++ "#") ]
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype LexicalForm = String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype LanguageTag = String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype ImportIRI = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype OntologyIRI = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype Class = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype Datatype = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maedertype ObjectProperty = IRI
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maedertype DataProperty = IRI
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maedertype AnnotationProperty = IRI
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maedertype Individual = IRI
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata EquivOrDisjoint = Equivalent | Disjoint
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder deriving (Show, Eq, Ord, Typeable, Data)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowEquivOrDisjoint :: EquivOrDisjoint -> String
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian MaedershowEquivOrDisjoint ed = case ed of
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder Equivalent -> equivalentToC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder Disjoint -> disjointWithC
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maederdata DomainOrRange = ADomain | ARange
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder deriving (Show, Eq, Ord, Typeable, Data)
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowDomainOrRange :: DomainOrRange -> String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowDomainOrRange dr = case dr of
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maeder ADomain -> domainC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder ARange -> rangeC
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maederdata SameOrDifferent = Same | Different
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maeder deriving (Show, Eq, Ord, Typeable, Data)
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowSameOrDifferent :: SameOrDifferent -> String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowSameOrDifferent sd = case sd of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder Same -> sameAsC
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder Different -> differentFromC
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata Relation =
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder EDRelation EquivOrDisjoint
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | SubPropertyOf
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | InverseOf
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | SubClass
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Types
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | DRRelation DomainOrRange
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | SDRelation SameOrDifferent
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder deriving (Show, Eq, Ord, Typeable, Data)
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowRelation :: Relation -> String
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedershowRelation r = case r of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder EDRelation ed -> showEquivOrDisjoint ed
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SubPropertyOf -> subPropertyOfC
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder InverseOf -> inverseOfC
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SubClass -> subClassOfC
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder Types -> typesC
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder DRRelation dr -> showDomainOrRange dr
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SDRelation sd -> showSameOrDifferent sd
462ec4b2fa3e0e788eb60dcb4aebc518298f342cChristian Maeder
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian MaedergetED :: Relation -> EquivOrDisjoint
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedergetED r = case r of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder EDRelation ed -> ed
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder _ -> error "not domain or range"
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedergetDR :: Relation -> DomainOrRange
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedergetDR r = case r of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder DRRelation dr -> dr
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder _ -> error "not domain or range"
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedergetSD :: Relation -> SameOrDifferent
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedergetSD s = case s of
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder SDRelation sd -> sd
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder _ -> error "not same or different"
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maederdata Character =
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder Functional
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder | InverseFunctional
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder | Reflexive
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder | Irreflexive
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder | Symmetric
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder | Asymmetric
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder | Antisymmetric
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder | Transitive
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder deriving (Enum, Bounded, Show, Eq, Ord, Typeable, Data)
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maederdata PositiveOrNegative = Positive | Negative
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder deriving (Show, Eq, Ord, Typeable, Data)
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maederdata QuantifierType = AllValuesFrom | SomeValuesFrom
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder deriving (Show, Eq, Ord, Typeable, Data)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowQuantifierType :: QuantifierType -> String
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowQuantifierType ty = case ty of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder AllValuesFrom -> onlyS
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder SomeValuesFrom -> someS
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder-- * Predefined IRI checkings
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederthingMap :: PreDefMaps
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaederthingMap = makeOWLPredefMaps predefClass
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisThing :: IRI -> Bool
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisThing = checkPredef thingMap
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
15bb922b665fcd44c6230a1202785d0c7890e90cChristian MaedermakePredefObjProp :: PreDefMaps
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaedermakePredefObjProp = makeOWLPredefMaps predefObjProp
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisPredefObjProp :: IRI -> Bool
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisPredefObjProp = checkPredef makePredefObjProp
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedermakePredefDataProp :: PreDefMaps
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedermakePredefDataProp = makeOWLPredefMaps predefDataProp
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisPredefDataProp :: IRI -> Bool
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisPredefDataProp = checkPredef makePredefDataProp
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedermakePredefRDFSAnnoProp :: PreDefMaps
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedermakePredefRDFSAnnoProp = preDefMaps predefRDFSAnnoProps "rdfs"
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
e76e6a43f51438215737d6fc176c89da05bb86daChristian MaederisPredefRDFSAnnoProp :: IRI -> Bool
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederisPredefRDFSAnnoProp = checkPredef makePredefRDFSAnnoProp
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedermakePredefOWLAnnoProp :: PreDefMaps
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaedermakePredefOWLAnnoProp = makeOWLPredefMaps predefOWLAnnoProps
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisPredefOWLAnnoProp :: IRI -> Bool
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaederisPredefOWLAnnoProp = checkPredef makePredefOWLAnnoProp
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaederisPredefAnnoProp :: IRI -> Bool
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederisPredefAnnoProp i = isPredefOWLAnnoProp i || isPredefRDFSAnnoProp i
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian Maeder
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaederisPredefPropOrClass :: IRI -> Bool
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaederisPredefPropOrClass i = isPredefAnnoProp i || isPredefDataProp i
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder || isPredefObjProp i || isThing i
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaederpredefIRIs :: Set.Set IRI
962d5c684e2b86d1f9c556c096b426e10cc74026Christian MaederpredefIRIs = Set.fromList $ map (setPrefix "xsd" . mkIRI) xsdKeys
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder ++ map (setPrefix "owl" . mkIRI) owlNumbers
962d5c684e2b86d1f9c556c096b426e10cc74026Christian Maeder ++ map (setPrefix "rdf" . mkIRI) [rdfsLiteral, stringS]
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder ++ [setPrefix "rdfs" $ mkIRI xmlLiteral]
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
b984ff0ba75221f64451c1e69b3977967d4e99a1Christian MaederisDatatypeKey :: IRI -> Bool
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederisDatatypeKey = not . null . isDatatypeKeyAux
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederxsdMap :: PreDefMaps
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederxsdMap = makeXsdMap xsdKeys
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederowlNumbersMap :: PreDefMaps
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederowlNumbersMap = makeOWLPredefMaps owlNumbers
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederrdfMap :: PreDefMaps
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederrdfMap = preDefMaps [xmlLiteral, stringS] "rdf"
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian Maeder
6cca02cb6a5ae882d887a879f8b7a71941c3715cChristian MaederrdfsMap :: PreDefMaps
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian MaederrdfsMap = preDefMaps [rdfsLiteral] "rdfs"
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisDatatypeKeyAux :: IRI -> [(String, String)]
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaederisDatatypeKeyAux i = mapMaybe (`checkPredefAux` i)
c797f343be2f3619bb1f5569753166ec49d27bdbChristian Maeder [ xsdMap, owlNumbersMap, rdfMap, rdfsMap ]
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maedertype PreDefMaps = ([String], String, String)
15bb922b665fcd44c6230a1202785d0c7890e90cChristian Maeder
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian MaederpreDefMaps :: [String] -> String -> PreDefMaps
preDefMaps sl pref = let
Just puri = Map.lookup pref predefPrefixes
Just sp = stripPrefix "http://www.w3.org/" puri
in (sl, pref, sp)
checkPredefAux :: PreDefMaps -> IRI -> Maybe (String, String)
checkPredefAux (sl, pref, exPref) u =
let lp = show $ iriPath u
res = Just (pref, lp)
in case prefixName u of
"http" -> case stripPrefix "//www." lp of
Just q -> case stripPrefix "w3.org/" q of
Just r -> case stripPrefix exPref r of
Just s | elem s sl -> Just (pref, s)
_ -> Nothing
Nothing -> Nothing
Nothing -> Nothing
pu | elem lp sl -> case pu of
"" -> let ex = iriToStringUnsecure u in
case stripPrefix "http://www." ex of
Just r | r == "w3.org/" ++ exPref ++ lp -- || r == lp
-> res
_ | null ex -> res
_ -> Nothing
_ | pref == pu || pref ++ ":" == pu -> Just (pref, lp)
_ -> Nothing
_ -> Nothing
checkPredef :: PreDefMaps -> IRI -> Bool
checkPredef ms = isJust . checkPredefAux ms
makeOWLPredefMaps :: [String] -> PreDefMaps
makeOWLPredefMaps sl = preDefMaps sl "owl"
-- | sets the correct prefix for the predefined datatypes
setDatatypePrefix :: IRI -> IRI
setDatatypePrefix i = case isDatatypeKeyAux i of
(p, l) : _ -> setPrefix p $ mkIRI l
_ -> error $ showIRICompact i ++ " 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 i = case prefixName i of
""
| isDatatypeKey i -> setDatatypePrefix i
| isThing i || isPredefDataProp i || isPredefOWLAnnoProp i
|| isPredefObjProp i -> setPrefix "owl" i
| isPredefRDFSAnnoProp i -> setPrefix "rdfs" i
_ -> i
stripReservedPrefix :: IRI -> IRI
stripReservedPrefix = idToIRI . uriToId
{- | Extracts Id from IRI
returns the name of the predefined IRI (e.g <xsd:string> returns "string"
or <http://www.w3.org/2002/07/owl#real> returns "real") -}
uriToId :: IRI -> Id
uriToId i =
if (prefixName i `elem` ["", "xsd", "rdf", "rdfs", "owl"])
|| ( null (iriScheme i)
&& null (iriQuery i)
&& null (iriFragment i)
&& isNothing (iriAuthority i))
then iriPath i
else stringToId $ case mapMaybe (`stripPrefix` showIRICompact i)
$ Map.elems predefPrefixes of
[s] -> s
_ -> showIRIFull i
getPredefName :: IRI -> String
getPredefName = show . uriToId
-- | Extracts Token from IRI
uriToTok :: IRI -> Token
uriToTok = mkSimpleId . show . getPredefName
-- | Extracts Id from Entities
entityToId :: Entity -> Id
entityToId = uriToId . cutIRI
printDatatype :: IRI -> String
printDatatype dt = showIRICompact $
if isDatatypeKey dt then stripReservedPrefix dt else dt
data DatatypeCat = OWL2Number | OWL2String | OWL2Bool | Other
deriving (Show, Eq, Ord, Typeable, Data)
getDatatypeCat :: IRI -> DatatypeCat
getDatatypeCat i = case isDatatypeKey i of
True
| checkPredef xsdBooleanMap i -> OWL2Bool
| checkPredef xsdNumbersMap i || checkPredef owlNumbersMap i
-> OWL2Number
| checkPredef xsdStringsMap i -> OWL2String
| otherwise -> Other
False -> Other
makeXsdMap :: [String] -> PreDefMaps
makeXsdMap sl = preDefMaps sl "xsd"
xsdBooleanMap :: PreDefMaps
xsdBooleanMap = makeXsdMap [booleanS]
xsdNumbersMap :: PreDefMaps
xsdNumbersMap = makeXsdMap xsdNumbers
xsdStringsMap :: PreDefMaps
xsdStringsMap = makeXsdMap xsdStrings
facetToIRI :: DatatypeFacet -> ConstrainingFacet
facetToIRI = setPrefix "xsd" . mkIRI . showFacet
-- * Cardinalities
data CardinalityType = MinCardinality | MaxCardinality | ExactCardinality
deriving (Show, Eq, Ord, Typeable, Data)
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, Typeable, Data)
data JunctionType = UnionOf | IntersectionOf
deriving (Show, Eq, Ord, Typeable, Data)
type ConstrainingFacet = IRI
type RestrictionValue = Literal
-- * ENTITIES
data Entity = Entity
{ label :: Maybe String
, entityKind :: EntityType
, cutIRI :: IRI }
deriving (Show, Typeable, Data)
mkEntity :: EntityType -> IRI -> Entity
mkEntity = Entity Nothing
mkEntityLbl :: String -> EntityType -> IRI -> Entity
mkEntityLbl = Entity . Just
instance Ord Entity where
compare (Entity _ ek1 ir1) (Entity _ ek2 ir2) = compare (ek1, ir1) (ek2, ir2)
instance Eq Entity where
e1 == e2 = compare e1 e2 == EQ
instance GetRange Entity where
getRange = iriPos . cutIRI
rangeSpan = iRIRange . cutIRI
data EntityType =
Datatype
| Class
| ObjectProperty
| DataProperty
| AnnotationProperty
| NamedIndividual
deriving (Enum, Bounded, Show, Read, Eq, Ord, Typeable, Data)
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]
pairSymbols :: Entity -> Entity -> Result Entity -- TODO: improve!
pairSymbols (Entity lb1 k1 i1) (Entity lb2 k2 i2) =
if k1 /= k2 then
error "can't pair symbols of different kind"
else do
let rest x = drop 1 $ dropWhile (/= '#') x
pairLables lbl1 lbl2 = case (lbl1, lbl2) of
(Nothing, _) -> pairLables lbl2 lbl1
(Just l1, Just l2) | l1 /= l2 -> Just $ l1 ++ ", " ++ l2
_ -> lbl1
pairIRIs iri1 iri2 = nullIRI
{ prefixName = prefixName iri1
, iriPath = if rest (show $ iriPath iri1) ==
rest (show $ iriPath iri2)
then iriPath iri1
else appendId (iriPath iri1) (iriPath iri2)
} -- TODO: improve, see #1597
return $ Entity (pairLables lb1 lb2) k1 $ pairIRIs i1 i2
-- * LITERALS
data TypedOrUntyped = Typed Datatype | Untyped (Maybe LanguageTag)
deriving (Show, Eq, Ord, Typeable, Data)
data Literal = Literal LexicalForm TypedOrUntyped | NumberLit FloatLit
deriving (Show, Eq, Ord, Typeable, Data)
-- | non-negative integers given by the sequence of digits
data NNInt = NNInt [Int] deriving (Eq, Ord, Typeable, Data)
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, Typeable, Data)
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, Typeable, Data)
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, Typeable, Data)
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, Typeable, Data)
objPropToIRI :: ObjectPropertyExpression -> Individual
objPropToIRI opExp = case opExp of
ObjectProp u -> u
ObjectInverseOf objProp -> objPropToIRI objProp
type DataPropertyExpression = DataProperty
-- * DATA RANGES
data DataRange =
DataType Datatype [(ConstrainingFacet, RestrictionValue)]
| DataJunction JunctionType [DataRange]
| DataComplementOf DataRange
| DataOneOf [Literal]
deriving (Show, Eq, Ord, Typeable, Data)
-- * 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, Typeable, Data)
-- * ANNOTATIONS
data Annotation = Annotation [Annotation] AnnotationProperty AnnotationValue
deriving (Show, Eq, Ord, Typeable, Data)
data AnnotationValue = AnnValue IRI | AnnValLit Literal
deriving (Show, Eq, Ord, Typeable, Data)