Coverage for lode / models / concept.py: 95%

81 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-22 15:54 +0000

1"""Concept: an OWL class or SKOS concept, with its hierarchy and matching relations.""" 

2from .resource import Resource 

3 

4class Concept(Resource): 

5 """An OWL class or a SKOS concept. 

6 

7 Holds the relations between concepts (sub/super, disjoint, equivalent, SKOS 

8 matches) and the inverse links to the properties and individuals that refer to it. 

9 """ 

10 

11 def __init__(self, **kwargs): 

12 """Create a concept with every relation list empty.""" 

13 

14 super().__init__(**kwargs) 

15 

16 # Relations with Concepts 

17 self.is_sub_concept_of = [] # 0..* 

18 self.is_super_concept_of = [] # 0..* 

19 self.is_disjoint_with = [] # 0..* 

20 self.is_equivalent_to = [] # 0..* 

21 self.is_related_to = [] # 0..* 

22 self.has_broad_match = [] # 0..* 

23 self.has_narrow_match = [] # 0..* 

24 self.has_related_match = [] # 0..* 

25 self.has_exact_match = [] # 0..*  

26 self.has_close_match = [] # 0..* 

27 self.is_top_concept_of = [] # 0..* (inverse of is Model.has_top_concept) 

28 self.has_key = [] # 0..* 

29 self.individuals_with_this_type = [] # 0..* (inverse of Individual.has_type) 

30 self.is_in_domain_of = [] # 0..* 

31 

32 def set_has_key(self, prop): 

33 """Add a property to has_key (owl:hasKey).""" 

34 self.has_key.append(prop) 

35 

36 def get_has_key(self): 

37 """Return the has_key list (not a copy).""" 

38 return self.has_key 

39 

40 # Setter and getter foris_sub_concept_of 

41 def set_is_sub_concept_of(self, concept): 

42 """Add a super concept to is_sub_concept_of and register self as its sub concept. 

43 

44 The inverse link is set only when the target supports it (a Restriction or 

45 TruthFunction may not have the attribute). 

46 """ 

47 if concept not in self.is_sub_concept_of: 

48 self.is_sub_concept_of.append(concept) 

49 # inverse: only if the target supports the relation 

50 inv = getattr(concept, 'is_super_concept_of', None) 

51 if inv is not None and self not in inv: 

52 inv.append(self) 

53 

54 def set_is_super_concept_of(self, concept): 

55 """Add a concept to is_super_concept_of, ignoring duplicates.""" 

56 if concept not in self.is_super_concept_of: 

57 self.is_super_concept_of.append(concept) 

58 

59 def get_is_super_concept_of(self): 

60 """Return the is_super_concept_of list (not a copy).""" 

61 return self.is_super_concept_of 

62 

63 def get_is_sub_concept_of(self): 

64 """Return the is_sub_concept_of list (not a copy).""" 

65 return self.is_sub_concept_of 

66 

67 # Setter and getter foris_disjoint_with 

68 def set_is_disjoint_with(self, concept): 

69 """Add a concept to is_disjoint_with.""" 

70 self.is_disjoint_with.append(concept) 

71 

72 def get_is_disjoint_with(self): 

73 """Return the is_disjoint_with list (not a copy).""" 

74 return self.is_disjoint_with 

75 

76 # Setter and getter foris_equivalent_to 

77 def set_is_equivalent_to(self, concept): 

78 """Add a concept to is_equivalent_to.""" 

79 self.is_equivalent_to.append(concept) 

80 

81 def get_is_equivalent_to(self): 

82 """Return the is_equivalent_to list (not a copy).""" 

83 return self.is_equivalent_to 

84 

85 # Setter and getter foris_related_to 

86 def set_is_related_to(self, concept): 

87 """Add a concept to is_related_to.""" 

88 self.is_related_to.append(concept) 

89 

90 def get_is_related_to(self): 

91 """Return the is_related_to list (not a copy).""" 

92 return self.is_related_to 

93 

94 # Setter and getter forhas_broad_match 

95 def set_has_broad_match(self, concept): 

96 """Add a concept to has_broad_match.""" 

97 self.has_broad_match.append(concept) 

98 

99 def get_has_broad_match(self): 

100 """Return the has_broad_match list (not a copy).""" 

101 return self.has_broad_match 

102 

103 # Setter and getter forhas_narrow_match 

104 def set_has_narrow_match(self, concept): 

105 """Add a concept to has_narrow_match.""" 

106 self.has_narrow_match.append(concept) 

107 

108 def get_has_narrow_match(self): 

109 """Return the has_narrow_match list (not a copy).""" 

110 return self.has_narrow_match 

111 

112 # Setter and getter forhas_related_match 

113 def set_has_related_match(self, concept): 

114 """Add a concept to has_related_match.""" 

115 self.has_related_match.append(concept) 

116 

117 def get_has_related_match(self): 

118 """Return the has_related_match list (not a copy).""" 

119 return self.has_related_match 

120 

121 # Setter and getter forhas_exact_match 

122 def set_has_exact_match(self, concept): 

123 """Add a concept to has_exact_match.""" 

124 self.has_exact_match.append(concept) 

125 

126 def get_has_exact_match(self): 

127 """Return the has_exact_match list (not a copy).""" 

128 return self.has_exact_match 

129 

130 # Setter and getter for has_close_match 

131 def set_has_close_match(self, concept): 

132 """Add a concept to has_close_match.""" 

133 self.has_close_match.append(concept) 

134 

135 def get_has_close_match(self): 

136 """Return the has_close_match list (not a copy).""" 

137 return self.has_close_match 

138 

139 # Setter and getter for is_top_concept_of 

140 def set_is_top_concept_of(self, model): 

141 """Add a concept to has_close_match.""" 

142 self.is_top_concept_of.append(model) 

143 

144 def get_is_top_concept_of(self): 

145 """Return the is_top_concept_of list""" 

146 return self.is_top_concept_of 

147 

148 def set_individuals_with_this_type(self, individual): 

149 """Add an individual to individuals_with_this_type (inverse of rdf:type), ignoring duplicates.""" 

150 if individual not in self.individuals_with_this_type: 

151 self.individuals_with_this_type.append(individual) 

152 

153 def get_individuals_with_this_type(self): 

154 """Return a copy of individuals_with_this_type.""" 

155 return list(self.individuals_with_this_type) 

156 

157 def set_is_in_domain_of(self, prop): 

158 """Add a property to is_in_domain_of, ignoring duplicates.""" 

159 if prop not in self.is_in_domain_of: 

160 self.is_in_domain_of.append(prop) 

161 

162 def get_is_in_domain_of(self): 

163 """Return a copy of is_in_domain_of.""" 

164 return list(self.is_in_domain_of) 

165