| 1 | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(b_machine_hierarchy,[analyse_hierarchy/2 | |
| 6 | ,analyse_eventb_hierarchy/2 | |
| 7 | ,main_machine_name/1 % find out name of main machine | |
| 8 | ,machine_name/1 % can be used to find out machine names | |
| 9 | ,machine_type/2 % machine_type(Name, R) - get the type | |
| 10 | % (abstract_machine, abstract_model, refinement, implementation) for the machine named Name | |
| 11 | ,machine_references/2 % get a list of references and the type of the reference. | |
| 12 | % Example: if A refines B, then machine_references('A',X) returns X= [ref(refines'B','')]. | |
| 13 | % The last argument in ref/3 is the prefix | |
| 14 | ,machine_identifiers/7 % gets Params,Sets,Abstract Variables,Concrete Variables,Abstract Constants and Concrete | |
| 15 | % Constants for a machine. Abstract constants/Variables are introduced in the machine | |
| 16 | % using the ABSTRACT_VARAIABLES/ABSTARCT_CONSTANTS keyword. The concrete versions | |
| 17 | % analogously via CONCRETE_VARAIBLES,CONCRETE_CONSTANTS | |
| 18 | % Example output for | |
| 19 | % MACHINE xx(T,u) | |
| 20 | % CONSTRAINTS u:T | |
| 21 | % SETS A; B={foo, bar} | |
| 22 | % CONCRETE_CONSTANTS cc | |
| 23 | % PROPERTIES cc:B | |
| 24 | % ABSTRACT_VARIABLES xx | |
| 25 | % CONCRETE_VARIABLES yy | |
| 26 | % INVARIANT | |
| 27 | % xx:INT & | |
| 28 | % yy : T | |
| 29 | % INITIALISATION xx,yy:= ({1|->2};{2|->4})(1), u | |
| 30 | % END | |
| 31 | % | |
| 32 | % machine_identifiers(A,B,C,D,E,F,G). | |
| 33 | % A = xx, | |
| 34 | % B = [identifier(pos(4,1,1,12,1,12),'T'),identifier(pos(5,1,1,14,1,14),u)], | |
| 35 | % C = [deferred_set(pos(11,1,3,6,3,6),'A'),enumerated_set(pos(12,1,3,9,3,20),'B',[identifier(pos(13,1,3,12,3,14),foo),identifier(pos(14,1,3,17,3,19),bar)])], | |
| 36 | % D = [identifier(pos(22,1,6,20,6,21),xx)], | |
| 37 | % E = [identifier(pos(24,1,7,20,7,21),yy)], | |
| 38 | % F = [], | |
| 39 | % G = [identifier(pos(16,1,4,20,4,21),cc)] | |
| 40 | ,get_machine_identifier_names/7 % a version returning atomic identifier names | |
| 41 | ,machine_has_constants/1 % check if machine has some constants | |
| 42 | ,get_machine_short_desc/2 | |
| 43 | ,abstract_constant/2, concrete_constant/2 | |
| 44 | ,possibly_abstract_constant/1 | |
| 45 | ,machine_operations/2 % machine_operations(M, Ops) gets the names of the Operations defined in Machine M | |
| 46 | ,machine_operation_calls/2 | |
| 47 | ,machine_hash/2 % stores a hash value for the machine | |
| 48 | ,properties_hash/2 % computes a hash over the constants and properties | |
| 49 | ,operation_hash/3 % computes a hash for the operation in a machine | |
| 50 | ,write_dot_hierarchy_to_file/1 | |
| 51 | ,write_dot_op_hierarchy_to_file/1 | |
| 52 | ,write_dot_event_hierarchy_to_file/1 | |
| 53 | ,write_dot_variable_hierarchy_to_file/1 | |
| 54 | ,get_machine_inclusion_graph/3 | |
| 55 | ,get_machine_topological_order/1 | |
| 56 | ,print_machine_topological_order/0 | |
| 57 | ]). | |
| 58 | ||
| 59 | :- use_module(library(lists)). | |
| 60 | :- use_module(library(ordsets)). | |
| 61 | :- use_module(bmachine,[b_get_definition/5, get_machine_file_number/4, b_get_relative_file_name_for_machine/2]). | |
| 62 | :- use_module(bmachine_construction). | |
| 63 | :- use_module(debug). | |
| 64 | :- use_module(self_check). | |
| 65 | :- use_module(specfile,[get_specification_description/2]). | |
| 66 | :- use_module(extension('probhash/probhash'),[raw_sha_hash/2]). | |
| 67 | :- use_module(input_syntax_tree). | |
| 68 | :- use_module(dotsrc(dot_graph_generator), [gen_dot_graph/6, dot_no_same_rank/1, | |
| 69 | use_new_dot_attr_pred/7, get_dot_cluster_name/2]). | |
| 70 | ||
| 71 | :- use_module(value_persistance,[cache_is_activated/0]). | |
| 72 | :- use_module(probsrc(tools),[split_list/4]). | |
| 73 | ||
| 74 | :- use_module(module_information,[module_info/2]). | |
| 75 | :- module_info(group,ast). | |
| 76 | :- module_info(description,'This module provides functionality to visualize the dependencies of a B machine (include and sees relations, etc.).'). | |
| 77 | ||
| 78 | :- volatile | |
| 79 | main_machine_name/1, | |
| 80 | machine_type/2, | |
| 81 | machine_package_directory/2, | |
| 82 | machine_references/2, | |
| 83 | machine_identifiers/7, | |
| 84 | machine_operations/2, | |
| 85 | machine_operation_calls/2, | |
| 86 | machine_values_identifiers/2, | |
| 87 | refines_event/4, | |
| 88 | machine_has_assertions/1, | |
| 89 | raw_machine/2, | |
| 90 | machine_hash_cached/2, | |
| 91 | properties_hash_cached/2, operation_hash_cached/3, | |
| 92 | basic_operation_hash/4, | |
| 93 | event_refinement_change/6. | |
| 94 | :- dynamic | |
| 95 | main_machine_name/1, | |
| 96 | machine_type/2, | |
| 97 | machine_package_directory/2, | |
| 98 | machine_references/2, | |
| 99 | machine_identifiers/7, | |
| 100 | machine_operations/2, | |
| 101 | machine_operation_calls/2, | |
| 102 | machine_values_identifiers/2, | |
| 103 | refines_event/4, | |
| 104 | machine_has_assertions/1, | |
| 105 | raw_machine/2, | |
| 106 | machine_hash_cached/2, | |
| 107 | properties_hash_cached/2, operation_hash_cached/3, | |
| 108 | basic_operation_hash/4, | |
| 109 | event_refinement_change/6. | |
| 110 | :- volatile abstract_constant/2, concrete_constant/2. | |
| 111 | :- dynamic abstract_constant/2. | |
| 112 | :- dynamic concrete_constant/2. | |
| 113 | ||
| 114 | :- use_module(specfile,[animation_minor_mode/1]). | |
| 115 | possibly_abstract_constant(ID) :- | |
| 116 | (abstract_constant(ID,_) ; animation_minor_mode(eventb),concrete_constant(ID,_) ). | |
| 117 | ||
| 118 | reset_hierarchy :- | |
| 119 | retract_all(main_machine_name/1), | |
| 120 | retract_all(machine_type/2), | |
| 121 | retract_all(machine_package_directory/2), | |
| 122 | retract_all(machine_references/2), | |
| 123 | retract_all(machine_identifiers/7), | |
| 124 | retract_all(abstract_constant/2), | |
| 125 | retract_all(concrete_constant/2), | |
| 126 | retract_all(machine_operations/2), | |
| 127 | retract_all(machine_operation_calls/2), | |
| 128 | retract_all(machine_values_identifiers/2), | |
| 129 | retract_all(refines_event/4), | |
| 130 | retract_all(machine_has_assertions/1), | |
| 131 | retract_all(machine_hash_cached/2), | |
| 132 | retract_all(raw_machine/2), | |
| 133 | retract_all(properties_hash_cached/2), | |
| 134 | retract_all(operation_hash_cached/3), | |
| 135 | retract_all(basic_operation_hash/4), | |
| 136 | retract_all(event_refinement_change/6). | |
| 137 | ||
| 138 | machine_name(Name) :- machine_type(Name,_). | |
| 139 | ||
| 140 | :- use_module(eventhandling,[register_event_listener/3]). | |
| 141 | :- register_event_listener(clear_specification,reset_hierarchy, | |
| 142 | 'Reset B Machine Hierarchy Facts.'). | |
| 143 | ||
| 144 | retract_all(Functor/Arity) :- | |
| 145 | functor(Pattern,Functor,Arity), | |
| 146 | retractall(Pattern). | |
| 147 | ||
| 148 | analyse_hierarchy(Main,Machines) :- (var(Main) ; var(Machines)),!, | |
| 149 | add_internal_error('Illegal call:',analyse_hierarchy(Main,Machines)). | |
| 150 | analyse_hierarchy(Main,Machines) :- | |
| 151 | reset_hierarchy, | |
| 152 | assertz(main_machine_name(Main)), | |
| 153 | analyse_machine(Main,Machines,main). | |
| 154 | ||
| 155 | :- use_module(error_manager). | |
| 156 | :- use_module(tools_strings,[ajoin/2]). | |
| 157 | :- use_module(bmachine,[b_filenumber/4]). | |
| 158 | :- public analyse_machine/3. | |
| 159 | analyse_machine(Name,_Machines,_) :- | |
| 160 | % machine already analysed | |
| 161 | machine_type(Name,_),!. | |
| 162 | analyse_machine(Name,Machines,_) :- | |
| 163 | debug:debug_println(19,analysing_machine(Name)), | |
| 164 | get_machine(Name,Machines,Type,Header,Refines,Body), | |
| 165 | !, | |
| 166 | ( cache_is_activated -> % we need the machines stored for later analysis | |
| 167 | assert_all_machines(Machines) | |
| 168 | ; true), | |
| 169 | assertz(machine_type(Name,Type)), | |
| 170 | (get_machine_parent_directory(Name,Dir) -> assertz(machine_package_directory(Name,Dir)) ; true), | |
| 171 | ( get_raw_section(assertions,Body,_) -> | |
| 172 | assertz(machine_has_assertions(Name)) | |
| 173 | ; true), | |
| 174 | store_identifiers(Name,Header,Body), | |
| 175 | store_operations(Name,Body), | |
| 176 | store_values(Name,Body), | |
| 177 | store_references(Name,Refines,Body,Machines). | |
| 178 | analyse_machine(Name,Machines,RefType) :- | |
| 179 | get_machine_file_number(Name,_Ext,Nr,File), | |
| 180 | get_ref_type_name(RefType,Clause), | |
| 181 | !, | |
| 182 | ( member(M,Machines),get_constructed_machine_name_and_filenumber(M,OtherName,Nr) | |
| 183 | -> ajoin(['Cannot use B machine "',Name,'" within ', Clause, | |
| 184 | ' clause. Rename machine "', OtherName,'" to "', Name, '" in file: '],Msg) | |
| 185 | ; findall(ModuleName,b_filenumber(ModuleName,_,_,_),List), | |
| 186 | ajoin(['Cannot find B machine "',Name,'" within ', Clause, | |
| 187 | ' clause (available: ',List,'). Check that machine name matches filename in: '],Msg) | |
| 188 | ), | |
| 189 | add_error_fail(invalid_machine_reference,Msg,File). | |
| 190 | analyse_machine(Name,_Machines,_RefType) :- | |
| 191 | bmachine:portray_filenumbers, | |
| 192 | add_error_fail(invalid_machine_reference, | |
| 193 | 'Could not find machine in parsed machine list (check that your machine names match your filenames): ',Name). | |
| 194 | ||
| 195 | :- use_module(tools,[get_parent_directory_name/2]). | |
| 196 | get_machine_parent_directory(Name,DirName) :- | |
| 197 | % try and get parent directory name; useful to distinguish different packages when using package pragma | |
| 198 | get_machine_file_number(Name,_Ext,_Nr,File), | |
| 199 | get_parent_directory_name(File,DirName). | |
| 200 | ||
| 201 | ||
| 202 | % store the un-typed input syntax tree for later analysis | |
| 203 | assert_all_machines(Machines) :- | |
| 204 | (raw_machine(_,_) | |
| 205 | -> true % machines already asserted | |
| 206 | ; maplist(assert_raw_machine,Machines)). | |
| 207 | assert_raw_machine(Machine) :- | |
| 208 | get_raw_machine_name(Machine,Name), | |
| 209 | (raw_machine(Name,_) -> add_warning(b_machine_hierarchy,'Raw machine already exists: ',Name) ; true), | |
| 210 | assertz( raw_machine(Name,Machine) ). | |
| 211 | ||
| 212 | machine_has_constants(MachName) :- | |
| 213 | machine_identifiers(MachName,_,_,_,_,AConsts,CConsts), | |
| 214 | (AConsts=[] -> CConsts = [_|_] ; true). | |
| 215 | ||
| 216 | get_machine_short_desc(MachName,Text) :- | |
| 217 | machine_identifiers(MachName,Params,Sets,AVars,CVars,AConsts,CConsts), | |
| 218 | length(Params,PL), length(Sets,SL), length(AVars,AVL), length(CVars,CVL), VL is AVL+CVL, | |
| 219 | length(AConsts,ACL), length(CConsts,CCL), CL is ACL+CCL, | |
| 220 | (machine_operations(MachName,Ops), length(Ops,OL) -> true ; OL=0), | |
| 221 | ajoin(['SETS:',SL,' CONSTANTS:',CL,' VARIABLES:',VL, ' PARAMETERS:',PL, ' OPERATIONS:',OL],Text). | |
| 222 | ||
| 223 | store_identifiers(Name,Header,Body) :- | |
| 224 | get_parameters(Header,Params), | |
| 225 | get_sets(Body,Sets), | |
| 226 | get_identifiers([abstract_variables,variables],Body,AVars), | |
| 227 | get_identifiers([concrete_variables],Body,CVars), | |
| 228 | get_identifiers([abstract_constants],Body,AConsts), | |
| 229 | get_identifiers([concrete_constants,constants],Body,CConsts), % fixed abstract -> concrete | |
| 230 | assertz(machine_identifiers(Name,Params,Sets,AVars,CVars,AConsts,CConsts)), | |
| 231 | maplist(assert_raw_id_with_position(abstract_constant),AConsts), | |
| 232 | maplist(assert_raw_id_with_position(concrete_constant),CConsts). | |
| 233 | ||
| 234 | ||
| 235 | raw_id_is_identifier2(description(_Pos,_Desc,Raw),ID) :- !, raw_id_is_identifier2(Raw,ID). | |
| 236 | raw_id_is_identifier2(deferred_set(_,ID),ID) :- !. | |
| 237 | raw_id_is_identifier2(enumerated_set(_,ID,_Elements),ID) :- !. | |
| 238 | raw_id_is_identifier2(Raw,ID) :- raw_id_is_identifier(Raw,_,ID). | |
| 239 | ||
| 240 | get_machine_identifier_names(Name,Params,Sets,AVars,CVars,AConsts,CConsts) :- | |
| 241 | machine_identifiers(Name,RawParams,RawSets,RawAVars,RawCVars,RawAConsts,RawCConsts), | |
| 242 | (maplist(raw_id_is_identifier2,RawParams,Params), | |
| 243 | maplist(raw_id_is_identifier2,RawSets,Sets), | |
| 244 | maplist(raw_id_is_identifier2,RawAVars,AVars), | |
| 245 | maplist(raw_id_is_identifier2,RawCVars,CVars), | |
| 246 | maplist(raw_id_is_identifier2,RawAConsts,AConsts), | |
| 247 | maplist(raw_id_is_identifier2,RawCConsts,CConsts) -> true | |
| 248 | ; add_internal_error('Could not extract raw identifiers:',Name)). | |
| 249 | ||
| 250 | ||
| 251 | machine_hash(Name,Hash) :- | |
| 252 | machine_hash_cached(Name,Hash1),!,Hash=Hash1. | |
| 253 | machine_hash(Name,Hash) :- | |
| 254 | compute_machine_hash(Name,Hash1), | |
| 255 | assertz( machine_hash_cached(Name,Hash1) ), | |
| 256 | Hash=Hash1. | |
| 257 | compute_machine_hash(Name,Digest) :- | |
| 258 | if(raw_machine(Name,Machine), | |
| 259 | raw_sha_hash(Machine,Digest), | |
| 260 | add_error_and_fail(compute_machine_hash,'Machine does not exist or has not been processed:',Name) | |
| 261 | ). | |
| 262 | ||
| 263 | :- use_module(pathes_extensions_db, [compile_time_unavailable_extension/2]). | |
| 264 | :- if(\+ compile_time_unavailable_extension(probhash_extension, _)). | |
| 265 | store_eventb_hash(Name,ContextMachTerm) :- | |
| 266 | raw_sha_hash(ContextMachTerm,Digest), | |
| 267 | assertz(machine_hash_cached(Name,Digest)). | |
| 268 | :- else. | |
| 269 | store_eventb_hash(Name,_) :- | |
| 270 | assertz( (machine_hash_cached(Name,_) :- | |
| 271 | add_error(b_machine_hierarchy,'prob_hash_extension not available for: ',Name),fail) ). | |
| 272 | :- endif. | |
| 273 | ||
| 274 | :- use_module(tools_strings,[get_hex_bytes/2]). | |
| 275 | operation_hash(MachName,OpName,Hash) :- | |
| 276 | operation_hash_cached(MachName,OpName,Hash1),!,Hash=Hash1. | |
| 277 | operation_hash(MachName,OpName,Hash) :- | |
| 278 | computed_basic_operation_hashes, | |
| 279 | basic_operation_hash(OpName,MachName,Hash1,OpCalls), | |
| 280 | (OpCalls = [] | |
| 281 | -> FinalHash=Hash1 % no recursive call of other operations | |
| 282 | ; transitive_op_calls(OpCalls,[],AllOpCalls), % called operations can themselves call operations | |
| 283 | maplist(get_basic_op_hash,AllOpCalls,OpDigests), | |
| 284 | raw_sha_hash(op(Hash1,OpDigests),FinalHash) % also include hash of called operations | |
| 285 | ), | |
| 286 | assertz( operation_hash_cached(MachName,OpName,FinalHash) ), | |
| 287 | get_hex_bytes(FinalHash,Hex), | |
| 288 | debug_format(19,'value caching: operation ~w in machine ~w has hash: ~s~n',[OpName,MachName,Hex]), | |
| 289 | Hash=FinalHash. | |
| 290 | ||
| 291 | get_basic_op_hash(OpName,Digest) :- | |
| 292 | basic_operation_hash_of_call(OpName,Digest,_OpCalls). | |
| 293 | ||
| 294 | % look up the basic hash of a called operation; the call may use a renaming prefix, e.g., i.op for INCLUDES i.M | |
| 295 | basic_operation_hash_of_call(OpName,Digest,OpCalls) :- | |
| 296 | basic_operation_hash(OpName,_MachName,Digest1,OpCalls1),!, | |
| 297 | Digest=Digest1, OpCalls=OpCalls1. | |
| 298 | basic_operation_hash_of_call(PrefixedOpName,Digest,OpCalls) :- | |
| 299 | atom(PrefixedOpName), | |
| 300 | sub_atom(PrefixedOpName,Before,1,After,'.'), | |
| 301 | \+ (sub_atom(PrefixedOpName,Before2,1,_,'.'), Before2 > Before), % split at the last dot | |
| 302 | !, | |
| 303 | sub_atom(PrefixedOpName,_,After,0,OpName), | |
| 304 | basic_operation_hash(OpName,_MachName,Digest1,OpCalls1),!, | |
| 305 | Digest=Digest1, OpCalls=OpCalls1. | |
| 306 | ||
| 307 | % compute the sorted list of all directly or indirectly called operations | |
| 308 | % for operations calling only operations without calls this is the sorted list of direct calls | |
| 309 | transitive_op_calls([],Acc,Acc). | |
| 310 | transitive_op_calls([OpName|T],Acc,Res) :- | |
| 311 | (ord_member(OpName,Acc) -> transitive_op_calls(T,Acc,Res) | |
| 312 | ; ord_add_element(Acc,OpName,Acc1), | |
| 313 | (basic_operation_hash_of_call(OpName,_,SubCalls) -> append(SubCalls,T,T1) ; T1=T), | |
| 314 | transitive_op_calls(T1,Acc1,Res) | |
| 315 | ). | |
| 316 | ||
| 317 | % compute basic hash, without taking recursive operation calls into account | |
| 318 | computed_basic_operation_hashes :- | |
| 319 | basic_operation_hash(_,_,_,_),!. % already computed | |
| 320 | computed_basic_operation_hashes :- | |
| 321 | get_raw_machine_operation_hash(MachName,OpName,Digest,OpCalls), | |
| 322 | (basic_operation_hash(OpName,_,_,_) | |
| 323 | -> add_warning(computed_basic_operation_hashes,'Multiple hashes for operation: ',OpName) | |
| 324 | ; true), | |
| 325 | assertz(basic_operation_hash(OpName,MachName,Digest,OpCalls)), | |
| 326 | get_hex_bytes(Digest,Hex), | |
| 327 | debug_format(19,'value caching: basic operation hash ~w in machine ~w: ~s (calls ~w)~n', | |
| 328 | [OpName,MachName,Hex,OpCalls]), | |
| 329 | fail. | |
| 330 | computed_basic_operation_hashes. | |
| 331 | ||
| 332 | % first computed basic, independent operation hashes: | |
| 333 | % only look up used definitions, but not yet called operations | |
| 334 | get_raw_machine_operation_hash(MachName,OpName,Digest,SOpCalls) :- | |
| 335 | raw_machine(MachName,Machine), | |
| 336 | get_machine(MachName,[Machine],_Type,_Header,_Refines,MachBody), | |
| 337 | get_opt_section(operations,MachBody,Operations), | |
| 338 | Op = operation(_,identifier(_,OpName),_,_,_), | |
| 339 | member(Op,Operations), | |
| 340 | get_raw_operation_id_and_body(Op,identifier(_,OpName),OpBody), | |
| 341 | extract_used_np_definitions(Op,MachBody,UsedDefinitions,DefsWithPos), | |
| 342 | remove_raw_position_info(Op,RawOperation), | |
| 343 | raw_sha_hash(op(RawOperation,UsedDefinitions),Digest), | |
| 344 | findall(Id, (get_raw_operation_call_id(OpBody,Id) % extract op calls in body of operation | |
| 345 | ; get_def_body(Def,DefsWithPos), get_raw_operation_call_id(Def,Id) % extract op calls in definitions | |
| 346 | ), | |
| 347 | OpCalls), | |
| 348 | sort(OpCalls,SOpCalls). | |
| 349 | ||
| 350 | get_def_body(Body,Defs) :- member(definition(_,_DefName,_,Body),Defs). | |
| 351 | ||
| 352 | ||
| 353 | :- use_module(debug,[debug_println/2]). | |
| 354 | assert_raw_id_with_position(PredFunctor,Rid) :- | |
| 355 | (raw_id_is_identifier(Rid,Pos,ID) | |
| 356 | -> true | |
| 357 | ; peel_desc(Rid,PRid), PRid = definition(Pos,ID,_) | |
| 358 | -> add_error(illegal_definition_use,'Definition cannot be used as identifier here: ',ID,Pos) | |
| 359 | ; add_error_fail(assert_raw_id_with_position,'Not identifier: ',Rid) | |
| 360 | ), | |
| 361 | Fact =.. [PredFunctor,ID,Pos], | |
| 362 | assertz(Fact), debug_println(9,Fact). | |
| 363 | ||
| 364 | raw_id_is_identifier(identifier(Pos,ID),Pos,ID). | |
| 365 | raw_id_is_identifier(unit(_,_,identifier(Pos,ID)),Pos,ID). | |
| 366 | raw_id_is_identifier(new_unit(_,_,identifier(Pos,ID)),Pos,ID). | |
| 367 | raw_id_is_identifier(inferred_unit(_,_,identifier(Pos,ID)),Pos,ID). | |
| 368 | raw_id_is_identifier(inferredunit(_,_,identifier(Pos,ID)),Pos,ID). % the (new?) parser seems to generate the wrong pragma in the .prob file; TO DO: investigate | |
| 369 | raw_id_is_identifier(description(_,_,RawID),Pos,ID) :- | |
| 370 | raw_id_is_identifier(RawID,Pos,ID). | |
| 371 | ||
| 372 | peel_desc(description(_,_,E),R) :- !, peel_desc(E,R). | |
| 373 | peel_desc(R,R). | |
| 374 | ||
| 375 | get_raw_identifier(Raw,Res) :- raw_id_is_identifier(Raw,_Pos,Id),!, Res=Id. | |
| 376 | get_raw_identifier(definition(_Pos,DID,[]),ID) :- % see also expand_definition_to_variable_list | |
| 377 | atom(DID),!, | |
| 378 | ajoin([DID,'(DEFINITION)'],ID). | |
| 379 | get_raw_identifier(deferred_set(_Pos,DID),ID) :- atom(DID),!, ID=DID. | |
| 380 | get_raw_identifier(enumerated_set(_Pos,DID,_List),ID) :- atom(DID),!, ID=DID. | |
| 381 | get_raw_identifier(description(_,_,RawID),ID) :- !, | |
| 382 | get_raw_identifier(RawID,ID). | |
| 383 | get_raw_identifier(Raw,Res) :- add_internal_error('Cannot get identifier:',Raw),Res='???'. | |
| 384 | ||
| 385 | raw_identifier_member(ID,List) :- member(Raw,List), raw_id_is_identifier(Raw,_Pos,ID). | |
| 386 | ||
| 387 | store_operations(MachName,Body) :- | |
| 388 | get_opt_section(operations,Body,Operations), | |
| 389 | findall(I,(member(Op,Operations),get_raw_operation_id_and_body(Op,I,_)),Ids), | |
| 390 | assertz(machine_operations(MachName,Ids)), | |
| 391 | findall(calls(I1,I2),(member(Op,Operations),get_raw_operation_call(Op,I1,I2)),Calls), | |
| 392 | sort(Calls,SCalls), | |
| 393 | debug_println(9,machine_operation_calls(MachName,SCalls)), | |
| 394 | assertz(machine_operation_calls(MachName,SCalls)). | |
| 395 | ||
| 396 | get_raw_operation_id_and_body(operation(_Pos,Id,_,_,Body),Id,Body). | |
| 397 | get_raw_operation_id_and_body(refined_operation(_Pos,Id,_Results,_Args,_RefinesID,Body),Id,Body). | |
| 398 | get_raw_operation_id_and_body(description_operation(_Pos,_,Op),Id,Body) :- get_raw_operation_id_and_body(Op,Id,Body). | |
| 399 | ||
| 400 | % get operations called in body | |
| 401 | get_raw_operation_call(Op,Id,CallsId) :- | |
| 402 | get_raw_operation_id_and_body(Op,identifier(_,Id),Body), | |
| 403 | get_raw_operation_call(Body,identifier(_,CallsId)). | |
| 404 | ||
| 405 | :- use_module(debug,[debug_format/3]). | |
| 406 | get_raw_operation_call(block(_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 407 | get_raw_operation_call(precondition(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 408 | get_raw_operation_call(assertion(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 409 | get_raw_operation_call(witness_then(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 410 | get_raw_operation_call(var(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 411 | get_raw_operation_call(select_when(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 412 | get_raw_operation_call(if_elsif(_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 413 | get_raw_operation_call(let(_,_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 414 | get_raw_operation_call(any(_,_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 415 | get_raw_operation_call(case(_,_,_,_,_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 416 | get_raw_operation_call(while(_,_Cond,Body,_,_),ID) :- !, get_raw_operation_call(Body,ID). | |
| 417 | get_raw_operation_call(parallel(_,List),ID) :- !,member(A,List), get_raw_operation_call(A,ID). | |
| 418 | get_raw_operation_call(sequence(_,List),ID) :- !,member(A,List), get_raw_operation_call(A,ID). | |
| 419 | get_raw_operation_call(if(_,_Test,Then,List,Else),ID) :- !,member(A,[Then,Else|List]), get_raw_operation_call(A,ID). | |
| 420 | get_raw_operation_call(select(_,_Cond,Body,List),ID) :- !,member(A,[Body|List]), get_raw_operation_call(A,ID). | |
| 421 | get_raw_operation_call(select(_,_Cond,Body,List,Else),ID) :- !,member(A,[Body,Else|List]), get_raw_operation_call(A,ID). | |
| 422 | get_raw_operation_call(choice(_,List),ID) :- !,member(A,List), get_raw_operation_call(A,ID). | |
| 423 | get_raw_operation_call(choice_or(_,Body),ID) :- !, get_raw_operation_call(Body,ID). | |
| 424 | get_raw_operation_call(operation_call(_,ID,_,_),ID) :- !. | |
| 425 | get_raw_operation_call(skip(_),_) :- !, fail. | |
| 426 | get_raw_operation_call(assign(_,_,_),_) :- !, fail. | |
| 427 | get_raw_operation_call(becomes_element_of(_,_,_),_) :- !, fail. | |
| 428 | get_raw_operation_call(becomes_such(_,_,_),_) :- !, fail. | |
| 429 | get_raw_operation_call(definition(_,Name,_),_) :- !, % TO DO: improve | |
| 430 | debug_format(19,'Ignoring operation calls in DEFINITION ~w for operation call diagram~n',[Name]), | |
| 431 | % b_get_definition(Name,_DefType,_Args,DefBody,_Deps), not yet precompiled ! | |
| 432 | fail. | |
| 433 | get_raw_operation_call(Subst,_) :- functor(Subst,F,N),print(uncovered_subst(F,N,Subst)),nl,fail. | |
| 434 | % we also do not find operation calls in expressions | |
| 435 | ||
| 436 | :- use_module(input_syntax_tree,[raw_operator_term/1, raw_literal_term/1, raw_special_set_term/1]). | |
| 437 | % avoid spurious uncovered_subst messages in a context where we are not sure we have a subst | |
| 438 | try_get_raw_operation_call(Term,_) :- raw_literal_term(Term),!, fail. | |
| 439 | try_get_raw_operation_call(Term,_) :- raw_operator_term(Term),!, fail. | |
| 440 | try_get_raw_operation_call(Term,_) :- raw_special_set_term(Term),!, fail. | |
| 441 | try_get_raw_operation_call(Term,_) :- functor(Term,F,_), definitely_not_subst(F),!, fail. | |
| 442 | try_get_raw_operation_call(Body,ID) :- get_raw_operation_call(Body,ID). | |
| 443 | ||
| 444 | % definitely expression: | |
| 445 | definitely_not_subst(let_expression). | |
| 446 | definitely_not_subst(let_predicate). | |
| 447 | % definitely predicate: | |
| 448 | definitely_not_subst(external_function_call). | |
| 449 | definitely_not_subst(external_pred_call). | |
| 450 | ||
| 451 | get_raw_operation_call_id(OpBody,CalledId) :- | |
| 452 | try_get_raw_operation_call(OpBody,identifier(_,CalledId)). | |
| 453 | ||
| 454 | store_references(Name,Refines,Body,Machines) :- | |
| 455 | get_refinements(Refines,Refs1), | |
| 456 | get_references(Body,Refs2), | |
| 457 | append(Refs1,Refs2,Refs), | |
| 458 | assertz(machine_references(Name,Refs)), | |
| 459 | follow_refs(Refs,Machines). | |
| 460 | ||
| 461 | get_references(Body,Refs) :- | |
| 462 | findrefs(Body,includes,Includes), | |
| 463 | findrefs(Body,extends,Extends), | |
| 464 | findrefs(Body,imports,Imports), | |
| 465 | findusessees(Body,uses,Uses), | |
| 466 | findusessees(Body,sees,Sees), | |
| 467 | append([Includes,Imports,Extends,Uses,Sees],Refs). | |
| 468 | ||
| 469 | get_refinements([],[]). | |
| 470 | get_refinements([Name|NRest],[ref(refines,Name,'')|RRest]) :- | |
| 471 | get_refinements(NRest,RRest). | |
| 472 | ||
| 473 | findrefs(Body,Type,Refs) :- | |
| 474 | get_opt_section(Type,Body,RawRefs), | |
| 475 | findrefs2(RawRefs,Type,Refs). | |
| 476 | findrefs2([],_Type,[]). | |
| 477 | findrefs2([MID|MRest],Type,[ref(Type,Name,Prefix)|RRest]) :- | |
| 478 | get_machine_id(MID,FullName), % should be machine_reference | |
| 479 | bmachine_construction:split_prefix(FullName,Prefix,Name), | |
| 480 | findrefs2(MRest,Type,RRest). | |
| 481 | ||
| 482 | findusessees(Body,Type,Refs) :- | |
| 483 | get_opt_section(Type,Body,RawRefs), | |
| 484 | findusessees2(RawRefs,Type,Refs). | |
| 485 | findusessees2([],_Type,[]). | |
| 486 | findusessees2([MID|MRest],Type,[ref(Type,Name,Prefix)|RRest]) :- | |
| 487 | get_machine_id(MID,FullName), % should be machine_reference | |
| 488 | % USE and SEES do not have machine_reference in AST but identifier | |
| 489 | bmachine_construction:split_prefix(FullName,Prefix,Name), | |
| 490 | findusessees2(MRest,Type,RRest). | |
| 491 | ||
| 492 | get_machine_id(identifier(_Pos,FN),FullName) :- !, FullName=FN. | |
| 493 | get_machine_id(machine_reference(_Pos,FN,_Params),FullName) :- !, FullName=FN. | |
| 494 | get_machine_id(Other,_) :- add_internal_error('Illegal machine id: ',get_machine_id(Other,_)),fail. | |
| 495 | ||
| 496 | ||
| 497 | follow_refs([],_Machines). | |
| 498 | follow_refs([ref(RefType,Name,_Prefix)|Rest],Machines) :- | |
| 499 | analyse_machine(Name,Machines,RefType), | |
| 500 | follow_refs(Rest,Machines). | |
| 501 | ||
| 502 | get_parameters(machine_header(_Pos,_Name,Params),Params). | |
| 503 | ||
| 504 | get_sets(Body,Sets) :- | |
| 505 | get_opt_section(sets,Body,Sets). | |
| 506 | ||
| 507 | get_identifiers([],_Body,[]). | |
| 508 | get_identifiers([Sec|Rest],Body,Ids) :- | |
| 509 | get_opt_section(Sec,Body,Ids1), | |
| 510 | append(Ids1,IRest,Ids), | |
| 511 | get_identifiers(Rest,Body,IRest). | |
| 512 | ||
| 513 | get_opt_sections([],_Body,[]). | |
| 514 | get_opt_sections([S|Srest],Body,Contents) :- | |
| 515 | get_opt_section(S,Body,L),append(L,Rest,Contents), | |
| 516 | get_opt_sections(Srest,Body,Rest). | |
| 517 | ||
| 518 | get_opt_section(Sec,Body,Result) :- | |
| 519 | ( get_raw_section(Sec,Body,Content) -> Result=Content; Result=[]). | |
| 520 | get_raw_section(Sec,Body,Content) :- % look for Sec(_Pos,Content) in Body list | |
| 521 | functor(Pattern,Sec,2),arg(2,Pattern,Content), | |
| 522 | memberchk(Pattern,Body). | |
| 523 | ||
| 524 | get_machine(Name,Machines,Type,Header,Refines,Body) :- | |
| 525 | get_machine1(Name,Machines,_Machine,Type,Header,Refines,Body). | |
| 526 | %get_raw_machine(Name,Machines,Machine) :- | |
| 527 | % get_machine1(Name,Machines,Machine,_Type,_Header,_Refines,_Body). | |
| 528 | get_machine1(Name,Machines,Machine,Type,Header,Refines,Body) :- | |
| 529 | Header = machine_header(_Pos,Name,_Params), | |
| 530 | member(Machine,Machines), | |
| 531 | get_machine2(Machine,Type,Header,Refines,Body),!. | |
| 532 | get_machine2(abstract_machine(_Pos,MS,Header,Body),TypeOfAbstractMachine,RHeader,Refines,RBody) :- !, | |
| 533 | Refines = [], RBody = Body, RHeader=Header, | |
| 534 | get_abstract_machine_type(MS,TypeOfAbstractMachine). | |
| 535 | get_machine2(refinement_machine(_Pos,Header,Refines,Body),refinement,RHeader,RRefines,RBody) :- !, | |
| 536 | RRefines = [Refines], RBody = Body, RHeader=Header. | |
| 537 | get_machine2(implementation_machine(_Pos,Header,Refines,Body),implementation,RHeader,RRefines,RBody) :- !, | |
| 538 | RRefines = [Refines], RBody = Body, RHeader=Header. | |
| 539 | get_machine2(generated(_Pos,Machine),A,B,C,D) :- !, % @generated Pragma used at top of file | |
| 540 | get_machine2(Machine,A,B,C,D). | |
| 541 | get_machine2(unit_alias(_Pos,_Name,_Alias,Machine),A,B,C,D) :- !, | |
| 542 | get_machine2(Machine,A,B,C,D). | |
| 543 | get_machine2(Other,A,B,C,D) :- | |
| 544 | add_internal_error('Illegal machine:',get_machine2(Other,A,B,C,D)), fail. | |
| 545 | ||
| 546 | get_raw_machine_name(Machine,Name) :- | |
| 547 | get_machine2(Machine,_,machine_header(_,Name,_),_,_). | |
| 548 | ||
| 549 | get_abstract_machine_type(machine(_Pos2),R) :- !,R=abstract_machine. | |
| 550 | get_abstract_machine_type(system(_Pos2),R) :- !,R=abstract_machine. | |
| 551 | get_abstract_machine_type(model(_Pos2),R) :- !,R=abstract_model. | |
| 552 | get_abstract_machine_type(X,R) :- atomic(X),!, | |
| 553 | add_error(get_abstract_machine_type,'Your parser seems out-dated. Assuming abstract_machine: ',X), | |
| 554 | R=abstract_machine. | |
| 555 | ||
| 556 | ||
| 557 | get_values_id(values_entry(Pos,ID,_Val),identifier(Pos,ID)). | |
| 558 | % Store the identifiers assigned to in VALUES clauses | |
| 559 | store_values(Name,Body) :- | |
| 560 | get_opt_section('values',Body,Values),!, | |
| 561 | maplist(get_values_id,Values,ValuesIDs), | |
| 562 | assertz(machine_values_identifiers(Name,ValuesIDs)). | |
| 563 | store_values(_,_). | |
| 564 | ||
| 565 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 566 | ||
| 567 | % write dot operation call graph | |
| 568 | % one cluster per machine (abstractions which are refined are not shown, their operations are re-declared), | |
| 569 | % one node per operation, one edge per operation call; prefixed calls (p.op) are linked to the operation | |
| 570 | % of the machine included/imported/extended with prefix p | |
| 571 | write_dot_op_hierarchy_to_file(File) :- | |
| 572 | (get_preference(dot_event_hierarchy_horizontal,true) -> Dir=[rankdir/'LR'] ; Dir=[]), | |
| 573 | (get_preference(dot_print_graph_title,true), main_machine_name(Main) | |
| 574 | -> ajoin([Main,' - operation calls'],Title), | |
| 575 | get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS] | |
| 576 | ; TitleAttrs = []), | |
| 577 | append([graph_name/operation_calls, no_page_size, fontname/'Helvetica', nodesep/0.25, ranksep/0.6, | |
| 578 | node_defaults/[fontname/'Helvetica', fontsize/11, shape/box, style/'rounded,filled', margin/'0.1,0.04'], | |
| 579 | edge_defaults/[fontname/'Helvetica', fontsize/9, arrowsize/0.7] | |
| 580 | | Dir], TitleAttrs, GraphAttrs), | |
| 581 | gen_dot_graph(File,GraphAttrs, | |
| 582 | use_new_dot_attr_pred(b_machine_hierarchy:dot_operation_node), | |
| 583 | use_new_dot_attr_pred(b_machine_hierarchy:dot_op_calls_op), | |
| 584 | dot_no_same_rank,b_machine_hierarchy:dot_op_cluster). | |
| 585 | ||
| 586 | :- use_module(bmachine,[b_top_level_operation/1, b_top_level_feasible_operation/1]). | |
| 587 | ||
| 588 | % machines whose operations are part of the model (abstractions are replaced by their refinement) | |
| 589 | op_machine(M) :- machine_type(M,Type), Type \= context, \+ refines_machine(_,M). | |
| 590 | ||
| 591 | op_of_machine(M,Op) :- machine_operations(M,Ops), raw_identifier_member(Op,Ops). | |
| 592 | ||
| 593 | :- public dot_op_cluster/3. | |
| 594 | dot_op_cluster(sub_graph_with_attributes(M,[label/htmllabel(HLabel), tooltip/Label, fillcolor/Fill, pencolor/Pen, | |
| 595 | fontname/'Helvetica', fontsize/11, fontcolor/Pen]), | |
| 596 | 'rounded,filled',none) :- | |
| 597 | op_machine(M), | |
| 598 | \+ \+ op_of_machine(M,_), | |
| 599 | machine_type(M,Type), get_machine_type_keyw(Type,Keyw), | |
| 600 | ajoin([Keyw,' ',M],Label), | |
| 601 | (main_machine_name(M) -> Fill='#F1F8F1', Pen='#2E7D32' ; Fill='#F3F7FB', Pen='#1F5F99'), | |
| 602 | html_escape(M,EM), | |
| 603 | ajoin(['<<font point-size="9" color="#707070">',Keyw,'</font> <b>',EM,'</b>>'],HLabel). | |
| 604 | ||
| 605 | :- public dot_operation_node/3. | |
| 606 | dot_operation_node(M:Op,M,[label/Op, tooltip/Tooltip | Attrs]) :- | |
| 607 | op_machine(M), | |
| 608 | machine_operations(M,Operations), | |
| 609 | (get_preference(dot_event_hierarchy_horizontal,true) | |
| 610 | -> reverse(Operations,ROps) % dot draws the first node of a rank at the bottom with rankdir=LR | |
| 611 | ; ROps=Operations), | |
| 612 | raw_identifier_member(Op,ROps), | |
| 613 | operation_class(M,Op,Class,FullName), | |
| 614 | op_class_attrs(Class,Attrs0), | |
| 615 | op_class_text(Class,ClassText), | |
| 616 | (FullName=Op -> ajoin(['OPERATION ',Op,' of ',M,' (',ClassText,')'],Tooltip) | |
| 617 | ; ajoin(['OPERATION ',Op,' of ',M,' (',ClassText,', visible as ',FullName,')'],Tooltip)), | |
| 618 | add_op_legend(Attrs0,ClassText,Class,Attrs). | |
| 619 | dot_operation_node(unresolved(Name),none,[label/Name, tooltip/Tooltip, style/'rounded,dashed', color/'#C62828', | |
| 620 | fontcolor/'#C62828' | Legend]) :- | |
| 621 | findall(Name,(op_call(_,_,Target), Target=unresolved(Name)),Names), | |
| 622 | sort(Names,SNames), member(Name,SNames), | |
| 623 | ajoin(['Called operation ',Name,' not found'],Tooltip), | |
| 624 | (get_preference(dot_print_legend,true) -> Legend=[legend/'called operation not found', legend_order/40] ; Legend=[]). | |
| 625 | ||
| 626 | add_op_legend(Attrs0,Text,Class,[legend/Text, legend_order/Order|Attrs0]) :- | |
| 627 | get_preference(dot_print_legend,true), !, | |
| 628 | op_class_order(Class,Order). | |
| 629 | add_op_legend(Attrs,_,_,Attrs). | |
| 630 | ||
| 631 | % top_level: operation is visible in the state space of the main machine | |
| 632 | operation_class(M,Op,Class,FullName) :- | |
| 633 | (op_instance_name(M,Op,FullName), b_top_level_operation(FullName) | |
| 634 | -> (b_top_level_feasible_operation(FullName) -> Class=top_level ; Class=top_level_infeasible) | |
| 635 | ; Class=internal, FullName=Op). | |
| 636 | ||
| 637 | op_class_attrs(top_level,[fillcolor/'#C8E6C9', color/'#2E7D32']). | |
| 638 | op_class_attrs(top_level_infeasible,[fillcolor/'#FFF3E0', color/'#C77700', style/'rounded,filled,dashed']). | |
| 639 | op_class_attrs(internal,[fillcolor/white, color/gray50]). | |
| 640 | op_class_text(top_level,'operation of the main machine'). | |
| 641 | op_class_text(top_level_infeasible,'operation of the main machine, never enabled'). | |
| 642 | op_class_text(internal,'internal operation, only called'). | |
| 643 | op_class_order(top_level,10). | |
| 644 | op_class_order(top_level_infeasible,20). | |
| 645 | op_class_order(internal,30). | |
| 646 | ||
| 647 | % the name under which an operation is visible from the main machine, taking prefixes into account | |
| 648 | op_instance_name(M,Op,FullName) :- | |
| 649 | op_instance_prefix(M,Prefix,[]), | |
| 650 | (Prefix='' -> FullName=Op ; ajoin([Prefix,'.',Op],FullName)). | |
| 651 | op_instance_prefix(M,'',_) :- main_machine_name(M). | |
| 652 | op_instance_prefix(M,Prefix,Visited) :- | |
| 653 | machine_references(Parent,Refs), | |
| 654 | member(ref(T,M,Pre),Refs), composition_ref(T), | |
| 655 | nonmember(Parent,Visited), | |
| 656 | op_instance_prefix(Parent,PP,[Parent|Visited]), | |
| 657 | join_prefix(PP,Pre,Prefix). | |
| 658 | op_instance_prefix(M,Prefix,Visited) :- % a refined abstraction is visible under the name of its refinement | |
| 659 | refines_machine(Ref,M), nonmember(Ref,Visited), | |
| 660 | op_instance_prefix(Ref,Prefix,[Ref|Visited]). | |
| 661 | ||
| 662 | composition_ref(includes). | |
| 663 | composition_ref(extends). | |
| 664 | composition_ref(imports). | |
| 665 | ||
| 666 | join_prefix('',P,P) :- !. | |
| 667 | join_prefix(P,'',P) :- !. | |
| 668 | join_prefix(P1,P2,P) :- ajoin([P1,'.',P2],P). | |
| 669 | ||
| 670 | % op_call(Caller,Prefix,Target): Caller=M:Op calls Target (M2:Op2 or unresolved(Name)) via prefix | |
| 671 | op_call(M:Op1,Prefix,Target) :- | |
| 672 | op_machine(M), | |
| 673 | machine_operation_calls(M,Calls), | |
| 674 | member(calls(Op1,Op2),Calls), | |
| 675 | resolve_op_call(M,Op2,Prefix,Target). | |
| 676 | ||
| 677 | resolve_op_call(M,Name,Prefix,Target) :- | |
| 678 | atom_codes(Name,Codes), append(PCodes,[0'.|RCodes],Codes), | |
| 679 | atom_codes(Pre,PCodes), atom_codes(Rest,RCodes), | |
| 680 | machine_references(M,Refs), member(ref(T,Callee,Pre),Refs), composition_ref(T), | |
| 681 | resolve_op_call(Callee,Rest,_,Target0), Target0 \= unresolved(_), !, | |
| 682 | Prefix=Pre, Target=Target0. | |
| 683 | resolve_op_call(M,Name,'',Target) :- | |
| 684 | find_unprefixed_op(M,Name,[],Callee), !, Target = Callee:Name. | |
| 685 | resolve_op_call(_,Name,'',unresolved(Name)). | |
| 686 | ||
| 687 | % look for operation in M or in machines included/extended/imported without a prefix | |
| 688 | find_unprefixed_op(M,Name,_,M) :- op_of_machine(M,Name), op_machine(M), !. | |
| 689 | find_unprefixed_op(M,Name,Visited,Callee) :- | |
| 690 | machine_references(M,Refs), | |
| 691 | member(ref(T,Sub,''),Refs), composition_ref(T), nonmember(Sub,Visited), | |
| 692 | find_unprefixed_op(Sub,Name,[M|Visited],Callee), !. | |
| 693 | find_unprefixed_op(M,Name,_,Abs) :- % operation inherited from a refined abstraction | |
| 694 | refines_machine(M,Abs), op_of_machine(Abs,Name), !. | |
| 695 | ||
| 696 | :- public dot_op_calls_op/3. | |
| 697 | dot_op_calls_op(From,To,[color/Col, tooltip/Tooltip | Attrs]) :- | |
| 698 | findall(From-To/Prefix,op_call(From,Prefix,To),L), | |
| 699 | sort(L,SL), | |
| 700 | group_op_calls(SL,Grouped), | |
| 701 | member(From-To/Prefixes,Grouped), | |
| 702 | From = M1:Op1, | |
| 703 | (To = M2:Op2 -> true ; To=unresolved(Op2), M2=none), | |
| 704 | (M1=M2 -> Col='#6A1B9A', Kind=local, LText='call of local operation', Order=60, | |
| 705 | Attrs0=[style/dashed] | |
| 706 | ; To=unresolved(_) -> Col='#C62828', Kind=unresolved, LText='call of unknown operation', Order=70, | |
| 707 | Attrs0=[style/dashed] | |
| 708 | ; Col='#1F5F99', Kind=external, LText='operation call', Order=50, Attrs0=[]), | |
| 709 | exclude(==(''),Prefixes,RealPrefixes), | |
| 710 | (RealPrefixes=[] -> Attrs1=Attrs0 | |
| 711 | ; ajoin_with_sep(RealPrefixes,', ',PL), Attrs1=[label/PL, fontcolor/Col|Attrs0]), | |
| 712 | (length(Prefixes,NP), NP>1 -> Attrs2=[penwidth/NP|Attrs1] ; Attrs2=Attrs1), | |
| 713 | (get_preference(dot_print_legend,true), Kind \= unresolved | |
| 714 | -> Attrs=[legend/LText, legend_order/Order|Attrs2] ; Attrs=Attrs2), | |
| 715 | (RealPrefixes=[] -> ajoin([Op1,' calls ',Op2],Tooltip) | |
| 716 | ; ajoin([Op1,' calls ',Op2,' via prefix ',PL],Tooltip)). | |
| 717 | ||
| 718 | group_op_calls([],[]). | |
| 719 | group_op_calls([F-T/P|Rest],[F-T/[P|Ps]|GR]) :- same_call(Rest,F,T,Ps,Rest2), group_op_calls(Rest2,GR). | |
| 720 | same_call([F-T/P|Rest],F,T,[P|Ps],Rest2) :- !, same_call(Rest,F,T,Ps,Rest2). | |
| 721 | same_call(Rest,_,_,[],Rest). | |
| 722 | ||
| 723 | ||
| 724 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 725 | % print hierarchy to dot file | |
| 726 | ||
| 727 | maxlinelength(30). | |
| 728 | ||
| 729 | :- use_module(tools_io). | |
| 730 | :- use_module(tools,[html_escape/2]). | |
| 731 | ||
| 732 | % write hierarchy of machines, with inclusion/refinement links | |
| 733 | % abstract machines and (extended/seen) contexts are drawn on top, refinements and extensions below | |
| 734 | write_dot_hierarchy_to_file(Filename) :- | |
| 735 | hierarchy_graph_attrs(GraphAttrs), | |
| 736 | gen_dot_graph(Filename,GraphAttrs, | |
| 737 | use_new_dot_attr_pred(b_machine_hierarchy:dot_hierarchy_node), | |
| 738 | use_new_dot_attr_pred(b_machine_hierarchy:dot_hierarchy_edge), | |
| 739 | dot_no_same_rank,dot_no_subgraph). | |
| 740 | ||
| 741 | hierarchy_graph_attrs([graph_name/machine_hierarchy, no_page_size, | |
| 742 | fontname/'Helvetica', nodesep/0.4, ranksep/0.5, | |
| 743 | node_defaults/[fontname/'Helvetica', shape/box, style/'rounded,filled', margin/'0.12,0.06'], | |
| 744 | edge_defaults/[fontname/'Helvetica', arrowsize/0.8] | |
| 745 | | TitleAttrs]) :- | |
| 746 | (get_preference(dot_print_graph_title,true), main_machine_name(Main) | |
| 747 | -> ajoin([Main,' - machine hierarchy'],Title), | |
| 748 | get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS] | |
| 749 | ; TitleAttrs = []). | |
| 750 | ||
| 751 | hierarchy_legend_wanted :- get_preference(dot_print_legend,true). | |
| 752 | ||
| 753 | % ---- nodes: one HTML-table node per machine/context | |
| 754 | ||
| 755 | :- public dot_hierarchy_node/3. | |
| 756 | dot_hierarchy_node(M,none,[label/htmllabel(Label), color/Col, fillcolor/Fill, tooltip/Tooltip | Attrs]) :- | |
| 757 | machine_type(M,Type), | |
| 758 | (main_machine_name(M) -> Kind=main ; Type=context -> Kind=context ; Kind=machine), | |
| 759 | hierarchy_node_colours(Kind,Col,Fill), | |
| 760 | (Kind=main -> Attrs=[penwidth/2.5|LAttrs] ; Attrs=LAttrs), | |
| 761 | (hierarchy_legend_wanted | |
| 762 | -> hierarchy_node_legend(Kind,Type,LText,Order), LAttrs=[legend/LText,legend_order/Order] | |
| 763 | ; LAttrs=[]), | |
| 764 | get_machine_type_keyw(Type,Keyw), | |
| 765 | ajoin([Keyw,' ',M],Tooltip), | |
| 766 | (hierarchy_node_label(M,Keyw,Label) -> true | |
| 767 | ; add_internal_error('Printing machine for hierarchy failed: ',M), html_escape(M,Label)). | |
| 768 | ||
| 769 | hierarchy_node_colours(main,'#2E7D32','#E4F2E5'). | |
| 770 | hierarchy_node_colours(machine,'#1F5F99','#E3EDF7'). | |
| 771 | hierarchy_node_colours(context,'#8A6D1E','#F6EFD9'). | |
| 772 | ||
| 773 | hierarchy_node_legend(main,context,'main context',10) :- !. | |
| 774 | hierarchy_node_legend(main,_,'main machine',10). | |
| 775 | hierarchy_node_legend(machine,_,machine,20). | |
| 776 | hierarchy_node_legend(context,_,context,30). | |
| 777 | ||
| 778 | hierarchy_node_label(M,Keyw,Label) :- | |
| 779 | (machine_package_directory(M,Dir), machine_package_directory(_M2,D2), D2 \= Dir | |
| 780 | -> html_escape(Dir,EDir), % show the name of the directory; probably the package pragma was used | |
| 781 | ajoin([' <font point-size="9">(',EDir,')</font>'],DirInfo) | |
| 782 | ; DirInfo=''), | |
| 783 | html_escape(M,EM), | |
| 784 | machine_sections(M,Sections), | |
| 785 | maplist(section_row,Sections,Rows), | |
| 786 | (Rows=[] -> Sep='' ; Sep='<hr/>'), | |
| 787 | ajoin(Rows,RowsAtom), | |
| 788 | ajoin(['<<table border="0" cellborder="0" cellspacing="0" cellpadding="2">', | |
| 789 | '<tr><td colspan="2"><font point-size="9" color="#555555">',Keyw,'</font></td></tr>', | |
| 790 | '<tr><td colspan="2"><b>',EM,'</b>',DirInfo,'</td></tr>', | |
| 791 | Sep,RowsAtom,'</table>>'],Label). | |
| 792 | ||
| 793 | section_row(sec(Title,Content),Row) :- | |
| 794 | html_escape(Title,ETitle), | |
| 795 | % align title with first line of multi-line content; otherwise centre it vertically with the content | |
| 796 | (atom(Content), sub_atom(Content,_,_,_,'<br/>') -> VAlign=' valign="top"' ; VAlign=''), | |
| 797 | ajoin(['<tr><td align="left"',VAlign,'><font point-size="9" color="#555555">',ETitle, | |
| 798 | '</font></td><td align="left" balign="left">',Content,'</td></tr>'],Row). | |
| 799 | ||
| 800 | % compute the sections (Title + HTML content) shown for a machine | |
| 801 | machine_sections(M,Sections) :- | |
| 802 | machine_identifiers(M,_Params,Sets,AVars,CVars,AConsts,CConsts), | |
| 803 | get_preference(dot_hierarchy_max_ids,MaxIds), % -1 means print all of them | |
| 804 | maplist(set_text(MaxIds),Sets,SetTexts), | |
| 805 | abstraction_info(M,VarsKind,ConstsKind,EventsKind), | |
| 806 | S0 = [sets('SETS',SetTexts), ids('VARIABLES',AVars,VarsKind), ids('CONCRETE VARIABLES',CVars,VarsKind), | |
| 807 | ids('ABSTRACT CONSTANTS',AConsts,ConstsKind), ids('CONSTANTS',CConsts,ConstsKind) | S1], | |
| 808 | (machine_values_identifiers(M,VConsts) -> S1 = [ids('VALUES',VConsts,plain)|S2] ; S1=S2), | |
| 809 | (machine_has_assertions(M) | |
| 810 | -> get_specification_description(assertions,AssStr), S2 = [flag(AssStr)|S3] | |
| 811 | ; S2=S3), | |
| 812 | (machine_promotes_operations(M,Promotes) -> S3 = [ids('PROMOTES',Promotes,plain)|S4] ; S3=S4), | |
| 813 | machine_operations(M,Operations), | |
| 814 | delete(Operations,identifier(_,'INITIALISATION'),Operations2), | |
| 815 | include(is_refinining_event(M),Operations2,RefiningOperations), | |
| 816 | exclude(is_refinining_event(M),Operations2,NewOperations), | |
| 817 | ((RefiningOperations=[] ; NewOperations=[]) | |
| 818 | -> get_specification_description(operations,OpStr), | |
| 819 | S4 = [ids(OpStr,Operations2,EventsKind)] | |
| 820 | ; S4 = [ids('EVENTS (refining)',RefiningOperations,EventsKind), | |
| 821 | ids('EVENTS (new)',NewOperations,EventsKind)] | |
| 822 | ), | |
| 823 | convert_sections(S0,MaxIds,Sections). | |
| 824 | ||
| 825 | is_refinining_event(M,identifier(_,Event)) :- (refines_event(M,Event,_,_) -> true). | |
| 826 | ||
| 827 | % determine how identifiers of a refinement relate to the abstract machine: | |
| 828 | % inherited identifiers (also in the abstraction) are greyed out, new ones are shown in bold | |
| 829 | abstraction_info(M,vars(AbsVars),consts(AbsConsts),events(M,AbsOps)) :- | |
| 830 | machine_references(M,Refs), member(ref(refines,Abs,_),Refs), | |
| 831 | machine_identifiers(Abs,_,_,AV,CV,AC,CC), !, | |
| 832 | append(AV,CV,AVars), maplist(get_raw_identifier,AVars,AbsVars), | |
| 833 | append(AC,CC,AConsts), maplist(get_raw_identifier,AConsts,AbsConsts), | |
| 834 | (machine_operations(Abs,Ops) -> maplist(get_raw_identifier,Ops,AbsOps) ; AbsOps=[]). | |
| 835 | abstraction_info(_,plain,plain,plain). | |
| 836 | ||
| 837 | % id_class(Kind,Name,Class) with Class = inherited, new or normal | |
| 838 | id_class(plain,_,normal). | |
| 839 | id_class(vars(Abs),Name,Class) :- (member(Name,Abs) -> Class=inherited ; Class=new). | |
| 840 | id_class(consts(Abs),Name,Class) :- (member(Name,Abs) -> Class=inherited ; Class=new). | |
| 841 | id_class(events(M,Abs),Name,Class) :- | |
| 842 | (member(Name,Abs) -> Class=inherited | |
| 843 | ; refines_event(M,Name,_,_) -> Class=normal % refines an event with a different name | |
| 844 | ; Class=new). | |
| 845 | ||
| 846 | % an item for wrap_items: plain length of the text and its HTML representation | |
| 847 | styled_item(Kind,Name,item(Len,Html)) :- | |
| 848 | atom_length(Name,Len), html_escape(Name,EName), | |
| 849 | id_class(Kind,Name,Class), | |
| 850 | (Class=inherited -> ajoin(['<font color="#909090">',EName,'</font>'],Html) | |
| 851 | ; Class=new -> ajoin(['<b>',EName,'</b>'],Html) | |
| 852 | ; Html=EName). | |
| 853 | ||
| 854 | convert_sections([],_,[]). | |
| 855 | convert_sections([S|T],MaxIds,Res) :- | |
| 856 | (convert_section(S,MaxIds,Sec) -> Res=[Sec|RT] ; Res=RT), | |
| 857 | convert_sections(T,MaxIds,RT). | |
| 858 | ||
| 859 | convert_section(sets(Title,SetTexts),_,sec(Title,Content)) :- SetTexts \= [], | |
| 860 | ajoin_with_sep(SetTexts,'<br/>',Content). | |
| 861 | convert_section(ids(Title,RawIds,Kind),MaxIds,sec(Title,Content)) :- RawIds \= [], | |
| 862 | maplist(get_raw_identifier,RawIds,Names), | |
| 863 | id_list_content(Names,Kind,MaxIds,Content). | |
| 864 | convert_section(flag(Title),_,sec(Title,'✓')). | |
| 865 | ||
| 866 | % show all identifiers if at most MaxIds+1 of them, otherwise the first MaxIds and a count | |
| 867 | id_list_content(Names,Kind,MaxIds,Content) :- | |
| 868 | length(Names,Len), | |
| 869 | (MaxIds < 0 ; Len =< MaxIds+1), !, | |
| 870 | maplist(styled_item(Kind),Names,Items), | |
| 871 | wrap_items(Items,Content). | |
| 872 | id_list_content(Names,Kind,0,Content) :- !, % just show the number of identifiers (and how many are new) | |
| 873 | length(Names,Len), | |
| 874 | include(is_new_id(Kind),Names,New), length(New,NewLen), | |
| 875 | (NewLen>0, NewLen<Len -> ajoin([Len,' (<b>',NewLen,' new</b>)'],Content) ; Content=Len). | |
| 876 | id_list_content(Names,Kind,MaxIds,Content) :- | |
| 877 | length(Shown,MaxIds), append(Shown,Rest,Names), length(Rest,RLen), | |
| 878 | maplist(styled_item(Kind),Shown,SItems), | |
| 879 | ajoin(['… (',RLen,' more)'],More), | |
| 880 | append(SItems,[More],Items), | |
| 881 | wrap_items(Items,Content). | |
| 882 | ||
| 883 | is_new_id(Kind,Name) :- id_class(Kind,Name,new). | |
| 884 | ||
| 885 | % text for a SETS entry, enumerated set elements are shortened to MaxIds elements | |
| 886 | set_text(MaxIds,description(_Pos,_Desc,Raw),Text) :- !, set_text(MaxIds,Raw,Text). | |
| 887 | set_text(_,deferred_set(_Pos,Name),Text) :- !, html_escape(Name,Text). | |
| 888 | set_text(MaxIds,enumerated_set(_Pos,Name,List),Text) :- !, | |
| 889 | maplist(get_raw_identifier,List,Elements), | |
| 890 | length(Elements,Len), | |
| 891 | Max is max(MaxIds,1), | |
| 892 | (MaxIds >= 0, Len > Max+1 | |
| 893 | -> length(Shown,Max), append(Shown,_,Elements), last(Elements,Last), | |
| 894 | append(Shown,['...',Last],Elements2) | |
| 895 | ; Elements2 = Elements), | |
| 896 | maplist(html_escape,Elements2,EElements), | |
| 897 | html_escape(Name,EName), | |
| 898 | (EElements = [First|RestEls] -> true ; First='', RestEls=[]), | |
| 899 | ajoin([EName,' = {',First],FirstItem), | |
| 900 | (RestEls = [] -> ajoin([FirstItem,'}'],Single), Items=[Single] | |
| 901 | ; append(Middle,[LastEl],RestEls), ajoin([LastEl,'}'],LastItem), | |
| 902 | append([FirstItem|Middle],[LastItem],Items)), | |
| 903 | wrap_items(Items,Text). | |
| 904 | set_text(_,Raw,Text) :- get_raw_identifier(Raw,Name), html_escape(Name,Text). | |
| 905 | ||
| 906 | % join items with ", " and break lines which get longer than maxlinelength | |
| 907 | % an item is either an atom or item(PlainLength,HtmlAtom) | |
| 908 | wrap_items(Items,Res) :- maxlinelength(Max), | |
| 909 | wrap_items(Items,0,Max,Parts), ajoin(Parts,Res). | |
| 910 | wrap_items([],_,_,[]). | |
| 911 | wrap_items([Item0|T],Len,Max,Parts) :- | |
| 912 | (Item0=item(IL,Item) -> true ; Item=Item0, atom_length(Item,IL)), | |
| 913 | (Len =:= 0 -> Parts=[Item|PT], NewLen=IL | |
| 914 | ; NL is Len+IL+2, NL =< Max -> Parts=[', ',Item|PT], NewLen=NL | |
| 915 | ; Parts=[',<br/>',Item|PT], NewLen=IL), | |
| 916 | wrap_items(T,NewLen,Max,PT). | |
| 917 | ||
| 918 | get_machine_type_keyw(abstract_machine,'MACHINE'). | |
| 919 | get_machine_type_keyw(abstract_model,'MODEL'). | |
| 920 | get_machine_type_keyw(refinement,'REFINEMENT'). | |
| 921 | get_machine_type_keyw(implementation,'IMPLEMENTATION'). | |
| 922 | get_machine_type_keyw(context,'CONTEXT'). | |
| 923 | ||
| 924 | % ---- edges: machine inclusion/refinement references | |
| 925 | ||
| 926 | :- public dot_hierarchy_edge/3. | |
| 927 | dot_hierarchy_edge(From,To,Attrs) :- | |
| 928 | machine_type(M,_), | |
| 929 | machine_references(M,Refs), | |
| 930 | filter_redundant_refs(Refs,Refs,UsefulRefs), | |
| 931 | sort(UsefulRefs,SUsefulRefs), | |
| 932 | (get_preference(dot_hierarchy_group_includes,true) -> Group=true ; Group=false), | |
| 933 | group_refs(SUsefulRefs,Group,Groups), | |
| 934 | member(grp(Type,Dest,Prefixes),Groups), | |
| 935 | machine_type(Dest,DestType), | |
| 936 | hierarchy_edge_style(Type,DestType,Col,Style,Layout,LText,Order), | |
| 937 | (Layout=up -> From=Dest, To=M, Attrs=[dir/back|A1] % so that abstract machines/contexts are shown on top | |
| 938 | ; From=M, To=Dest, Attrs=A1), | |
| 939 | length(Prefixes,NrPrefixes), | |
| 940 | (NrPrefixes>1 -> A1=[penwidth/NrPrefixes|A2] ; A1=A2), | |
| 941 | (Style=solid -> A2=A3 ; A2=[style/Style|A3]), | |
| 942 | A3 = [color/Col, fontcolor/Col | A4], | |
| 943 | exclude(==(''),Prefixes,RealPrefixes), | |
| 944 | (hierarchy_legend_wanted | |
| 945 | -> A4 = [legend/LText, legend_order/Order | A5], | |
| 946 | (RealPrefixes=[] -> A5=[] ; prefix_label(RealPrefixes,PLabel), A5=[label/PLabel]) | |
| 947 | ; (get_ref_type_name(Type,RefName) -> true), | |
| 948 | (RealPrefixes=[] -> Label=RefName | |
| 949 | ; prefix_label(RealPrefixes,PLabel), ajoin([RefName,' (',PLabel,')'],Label)), | |
| 950 | A4 = [label/Label] | |
| 951 | ). | |
| 952 | ||
| 953 | % group references to the same machine with the same inclusion type (and different prefixes) | |
| 954 | group_refs([],_,[]). | |
| 955 | group_refs([ref(Type,Dest,Prefix)|Rest],Group,[grp(Type,Dest,[Prefix|OtherPrefixes])|GT]) :- | |
| 956 | (Group=true -> get_same_dest(Rest,Type,Dest,OtherPrefixes,Rest2) | |
| 957 | ; OtherPrefixes=[], Rest2=Rest), | |
| 958 | group_refs(Rest2,Group,GT). | |
| 959 | ||
| 960 | prefix_label([P],P) :- !. | |
| 961 | prefix_label([P1,P2],L) :- !, ajoin([P1,',',P2],L). | |
| 962 | prefix_label([P1|T],L) :- length(T,Len), ajoin([P1,',... (',Len,' more)'],L). | |
| 963 | ||
| 964 | % hierarchy_edge_style(RefType,DestType,Colour,Style,Layout,LegendText,LegendOrder) | |
| 965 | hierarchy_edge_style(refines,_,'#1F5F99',solid,up,refines,200) :- !. | |
| 966 | hierarchy_edge_style(extends,_,'#8A6D1E',solid,up,extends,210) :- !. | |
| 967 | hierarchy_edge_style(sees,context,'#757575',dashed,up,sees,220) :- !. | |
| 968 | hierarchy_edge_style(sees,_,'#757575',dashed,down,sees,220) :- !. | |
| 969 | hierarchy_edge_style(includes,_,'#2E7D32',solid,down,includes,230) :- !. | |
| 970 | hierarchy_edge_style(imports,_,'#6A1B9A',solid,down,imports,240) :- !. | |
| 971 | hierarchy_edge_style(uses,_,'#757575',dotted,down,uses,250) :- !. | |
| 972 | hierarchy_edge_style(UNKNOWN,_,red,bold,down,UNKNOWN,300) :- | |
| 973 | add_internal_error('Unknown : ',hierarchy_edge_style(UNKNOWN,_,_,_,_,_,_)). | |
| 974 | ||
| 975 | % get references to same machine with same inclusion type, with different prefix | |
| 976 | get_same_dest([ref(Type,Dest,Prefix)|T],Type,Dest,[Prefix|PT],Rest) :- !, get_same_dest(T,Type,Dest,PT,Rest). | |
| 977 | get_same_dest(Rest,_,_,[],Rest). | |
| 978 | ||
| 979 | get_ref_type_name(includes,'INCLUDES'). | |
| 980 | get_ref_type_name(imports,'IMPORTS'). | |
| 981 | get_ref_type_name(extends,'EXTENDS'). | |
| 982 | get_ref_type_name(uses,'USES'). | |
| 983 | get_ref_type_name(sees,'SEES'). | |
| 984 | get_ref_type_name(refines,'REFINEMENT'). | |
| 985 | get_ref_type_name(main,'MACHINE'). | |
| 986 | get_ref_type_name(UNKNOWN,UNKNOWN). | |
| 987 | ||
| 988 | ||
| 989 | % remove redundant references (has to be done after analyse_eventb_machine has asserted all facts) | |
| 990 | filter_redundant_refs([],_,[]). | |
| 991 | filter_redundant_refs([ref(sees,Dest,_)|T],All,R) :- | |
| 992 | machine_type(Dest,context), | |
| 993 | member(ref(sees,Other,_),All), | |
| 994 | machine_references(Other,Refs), | |
| 995 | member(ref(extends,Dest,_),Refs), % Dest already seen by other seen context; | |
| 996 | % Note: Rodin export contains transitive sees relation; | |
| 997 | % if the user had included Dest in the sees clause we would have a warning "Redundant seen context" | |
| 998 | !, | |
| 999 | %format(user_output,'Redundant sees of ~w (~w)~n',[Dest,Other]), | |
| 1000 | filter_redundant_refs(T,All,R). | |
| 1001 | filter_redundant_refs([H|T],All,[H|R]) :- | |
| 1002 | filter_redundant_refs(T,All,R). | |
| 1003 | ||
| 1004 | /************************************************************************/ | |
| 1005 | /* The same for Event-B */ | |
| 1006 | /************************************************************************/ | |
| 1007 | ||
| 1008 | analyse_eventb_hierarchy(Machines,Contextes) :- | |
| 1009 | reset_hierarchy, | |
| 1010 | get_eventb_name(Machines,Contextes,MainName), | |
| 1011 | assertz(main_machine_name(MainName)), | |
| 1012 | maplist(analyse_eventb_machine,Machines), | |
| 1013 | analyse_eventb_refinement_types(Machines), | |
| 1014 | maplist(analyse_eventb_context,Contextes),!. | |
| 1015 | analyse_eventb_hierarchy(Machines,Contextes) :- | |
| 1016 | add_internal_error('Analyzing Event-B Hierarchy Failed: ',analyse_eventb_hierarchy(Machines,Contextes)). | |
| 1017 | ||
| 1018 | get_eventb_name([MainMachine|_AbstractMachines],_Contextes,Name) :- | |
| 1019 | event_b_model(MainMachine,Name,_),!. | |
| 1020 | get_eventb_name(_Machines,[MainContext|_AbstractContextes],Name) :- | |
| 1021 | event_b_context(MainContext,Name,_). | |
| 1022 | ||
| 1023 | event_b_model(event_b_model(_,Name,Sections),Name,Sections). | |
| 1024 | event_b_context(event_b_context(_,Name,Sections),Name,Sections). | |
| 1025 | ||
| 1026 | analyse_eventb_machine(Machine) :- | |
| 1027 | event_b_model(Machine,Name,Sections), | |
| 1028 | % print(analyzing(Name)),nl, | |
| 1029 | ( memberchk(refines(_,RName),Sections) -> Type=refinement, RRefs=[ref(refines,RName,'')] | |
| 1030 | ; Type=abstract_model, RRefs=[], RName='$none'), | |
| 1031 | get_identifiers([variables],Sections,Vars), | |
| 1032 | get_sees_context_refs(Sections,SRefs), | |
| 1033 | append(RRefs,SRefs,Refs), | |
| 1034 | get_events(Name,Sections,Events,RName), | |
| 1035 | store_eventb_hash(Name,Machine), | |
| 1036 | assertz(machine_type(Name,Type)), | |
| 1037 | assertz(machine_identifiers(Name,[],[],Vars,[],[],[])), | |
| 1038 | assertz(machine_references(Name,Refs)), | |
| 1039 | assertz(machine_operations(Name,Events)), | |
| 1040 | assertz(machine_operation_calls(Name,[])), % Event-B events cannot call other events | |
| 1041 | assert_if_has_theorems(Name,Sections). | |
| 1042 | ||
| 1043 | ||
| 1044 | % analyze which kinds of refinments we have between events | |
| 1045 | analyse_eventb_refinement_types([]). | |
| 1046 | analyse_eventb_refinement_types([RefMachine,AbsMachine|_]) :- | |
| 1047 | event_b_model(RefMachine,RefName,RefSections), | |
| 1048 | memberchk(refines(_,AbsName),RefSections), | |
| 1049 | event_b_model(AbsMachine,AbsName,AbsSections), | |
| 1050 | get_opt_section(events,RefSections,RawEvents), | |
| 1051 | get_opt_section(events,AbsSections,AbsRawEvents), | |
| 1052 | member(RawEvent,RawEvents), | |
| 1053 | bmachine_eventb:raw_event(RawEvent,_,RefEvName,_St1,Ref,_Prm1,RefGrd,_Thm1,RefAct,_Wit1,_Desc1), | |
| 1054 | Ref = [AbsEvName], | |
| 1055 | member(AbsRawEvent,AbsRawEvents), | |
| 1056 | bmachine_eventb:raw_event(AbsRawEvent,_,AbsEvName,_St2,_,_Prm2,AbsGrd,_Thm2,AbsAct,_Wit2,_Desc2), | |
| 1057 | check_raw_prefix(AbsGrd,RefGrd,SameGuard), | |
| 1058 | check_raw_prefix(AbsAct,RefAct,SameAct), | |
| 1059 | %% format('~nEvent refinement change ~w (~w) -> ~w (~w) guard: ~w, action: ~w~n',[RefEvName,RefName,AbsEvName,AbsName,SameGuard,SameAct]), | |
| 1060 | %print(rawgrd(RefGrd,AbsGrd)), nl, print(rawact(RefAct,AbsAct)),nl, | |
| 1061 | assertz(event_refinement_change(RefName,RefEvName,AbsName,AbsEvName,SameGuard,SameAct)), | |
| 1062 | fail. | |
| 1063 | analyse_eventb_refinement_types([_|T]) :- analyse_eventb_refinement_types(T). | |
| 1064 | ||
| 1065 | check_raw_prefix([],[],Res) :- !, Res=unchanged. | |
| 1066 | check_raw_prefix([],[_|_],Res) :- !, Res=extends. % the refinement has some more actions/guards | |
| 1067 | check_raw_prefix([Abs|AT],[Ref|RT],Result) :- same_raw_expression(Abs,Ref),!, | |
| 1068 | check_raw_prefix(AT,RT,Result). | |
| 1069 | check_raw_prefix(_,_,refines). | |
| 1070 | ||
| 1071 | % TO DO: check if we have a more complete version of this predicate; to do: handle @desc description/3 terms | |
| 1072 | same_raw_expression(identifier(_,A),RHS) :- !, RHS=identifier(_,B), A=B. | |
| 1073 | same_raw_expression(equal(_,A,B),RHS) :- !, RHS=equal(_,A2,B2), same_raw_expression(A,A2), same_raw_expression(B,B2). | |
| 1074 | same_raw_expression(assign(_,A1,A2),RHS) :- !, RHS=assign(_,B1,B2), | |
| 1075 | maplist(same_raw_expression,A1,B1), maplist(same_raw_expression,A2,B2). | |
| 1076 | same_raw_expression(A,B) :- atomic(A),!, A=B. | |
| 1077 | same_raw_expression(A,B) :- A =.. [F,_|AA], % print(match(F,AA)),nl, | |
| 1078 | B=.. [F,_|BB], maplist(same_raw_expression,AA,BB). | |
| 1079 | ||
| 1080 | ||
| 1081 | ||
| 1082 | get_sees_context_refs(Sections,SRefs) :- | |
| 1083 | get_opt_section(sees,Sections,Seen), | |
| 1084 | findall(ref(sees,I,''),member(I,Seen),SRefs). | |
| 1085 | ||
| 1086 | :- use_module(bmachine_eventb,[raw_event/11]). | |
| 1087 | get_events(Name,Sections,Events,AbsMachineName) :- | |
| 1088 | get_opt_section(events,Sections,RawEvents), | |
| 1089 | compute_event_refines(Name,RawEvents,AbsMachineName), | |
| 1090 | findall( identifier(Pos,EvName), | |
| 1091 | ( member(RawEvent,RawEvents), | |
| 1092 | raw_event(RawEvent,Pos,EvName,_St,_Rf,_Prm,_Grd,_Thm,_Act,_Wit,_Desc) ), | |
| 1093 | Events). | |
| 1094 | ||
| 1095 | % TO DO: detect when an event extends another one without changing it | |
| 1096 | compute_event_refines(MachineName,RawEvents,AbsMachineName) :- | |
| 1097 | member(RawEvent,RawEvents), | |
| 1098 | bmachine_eventb:raw_event(RawEvent,_Pos,EvName,_St,Refines,_Prm,_Grd,_Thm,_Act,_Wit,_Desc), | |
| 1099 | member(RefEvent,Refines), | |
| 1100 | % print(refines(MachineName,EvName,AbsMachineName,RefEvent)),nl, | |
| 1101 | assertz(refines_event(MachineName,EvName,AbsMachineName,RefEvent)), | |
| 1102 | fail. | |
| 1103 | compute_event_refines(_,_,_). | |
| 1104 | ||
| 1105 | ||
| 1106 | % try and get the name of the machine we refine | |
| 1107 | %machine_refines(Machine,AbsMachine) :- machine_references(Machine,Refs), member(ref(refines,AbsMachine,_),Refs). | |
| 1108 | ||
| 1109 | analyse_eventb_context(Context) :- | |
| 1110 | event_b_context(Context,Name,Sections), | |
| 1111 | get_identifiers([constants],Sections,ConcreteConstants), | |
| 1112 | get_identifiers([abstract_constants],Sections,AbstractConstants), | |
| 1113 | append([ConcreteConstants,AbstractConstants],AllConstants), | |
| 1114 | get_sets(Sections,Sets), | |
| 1115 | get_extends_refs(Sections,Refs), | |
| 1116 | store_eventb_hash(Name,Context), | |
| 1117 | assertz(machine_type(Name,context)), | |
| 1118 | assertz(machine_identifiers(Name,[],Sets,[],[],[],AllConstants)), | |
| 1119 | assertz(machine_references(Name,Refs)), | |
| 1120 | assertz(machine_operations(Name,[])), | |
| 1121 | assertz(machine_operation_calls(Name,[])), | |
| 1122 | maplist(assert_raw_id_with_position(concrete_constant),ConcreteConstants), | |
| 1123 | maplist(assert_raw_id_with_position(abstract_constant),AbstractConstants), | |
| 1124 | assert_if_has_theorems(Name,Sections). | |
| 1125 | ||
| 1126 | get_extends_refs(Sections,Refs) :- | |
| 1127 | get_opt_section(extends,Sections,Extended), | |
| 1128 | findall(ref(extends,E,''),member(E,Extended),Refs). | |
| 1129 | ||
| 1130 | assert_if_has_theorems(Name,Sections) :- | |
| 1131 | get_opt_section(theorems,Sections,[_|_]), | |
| 1132 | assertz(machine_has_assertions(Name)). | |
| 1133 | assert_if_has_theorems(_Name,_Sections). | |
| 1134 | ||
| 1135 | ||
| 1136 | % compute a hash based on constants and properties | |
| 1137 | properties_hash(MachineName,Hash) :- | |
| 1138 | properties_hash_cached(MachineName,Hash1),!, | |
| 1139 | Hash=Hash1. | |
| 1140 | properties_hash(MachineName,Hash) :- | |
| 1141 | compute_properties_hash(MachineName,Hash1), | |
| 1142 | assertz(properties_hash_cached(MachineName,Hash1)), | |
| 1143 | Hash=Hash1. | |
| 1144 | compute_properties_hash(Name,Hash) :- | |
| 1145 | raw_machine(Name,Machine), | |
| 1146 | get_machine(Name,[Machine],_Type,_Header,_Refines,Body), | |
| 1147 | extract_sorted_np_sets(Body,Sets), | |
| 1148 | extract_sorted_np_constants(Body,Constants), | |
| 1149 | extract_np_properties(Body,Properties), | |
| 1150 | extract_used_np_definitions_from_properties(Body,Definitions), | |
| 1151 | ToHash = [Sets,Constants,Properties,Definitions], | |
| 1152 | raw_sha_hash(ToHash,Hash). | |
| 1153 | % save_properties_hash(Name,ToHash,Hash). | |
| 1154 | ||
| 1155 | extract_sorted_np_sets(Body,Sets) :- | |
| 1156 | get_sets(Body,PosSets), | |
| 1157 | remove_raw_position_info(PosSets,UnsortedSets), | |
| 1158 | sort(UnsortedSets,Sets). | |
| 1159 | extract_sorted_np_constants(Body,Constants) :- | |
| 1160 | get_opt_sections([constants,concrete_constants,abstract_constants],Body,PosConstants), | |
| 1161 | remove_raw_position_info(PosConstants,UnsortedConstants), | |
| 1162 | sort(UnsortedConstants,Constants). | |
| 1163 | extract_np_properties(Body,Properties) :- | |
| 1164 | get_opt_section(properties,Body,PosProperties), | |
| 1165 | remove_raw_position_info(PosProperties,Properties). | |
| 1166 | extract_used_np_definitions_from_properties(Body,Definitions) :- | |
| 1167 | get_opt_section(properties,Body,Properties), | |
| 1168 | extract_used_np_definitions(Properties,Body,Definitions,_). | |
| 1169 | extract_used_np_definitions(RawSyntax,Body,Definitions,PosDefinitions) :- | |
| 1170 | extract_raw_identifiers(RawSyntax,UsedIds), | |
| 1171 | all_definition_ids(Body,AllDefs), | |
| 1172 | ord_intersection(UsedIds,AllDefs,UsedDefNames), | |
| 1173 | transitive_used_definitions(UsedDefNames,AllUsedDefs), | |
| 1174 | findall( definition(none,Name,Args,DefBody), | |
| 1175 | ( member(Name,AllUsedDefs),b_get_definition(Name,_DefType,Args,DefBody,_Deps)), | |
| 1176 | PosDefinitions), | |
| 1177 | maplist(remove_raw_position_info,PosDefinitions,Definitions). | |
| 1178 | all_definition_ids(Body,Ids) :- | |
| 1179 | get_opt_section(definitions,Body,Definitions), | |
| 1180 | convlist(get_definition_name,Definitions,Ids1), | |
| 1181 | sort(Ids1,Ids). | |
| 1182 | transitive_used_definitions(Defs,TransDefs) :- | |
| 1183 | findall( D, reachable_definition(Defs,D), TD1), | |
| 1184 | sort(TD1,TransDefs). | |
| 1185 | reachable_definition(Defs,D) :- | |
| 1186 | member(N,Defs), | |
| 1187 | ( D=N | |
| 1188 | ; b_get_definition(N,_DefType,_Args,_Body,Deps), | |
| 1189 | reachable_definition(Deps,D)). | |
| 1190 | ||
| 1191 | ||
| 1192 | /* just for debugging: | |
| 1193 | save_properties_hash(MachineName,ToHash,Hash) :- | |
| 1194 | main_machine_name(Main), | |
| 1195 | open('/home/plagge/hashes.pl',append,S), | |
| 1196 | writeq(S,hash(Main,MachineName,ToHash,Hash)), | |
| 1197 | write(S,'.\n'), | |
| 1198 | close(S). | |
| 1199 | */ | |
| 1200 | ||
| 1201 | % --------------------------- | |
| 1202 | :- dynamic event_info/3. | |
| 1203 | analyze_extends_relation :- | |
| 1204 | retractall(event_info(_,_,_)), | |
| 1205 | bmachine:b_get_machine_operation(_Name,_Results,_RealParameters,TBody,_OType,_OpPos), | |
| 1206 | treat_event_body(TBody),fail. | |
| 1207 | analyze_extends_relation. | |
| 1208 | ||
| 1209 | treat_event_body(TBody) :- | |
| 1210 | rlevent_info(TBody,EventName,Machine,Status,AbstractEvents), | |
| 1211 | % format('Event ~w:~w ~w~n',[Machine,EventName,Status]), | |
| 1212 | assertz(event_info(Machine,EventName,Status)), | |
| 1213 | maplist(treat_event_body,AbstractEvents). | |
| 1214 | ||
| 1215 | :- use_module(bsyntaxtree,[get_texpr_expr/2]). | |
| 1216 | rlevent_info(TBody,EventName,Machine,FStatus,AbstractEvents) :- | |
| 1217 | get_texpr_expr(TBody,Event), | |
| 1218 | Event = rlevent(EventName,Machine,TStatus,_Params,_Guard,_Theorems,_Actions,_VWit,_PWit,_Unmod,AbstractEvents), | |
| 1219 | bsyntaxtree:get_texpr_expr(TStatus,Status), %ordinary, convergent, anticipated | |
| 1220 | functor(Status,FStatus,_). | |
| 1221 | ||
| 1222 | ||
| 1223 | % write the event refinement hierarchy to a dot file | |
| 1224 | % (currently) only makes sense for Event-B models | |
| 1225 | ||
| 1226 | :- use_module(preferences,[get_preference/2]). | |
| 1227 | :- use_module(tools_strings,[ajoin/2, ajoin_with_sep/3]). | |
| 1228 | ||
| 1229 | :- public dot_refinement_node_new/4. | |
| 1230 | % variation for: use_new_dot_attr_pred | |
| 1231 | dot_refinement_node_new(event_refinement,M:Ev,M,[label/htmllabel(Label),tooltip/Tooltip|Attrs]) :- | |
| 1232 | dot_event_node(M:Ev,M,Label,Attrs), | |
| 1233 | (event_info(M,Ev,Status) -> true ; Status=unknown), | |
| 1234 | (event_refinement_change(M,Ev,AbsName,AbsEvName,SameGuard,SameAct) | |
| 1235 | -> ajoin(['Event ',Ev,' in ',M, | |
| 1236 | '\n status: ',Status, | |
| 1237 | '\n refines ',AbsEvName,' in ',AbsName, | |
| 1238 | '\n guard: ',SameGuard, | |
| 1239 | '\n action: ',SameAct], Tooltip) | |
| 1240 | ; ajoin(['Event ',Ev,' in ',M, | |
| 1241 | '\n status: ',Status], Tooltip)). | |
| 1242 | dot_refinement_node_new(variable_refinement(With),M:Var,M,[label/htmllabel(Label),tooltip/Tooltip|Attrs]) :- | |
| 1243 | (With=with_constants -> machine_ids(M,Vars) ; machine_variables(M,Vars)), | |
| 1244 | % sort variables by the machine introducing them and their position there, so that rows align | |
| 1245 | findall(Key-Var,(nth1(Pos,Vars,Var),variable_intro_key(M,Var,Pos,Key)),KL), | |
| 1246 | keysort(KL,SKL), | |
| 1247 | (get_preference(dot_event_hierarchy_horizontal,true) | |
| 1248 | -> reverse(SKL,RKL) % dot draws the first node of a rank at the bottom with rankdir=LR | |
| 1249 | ; RKL=SKL), | |
| 1250 | member(_-Var,RKL), | |
| 1251 | variable_class(M,Var,Class,Tooltip), | |
| 1252 | (removed_in_refinement(M,Var,Ref) -> ajoin([Class,'_removed'],LClass0), Removed=Ref ; LClass0=Class, Removed=none), | |
| 1253 | variable_class_attrs(Class,Removed,Attrs0), | |
| 1254 | html_escape(Var,EVar), | |
| 1255 | (Class=kept -> ajoin(['<font color="#707070">',EVar,'</font>'],VarHtml) | |
| 1256 | ; ajoin(['<b>',EVar,'</b>'],VarHtml)), | |
| 1257 | (Removed \= none -> html_escape(Removed,ERef), | |
| 1258 | ajoin([VarHtml,'<br/><font point-size="9" color="#505050">removed in ',ERef,'</font>'],L0) | |
| 1259 | ; L0=VarHtml), | |
| 1260 | ajoin(['<',L0,'>'],Label), | |
| 1261 | (get_preference(dot_print_legend,true), variable_class_legend(LClass0,LText,Order) | |
| 1262 | -> Attrs = [legend/LText, legend_order/Order|Attrs0] | |
| 1263 | ; Attrs = Attrs0). | |
| 1264 | ||
| 1265 | variable_class(M,Var,kept,Tooltip) :- id_exists_in_abstraction(M,Anc,Var),!, | |
| 1266 | ajoin(['Kept from abstraction ',Anc],Tooltip). | |
| 1267 | variable_class(M,Var,new_set,Tooltip) :- machine_type(M,context), machine_sets(M,Sets), member(Var,Sets),!, | |
| 1268 | ajoin(['New set in ',M],Tooltip). | |
| 1269 | variable_class(M,_,new_constant,Tooltip) :- machine_type(M,context),!, % it must be a constant | |
| 1270 | ajoin(['New constant in ',M],Tooltip). | |
| 1271 | variable_class(M,_,new_variable,Tooltip) :- ajoin(['New variable in ',M],Tooltip). | |
| 1272 | ||
| 1273 | % a variable which is not kept in a refinement of M | |
| 1274 | removed_in_refinement(M,Var,Ref) :- \+ machine_type(M,context), | |
| 1275 | refines_machine(Ref,M), machine_variables(Ref,RVars), nonmember(Var,RVars), !. | |
| 1276 | ||
| 1277 | variable_class_attrs(kept,Removed,[shape/box, style/Style, fillcolor/Col, color/Col2]) :- !, | |
| 1278 | get_preference(dot_event_hierarchy_unchanged_event_colour,Col), | |
| 1279 | (Removed=none -> Style='rounded,filled', Col2=gray80 ; Style='rounded,filled,dashed', Col2=gray45). | |
| 1280 | variable_class_attrs(new_set,Removed,[shape/box, style/Style, fillcolor/Col, color/gray45]) :- !, | |
| 1281 | get_preference(dot_event_hierarchy_refines_colour,Col), | |
| 1282 | (Removed=none -> Style='rounded,filled' ; Style='rounded,filled,dashed'). | |
| 1283 | variable_class_attrs(_,Removed,[shape/box, style/Style, fillcolor/white, color/Col, penwidth/1.5]) :- | |
| 1284 | get_preference(dot_event_hierarchy_new_event_colour,Col), | |
| 1285 | (Removed=none -> Style='rounded,filled' ; Style='rounded,filled,dashed'). | |
| 1286 | ||
| 1287 | variable_class_legend(new_variable,'new variable',10). | |
| 1288 | variable_class_legend(kept,'kept from abstraction',20). | |
| 1289 | variable_class_legend(new_variable_removed,'new variable, removed in refinement',30). | |
| 1290 | variable_class_legend(kept_removed,'kept, removed in refinement',40). | |
| 1291 | variable_class_legend(new_set,'new set',50). | |
| 1292 | variable_class_legend(new_constant,'new constant',60). | |
| 1293 | ||
| 1294 | % sort key: refinement depth of the machine introducing Var, and its position there | |
| 1295 | variable_intro_key(M,Var,Pos,Depth-IntroPos) :- | |
| 1296 | (id_exists_in_abstraction(M,Anc,Var), machine_ids(Anc,AncIds), nth1(AncPos,AncIds,Var) | |
| 1297 | -> variable_intro_key(Anc,Var,AncPos,Depth-IntroPos) | |
| 1298 | ; machine_depth(M,Depth), IntroPos=Pos). | |
| 1299 | ||
| 1300 | % number of abstractions (refined or extended machines) above M | |
| 1301 | machine_depth(M,Depth) :- machine_depth(M,[],Depth). | |
| 1302 | machine_depth(M,Visited,Depth) :- | |
| 1303 | (refines_or_extends_machine(M,Anc), nonmember(Anc,Visited) | |
| 1304 | -> machine_depth(Anc,[M|Visited],D1), Depth is D1+1 | |
| 1305 | ; Depth=0). | |
| 1306 | %dot_refinement_node_new(variable_refinement(_),C:Cst,M,[label/Desc,shape/rect,color/Color,tooltip/Desc|T1]) :- | |
| 1307 | % new_seen_context(M,C), machine_constants(C,Csts), member(Cst,Csts), | |
| 1308 | % Desc=Cst, T1=[style/rounded], get_preference(dot_event_hierarchy_extends_colour,Color). | |
| 1309 | ||
| 1310 | % the node id is M:Ev as Ev can and usually does occur multiple times | |
| 1311 | % events are sorted by their refinement path from the most abstract event, so that | |
| 1312 | % refined events appear in the same row as their abstract events | |
| 1313 | dot_event_node(M:Ev,M,Label,Attrs) :- | |
| 1314 | findall(Path-showev(M,Ev),(event_to_show(M,Ev),event_refinement_path(M,Ev,[],Path)),List), | |
| 1315 | keysort(List,SList), % TODO: use same order of new events as in the machine? | |
| 1316 | (get_preference(dot_event_hierarchy_horizontal,true) | |
| 1317 | -> reverse(SList,RList) % dot draws the first node of a rank at the bottom with rankdir=LR | |
| 1318 | ; RList=SList), | |
| 1319 | member(_-showev(M,Ev),RList), | |
| 1320 | (event_info(M,Ev,Status) -> true ; Status=unknown), | |
| 1321 | (event_refinement_change(M,Ev,_,_,SameGuard,SameAction) | |
| 1322 | -> true ; SameGuard=unknown, SameAction=unknown), | |
| 1323 | event_class(M,Ev,SameGuard,SameAction,Class), | |
| 1324 | event_change_description(M,Ev,Class,SameGuard,SameAction,Status,Details), | |
| 1325 | html_escape(Ev,EEv), | |
| 1326 | (Class=unchanged | |
| 1327 | -> ajoin(['<font color="#707070">',EEv,'</font>'],Name) ; ajoin(['<b>',EEv,'</b>'],Name)), | |
| 1328 | (Details=[] -> Label0=Name | |
| 1329 | ; ajoin_with_sep(Details,', ',DetailsAtom), | |
| 1330 | ajoin([Name,'<br/><font point-size="9" color="#505050">',DetailsAtom,'</font>'],Label0)), | |
| 1331 | ajoin(['<',Label0,'>'],Label), | |
| 1332 | event_class_attrs(Class,Attrs0), | |
| 1333 | (get_preference(dot_print_legend,true) | |
| 1334 | -> event_class_legend(Class,LText,Order), Attrs = [legend/LText, legend_order/Order|Attrs0] | |
| 1335 | ; Attrs = Attrs0). | |
| 1336 | ||
| 1337 | % list of event names from the most abstract event down to Ev (used for sorting) | |
| 1338 | event_refinement_path(M,Ev,Acc,Path) :- | |
| 1339 | refines_event(M,Ev,AbsM,AbsEv), \+ member(AbsEv,Acc), !, % TODO: deal with merged events | |
| 1340 | event_refinement_path(AbsM,AbsEv,[Ev|Acc],Path). | |
| 1341 | event_refinement_path(_,Ev,Acc,[Ev|Acc]). | |
| 1342 | ||
| 1343 | % classify an event of a machine with respect to its abstract event | |
| 1344 | event_class(M,Ev,_,_,new) :- \+ refines_event(M,Ev,_,_),!. | |
| 1345 | event_class(M,Ev,SameGuard,SameAction,Class) :- | |
| 1346 | refines_event(M,Ev,_,Ev2), dif(Ev2,Ev),!, % changes name | |
| 1347 | ((SameGuard,SameAction)=(unchanged,unchanged) -> Class=renamed_unchanged ; Class=renamed). | |
| 1348 | event_class(_,_,unchanged,unchanged,unchanged) :- !. % keeps name and adds no guard or action | |
| 1349 | event_class(_,_,_,unchanged,guard_changed) :- !. % keeps name and adds no action, but modifies guard | |
| 1350 | event_class(_,_,unchanged,_,action_changed) :- !. % keeps name and adds action but keeps guard | |
| 1351 | event_class(_,_,_,_,changed). % keeps name but adds or modifies guard and action | |
| 1352 | ||
| 1353 | event_class_attrs(new,[shape/box, style/'rounded,filled', fillcolor/white, color/Col, penwidth/1.5]) :- !, | |
| 1354 | get_preference(dot_event_hierarchy_new_event_colour,Col). | |
| 1355 | event_class_attrs(unchanged,[shape/box, style/'rounded,filled', fillcolor/Col, color/gray80]) :- !, | |
| 1356 | get_preference(dot_event_hierarchy_unchanged_event_colour,Col). | |
| 1357 | event_class_attrs(Class,[shape/box, style/'rounded,filled', fillcolor/Col, color/gray45]) :- | |
| 1358 | event_class_colour_pref(Class,Pref), get_preference(Pref,Col). | |
| 1359 | ||
| 1360 | event_class_colour_pref(renamed_unchanged,dot_event_hierarchy_rename_unchanged_event_colour). | |
| 1361 | event_class_colour_pref(renamed,dot_event_hierarchy_rename_event_colour). | |
| 1362 | event_class_colour_pref(guard_changed,dot_event_hierarchy_grd_strengthening_event_colour). | |
| 1363 | event_class_colour_pref(action_changed,dot_event_hierarchy_grd_keeping_event_colour). | |
| 1364 | event_class_colour_pref(changed,dot_event_hierarchy_refines_colour). | |
| 1365 | ||
| 1366 | event_class_legend(new,'new event',10). | |
| 1367 | event_class_legend(unchanged,'unchanged',20). | |
| 1368 | event_class_legend(guard_changed,'guard changed, same action',30). | |
| 1369 | event_class_legend(action_changed,'action changed, same guard',40). | |
| 1370 | event_class_legend(changed,'guard and action changed',50). | |
| 1371 | event_class_legend(renamed_unchanged,'refines other event, unchanged',60). | |
| 1372 | event_class_legend(renamed,'refines other event',70). | |
| 1373 | ||
| 1374 | % short description of what changed with respect to the abstract event | |
| 1375 | event_change_description(M,Ev,Class,SameGuard,SameAction,Status,Details) :- | |
| 1376 | ((Class=renamed ; Class=renamed_unchanged), refines_event(M,Ev,_,AbsEv) | |
| 1377 | -> html_escape(AbsEv,EAbsEv), ajoin(['refines ',EAbsEv],R), D1=[R|D2] | |
| 1378 | ; D1=D2), | |
| 1379 | (change_text(SameGuard,grd,G) -> D2=[G|D3] ; D2=D3), | |
| 1380 | (change_text(SameAction,act,A) -> D3=[A|D4] ; D3=D4), | |
| 1381 | (Status=convergent -> D4=['convergent (<)'] | |
| 1382 | ; Status=anticipated -> D4=['anticipated (<=)'] | |
| 1383 | ; D4=[]), | |
| 1384 | Details=D1. | |
| 1385 | ||
| 1386 | change_text(extends,What,Txt) :- ajoin(['extends ',What],Txt). | |
| 1387 | change_text(refines,What,Txt) :- ajoin(['refines ',What],Txt). | |
| 1388 | ||
| 1389 | event_to_show(M,Ev) :- machine_operations(M,Evs), | |
| 1390 | raw_identifier_member(Ev,Evs), Ev \= 'INITIALISATION'. | |
| 1391 | event_to_show(M,Ev) :- | |
| 1392 | event_refinement_change(M,Ev,_,_,_,_), % for events that disappear, i.e., are not refined until bottom level | |
| 1393 | Ev \= 'INITIALISATION'. | |
| 1394 | ||
| 1395 | :- public dot_refines_event/4. | |
| 1396 | % dot transition predicate for event and variable refinement hierarchy diagram | |
| 1397 | dot_refines_event(event_refinement,M2:Ev2,M1:Ev1,[label/Label,color/Color,style/Style]) :- | |
| 1398 | Label = '', % TO DO: detect refine, extends, identical | |
| 1399 | refines_event(M1,Ev1,M2,Ev2), | |
| 1400 | Ev1 \= 'INITIALISATION', | |
| 1401 | (event_refinement_change(M1,Ev1,_,_,SameGuard,SameAct) | |
| 1402 | -> arrow_style(SameGuard,SameAct,Style,ColPref), get_preference(ColPref,Color) | |
| 1403 | ; Style=solid, Color=red % should not happen | |
| 1404 | ). | |
| 1405 | dot_refines_event(variable_refinement(With),M1:Var,M2:Var2,[label/Label,color/Color,style/Style|T1]) :- | |
| 1406 | (With=with_constants -> machine_ids(M2,Vars2) ; machine_variables(M2,Vars2)), | |
| 1407 | if((member(Var,Vars2), | |
| 1408 | id_exists_in_abstraction(M2,M1,Var)), | |
| 1409 | (Label='',Var2=Var,Style=arrowhead(none,solid), Color=gray60, T1=[]), | |
| 1410 | (% no variable of M2 exists in M1 | |
| 1411 | get_preference(dot_event_hierarchy_edge_colour,Color), | |
| 1412 | refines_or_extends_machine(M2,M1), | |
| 1413 | machine_ids(M1,[Var|_]), % get first variable of M1 | |
| 1414 | Vars2=[Var2|_], Style=dotted, % add virtual edge to first variable of M2 if no variable is kept | |
| 1415 | get_dot_cluster_name(M1,M1C), get_dot_cluster_name(M2,M2C), | |
| 1416 | T1 = [ltail/M1C, lhead/M2C], | |
| 1417 | (machine_type(M2,context) -> Label='extends' ; Label='') | |
| 1418 | ) | |
| 1419 | ). | |
| 1420 | dot_refines_event(variable_refinement(with_constants),M1:Var1,M2:Cst2,[label/Label,color/Color,style/Style|T1]) :- Label='sees', | |
| 1421 | Color=gray80, Style=solid, | |
| 1422 | new_seen_context(M1,M2), | |
| 1423 | machine_ids(M1,[Var1|_]), | |
| 1424 | machine_ids(M2,[Cst2|_]), | |
| 1425 | get_dot_cluster_name(M1,M1C), get_dot_cluster_name(M2,M2C), | |
| 1426 | T1 = [ltail/M1C, lhead/M2C, dir/forward]. | |
| 1427 | ||
| 1428 | arrow_style(unchanged,unchanged,Style,ColPref) :- !, | |
| 1429 | Style=arrowhead(none,solid), ColPref=dot_event_hierarchy_extends_colour. | |
| 1430 | arrow_style(refines,_,Style,ColPref) :- !, Style=solid, ColPref=dot_event_hierarchy_edge_colour. | |
| 1431 | arrow_style(_,refines,Style,ColPref) :- !, Style=solid, ColPref=dot_event_hierarchy_edge_colour. | |
| 1432 | %arrow_style(_,_,Style) :- Style = arrowhead(vee,arrowtail(box,solid)). % we have extends | |
| 1433 | arrow_style(_,_,Style,dot_event_hierarchy_extends_colour) :- Style = arrowhead(vee,solid). % we have extends | |
| 1434 | ||
| 1435 | ||
| 1436 | %dot_same_rank(SameRankVals) :- machine_operations(M,Evs), | |
| 1437 | % findall(M:Ev,raw_identifier_member(Ev,Evs),SameRankVals). | |
| 1438 | ||
| 1439 | dot_subgraph(Kind,sub_graph_with_attributes(M,Attrs), filled,Colour) :- | |
| 1440 | get_preference(dot_event_hierarchy_machine_colour,MColour), | |
| 1441 | Attrs = [label/Label, tooltip/ToolTip], | |
| 1442 | machine_operations(M,Ops), | |
| 1443 | (Kind=event_refinement -> Ops=[_|_] ; true), | |
| 1444 | (machine_type(M,context) -> IDS = 'csts', Colour=gray90 | |
| 1445 | ; IDS = 'vars', Colour=MColour), | |
| 1446 | (machine_ids(M,Vars) | |
| 1447 | -> length(Vars,V), | |
| 1448 | findall(C,new_seen_context(M,C),NewC), | |
| 1449 | split_list(id_exists_in_abstraction(M),Vars,_Old,NewVars), | |
| 1450 | length(NewVars,NewNr), | |
| 1451 | findall(Del,(var_exists_in_abstraction(M,Del), nonmember(Del,Vars)),DelVars), | |
| 1452 | length(DelVars,DelNr), | |
| 1453 | (Kind=event_refinement, get_preference(dot_hierarchy_show_extra_detail,false) | |
| 1454 | -> Label=M | |
| 1455 | ; ajoin([M,'\\n#',IDS,'=',V, ' (+',NewNr,',-', DelNr,')'],Label) | |
| 1456 | ), | |
| 1457 | ajoin_with_sep(NewVars,',',NV), | |
| 1458 | ajoin_with_sep(DelVars,',',DV), | |
| 1459 | ajoin_with_sep(NewC,',',NC), | |
| 1460 | ajoin(['machine ',M,'\\n#',IDS,'=',V, ' (+',NewNr,',-', DelNr,')', | |
| 1461 | '\\nnew sees=',NC, | |
| 1462 | '\\nnew ',IDS,'=',NV, | |
| 1463 | '\\ndel ',IDS,'=',DV],ToolTip) | |
| 1464 | ; Label=M, ToolTip=M). | |
| 1465 | ||
| 1466 | machine_variables(M,Vars) :- machine_identifiers(M,_Params,_Sets,AVars,CVars,_AConsts,_CConsts), | |
| 1467 | append(CVars,AVars,RVars), % for Event-B: CVars=[] | |
| 1468 | maplist(get_raw_identifier,RVars,Vars). | |
| 1469 | %machine_constants(M,Consts) :- machine_identifiers(M,_Params,_Sets,_AVars,_CVars,AConsts,CConsts), | |
| 1470 | % append(CConsts,AConsts,Raw), | |
| 1471 | % maplist(get_raw_identifier,Raw,Consts). | |
| 1472 | machine_ids(M,Vars) :- machine_identifiers(M,_Params,Sets,AVars,CVars,AConsts,CConsts), | |
| 1473 | append([Sets,CConsts,CVars,AConsts,AVars],RVars), | |
| 1474 | maplist(get_raw_identifier,RVars,Vars). | |
| 1475 | machine_sets(M,Vars) :- machine_identifiers(M,_,Sets,_,_,_,_), | |
| 1476 | maplist(get_raw_identifier,Sets,Vars). | |
| 1477 | ||
| 1478 | var_exists_in_abstraction(M,Var) :- | |
| 1479 | var_exists_in_abstraction(M,_Anc,Var). | |
| 1480 | var_exists_in_abstraction(M,Anc,Var) :- | |
| 1481 | refines_machine(M,Anc), | |
| 1482 | machine_variables(Anc,AncVars), | |
| 1483 | member(Var,AncVars). | |
| 1484 | ||
| 1485 | refines_machine(M,Anc) :- | |
| 1486 | machine_references(M,Refs), | |
| 1487 | member(ref(refines,Anc,_),Refs). | |
| 1488 | ||
| 1489 | % variable or constant exists in abstraction | |
| 1490 | id_exists_in_abstraction(M,Var) :- | |
| 1491 | id_exists_in_abstraction(M,_Anc,Var). | |
| 1492 | id_exists_in_abstraction(M,Anc,Var) :- | |
| 1493 | refines_or_extends_machine(M,Anc), | |
| 1494 | machine_ids(Anc,AncVars), | |
| 1495 | member(Var,AncVars). | |
| 1496 | ||
| 1497 | refines_or_extends_machine(M,Anc) :- | |
| 1498 | machine_references(M,Refs), | |
| 1499 | (member(ref(refines,Anc,_),Refs) -> true ; member(ref(extends,Anc,_),Refs)). | |
| 1500 | ||
| 1501 | new_seen_context(M,Context) :- machine_references(M,Refs), | |
| 1502 | member(ref(sees,Context,_),Refs), | |
| 1503 | \+ (member(ref(refines,Anc,_),Refs), | |
| 1504 | sees_context(Anc,Context)). | |
| 1505 | ||
| 1506 | sees_context(M,Context) :- machine_references(M,Refs), member(ref(sees,Context,_),Refs). | |
| 1507 | ||
| 1508 | ||
| 1509 | write_dot_event_hierarchy_to_file(File) :- | |
| 1510 | write_dot_ref_hierarchy_to_file(event_refinement,File). | |
| 1511 | write_dot_variable_hierarchy_to_file(File) :- | |
| 1512 | (get_preference(dot_hierarchy_show_extra_detail,false) -> With=no_constants ; With=with_constants), | |
| 1513 | write_dot_ref_hierarchy_to_file(variable_refinement(With),File). | |
| 1514 | write_dot_ref_hierarchy_to_file(Kind,File) :- | |
| 1515 | analyze_extends_relation, | |
| 1516 | (get_preference(dot_event_hierarchy_horizontal,true) | |
| 1517 | -> PageOpts=[compound/true,rankdir/'LR',no_page_size|Opts] | |
| 1518 | ; PageOpts=[compound/true|Opts]), | |
| 1519 | Opts = [fontname/'Helvetica', nodesep/0.15, ranksep/0.6, | |
| 1520 | node_defaults/[fontname/'Helvetica'], edge_defaults/[fontname/'Helvetica', arrowsize/0.8] | |
| 1521 | | TitleAttrs], | |
| 1522 | (get_preference(dot_print_graph_title,true), main_machine_name(Main) | |
| 1523 | -> (Kind=event_refinement -> What='event refinement' ; What='variable refinement'), | |
| 1524 | ajoin([Main,' - ',What],Title), | |
| 1525 | get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS] | |
| 1526 | ; TitleAttrs = []), | |
| 1527 | gen_dot_graph(File,PageOpts, | |
| 1528 | use_new_dot_attr_pred(b_machine_hierarchy:dot_refinement_node_new(Kind)), | |
| 1529 | use_new_dot_attr_pred(b_machine_hierarchy:dot_refines_event(Kind)), | |
| 1530 | dot_no_same_rank,dot_subgraph(Kind)). | |
| 1531 | %gen_dot_graph(File,PageOpts,dot_event_node,dot_refines_event,dot_no_same_rank,dot_subgraph). | |
| 1532 | ||
| 1533 | % -------------------- | |
| 1534 | ||
| 1535 | reference_link(FromMachine,DestMachine) :- | |
| 1536 | machine_references(FromMachine,Refs), | |
| 1537 | filter_redundant_refs(Refs,Refs,UsefulRefs), | |
| 1538 | member(ref(_Type,DestMachine,_Prefix),UsefulRefs). | |
| 1539 | ||
| 1540 | :- use_module(library(ugraphs),[vertices_edges_to_ugraph/3, top_sort/2, transitive_closure/2, | |
| 1541 | min_paths/3, min_path/5, neighbours/3, del_vertices/3]). | |
| 1542 | ||
| 1543 | % b_machine_hierarchy:get_machine_topological_order(V) | |
| 1544 | % get machine in topological order of inclusion | |
| 1545 | get_machine_topological_order(SortedVertices) :- | |
| 1546 | get_machine_inclusion_graph(_Vertices,_Edges,Graph), | |
| 1547 | top_sort(Graph,SortedVertices). | |
| 1548 | ||
| 1549 | % get machine inclusion graph in format for ugraphs (unweighted graphs) library | |
| 1550 | get_machine_inclusion_graph(Vertices,Edges,Graph) :- | |
| 1551 | findall(Mach,machine_name(Mach),Vertices), | |
| 1552 | findall(From-To,reference_link(From,To),Edges), | |
| 1553 | vertices_edges_to_ugraph(Vertices, Edges, Graph). | |
| 1554 | ||
| 1555 | print_machine_topological_order :- | |
| 1556 | format('Topological sorting of B machine references~n',[]), | |
| 1557 | get_machine_inclusion_graph(Vertices,Edges,Graph), | |
| 1558 | length(Vertices,LenV),format(' * number of machines: ~w~n',[LenV]), | |
| 1559 | length(Edges,LenE), format(' * number of reference links: ~w~n',[LenE]), | |
| 1560 | top_sort(Graph,SortedVertices), | |
| 1561 | main_machine_name(Main), | |
| 1562 | %min_paths(Main,Graph,MinPaths), | |
| 1563 | transitive_closure(Graph,TGraph), | |
| 1564 | maplist(top_print_machine(Main,Graph,TGraph),SortedVertices), | |
| 1565 | greedy_cover(Main,Graph,TGraph,GreedyCover), length(GreedyCover,GLen), | |
| 1566 | get_rel_file_name(GreedyCover,FGreedy), | |
| 1567 | ajoin_with_sep(FGreedy,' ',GreedyList), % generate list which can be used for probcli or Makefile | |
| 1568 | format(' * sufficient includes (~w) to cover all machines: ~w~n',[GLen,GreedyList]), | |
| 1569 | ||
| 1570 | format(' (for each machine do: probcli FILE.mch -init -cache DIR)~n',[]). | |
| 1571 | ||
| 1572 | top_print_machine(Main,Graph,TGraph,M) :- | |
| 1573 | (machine_references(M,Refs) -> length(Refs,Len) ; Len=0), | |
| 1574 | neighbours(M,TGraph,N), length(N,Len2), | |
| 1575 | format(' ~w has ~w direct references and ~w indirect ones~n',[M,Len,Len2]), | |
| 1576 | min_path(Main, M, Graph, Path, Length), | |
| 1577 | format(' inclusion length ~w: ~w~n',[Length,Path]). | |
| 1578 | ||
| 1579 | get_rel_file_name([],[]). | |
| 1580 | get_rel_file_name([Machine|T],[File|TF]) :- | |
| 1581 | (b_get_relative_file_name_for_machine(Machine,File) -> true ; File=Machine), | |
| 1582 | get_rel_file_name(T,TF). | |
| 1583 | ||
| 1584 | greedy_cover(Main,Graph,TGraph,GreedyCover) :- | |
| 1585 | neighbours(Main,Graph,TopLevelIncludes), | |
| 1586 | greedy_cover(TopLevelIncludes,TGraph,GreedyCover). | |
| 1587 | ||
| 1588 | % compute a list of included machines which cover all required inclusions | |
| 1589 | % we try to make this inclusion minimal, using a greedy algorithm | |
| 1590 | greedy_cover([],_TGraph,Cover) :- !, Cover=[]. | |
| 1591 | greedy_cover(RemainingIncludes,TGraph, GreedyCover) :- | |
| 1592 | findall(candidate(NrCovered,Machine), | |
| 1593 | (member(Machine,RemainingIncludes), | |
| 1594 | neighbours(Machine,TGraph,N), length(N,NrCovered)), Cands), | |
| 1595 | (max_member(candidate(MaxNrCov,NextChoice),Cands) % pick the machine covering the most other ones | |
| 1596 | -> | |
| 1597 | (MaxNrCov =< 0 | |
| 1598 | -> GreedyCover = [] % everything is included already | |
| 1599 | ; GreedyCover = [NextChoice|TGreedyCover], | |
| 1600 | select(NextChoice,RemainingIncludes,Rem2), % this include is no longer available | |
| 1601 | neighbours(NextChoice,TGraph,NowCovered), | |
| 1602 | del_vertices(TGraph, [NextChoice|NowCovered], TGraph2), % delete all machines included by NextChoice | |
| 1603 | greedy_cover(Rem2,TGraph2,TGreedyCover) | |
| 1604 | ) | |
| 1605 | ; GreedyCover = [] % everything is included already; no node appears in the graph anymore | |
| 1606 | ). | |
| 1607 |