-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSign.hs
157 lines (132 loc) · 5.59 KB
/
Sign.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{-# LANGUAGE DeriveDataTypeable #-}
{- |
Module : ./OWL2/Sign.hs
Copyright : Heng Jiang, Uni Bremen 2007
License : GPLv2 or higher, see LICENSE.txt
Maintainer : Christian.Maeder@dfki.de
Stability : provisional
Portability : portable
OWL 2 signature and sentences
-}
module OWL2.Sign where
import Common.IRI
import OWL2.AS
import qualified Data.Set as Set
import qualified Data.Map as Map
import Common.Lib.State
import Common.Result
import Control.Monad
import Data.Data
data Sign = Sign
{ concepts :: Set.Set Class
-- classes
, datatypes :: Set.Set Datatype -- datatypes
, objectProperties :: Set.Set ObjectProperty
-- object properties
, dataProperties :: Set.Set DataProperty
-- data properties
, annotationRoles :: Set.Set AnnotationProperty
-- annotation properties
, individuals :: Set.Set NamedIndividual -- named individuals
, labelMap :: Map.Map IRI String -- labels (for better readability)
, prefixMap :: PrefixMap
} deriving (Show, Typeable, Data)
instance Ord Sign where
compare (Sign c1 d1 op1 dp1 ar1 iv1 _ _) (Sign c2 d2 op2 dp2 ar2 iv2 _ _)
= compare (c1, d1, op1, dp1, ar1, iv1) (c2, d2, op2, dp2, ar2, iv2)
instance Eq Sign where
s1 == s2 = compare s1 s2 == EQ
data SignAxiom =
Subconcept ClassExpression ClassExpression -- subclass, superclass
| Role (DomainOrRangeOrFunc (RoleKind, RoleType)) ObjectPropertyExpression
| Data (DomainOrRangeOrFunc ()) DataPropertyExpression
| Conceptmembership Individual ClassExpression
deriving (Show, Eq, Ord, Typeable, Data)
data RoleKind = FuncRole | RefRole deriving (Show, Eq, Ord, Typeable, Data)
data RoleType = IRole | DRole deriving (Show, Eq, Ord, Typeable, Data)
data DesKind = RDomain | DDomain | RIRange
deriving (Show, Eq, Ord, Typeable, Data)
data DomainOrRangeOrFunc a =
DomainOrRange DesKind ClassExpression
| RDRange DataRange
| FuncProp a
deriving (Show, Eq, Ord, Typeable, Data)
emptySign :: Sign
emptySign = Sign
{ concepts = Set.empty
, datatypes = Set.empty
, objectProperties = Set.empty
, dataProperties = Set.empty
, annotationRoles = Set.empty
, individuals = Set.empty
, labelMap = Map.empty
, prefixMap = Map.empty
}
diffSig :: Sign -> Sign -> Sign
diffSig a b =
a { concepts = concepts a `Set.difference` concepts b
, datatypes = datatypes a `Set.difference` datatypes b
, objectProperties = objectProperties a
`Set.difference` objectProperties b
, dataProperties = dataProperties a `Set.difference` dataProperties b
, annotationRoles = annotationRoles a `Set.difference` annotationRoles b
, individuals = individuals a `Set.difference` individuals b
}
addSymbToSign :: Sign -> Entity -> Result Sign
addSymbToSign sig ent =
case ent of
Entity _ Class eIri ->
return sig {concepts = Set.insert eIri $ concepts sig}
Entity _ ObjectProperty eIri ->
return sig {objectProperties = Set.insert eIri $ objectProperties sig}
Entity _ NamedIndividual eIri ->
return sig {individuals = Set.insert eIri $ individuals sig}
_ -> return sig
addSign :: Sign -> Sign -> Sign
addSign toIns totalSign =
totalSign {
concepts = Set.union (concepts totalSign)
(concepts toIns),
datatypes = Set.union (datatypes totalSign)
(datatypes toIns),
objectProperties = Set.union (objectProperties totalSign)
(objectProperties toIns),
dataProperties = Set.union (dataProperties totalSign)
(dataProperties toIns),
annotationRoles = Set.union (annotationRoles totalSign)
(annotationRoles toIns),
individuals = Set.union (individuals totalSign)
(individuals toIns)
}
isSubSign :: Sign -> Sign -> Bool
isSubSign a b =
Set.isSubsetOf (concepts a) (concepts b)
&& Set.isSubsetOf (datatypes a) (datatypes b)
&& Set.isSubsetOf (objectProperties a) (objectProperties b)
&& Set.isSubsetOf (dataProperties a) (dataProperties b)
&& Set.isSubsetOf (annotationRoles a) (annotationRoles b)
&& Set.isSubsetOf (individuals a) (individuals b)
symOf :: Sign -> Set.Set Entity
symOf s = Set.unions
[ Set.map (\ ir -> Entity (Map.lookup ir $ labelMap s) Class ir) $ concepts s
, Set.map (mkEntity Datatype) $ datatypes s
, Set.map (mkEntity ObjectProperty) $ objectProperties s
, Set.map (mkEntity DataProperty) $ dataProperties s
, Set.map (mkEntity NamedIndividual) $ individuals s
, Set.map (mkEntity AnnotationProperty) $ annotationRoles s ]
-- | takes an entity and modifies the sign according to the given function
modEntity :: (IRI -> Set.Set IRI -> Set.Set IRI) -> Entity -> State Sign ()
modEntity f (Entity _ ty u) = do
s <- get
let chg = f u
unless (isDatatypeKey u || isThing u) $ put $ case ty of
Datatype -> s { datatypes = chg $ datatypes s }
Class -> s { concepts = chg $ concepts s }
ObjectProperty -> s { objectProperties = chg $ objectProperties s }
DataProperty -> s { dataProperties = chg $ dataProperties s }
NamedIndividual -> if isAnonymous u then s
else s { individuals = chg $ individuals s }
AnnotationProperty -> s {annotationRoles = chg $ annotationRoles s}
-- | adding entities to the signature
addEntity :: Entity -> State Sign ()
addEntity = modEntity Set.insert