Coverage for oc_ocdm / graph / entities / bibliographic / bibliographic_reference.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-05-08 20:23 +0000

1#!/usr/bin/python 

2 

3# SPDX-FileCopyrightText: 2020-2022 Simone Persiani <iosonopersia@gmail.com> 

4# 

5# SPDX-License-Identifier: ISC 

6 

7# -*- coding: utf-8 -*- 

8from __future__ import annotations 

9 

10from typing import TYPE_CHECKING 

11 

12from triplelite import RDFTerm 

13 

14from oc_ocdm.decorators import accepts_only 

15 

16if TYPE_CHECKING: 

17 from typing import List, Optional 

18 

19 from oc_ocdm.graph.entities.bibliographic.bibliographic_resource import BibliographicResource 

20 from oc_ocdm.graph.entities.bibliographic.reference_annotation import ReferenceAnnotation 

21from oc_ocdm.graph.entities.bibliographic_entity import BibliographicEntity 

22from oc_ocdm.graph.graph_entity import GraphEntity 

23 

24 

25class BibliographicReference(BibliographicEntity): 

26 """Bibliographic reference (short: be): the particular textual bibliographic reference, 

27 usually occurring in the reference list (and denoted by one or more in-text reference 

28 pointers within the text of a citing bibliographic resource), that references another 

29 bibliographic resource. 

30 """ 

31 

32 def _merge_properties(self, other: GraphEntity, prefer_self: bool) -> None: 

33 """ 

34 The merge operation allows combining two ``BibliographicReference`` entities into a single one, 

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

36 ``BibliographicReference``. Moreover, every triple from the containing ``GraphSet`` referring to the second 

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

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

39 

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

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

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

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

44 

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

46 be merged into the current entity. 

47 :type other: BibliographicReference 

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

49 :return: None 

50 """ 

51 super()._merge_properties(other, prefer_self) 

52 assert isinstance(other, BibliographicReference) 

53 

54 content: Optional[str] = other.get_content() 

55 if content is not None: 

56 self.has_content(content) 

57 

58 annotations_list: List[ReferenceAnnotation] = other.get_annotations() 

59 for cur_annotation in annotations_list: 

60 self.has_annotation(cur_annotation) 

61 

62 referenced_br: Optional[BibliographicResource] = other.get_referenced_br() 

63 if referenced_br is not None: 

64 self.references_br(referenced_br) 

65 

66 # HAS BIBLIOGRAPHIC REFERENCE TEXT 

67 def get_content(self) -> Optional[str]: 

68 """ 

69 Getter method corresponding to the ``c4o:hasContent`` RDF predicate. 

70 

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

72 """ 

73 return self._get_literal(GraphEntity.iri_has_content) 

74 

75 def has_content(self, string: str) -> None: 

76 """ 

77 Setter method corresponding to the ``c4o:hasContent`` RDF predicate. 

78 

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

80 

81 `The literal text of a bibliographic reference occurring in the reference list (or 

82 elsewhere) within a bibliographic resource, that references another bibliographic 

83 resource. The reference text should be recorded “as given” in the citing bibliographic 

84 resource, including any errors (e.g. mis-spellings of authors’ names, or changes from 

85 “β” in the original published title to “beta” in the reference text) or omissions (e.g. 

86 omission of the title of the referenced bibliographic resource, or omission of sixth and 

87 subsequent authors’ names, as required by certain publishers), and in whatever format 

88 it has been made available. For instance, the reference text can be either as plain text 

89 or as a block of XML.` 

90 

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

92 :type string: str 

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

94 :return: None 

95 """ 

96 self.remove_content() 

97 self._create_literal(GraphEntity.iri_has_content, string) 

98 

99 def remove_content(self) -> None: 

100 """ 

101 Remover method corresponding to the ``c4o:hasContent`` RDF predicate. 

102 

103 :return: None 

104 """ 

105 self.g.remove((self.res, GraphEntity.iri_has_content, None)) 

106 

107 # HAS ANNOTATION (ReferenceAnnotation) 

108 def get_annotations(self) -> List[ReferenceAnnotation]: 

109 """ 

110 Getter method corresponding to the ``oco:hasAnnotation`` RDF predicate. 

111 

112 :return: A list containing the requested values if found, None otherwise 

113 """ 

114 uri_list: List[str] = self._get_multiple_uri_references(GraphEntity.iri_has_annotation, 'an') 

115 result: List[ReferenceAnnotation] = [] 

116 for uri in uri_list: 

117 result.append(self.g_set.add_an(self.resp_agent, self.source, uri)) 

118 return result 

119 

120 @accepts_only('an') 

121 def has_annotation(self, an_res: ReferenceAnnotation) -> None: 

122 """ 

123 Setter method corresponding to the ``oco:hasAnnotation`` RDF predicate. 

124 

125 `An annotation characterizing the related citation, in terms of its citation function (the 

126 reason for that citation).` 

127 

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

129 :type an_res: ReferenceAnnotation 

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

131 :return: None 

132 """ 

133 self.g.add((self.res, GraphEntity.iri_has_annotation, RDFTerm("uri", str(an_res.res)))) 

134 

135 @accepts_only('an') 

136 def remove_annotation(self, an_res: ReferenceAnnotation | None = None) -> None: 

137 """ 

138 Remover method corresponding to the ``oco:hasAnnotation`` RDF predicate. 

139 

140 **WARNING: this is a non-functional property, hence, if the parameter 

141 is None, any existing value will be removed!** 

142 

143 :param an_res: If not None, the specific object value that will be removed from the property 

144 related to this method (defaults to None) 

145 :type an_res: ReferenceAnnotation 

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

147 :return: None 

148 """ 

149 if an_res is not None: 

150 self.g.remove((self.res, GraphEntity.iri_has_annotation, RDFTerm("uri", str(an_res.res)))) 

151 else: 

152 self.g.remove((self.res, GraphEntity.iri_has_annotation, None)) 

153 

154 # REFERENCES (BibliographicResource) 

155 def get_referenced_br(self) -> Optional[BibliographicResource]: 

156 """ 

157 Getter method corresponding to the ``biro:references`` RDF predicate. 

158 

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

160 """ 

161 uri: Optional[str] = self._get_uri_reference(GraphEntity.iri_references, 'br') 

162 if uri is not None: 

163 return self.g_set.add_br(self.resp_agent, self.source, uri) 

164 

165 @accepts_only('br') 

166 def references_br(self, br_res: BibliographicResource) -> None: 

167 """ 

168 Setter method corresponding to the ``biro:references`` RDF predicate. 

169 

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

171 

172 `The bibliographic reference that cites this bibliographic resource.` 

173 

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

175 :type br_res: BibliographicResource 

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

177 :return: None 

178 """ 

179 self.remove_referenced_br() 

180 self.g.add((self.res, GraphEntity.iri_references, RDFTerm("uri", str(br_res.res)))) 

181 

182 def remove_referenced_br(self) -> None: 

183 """ 

184 Remover method corresponding to the ``biro:references`` RDF predicate. 

185 

186 :return: None 

187 """ 

188 self.g.remove((self.res, GraphEntity.iri_references, None))