Coverage for oc_ocdm/abstract_set.py: 89%

27 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-12-05 23:58 +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 abc import ABC, abstractmethod 

19from typing import TYPE_CHECKING 

20 

21from oc_ocdm.abstract_entity import AbstractEntity 

22 

23if TYPE_CHECKING: 

24 from typing import List, ClassVar, Dict, Optional 

25 from rdflib import URIRef, Graph 

26 

27 

28class AbstractSet(ABC): 

29 """ 

30 Abstract class which represents a generic set of entities. 

31 It is the base class for each concrete set of entities. 

32 """ 

33 

34 short_name_to_type_iri: ClassVar[Dict[str, URIRef]] = {} 

35 

36 def __init__(self) -> None: 

37 """ 

38 Constructor of the ``AbstractSet`` class. 

39 """ 

40 self.res_to_entity: Dict[URIRef, AbstractEntity] = {} 

41 

42 def graphs(self) -> List[Graph]: 

43 """ 

44 A utility method that allows to retrieve the list of ``rdflib.Graph`` 

45 instances corresponding to each entity contained in the set. 

46 

47 :return: The requested list of graphs 

48 """ 

49 result: List[Graph] = [] 

50 for entity in self.res_to_entity.values(): 

51 if len(entity.g) > 0: 

52 result.append(entity.g) 

53 return result 

54 

55 def __getstate__(self): 

56 """ 

57 Support for pickle serialization. 

58 

59 RDFLib Graph objects contain threading locks that are not directly picklable, 

60 but Python's pickle module can handle them. This method provides standard 

61 pickle protocol support for AbstractSet instances. 

62 """ 

63 return self.__dict__.copy() 

64 

65 def __setstate__(self, state): 

66 """ 

67 Support for pickle deserialization. 

68 

69 Restores the AbstractSet state from a pickled representation. 

70 """ 

71 self.__dict__.update(state) 

72 

73 @abstractmethod 

74 def get_entity(self, res: URIRef) -> Optional[AbstractEntity]: 

75 """ 

76 Method signature for concrete implementations that allow 

77 to retrieve a contained entity identified by its URI. 

78 

79 :param res: The URI that identifies the requested entity 

80 :type res: URIRef 

81 :return: The requested entity if found, None otherwise 

82 """ 

83 raise NotImplementedError 

84 

85 @staticmethod 

86 def get_graph_iri(g: Graph) -> str: 

87 """ 

88 A utility method that allows to retrieve the IRI which represents 

89 the name of a given named graph. 

90 

91 **NOTE: this is a static function!** 

92 

93 :param g: The named graph whose name will be returned 

94 :type g: Graph 

95 :return: The requested string whose content is the IRI associated to the given named graph 

96 """ 

97 return str(g.identifier)