Coverage for oc_ocdm/graph/entities/bibliographic/agent_role.py: 70%

61 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-05-30 22:05 +0000

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3# Copyright (c) 2016, Silvio Peroni <essepuntato@gmail.com> 

4# 

5# Permission to use, copy, modify, and/or distribute this software for any purpose 

6# with or without fee is hereby granted, provided that the above copyright notice 

7# and this permission notice appear in all copies. 

8# 

9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 

10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 

11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, 

12# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 

13# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 

14# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 

15# SOFTWARE. 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19 

20from oc_ocdm.decorators import accepts_only 

21 

22if TYPE_CHECKING: 

23 from typing import Optional 

24 from rdflib import URIRef 

25 from oc_ocdm.graph.entities.bibliographic.responsible_agent import ResponsibleAgent 

26from oc_ocdm.graph.graph_entity import GraphEntity 

27from oc_ocdm.graph.entities.bibliographic_entity import BibliographicEntity 

28 

29 

30class AgentRole(BibliographicEntity): 

31 """Agent role (short: ar): a particular role held by an agent with respect to a bibliographic resource.""" 

32 

33 @accepts_only('ar') 

34 def merge(self, other: AgentRole) -> None: 

35 """ 

36 The merge operation allows combining two ``AgentRole`` entities into a single one, 

37 by marking the second entity as to be deleted while also copying its data into the current 

38 ``AgentRole``. Moreover, every triple from the containing ``GraphSet`` referring to the second 

39 entity gets "redirected" to the current entity: **every other reference contained inside a 

40 different source (e.g. a triplestore) must be manually handled by the user!** 

41 

42 In case of functional properties, values from the current entity get overwritten 

43 by those coming from the second entity while, in all other cases, values from the 

44 second entity are simply appended to those of the current entity. In this context, 

45 ``rdfs:label`` is considered as a functional property, while ``rdf:type`` is not. 

46 

47 :param other: The entity which will be marked as to be deleted and whose properties will 

48 be merged into the current entity. 

49 :type other: AgentRole 

50 :raises TypeError: if the parameter is of the wrong type 

51 :return: None 

52 """ 

53 super(AgentRole, self).merge(other) 

54 

55 next_ar: Optional[AgentRole] = other.get_next() 

56 if next_ar is not None: 

57 self.has_next(next_ar) 

58 

59 resp_agent: Optional[ResponsibleAgent] = other.get_is_held_by() 

60 if resp_agent is not None: 

61 self.is_held_by(resp_agent) 

62 

63 role_type: Optional[URIRef] = other.get_role_type() 

64 if role_type is not None: 

65 if role_type == GraphEntity.iri_publisher: 

66 self.create_publisher() 

67 elif role_type == GraphEntity.iri_author: 

68 self.create_author() 

69 elif role_type == GraphEntity.iri_editor: 

70 self.create_editor() 

71 

72 # HAS NEXT (AgentRole) 

73 def get_next(self) -> Optional[AgentRole]: 

74 """ 

75 Getter method corresponding to the ``oco:hasNext`` RDF predicate. 

76 

77 :return: The requested value if found, None otherwise 

78 """ 

79 uri: Optional[URIRef] = self._get_uri_reference(GraphEntity.iri_has_next, 'ar') 

80 if uri is not None: 

81 return self.g_set.add_ar(self.resp_agent, self.source, uri) 

82 

83 @accepts_only('ar') 

84 def has_next(self, ar_res: AgentRole) -> None: 

85 """ 

86 Setter method corresponding to the ``oco:hasNext`` RDF predicate. 

87 

88 **WARNING: this is a functional property, hence any existing value will be overwritten!** 

89 

90 `The previous role in a sequence of agent roles of the same type associated with the 

91 same bibliographic resource (so as to define, for instance, an ordered list of authors).` 

92 

93 :param ar_res: The value that will be set as the object of the property related to this method 

94 :type ar_res: AgentRole 

95 :raises TypeError: if the parameter is of the wrong type 

96 :return: None 

97 """ 

98 self.remove_next() 

99 self.g.add((self.res, GraphEntity.iri_has_next, ar_res.res)) 

100 

101 def remove_next(self) -> None: 

102 """ 

103 Remover method corresponding to the ``oco:hasNext`` RDF predicate. 

104 

105 :return: None 

106 """ 

107 self.g.remove((self.res, GraphEntity.iri_has_next, None)) 

108 

109 # IS HELD BY (ResponsibleAgent) 

110 def get_is_held_by(self) -> Optional[ResponsibleAgent]: 

111 """ 

112 Getter method corresponding to the ``pro:isHeldBy`` RDF predicate. 

113 

114 :return: The requested value if found, None otherwise 

115 """ 

116 uri: Optional[URIRef] = self._get_uri_reference(GraphEntity.iri_is_held_by, 'ra') 

117 if uri is not None: 

118 return self.g_set.add_ra(self.resp_agent, self.source, uri) 

119 

120 @accepts_only('ra') 

121 def is_held_by(self, ra_res: ResponsibleAgent): 

122 """ 

123 Setter method corresponding to the ``pro:isHeldBy`` RDF predicate. 

124 

125 **WARNING: this is a functional property, hence any existing value will be overwritten!** 

126 

127 `The agent holding this role with respect to a particular bibliographic resource.` 

128 

129 :param ra_res: The value that will be set as the object of the property related to this method 

130 :type ra_res: ResponsibleAgent 

131 :raises TypeError: if the parameter is of the wrong type 

132 :return: None 

133 """ 

134 self.remove_is_held_by() 

135 self.g.add((self.res, GraphEntity.iri_is_held_by, ra_res.res)) 

136 

137 def remove_is_held_by(self) -> None: 

138 """ 

139 Remover method corresponding to the ``pro:isHeldBy`` RDF predicate. 

140 

141 :return: None 

142 """ 

143 self.g.remove((self.res, GraphEntity.iri_is_held_by, None)) 

144 

145 # HAS ROLE TYPE 

146 def get_role_type(self) -> Optional[URIRef]: 

147 """ 

148 Getter method corresponding to the ``pro:withRole`` RDF predicate. 

149 

150 :return: The requested value if found, None otherwise 

151 """ 

152 uri: Optional[URIRef] = self._get_uri_reference(GraphEntity.iri_with_role) 

153 return uri 

154 

155 def create_publisher(self) -> None: 

156 """ 

157 Setter method corresponding to the ``pro:withRole`` RDF predicate. 

158 It implicitly sets the object value ``pro:publisher``. 

159 

160 **WARNING: this is a functional property, hence any existing value will be overwritten!** 

161 

162 `The specific type of role under consideration (e.g. author, editor or publisher).` 

163 

164 :return: None 

165 """ 

166 self.remove_role_type() 

167 self.g.add((self.res, GraphEntity.iri_with_role, GraphEntity.iri_publisher)) 

168 

169 def create_author(self) -> None: 

170 """ 

171 Setter method corresponding to the ``pro:withRole`` RDF predicate. 

172 It implicitly sets the object value ``pro:author``. 

173 

174 **WARNING: this is a functional property, hence any existing value will be overwritten!** 

175 

176 `The specific type of role under consideration (e.g. author, editor or publisher).` 

177 

178 :return: None 

179 """ 

180 self.remove_role_type() 

181 self.g.add((self.res, GraphEntity.iri_with_role, GraphEntity.iri_author)) 

182 

183 def create_editor(self) -> None: 

184 """ 

185 Setter method corresponding to the ``pro:withRole`` RDF predicate. 

186 It implicitly sets the object value ``pro:editor``. 

187 

188 **WARNING: this is a functional property, hence any existing value will be overwritten!** 

189 

190 `The specific type of role under consideration (e.g. author, editor or publisher).` 

191 

192 :return: None 

193 """ 

194 self.remove_role_type() 

195 self.g.add((self.res, GraphEntity.iri_with_role, GraphEntity.iri_editor)) 

196 

197 def remove_role_type(self) -> None: 

198 """ 

199 Remover method corresponding to the ``pro:withRole`` RDF predicate. 

200 

201 :return: None 

202 """ 

203 self.g.remove((self.res, GraphEntity.iri_with_role, None))