-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptation.py
More file actions
229 lines (159 loc) · 7.31 KB
/
adaptation.py
File metadata and controls
229 lines (159 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import inspect
import logging
from arena import introspection
logger = logging.getLogger(__name__)
class AdaptedMethod:
"""
An adapted method (mapping between LQL signature and python callable)
"""
def __init__(self, adapted_implementation, method_signature, member):
self.adapted_implementation = adapted_implementation
self.method_signature = method_signature
self.member = member
class AdaptedInitializer:
"""
An adapted initializer (mapping between LQL signature and python callable)
"""
def __init__(self, adapted_implementation, method_signature, member):
self.adapted_implementation = adapted_implementation
self.method_signature = method_signature
self.member = member
class AdaptedImplementation:
"""
Model for adapted implementation
"""
def __init__(self, cut, initializer_mapping: dict, method_mapping: dict):
self.cut = cut
self.initializer_mapping = initializer_mapping
self.method_mapping = method_mapping
self.adapter_id = "0"
self.measures = {}
def get_method(self, interface_specification, index: int) -> AdaptedMethod:
method_signature = interface_specification.methods[index]
method = self.method_mapping[method_signature]
return AdaptedMethod(self, method_signature, method)
def get_initializer(self, interface_specification, index: int):
init_signature = interface_specification.constructors[index]
init = self.initializer_mapping[init_signature]
return AdaptedMethod(self, init_signature, init)
def __str__(self):
return f"{str(self.cut.id)}_{self.cut.variant_id}_{self.adapter_id}"
class AdaptationStrategy:
"""
Interface for adaptation strategies
"""
def adapt(self, interface_specification, cut, no_adapters: int) -> [AdaptedImplementation]:
pass
class PassThroughAdaptationStrategy(AdaptationStrategy):
"""
Simple pass through strategy (1:1 mapping between interface spec and python callables)
"""
def adapt(self, interface_specification, cut, no_adapters: int) -> [AdaptedImplementation]:
"""
Only one adapter is created. The interface of the class under test is assumed to match the lql interface.
:param interface_specification:
:param class_under_test:
:return:
"""
initializer_mapping = self.resolve_initializers(interface_specification, cut.class_under_test)
method_mapping = self.resolve_methods(interface_specification, cut.class_under_test)
return [AdaptedImplementation(cut, initializer_mapping, method_mapping)]
def resolve_methods(self, interface_specification, class_under_test):
"""
Resolve method bindings
:param interface_specification:
:param class_under_test:
:return:
"""
mapping = dict()
for method_spec in interface_specification.get_methods():
callable_obj = None
# python builtin functions in resolution process
if method_spec.name in introspection.builtin_map:
callable_obj = introspection.builtin_map[method_spec.name]
else:
callable_obj = getattr(class_under_test, method_spec.name)
mapping[method_spec] = callable_obj
return mapping
def resolve_initializers(self, interface_specification, class_under_test):
"""
Resolve initializer bindings
:param interface_specification:
:param class_under_test:
:return:
"""
mapping = dict()
constructors = interface_specification.get_constructors()
# should be only one constructor
if len(constructors) > 0:
assert 1 == len(constructors), "only one constructor supported in Python"
constructor = constructors[0]
callable_obj = getattr(class_under_test, "__init__")
mapping[constructor] = callable_obj
return mapping
class SingleFunctionAdaptationStrategy(AdaptationStrategy):
"""
Simple adaptation based on the assumption that only one function exists in a module (*.py file) that will match our signature.
Only works for single functions.
"""
def adapt(self, interface_specification, cut, no_adapters: int) -> [AdaptedImplementation]:
"""
Only one adapter is created. The interface of the class under test is assumed to match the lql interface.
:param interface_specification:
:param class_under_test:
:return:
"""
initializer_mapping = self.resolve_initializers(interface_specification, cut.class_under_test)
method_mapping = self.resolve_methods(interface_specification, cut.class_under_test)
return [AdaptedImplementation(cut, initializer_mapping, method_mapping)]
def resolve_methods(self, interface_specification, class_under_test):
"""
Resolve method bindings
:param interface_specification:
:param class_under_test:
:return:
"""
mapping = dict()
for method_spec in interface_specification.get_methods():
callable_obj = None
# python builtin functions in resolution process
if method_spec.name in introspection.builtin_map:
callable_obj = introspection.builtin_map[method_spec.name]
else:
try:
callable_obj = getattr(class_under_test, method_spec.name)
except AttributeError as ae:
# method/function does not exist at all, try to adapt
logger.info(f"Method/Function '{method_spec.name}' not found. Adaption needed.")
declared_functions = []
for name, obj in inspect.getmembers(class_under_test, inspect.isfunction):
#if obj.__module__ == my_module.__name__:
declared_functions.append((name, obj))
logger.info("Declared-in-this-module functions:")
for name, fn in declared_functions:
logger.debug(f" - {name}: {fn}")
if len(declared_functions) < 1:
raise AttributeError("no declared function found")
if len(declared_functions) > 1:
logger.warning(f"more than one function found for adaptation: {len(declared_functions)}")
# settle on the first one
callable_obj = declared_functions[0][1] # is tuple
logger.info(f"set adapted to {callable_obj}")
mapping[method_spec] = callable_obj
return mapping
def resolve_initializers(self, interface_specification, class_under_test):
"""
Resolve initializer bindings
:param interface_specification:
:param class_under_test:
:return:
"""
mapping = dict()
constructors = interface_specification.get_constructors()
# should be only one constructor
if len(constructors) > 0:
assert 1 == len(constructors), "only one constructor supported in Python"
constructor = constructors[0]
callable_obj = getattr(class_under_test, "__init__")
mapping[constructor] = callable_obj
return mapping