Coverage for oc_ocdm/graph/entities/bibliographic/reference_annotation.py: 62%

26 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.citation import Citation 

26from oc_ocdm.graph.graph_entity import GraphEntity 

27from oc_ocdm.graph.entities.bibliographic_entity import BibliographicEntity 

28 

29 

30class ReferenceAnnotation(BibliographicEntity): 

31 """Reference annotation (short: an): an annotation, attached either to an in-text 

32 reference pointer or to a bibliographic reference, describing the related citation. If an 

33 in-text reference pointer is annotated, the related citation may be characterized with a 

34 citation function (the reason for that citation) specific to the textual location of that 

35 in-text reference pointer within the citing entity. If a bibliographic reference is 

36 annotated, the related citation may be similarly characterized in a more general way 

37 with a citation function (the reason for that citation).""" 

38 

39 @accepts_only('an') 

40 def merge(self, other: ReferenceAnnotation) -> None: 

41 """ 

42 The merge operation allows combining two ``ReferenceAnnotation`` entities into a single one, 

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

44 ``ReferenceAnnotation``. Moreover, every triple from the containing ``GraphSet`` referring to the second 

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

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

47 

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

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

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

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

52 

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

54 be merged into the current entity. 

55 :type other: ReferenceAnnotation 

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

57 :return: None 

58 """ 

59 super(ReferenceAnnotation, self).merge(other) 

60 

61 citation: Optional[Citation] = other.get_body_annotation() 

62 if citation is not None: 

63 self.has_body_annotation(citation) 

64 

65 # HAS CITATION (Citation) 

66 def get_body_annotation(self) -> Optional[Citation]: 

67 """ 

68 Getter method corresponding to the ``oa:hasBody`` RDF predicate. 

69 

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

71 """ 

72 uri: Optional[URIRef] = self._get_uri_reference(GraphEntity.iri_has_body, 'ci') 

73 if uri is not None: 

74 return self.g_set.add_ci(self.resp_agent, self.source, uri) 

75 

76 @accepts_only('ci') 

77 def has_body_annotation(self, ci_res: Citation) -> None: 

78 """ 

79 Setter method corresponding to the ``oa:hasBody`` RDF predicate. 

80 

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

82 

83 `The citation to which the annotation relates, that is relevant either to a bibliographic 

84 reference or to an in-text reference pointer that denotes such a bibliographic reference.` 

85 

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

87 :type ci_res: Citation 

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

89 :return: None 

90 """ 

91 self.remove_body_annotation() 

92 self.g.add((self.res, GraphEntity.iri_has_body, ci_res.res)) 

93 

94 def remove_body_annotation(self) -> None: 

95 """ 

96 Remover method corresponding to the ``oa:hasBody`` RDF predicate. 

97 

98 :return: None 

99 """ 

100 self.g.remove((self.res, GraphEntity.iri_has_body, None))