1 | :- module(ground_truth, [get_empty_ground_truth/1, | |
2 | get_library_components_from_pred_or_expr/2, | |
3 | get_missing_components/1, | |
4 | contains_record_type/1, | |
5 | current_machine_uses_records/0, | |
6 | get_library_components_for_params_from_pred/3, | |
7 | get_ground_truth_from_operation_body/4, | |
8 | extend_ground_truth/4, | |
9 | get_library_components_from_op_ast/7]). | |
10 | ||
11 | :- use_module(library(avl)). | |
12 | :- use_module(library(sets)). | |
13 | :- use_module(library(lists)). | |
14 | :- use_module(probsrc(bmachine)). | |
15 | :- use_module(probsrc(bsyntaxtree)). | |
16 | :- use_module(probsrc(error_manager)). | |
17 | :- use_module(probsrc('synthesis/deep_learning/b_machine_identifier_normalization')). | |
18 | ||
19 | :- dynamic missing_component/1,operation_parameter/1,explicit_collected_component/1. | |
20 | :- volatile missing_component/1,operation_parameter/1,explicit_collected_component/1. | |
21 | ||
22 | %% contains_record_type(+Ast). | |
23 | % | |
24 | % True if the given typed ast contains at least one record type. | |
25 | contains_record_type(b(_,Type,_)) :- | |
26 | contains_record_type_aux(Type). | |
27 | ||
28 | contains_record_type_aux(field(_,_)) :- | |
29 | !. | |
30 | contains_record_type_aux(record(_)) :- | |
31 | !. | |
32 | contains_record_type_aux(Type) :- | |
33 | Type =.. [_|T], | |
34 | member(Arg, T), | |
35 | contains_record_type_aux(Arg). | |
36 | ||
37 | %% current_machine_uses_records. | |
38 | % | |
39 | % True if the current machine has a machine variable or constant of type record. | |
40 | current_machine_uses_records :- | |
41 | b_get_machine_variables(MachineVars), | |
42 | current_machine_uses_records_aux(MachineVars). | |
43 | current_machine_uses_records :- | |
44 | b_get_machine_constants(MachineConstants), | |
45 | current_machine_uses_records_aux(MachineConstants). | |
46 | ||
47 | current_machine_uses_records_aux(Vars) :- | |
48 | member(Var, Vars), | |
49 | contains_record_type(Var). | |
50 | ||
51 | %% get_missing_components(-MissingComponents) | |
52 | % | |
53 | get_missing_components(MissingComponents) :- | |
54 | findall(C, missing_component(C), MissingComponents). | |
55 | ||
56 | %% get_empty_ground_truth(-GroundTruth) | |
57 | % | |
58 | get_empty_ground_truth(GroundTruth) :- | |
59 | findall(CC, ( ground_truth_component(C), | |
60 | CC = C-0 | |
61 | ), Components), | |
62 | list_to_avl(Components, GroundTruth). | |
63 | ||
64 | %% get_empty_ground_truth(+MachineVars, -GroundTruth) | |
65 | % | |
66 | get_empty_ground_truth_for_vars(MachineVars, GroundTruth) :- | |
67 | findall((VarName,EmptyGt), ( member(b(identifier(VarName),_,_), MachineVars), | |
68 | get_empty_ground_truth(EmptyGt) | |
69 | ), GroundTruth). | |
70 | ||
71 | %% get_ground_truth_from_operation_body(+OpReturnVars, +OperationBody, +ParameterIds, -UsedComponents) | |
72 | % | |
73 | get_ground_truth_from_operation_body(OpReturnVars, OperationBody, ParameterIds, UsedComponents) :- | |
74 | b_get_machine_variables(MachineVars), | |
75 | append(MachineVars, ParameterIds, Vars), | |
76 | get_empty_ground_truth_for_vars([b(identifier(global_ground_truth_vars),_,_),b(identifier(global_ground_truth_params),_,_)|Vars], Acc), | |
77 | retractall(operation_parameter(_)), | |
78 | retractall(explicit_collected_component(_)), | |
79 | findall(_, ( member(b(identifier(Name),_,_), ParameterIds), | |
80 | \+ operation_parameter(Name), | |
81 | assert(operation_parameter(Name)) | |
82 | ), _), | |
83 | get_library_components_from_op_ast_state(OpReturnVars, [], none, [OperationBody], _, Acc, UsedComponents), | |
84 | !, | |
85 | ( Acc == UsedComponents | |
86 | -> | |
87 | add_warning(get_ground_truth_from_operation_body, 'Empty ground truth of B components for operation/event:', [OperationBody]), | |
88 | fail | |
89 | ; true | |
90 | ). | |
91 | ||
92 | wrap_list_if_flat(H, H) :- | |
93 | is_list(H), | |
94 | !. | |
95 | wrap_list_if_flat(H, [H]). | |
96 | ||
97 | split_conjunct_or_disjunct(conjunct(Lhs, Rhs), conjunct, Lhs, Rhs). | |
98 | split_conjunct_or_disjunct(disjunct(Lhs, Rhs), disjunct, Lhs, Rhs). | |
99 | ||
100 | get_parameter_ids_from_list(Ids, PIds) :- | |
101 | findall(Id, ( member(Id, Ids), | |
102 | operation_parameter(Id) | |
103 | ), PIds). | |
104 | ||
105 | % Only keep conjunct/2 and disjunct/2 if necessary for the parameters. | |
106 | get_library_components_for_params_from_pred(b(Node,_,_), Acc, UsedComponents) :- | |
107 | split_conjunct_or_disjunct(Node, Functor, Lhs, Rhs), | |
108 | !, | |
109 | find_identifier_uses(Lhs, [], UsedIdsLhs), | |
110 | find_identifier_uses(Rhs, [], UsedIdsRhs), | |
111 | get_parameter_ids_from_list(UsedIdsLhs, ParamIdsLhs), | |
112 | get_parameter_ids_from_list(UsedIdsRhs, ParamIdsRhs), | |
113 | ( ParamIdsLhs \== [] -> | |
114 | get_library_components_for_params_from_pred(Lhs, Acc, NewAcc1) | |
115 | ; NewAcc1 = Acc | |
116 | ), | |
117 | ( ParamIdsRhs \== [] -> | |
118 | get_library_components_for_params_from_pred(Rhs, NewAcc1, NewAcc2) | |
119 | ; NewAcc2 = NewAcc1 | |
120 | ), | |
121 | ( ( ParamIdsLhs \== [], | |
122 | ParamIdsRhs \== [] | |
123 | ) | |
124 | -> | |
125 | extend_ground_truth_for_vars([], [global_ground_truth_params], [Functor], NewAcc2, UsedComponents) | |
126 | ; UsedComponents = NewAcc2 | |
127 | ), | |
128 | !. | |
129 | get_library_components_for_params_from_pred(b(equal(b(identifier(Name),_,_),Rhs),_,_), Acc, UsedComponents) :- | |
130 | % like p = x + 1 and we only want to collect equal and add for p but not for x | |
131 | operation_parameter(Name), | |
132 | get_library_components_from_op_ast_state([], [equal], none, [Rhs], CarryComponentsAcc, Acc, _), | |
133 | !, | |
134 | extend_ground_truth_for_vars([], [Name], CarryComponentsAcc, Acc, UsedComponents). | |
135 | get_library_components_for_params_from_pred(b(Node,_,_), Acc, UsedComponents) :- | |
136 | Node =.. [Functor|Args], | |
137 | ground_truth_component(Functor), | |
138 | find_identifier_uses(b(Node,_,_), [], UsedIds), | |
139 | findall(Id, ( member(Id, UsedIds), | |
140 | operation_parameter(Id) | |
141 | ), ParamIds), | |
142 | ParamIds \== [], | |
143 | get_library_components_from_op_ast_state([], [], none, Args, CarryComponents, Acc, _), | |
144 | !, | |
145 | extend_ground_truth_for_vars([], [global_ground_truth_params], [Functor|CarryComponents], Acc, UsedComponents). | |
146 | get_library_components_for_params_from_pred(_, Acc, Acc). | |
147 | ||
148 | % Filter used components from avl pairs and convert to tuples. | |
149 | filter_used_components([], Acc, Acc). | |
150 | filter_used_components([Component-Amount|T], Acc, UsedComponentsList) :- | |
151 | Amount \== 0, | |
152 | !, | |
153 | filter_used_components(T, [(Component,Amount)|Acc], UsedComponentsList). | |
154 | filter_used_components([_|T], Acc, UsedComponentsList) :- | |
155 | filter_used_components(T, Acc, UsedComponentsList). | |
156 | ||
157 | %% get_library_components_from_pred_or_expr(+Ast, -UsedComponents). | |
158 | % | |
159 | % Returns the B components used in Ast as a list of tuples ComponentName-ComponentAmount. | |
160 | get_library_components_from_pred_or_expr(Ast, UsedComponentsList) :- | |
161 | % if no assignment is made in Ast, the CarryComponents are the UsedComponents | |
162 | get_empty_ground_truth(EmptyGt), | |
163 | get_library_components_from_op_ast([], [], none, Ast, CarryComponents, [], _), | |
164 | extend_ground_truth_for_vars([], [global], CarryComponents, [(global,EmptyGt)], UsedComponents), | |
165 | member((global,GroundTruth), UsedComponents), | |
166 | avl_to_list(GroundTruth, TempUsedComponentsList), | |
167 | filter_used_components(TempUsedComponentsList, [], UsedComponentsList). | |
168 | ||
169 | % CarryComponents used to collect components for single variables in a machine operation especially | |
170 | % when using nested operators like if-then-else. | |
171 | % Predicate fails if a component from skip_record_for_operator/1 is used. | |
172 | get_library_components_from_op_ast(ExcludeVars, CarryComponents, AssigningVar, In, NewCarryComponentsAcc, Acc, UsedComponents) :- | |
173 | retractall(operation_parameter(_)), | |
174 | retractall(explicit_collected_component(_)), | |
175 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, In, NewCarryComponentsAcc, Acc, UsedComponents). | |
176 | ||
177 | % Note: this predicate is not pure see operation_parameter/1 and explicit_collected_component/1. | |
178 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, In, NewCarryComponentsAcc, Acc, UsedComponents) :- | |
179 | wrap_list_if_flat(In, NIn), | |
180 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, NIn, NewCarryComponentsAcc, Acc, UsedComponents). | |
181 | ||
182 | get_library_components_from_op_ast_state_l(_, CarryComponents, _, [], CarryComponents, Acc, NewAcc) :- | |
183 | !, | |
184 | NewAcc = Acc. | |
185 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [Ast|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
186 | Ast = b(rlevent(_Name,_Section,_Status,_Params,Guard,_Theorems,Actions,_VWitnesses,_PWitnesses,_Unmod,_AbstractEvents),_,_), | |
187 | !, | |
188 | get_library_components_for_params_from_pred(Guard, Acc, NewAcc1), | |
189 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, Actions, _, NewAcc1, NewAcc2), | |
190 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
191 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(Substitution,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
192 | Substitution =.. [Functor,Assignments], | |
193 | ( Functor = parallel | |
194 | ; Functor = sequence | |
195 | ), | |
196 | !, | |
197 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, Assignments, _, Acc, NewAcc), | |
198 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
199 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(Node,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
200 | Node =.. [Functor,_Ids,Pred,Substitutions], | |
201 | ( Functor = let | |
202 | ; Functor = any | |
203 | ), | |
204 | !, | |
205 | get_library_components_for_params_from_pred(Pred, Acc, NewAcc1), | |
206 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, Substitutions, CarryComponentsAcc, NewAcc1, NewAcc2), | |
207 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
208 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(precondition(Pred,Substitution),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
209 | !, | |
210 | get_library_components_for_params_from_pred(Pred, Acc, NewAcc1), | |
211 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, [Substitution], _, NewAcc1, NewAcc2), | |
212 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
213 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(select([Substitution1],Substitution),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
214 | !, | |
215 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, [Substitution,Substitution1], _, Acc, NewAcc), | |
216 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
217 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(select(Substitutions),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
218 | !, | |
219 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, Substitutions, CarryComponentsAcc, Acc, NewAcc), | |
220 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
221 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(select_when(Pred,Substitution),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
222 | !, | |
223 | get_library_components_for_params_from_pred(Pred, Acc, NewAcc1), | |
224 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, [Substitution], CarryComponentsAcc, NewAcc1, NewAcc2), | |
225 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
226 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(if_then_else(Pred,If,Else),subst,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
227 | !, | |
228 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, [Pred], CarryComponentsAccPred, Acc, _), | |
229 | get_library_components_from_op_ast_state(ExcludeVars, [if_then_else|CarryComponentsAccPred], AssigningVar, [If,Else], CarryComponentsAcc, Acc, NewAcc1), | |
230 | extend_ground_truth_for_vars(ExcludeVars, [global_ground_truth_vars], [if_then_else|CarryComponentsAccPred], NewAcc1, NewAcc2), | |
231 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
232 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(if_then_else(Pred,If,Else),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
233 | !, | |
234 | % if_then_else expression | |
235 | get_library_components_from_op_ast_state(ExcludeVars, [if_then_else|CarryComponents], AssigningVar, [Pred,If,Else], CarryComponentsAcc, Acc, NewAcc), | |
236 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
237 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(if(SubstitutionList),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
238 | !, | |
239 | extend_ground_truth_for_vars(ExcludeVars, [global_ground_truth_vars], [if_then_else], Acc, NewAcc1), | |
240 | get_library_components_from_op_ast_state(ExcludeVars, [if_then_else|CarryComponents], AssigningVar, SubstitutionList, _, NewAcc1, NewAcc2), | |
241 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
242 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(if_elsif(Condition,Substitution),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
243 | !, | |
244 | get_library_components_from_op_ast_state(ExcludeVars, [], AssigningVar, [Condition], CarryComponentsAccPred, Acc, _), | |
245 | extend_ground_truth_for_vars(ExcludeVars, [global_ground_truth_vars], [if_then_else|CarryComponentsAccPred], Acc, NewAcc1), | |
246 | get_library_components_from_op_ast_state(ExcludeVars, [if_then_else|CarryComponentsAccPred], AssigningVar, [Substitution], _, NewAcc1, NewAcc2), | |
247 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
248 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(assign(Ids,Assignments),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
249 | !, | |
250 | get_library_components_from_assignments(ExcludeVars, CarryComponents, Ids, Assignments, Acc, NewAcc1), | |
251 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc1, UsedComponents). | |
252 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(identifier(_Name),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
253 | !, | |
254 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, Acc, UsedComponents). | |
255 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(skip,subst,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
256 | !, | |
257 | b_get_machine_variables(MachineVars), | |
258 | findall(N, member(b(identifier(N),_,_), MachineVars), MachineVarNames), | |
259 | extend_ground_truth_for_vars(ExcludeVars, MachineVarNames, [skip|CarryComponents], Acc, NewAcc), | |
260 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
261 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, none, [b(assign_single_id(b(identifier(Name),_,_),b(identifier(Name),_,_)),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
262 | !, | |
263 | extend_ground_truth_for_vars(ExcludeVars, [Name], [skip|CarryComponents], Acc, NewAcc), | |
264 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
265 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, none, [b(assign_single_id(b(identifier(Name),_,_),_),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
266 | % exclude assignments of operation return values | |
267 | member(b(identifier(Name),_,_), ExcludeVars), | |
268 | !, | |
269 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, T, NewCarryComponentsAcc, Acc, UsedComponents). | |
270 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, none, [b(assign_single_id(b(identifier(Name),_,_),Arg),_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
271 | !, | |
272 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, [Arg], CarryComponentsAcc, Acc, NewAcc1), | |
273 | extend_ground_truth_for_vars(ExcludeVars, [Name], CarryComponentsAcc, NewAcc1, NewAcc2), | |
274 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
275 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, none, [b(Node,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
276 | % exclude assignments of operation return values | |
277 | Node =.. [Functor,Ids,_], | |
278 | ( Functor == becomes_element_of | |
279 | ; Functor == becomes_such | |
280 | ), | |
281 | findall(Name, ( member(b(identifier(Name),_,_), Ids), | |
282 | \+ member(b(identifier(Name),_,_), ExcludeVars) | |
283 | ), IdNames), | |
284 | IdNames \== [], | |
285 | !, | |
286 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, T, NewCarryComponentsAcc, Acc, UsedComponents). | |
287 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, none, [b(Node,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
288 | % treat becomes_element_of and becomes_such like assignments when collecting components since the semantics do not matter here | |
289 | Node =.. [Functor,Ids,Arg], | |
290 | ( Functor == becomes_element_of | |
291 | ; Functor == becomes_such | |
292 | ), | |
293 | !, | |
294 | findall(Name, member(b(identifier(Name),_,_), Ids), IdNames), | |
295 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, [Arg], CarryComponentsAcc, Acc, NewAcc1), | |
296 | extend_ground_truth_for_vars(ExcludeVars, IdNames, [becomes_element_of|CarryComponentsAcc], NewAcc1, NewAcc2), | |
297 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, T, NewCarryComponentsAcc, NewAcc2, UsedComponents). | |
298 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [Constant|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
299 | get_constant_component(Constant, ConstantComponent), | |
300 | !, | |
301 | % do not duplicate constant components for the same constant value | |
302 | ( explicit_component_not_collected_yet(Constant) -> | |
303 | CarryComponents2 = [ConstantComponent|CarryComponents] | |
304 | ; CarryComponents2 = CarryComponents | |
305 | ), | |
306 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents2, AssigningVar, T, NewCarryComponentsAcc, Acc, UsedComponents). | |
307 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(Node,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
308 | Node =.. [Component|Args], | |
309 | \+ member(Component, [assign_single_id,assign,choice]), | |
310 | explicit_component_not_collected_yet(b(Node,_,_)), | |
311 | !, | |
312 | get_library_components_from_op_ast_state_l(ExcludeVars, [Component|CarryComponents], AssigningVar, Args, CarryComponentsAcc, Acc, NewAcc), | |
313 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
314 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [List|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
315 | is_list(List), | |
316 | !, | |
317 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, List, CarryComponentsAcc, Acc, NewAcc), | |
318 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponentsAcc, AssigningVar, T, NewCarryComponentsAcc, NewAcc, UsedComponents). | |
319 | get_library_components_from_op_ast_state_l(ExcludeVars, CarryComponents, AssigningVar, [b(Node,_,_)|T], NewCarryComponentsAcc, Acc, UsedComponents) :- | |
320 | ( skip_record_for_operator(Node) -> | |
321 | !, | |
322 | fail | |
323 | ; true | |
324 | ), | |
325 | Node =.. [Functor|_], | |
326 | ( \+ explicit_component_not_collected_yet(b(Node,_,_)) -> | |
327 | true | |
328 | ; add_warning(get_library_components_from_op_ast, 'Warning: Skipped component with functor:', [Functor,Node]) | |
329 | ), | |
330 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, AssigningVar, T, NewCarryComponentsAcc, Acc, UsedComponents). | |
331 | ||
332 | skip_record_for_operator(assign_single_id(b(function(_,_),_,_),_)). | |
333 | skip_record_for_operator(choice(_)). | |
334 | ||
335 | get_library_components_from_assignments(_, _, [], [], Acc, Acc). | |
336 | get_library_components_from_assignments(ExcludeVars, CarryComponents, [Id|TId], [Assignment|TA], Acc, UsedComponents) :- | |
337 | get_library_components_from_op_ast_state(ExcludeVars, CarryComponents, none, [b(assign_single_id(Id,Assignment),subst,[])], _, Acc, NewAcc1), | |
338 | get_library_components_from_assignments(ExcludeVars, CarryComponents, TId, TA, NewAcc1, UsedComponents). | |
339 | ||
340 | explicit_component_not_collected_yet(Ast) :- | |
341 | remove_all_infos(Ast, CleanAst), | |
342 | \+ explicit_collected_component(CleanAst), | |
343 | assert(explicit_collected_component(CleanAst)). | |
344 | ||
345 | extend_ground_truth_for_vars(ExcludeVars, VarNames, Components, Acc, New) :- | |
346 | group_atom_list(Components, GroupedComponents), | |
347 | extend_ground_truth_for_vars_aux1(ExcludeVars, VarNames, GroupedComponents, Acc, New), | |
348 | !. | |
349 | ||
350 | extend_ground_truth_for_vars_aux1(_, [], _, Acc, Acc). | |
351 | extend_ground_truth_for_vars_aux1(ExcludeVars, [VarName|T], GroupedComponents, Acc, New) :- | |
352 | extend_ground_truth_for_vars_aux2(ExcludeVars, VarName, GroupedComponents, Acc, TempNewAcc), | |
353 | extend_ground_truth_for_vars_aux1(ExcludeVars, T, GroupedComponents, TempNewAcc, New). | |
354 | ||
355 | extend_ground_truth_for_vars_aux2(_, VarName, GroupedComponents, Acc, [(VarName,NewVarNameAcc)|TempAcc]) :- | |
356 | select((VarName,VarNameAcc), Acc, TempAcc), | |
357 | !, | |
358 | extend_ground_truth_for_vars_aux3(GroupedComponents, VarNameAcc, NewVarNameAcc). | |
359 | extend_ground_truth_for_vars_aux2(ExcludeVars, VarName, GroupedComponents, Acc, [(VarName,NewVarNameAcc)|Acc]) :- | |
360 | b_get_machine_variables(MachineVars), | |
361 | \+ member(b(identifier(VarName),_,_), MachineVars), | |
362 | \+ member(b(identifier(VarName),_,_), ExcludeVars), | |
363 | % parameter ids have been added to the accumulator beforehand | |
364 | % any missing id at this point that is no operation return variable has been introduced by a let or any | |
365 | % we collect the components used for those ids as well as parameters to insert them later on in case an id is used | |
366 | get_empty_ground_truth(EmptyGt), | |
367 | extend_ground_truth_for_vars_aux3(GroupedComponents, EmptyGt, NewVarNameAcc). | |
368 | extend_ground_truth_for_vars_aux2(_, _, _, Acc, Acc). | |
369 | ||
370 | extend_ground_truth_for_vars_aux3([], VarNameAcc, VarNameAcc). | |
371 | extend_ground_truth_for_vars_aux3([(Component,Amount)|T], VarNameAcc, NewVarNameAcc) :- | |
372 | extend_ground_truth(Component, Amount, VarNameAcc, TempVarNameAcc), | |
373 | extend_ground_truth_for_vars_aux3(T, TempVarNameAcc, NewVarNameAcc). | |
374 | ||
375 | %% extend_ground_truth(+Component, +Amount, +Old, -New). | |
376 | % | |
377 | extend_ground_truth(_, Amount, Acc, Acc) :- | |
378 | Amount =< 0, | |
379 | !. | |
380 | extend_ground_truth(Component, Amount, Old, New) :- | |
381 | extend_ground_truth(Component, Old, Acc), | |
382 | Amount1 is Amount-1, | |
383 | extend_ground_truth(Component, Amount1, Acc, New), | |
384 | !. | |
385 | ||
386 | extend_ground_truth(Component, Old, New) :- | |
387 | avl_member(Component, Old), | |
388 | !, | |
389 | avl_incr(Component, Old, 1, New). | |
390 | extend_ground_truth(Component, Old, Old) :- | |
391 | assert(missing_component(Component)). | |
392 | ||
393 | % TODO: constant_global? | |
394 | % TODO: equal-integer? mehr komponenten? | |
395 | get_constant_component(b(integer_set('INT'),_,_), int_set). | |
396 | get_constant_component(b(integer_set('INTEGER'),_,_), integer_set). | |
397 | get_constant_component(b(integer_set('NAT'),_,_), nat_set). | |
398 | get_constant_component(b(integer_set('NAT1'),_,_), nat1_set). | |
399 | get_constant_component(b(integer_set('NATURAL'),_,_), natural_set). | |
400 | get_constant_component(b(integer_set('NATURAL1'),_,_), natural1_set). | |
401 | get_constant_component(b(bool_set,_,_), constant_set). | |
402 | get_constant_component(b(boolean_true,boolean,_), constant_boolean). | |
403 | get_constant_component(b(boolean_false,boolean,_), constant_boolean). | |
404 | get_constant_component(b(integer(_),integer,_), constant_integer). | |
405 | get_constant_component(b(string(_),string,_), constant_string). | |
406 | get_constant_component(b(value(_),Type,_), ConstantComponent) :- | |
407 | Type =.. [TopLevelType|_], | |
408 | atom_concat(constant_, TopLevelType, ConstantComponent). | |
409 | get_constant_component(b(record_field(_,_),_,_), constant_recordfield). | |
410 | get_constant_component(b(empty_set,_,_), constant_set). | |
411 | get_constant_component(b(set_extension(_),_,_), constant_set). | |
412 | get_constant_component(b(rec(_),_,_), constant_record). | |
413 | get_constant_component(b(Node,_,_), constant_seq) :- | |
414 | ( Node == empty_seq | |
415 | ; Node == empty_sequence | |
416 | ). | |
417 | get_constant_component(b(sequence_extension(_),_,_), constant_seq). | |
418 | ||
419 | ground_truth_component(C) :- | |
420 | b_component(C). | |
421 | ground_truth_component(constant_seq). | |
422 | ground_truth_component(constant_set). | |
423 | ground_truth_component(constant_boolean). | |
424 | ground_truth_component(constant_integer). | |
425 | ground_truth_component(constant_string). | |
426 | ground_truth_component(constant_recordfield). | |
427 | ground_truth_component(constant_record). | |
428 | ||
429 | group_atom_list([], []). | |
430 | group_atom_list([X], [(X,1)]). | |
431 | group_atom_list([X|T], [(X,N1)|Compressed]) :- | |
432 | findall(X, member(X, T), LX), | |
433 | length(LX, N), | |
434 | N1 is N+1, | |
435 | subtract(T, [X], NewT), | |
436 | group_atom_list(NewT, Compressed). |