Coverage for skema/gromet/execution_engine/primitive_map.py: 59%

54 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1from typing import Iterator, Union, Any, List, Dict, Set, Tuple 

2import sys 

3import inspect 

4 

5import skema.gromet.execution_engine.types 

6from skema.gromet.execution_engine.types import ( 

7 array, 

8 binary, 

9 indexable, 

10 iterable, 

11 list, 

12 map, 

13 other, 

14 record, 

15 sequence, 

16 set, 

17 tuple, 

18 unary, 

19) 

20 

21#### Interface for accessing fields of classes 

22def get_class_obj( 

23 op: str, language: str, debug=False 

24) -> Any: # TODO: Update the type hints for this 

25 global primitive_map 

26 

27 try: 

28 return primitive_map[language][op] 

29 except: 

30 if debug: 

31 print( 

32 f"Operation not supported: {op} for language {language} ... Returning None" 

33 ) 

34 return None 

35 

36 

37def get_shorthand(op: str, language: str) -> str: 

38 class_obj = get_class_obj(op, language, debug=True) 

39 

40 if class_obj: 

41 try: 

42 return class_obj.shorthand 

43 except: 

44 print( 

45 f"Operation has no shorthand: {op} for language {language} ... Returning None" 

46 ) 

47 

48 return None 

49 

50 

51def get_inputs(op: str, language: str) -> str: 

52 class_obj = get_class_obj(op, language, debug=True) 

53 

54 if class_obj: 

55 try: 

56 return class_obj.inputs 

57 except: 

58 print( 

59 f"Operation has no inputs: {op} for language {language} ... Returning None" 

60 ) 

61 

62 return None 

63 

64 

65def get_outputs(op: str, language: str, debug=True) -> str: 

66 class_obj = get_class_obj(op, language) 

67 

68 if class_obj: 

69 try: 

70 return class_obj.outputs 

71 except: 

72 print( 

73 f"Operation has no outputs: {op} for language {language} ... Returning None" 

74 ) 

75 

76 return None 

77 

78 

79def is_primitive(op: str, language: str): 

80 return get_class_obj(op, language) is not None 

81 

82 

83# Create table ob primitive op classes 

84submodules = inspect.getmembers( 

85 skema.gromet.execution_engine.types, predicate=inspect.ismodule 

86) 

87primitive_ops = [] 

88for module_name, module in submodules: 

89 primitive_ops += inspect.getmembers(module, predicate=inspect.isclass) 

90# primitive_ops = inspect.getmembers(sys.modules[__name__], predicate=inspect.isclass) 

91 

92# Create map between langauge names and python class objects 

93python_primitive_dict = {} 

94gcc_primitive_dict = {} 

95cast_primitive_dict = {} 

96for class_name, class_obj in primitive_ops: 

97 if hasattr(class_obj, "source_language_name"): 

98 if "Python" in class_obj.source_language_name: 

99 python_primitive_dict[ 

100 class_obj.source_language_name["Python"] 

101 ] = class_obj 

102 if "GCC" in class_obj.source_language_name: 

103 gcc_primitive_dict[ 

104 class_obj.source_language_name["GCC"] 

105 ] = class_obj 

106 if "CAST" in class_obj.source_language_name: 

107 cast_primitive_dict[ 

108 class_obj.source_language_name["CAST"] 

109 ] = class_obj 

110 

111primitive_map = { 

112 "Python": python_primitive_dict, 

113 "GCC": gcc_primitive_dict, 

114 "CAST": cast_primitive_dict, 

115}