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

22 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-28 18:52 +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 oc_ocdm.decorators import accepts_only 

13 

14if TYPE_CHECKING: 

15 from typing import Optional 

16 from rdflib import URIRef 

17 from oc_ocdm.graph.entities.bibliographic.citation import Citation 

18from oc_ocdm.graph.graph_entity import GraphEntity 

19from oc_ocdm.graph.entities.bibliographic_entity import BibliographicEntity 

20 

21 

22class ReferenceAnnotation(BibliographicEntity): 

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

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

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

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

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

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

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

30 

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

32 """ 

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

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

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

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

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

38 

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

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

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

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

43 

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

45 be merged into the current entity. 

46 :type other: ReferenceAnnotation 

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

48 :return: None 

49 """ 

50 super()._merge_properties(other, prefer_self) 

51 assert isinstance(other, ReferenceAnnotation) 

52 

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

54 if citation is not None: 

55 self.has_body_annotation(citation) 

56 

57 # HAS CITATION (Citation) 

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

59 """ 

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

61 

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

63 """ 

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

65 if uri is not None: 

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

67 

68 @accepts_only('ci') 

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

70 """ 

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

72 

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

74 

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

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

77 

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

79 :type ci_res: Citation 

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

81 :return: None 

82 """ 

83 self.remove_body_annotation() 

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

85 

86 def remove_body_annotation(self) -> None: 

87 """ 

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

89 

90 :return: None 

91 """ 

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