Coverage for oc_ocdm/abstract_set.py: 87%

23 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 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 @abstractmethod 

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

57 """ 

58 Method signature for concrete implementations that allow 

59 to retrieve a contained entity identified by its URI. 

60 

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

62 :type res: URIRef 

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

64 """ 

65 raise NotImplementedError 

66 

67 @staticmethod 

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

69 """ 

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

71 the name of a given named graph. 

72 

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

74 

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

76 :type g: Graph 

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

78 """ 

79 return str(g.identifier)