Coverage for oc_ocdm/graph/entities/bibliographic/pointer_list.py: 54%

41 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, List 

24 from rdflib import URIRef 

25 from oc_ocdm.graph.entities.bibliographic.reference_pointer import ReferencePointer 

26from oc_ocdm.graph.graph_entity import GraphEntity 

27from oc_ocdm.graph.entities.bibliographic_entity import BibliographicEntity 

28 

29 

30class PointerList(BibliographicEntity): 

31 """Pointer list (short: pl): a textual device (e.g. '[1, 2, 3]' or '[4-9]') which includes a 

32 number of reference pointers denoting the specific bibliographic references to which 

33 the list pertains.""" 

34 

35 @accepts_only('pl') 

36 def merge(self, other: PointerList) -> None: 

37 """ 

38 The merge operation allows combining two ``PointerList`` entities into a single one, 

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

40 ``PointerList``. Moreover, every triple from the containing ``GraphSet`` referring to the second 

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

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

43 

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

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

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

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

48 

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

50 be merged into the current entity. 

51 :type other: PointerList 

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

53 :return: None 

54 """ 

55 super(PointerList, self).merge(other) 

56 

57 content: Optional[str] = self.get_content() 

58 if content is not None: 

59 self.has_content(content) 

60 

61 rp_list: List[ReferencePointer] = other.get_contained_elements() 

62 for cur_rp in rp_list: 

63 self.contains_element(cur_rp) 

64 

65 # HAS POINTER LIST TEXT 

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

67 """ 

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

69 

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

71 """ 

72 return self._get_literal(GraphEntity.iri_has_content) 

73 

74 @accepts_only('literal') 

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 :param string: The value that will be set as the object of the property related to this method 

82 :type string: str 

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

84 :return: None 

85 """ 

86 self.remove_content() 

87 self._create_literal(GraphEntity.iri_has_content, string) 

88 

89 def remove_content(self) -> None: 

90 """ 

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

92 

93 :return: None 

94 """ 

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

96 

97 # HAS ELEMENT (ReferencePointer) 

98 def get_contained_elements(self) -> List[ReferencePointer]: 

99 """ 

100 Getter method corresponding to the ``co:element`` RDF predicate. 

101 

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

103 """ 

104 uri_list: List[URIRef] = self._get_multiple_uri_references(GraphEntity.iri_has_element, 'rp') 

105 result: List[ReferencePointer] = [] 

106 for uri in uri_list: 

107 result.append(self.g_set.add_rp(self.resp_agent, self.source, uri)) 

108 return result 

109 

110 @accepts_only('rp') 

111 def contains_element(self, rp_res: ReferencePointer) -> None: 

112 """ 

113 Setter method corresponding to the ``co:element`` RDF predicate. 

114 

115 `The in-text reference pointer that is part of the in-text reference pointer list present at 

116 a particular location within the body of the citing work.` 

117 

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

119 :type rp_res: ReferencePointer 

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

121 :return: None 

122 """ 

123 self.g.add((self.res, GraphEntity.iri_has_element, rp_res.res)) 

124 

125 @accepts_only('rp') 

126 def remove_contained_element(self, rp_res: ReferencePointer = None) -> None: 

127 """ 

128 Remover method corresponding to the ``co:element`` RDF predicate. 

129 

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

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

132 

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

134 related to this method (defaults to None) 

135 :type rp_res: ReferencePointer 

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

137 :return: None 

138 """ 

139 if rp_res is not None: 

140 self.g.remove((self.res, GraphEntity.iri_has_element, rp_res.res)) 

141 else: 

142 self.g.remove((self.res, GraphEntity.iri_has_element, None))