Coverage for oc_ocdm/prov/prov_entity.py: 95%

40 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 rdflib import Graph, Namespace, URIRef 

21 

22from oc_ocdm.abstract_entity import AbstractEntity 

23from oc_ocdm.graph.graph_entity import GraphEntity 

24 

25if TYPE_CHECKING: 

26 from typing import ClassVar, Dict 

27 

28 from oc_ocdm.prov.prov_set import ProvSet 

29 

30 

31class ProvEntity(AbstractEntity): 

32 """Snapshot of entity metadata (short: se): a particular snapshot recording the 

33 metadata associated with an individual entity (either a bibliographic entity or an 

34 identifier) at a particular date and time, including the agent, such as a person, 

35 organisation or automated process that created or modified the entity metadata. 

36 """ 

37 

38 PROV: ClassVar[Namespace] = Namespace("http://www.w3.org/ns/prov#") 

39 

40 iri_entity: ClassVar[URIRef] = PROV.Entity 

41 iri_generated_at_time: ClassVar[URIRef] = PROV.generatedAtTime 

42 iri_invalidated_at_time: ClassVar[URIRef] = PROV.invalidatedAtTime 

43 iri_specialization_of: ClassVar[URIRef] = PROV.specializationOf 

44 iri_was_derived_from: ClassVar[URIRef] = PROV.wasDerivedFrom 

45 iri_had_primary_source: ClassVar[URIRef] = PROV.hadPrimarySource 

46 iri_was_attributed_to: ClassVar[URIRef] = PROV.wasAttributedTo 

47 iri_description: ClassVar[URIRef] = GraphEntity.DCTERMS.description 

48 iri_has_update_query: ClassVar[URIRef] = GraphEntity.OCO.hasUpdateQuery 

49 

50 short_name_to_type_iri: ClassVar[Dict[str, URIRef]] = { 

51 'se': iri_entity 

52 } 

53 

54 def __init__(self, prov_subject: GraphEntity, g: Graph, p_set: ProvSet, 

55 res: URIRef = None, resp_agent: str = None, source: str = None, 

56 res_type: URIRef = None, count: str = None, label: str = None, 

57 short_name: str = "") -> None: 

58 super(ProvEntity, self).__init__() 

59 self.prov_subject: GraphEntity = prov_subject 

60 

61 self.g: Graph = g 

62 self.resp_agent: str = resp_agent 

63 self.source: str = source 

64 self.short_name: str = short_name 

65 self.p_set: ProvSet = p_set 

66 

67 # If res was not specified, create from scratch the URI reference for this entity, 

68 # otherwise use the provided one 

69 if res is None: 

70 self.res = self._generate_new_res(g, count, short_name) 

71 else: 

72 self.res = res 

73 

74 if p_set is not None: 

75 # If not already done, register this ProvEntity instance inside the ProvSet 

76 if self.res not in p_set.res_to_entity: 

77 p_set.res_to_entity[self.res] = self 

78 

79 # Add mandatory information to the entity graph 

80 self._create_type(res_type) 

81 if label is not None: 

82 self.create_label(label) 

83 

84 @staticmethod 

85 def _generate_new_res(g: Graph, count: str, short_name: str) -> URIRef: 

86 return URIRef(str(g.identifier) + short_name + "/" + count)