| 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 | :- module(bvisual, | |
| 5 | [ | |
| 6 | get_tree_from_expr/4, | |
| 7 | %get_tree_from_expr2/4, | |
| 8 | write_dot_graph_to_file/2, | |
| 9 | %print_dot_graph/1, | |
| 10 | get_any/5 | |
| 11 | %get_any_most_satisfier/5, | |
| 12 | ||
| 13 | ] | |
| 14 | ). | |
| 15 | ||
| 16 | :- meta_predicate visit_tree(5,3,-,-,-). | |
| 17 | ||
| 18 | :- use_module(probsrc(module_information),[module_info/2]). | |
| 19 | :- module_info(group,dot). | |
| 20 | :- module_info(description,'Generate a dot visualization of B predicates and expressions.'). | |
| 21 | ||
| 22 | :- use_module(library(lists), [maplist/3, scanlist/4]). | |
| 23 | %:- use_module(library(terms)). | |
| 24 | ||
| 25 | :- use_module(probsrc(bsyntaxtree)). | |
| 26 | :- use_module(probsrc(translate), [translate_bvalue_with_limit/3, | |
| 27 | translate_bexpression_with_limit/3]). | |
| 28 | :- use_module(probsrc(kernel_waitflags), [ | |
| 29 | init_wait_flags/2, | |
| 30 | ground_wait_flags/1]). | |
| 31 | ||
| 32 | :- use_module(dotsrc(bvisual_any_maxsolver)). | |
| 33 | :- use_module(probsrc(error_manager)). | |
| 34 | :- use_module(probsrc(preferences)). | |
| 35 | ||
| 36 | ||
| 37 | %:- use_module(probsrc(self_check)). | |
| 38 | ||
| 39 | ||
| 40 | ||
| 41 | % PARAMETERS | |
| 42 | ||
| 43 | max_chars_per_line(40). | |
| 44 | max_lines(10). | |
| 45 | max_chars_total(400). % :- max_chars_per_line(X), max_lines(ML), T is X*ML. | |
| 46 | ||
| 47 | %% | |
| 48 | % Tree format: | |
| 49 | % Trees used by write_dot_graph_to_file/2 are modelled by the bind/3 | |
| 50 | % predicate: | |
| 51 | % bind(Children, NewLocalState, Metas). | |
| 52 | % Children is a list of subexpressions, whose numbers may be greater than two. | |
| 53 | % NewLocalState is a new local state for the subexpressions. | |
| 54 | % Metas are informations about the expression, like value, label etc. | |
| 55 | % | |
| 56 | % Important: Avoid access bind directly since it's structure may | |
| 57 | % be changed. Use accessors defined below. | |
| 58 | % They provide a convenient way to access the meta informations. | |
| 59 | % | |
| 60 | % Use get_tree_from_expr(Tree, Expr, LocalStale, State) to gain | |
| 61 | % a bind based tree from a predicate or expression evaluated in LocalState | |
| 62 | % and State. | |
| 63 | % | |
| 64 | % Use get_tree_from_expr2(State, LocalState, TExpr, Tree) to inspect the | |
| 65 | % predicate or expression only without walking down the children recursively. | |
| 66 | % | |
| 67 | ||
| 68 | %% | |
| 69 | % accessors & shortcuts | |
| 70 | % | |
| 71 | ||
| 72 | create_long_tree_node(bind(Children, Label, Type, Value, LongDesc), | |
| 73 | Children, Label, Type, Value, LongDesc). | |
| 74 | get_tree_node_info(NODE, Children, NewLocalState, Metas) :- | |
| 75 | (create_tree_node(NODE, C,N,M) -> Children=C, NewLocalState=N, Metas=M | |
| 76 | ; add_error(bvisual,'Illegal node: ',NODE)). | |
| 77 | create_tree_node(bind(Children, NewLocalState, Metas), | |
| 78 | Children, NewLocalState, Metas). | |
| 79 | ||
| 80 | ||
| 81 | % bind([],cv_o,nonboolean,137,b(identifier(cv_o),integer,[nodeid(pos(139677,56,192,18,192,21))])) | |
| 82 | ||
| 83 | has_no_children(Node) :- child_nodes(Node,[]). | |
| 84 | ||
| 85 | child_nodes(bind(ChildNodes,_,_,_,_), ChildNodes). | |
| 86 | child_nodes(bind(ChildNodes,_,_), ChildNodes). | |
| 87 | ||
| 88 | label(bind(_,Label,_,_,_), Label). | |
| 89 | label(bind(_,_, Metas), Val) :- member(label(Val), Metas),!. | |
| 90 | ||
| 91 | value(bind(_,_,_,Val,_), Val). | |
| 92 | value(bind(_,_, Metas), Val) :- member(value(Val), Metas),!. | |
| 93 | ||
| 94 | long_desc(bind(_,_,_,_,Val), Val). | |
| 95 | long_desc(bind(_,_, Metas), Val):- member(long_desc(Val), Metas),!. | |
| 96 | ||
| 97 | is_boolean(bind(_,_,boolean,_,_)). | |
| 98 | is_boolean(bind(_,_, Metas)):- member(type(boolean), Metas),!. | |
| 99 | ||
| 100 | %type(bind(_,_, Metas), Val):- member(type(Val), Metas),!. | |
| 101 | ||
| 102 | is_explicit_value_node(Node) :- long_desc(Node,Val), is_explicit_value(Val). | |
| 103 | ||
| 104 | %% | |
| 105 | % Evaluate an expression and it's subexpressions and | |
| 106 | % gain their values as a tree. | |
| 107 | % | |
| 108 | :- use_module(probsrc(tools),[catch_call/1]). | |
| 109 | :- use_module(probsrc(b_global_sets),[add_prob_deferred_set_elements_to_store/3]). | |
| 110 | ||
| 111 | ||
| 112 | get_tree_from_expr(Tree, Expr, LocalState, State) :- | |
| 113 | ground_store(LocalState), ground_store(State), | |
| 114 | (get_tree_from_expr0(R, Expr, LocalState, State) -> Tree=R | |
| 115 | ; add_error(bvisual,'Failed to construct formula visualisation:',Expr), | |
| 116 | Tree = bind([],'FAILED TO CONSTRUCT TREE',error,'UNKNOWN','UNKNOWN') | |
| 117 | ). | |
| 118 | ||
| 119 | % ground store, to ensure that no variables are left free; should normally not be necessary | |
| 120 | % if we have free variables we get confusing visualisations, where different values are chosen for different subformulas | |
| 121 | ground_store([]). | |
| 122 | ground_store([bind(_,Val)|T]) :- ground_to_any_value(Val), ground_store(T). | |
| 123 | ||
| 124 | :- use_module(probsrc(b_global_sets),[b_get_fd_type_bounds/3]). | |
| 125 | ground_to_any_value([H|T]) :- !, ground_to_any_value(H), ground_to_any_value(T). | |
| 126 | ground_to_any_value((A,B)) :- !, ground_to_any_value(A), ground_to_any_value(B). | |
| 127 | ground_to_any_value(fd(X,Type)) :- var(X),!, b_get_fd_type_bounds(Type,Low,_),X=Low. | |
| 128 | ground_to_any_value(int(X)) :- var(X),!, X=0. | |
| 129 | ground_to_any_value(term(floating(X))) :- var(X),!, X=0.0. | |
| 130 | ground_to_any_value(string(X)) :- var(X),!, X='some_string'. | |
| 131 | ground_to_any_value(rec(Fields)) :- !, ground_to_any_value(Fields). | |
| 132 | ground_to_any_value(field(_,V)) :- !, ground_to_any_value(V). | |
| 133 | % TO DO: free types | |
| 134 | ground_to_any_value(_). | |
| 135 | ||
| 136 | ||
| 137 | get_tree_from_expr0(Tree, Expr, LocalState, State) :- | |
| 138 | temp_set_prefs(CHNG), | |
| 139 | add_prob_deferred_set_elements_to_store(State,State1,visible), | |
| 140 | call_cleanup(catch_call(get_tree_from_expr3(State1, LocalState, Expr, Tree)), | |
| 141 | reset_prefs(CHNG)). | |
| 142 | % catch: as this can sometimes generate resource_error(memory) error exceptions | |
| 143 | ||
| 144 | temp_set_prefs([CHNG,Level]) :- | |
| 145 | enter_new_clean_error_scope(Level,temp_set_prefs), | |
| 146 | (get_preference(dot_use_unicode,true) -> translate:set_unicode_mode ; true), | |
| 147 | temporary_set_preference(raise_abort_immediately,false,CHNG). | |
| 148 | reset_prefs([CHNG,Level]) :- | |
| 149 | clear_events_in_error_scope(Level,_), % avoid errors propagating into outside | |
| 150 | exit_error_scope(Level,_ErrOccured,bvisual), | |
| 151 | reset_temporary_preference(raise_abort_immediately, CHNG), | |
| 152 | (get_preference(dot_use_unicode,true) -> translate:unset_unicode_mode ; true). | |
| 153 | ||
| 154 | % whole tree at once | |
| 155 | get_tree_from_expr3(State, LocalState, TExpr, Tree) :- rewrite_expression(TExpr,NewTExpr),!, | |
| 156 | get_tree_from_expr3(State, LocalState, NewTExpr, Tree). | |
| 157 | get_tree_from_expr3(State, LocalState, Expr, Tree) :- | |
| 158 | get_tree_from_expr2(State, LocalState, Expr, NewTree), | |
| 159 | get_tree_node_info(NewTree, SubExprs, NewLocalState, _), | |
| 160 | label(NewTree, Label), | |
| 161 | value(NewTree, Value), | |
| 162 | create_long_tree_node(Tree, Children, Label, Type, Value, Expr), | |
| 163 | % print(value(Value)),nl, | |
| 164 | ( is_boolean(NewTree) -> Type = boolean ; Type = nonboolean ), | |
| 165 | !, | |
| 166 | maplist(get_tree_from_expr3(State, NewLocalState), SubExprs, Children). | |
| 167 | ||
| 168 | ||
| 169 | ||
| 170 | ||
| 171 | % only one layer | |
| 172 | %get_tree_from_expr2(State, LocalState, b(B,T,_), Tree) :- functor(B,F,N), print(visit(F,N,T)),nl,fail. | |
| 173 | get_tree_from_expr2(State, LocalState, TExpr, Tree) :- | |
| 174 | get_texpr_expr(TExpr, Expr), | |
| 175 | functor(Expr, Op, _Arity), | |
| 176 | x_is_quantifier_like(Op),!, | |
| 177 | x_get_metas(State, LocalState, TExpr, Metas), | |
| 178 | member(value(Value), Metas), | |
| 179 | (create_new_local_state(TExpr, Value, State, LocalState, NewLocalState) -> | |
| 180 | x_get_sub_expressions(TExpr, State, LocalState, Children), | |
| 181 | create_tree_node(Tree, Children, NewLocalState, Metas) | |
| 182 | ; | |
| 183 | create_tree_node(Tree, [], LocalState, Metas) | |
| 184 | ),!. | |
| 185 | get_tree_from_expr2(State, LocalState, TExpr, Tree):- | |
| 186 | x_get_metas(State, LocalState, TExpr, Metas), | |
| 187 | x_get_sub_expressions(TExpr, State, LocalState, Children), | |
| 188 | create_tree_node(Tree, Children, LocalState, Metas),!. | |
| 189 | ||
| 190 | ||
| 191 | ||
| 192 | %% | |
| 193 | % Get all meta information for an expression/predicate | |
| 194 | % | |
| 195 | x_get_metas(State, LocalState, TExpr, Metas):- | |
| 196 | findall(MetaValue, x_get_meta(State, LocalState, TExpr, MetaValue), Metas). | |
| 197 | ||
| 198 | ||
| 199 | :- use_module(probsrc(eval_interface),[get_value_string_of_formula/5, b_compute_expression_no_wd/4]). | |
| 200 | %% | |
| 201 | % Helpers, each single meta info. | |
| 202 | % There are value/1, type/1, label/1 and long_desc/1 | |
| 203 | % | |
| 204 | % compute the various attributes of a node: | |
| 205 | x_get_meta(State, LocalState, Expr, value(Value)):- | |
| 206 | max_chars_total(Lim), | |
| 207 | get_value_string_of_formula(State, LocalState, Expr, Lim, Value). | |
| 208 | x_get_meta(_State, _LocalState, Expr, type(Type)):- | |
| 209 | ( check_if_typed_predicate(Expr) -> | |
| 210 | Type = boolean | |
| 211 | ; | |
| 212 | Type = nonboolean | |
| 213 | ). | |
| 214 | x_get_meta(_State, _LocalState, Expr, label(Label)):- | |
| 215 | once(x_get_label(Expr, Label)). | |
| 216 | x_get_meta(_State, _LocalState, TExpr, long_desc(Expr)):- | |
| 217 | get_texpr_expr(TExpr, Expr). | |
| 218 | x_get_meta(_State, _LocalState, TExpr, rodin_label(Label)):- | |
| 219 | once(get_texpr_label(TExpr,Label)). | |
| 220 | ||
| 221 | ||
| 222 | ||
| 223 | %% | |
| 224 | % Generate a new local state for subexpressions if necessary, like a forall | |
| 225 | % predicate. called for x_is_quantifier/1 predicate. | |
| 226 | % | |
| 227 | ||
| 228 | create_new_local_state(TExpr, Value, State, LocalState, NewLocalState):- | |
| 229 | get_texpr_expr(TExpr, Expr), | |
| 230 | (create_new_local_state2(Expr, Value, State, LocalState, NewLocalState) | |
| 231 | -> true | |
| 232 | ; %trace, create_new_local_state2(Expr, Value, State, LocalState, NewLocalState), | |
| 233 | fail). | |
| 234 | ||
| 235 | :- use_module(probsrc(bsyntaxtree),[create_typed_id/3, create_typed_ids/3]). | |
| 236 | create_new_local_state2(forall(IDs, LHS, _), true, State, LocalState, NewLocalState) :- !, | |
| 237 | (get_any(IDs, LHS, State, LocalState, NewLocalState) | |
| 238 | -> true /* there is a solution for LHS */ | |
| 239 | ; /* Forall true because no solution for LHS */ | |
| 240 | get_any(IDs, b(truth,pred,[]), State, LocalState, NewLocalState) | |
| 241 | ). | |
| 242 | create_new_local_state2(forall(IDs, LHS, RHS), false, State, LocalState, NewLocalState) :- !, | |
| 243 | safe_create_texpr(negation(RHS), pred, NotRHS), | |
| 244 | conjunct_predicates([LHS, NotRHS], Condition), | |
| 245 | get_any(IDs, Condition, State, LocalState, NewLocalState). | |
| 246 | create_new_local_state2(let_expression(Ids,AssignmentExprs,Expr), Value, State, LocalState, NewLocalState) :- !, | |
| 247 | create_typed_id('$VALUE',EType,TID), get_texpr_type(Expr,EType), | |
| 248 | construct_pred([TID],[Expr],[],Preds0), | |
| 249 | construct_pred(Ids,AssignmentExprs,Preds0,Preds), | |
| 250 | conjunct_predicates(Preds,NewPred), | |
| 251 | EXISTS = exists([TID|Ids],NewPred), %translate:print_bexpr(b(EXISTS,pred,[])),nl, | |
| 252 | (Value=unknown -> ExValue = undefined ; ExValue=true), | |
| 253 | create_new_local_state2(EXISTS, ExValue, State, LocalState, NewLocalState). | |
| 254 | create_new_local_state2(let_predicate(Ids,AssignmentExprs,Pred), Value, State, LocalState, NewLocalState) :- | |
| 255 | construct_pred(Ids,AssignmentExprs,[Pred],Preds), | |
| 256 | conjunct_predicates(Preds,NewPred), | |
| 257 | EXISTS = exists(Ids,NewPred), | |
| 258 | create_new_local_state2(EXISTS, Value, State, LocalState, NewLocalState). | |
| 259 | create_new_local_state2(exists(IDs, LHS), true, State, LocalState, NewLocalState) :- !, | |
| 260 | get_any(IDs, LHS, State, LocalState, NewLocalState). | |
| 261 | create_new_local_state2(exists(IDs, LHS), Status, State, LocalState, NewLocalState) :- | |
| 262 | (Status=false ; Status=undefined),!, | |
| 263 | % for Status=undefined get_any can fail but get_any_most_satisfier should always succeed | |
| 264 | get_any_most_satisfier(IDs, LHS, State, LocalState, NewLocalState). | |
| 265 | create_new_local_state2(X, undefined, State, LocalState, NewLocalState) :- !, | |
| 266 | create_new_local_state2(X, true, State, LocalState, NewLocalState). | |
| 267 | create_new_local_state2(X, x_both_true_and_not_true, State, LocalState, NewLocalState) :- | |
| 268 | %print(x_both_true_and_not_true(X)),nl, % probably means a PROB BUG ! | |
| 269 | (X = forall(_,_,_) -> VAL=false ; VAL=true), | |
| 270 | create_new_local_state2(X, VAL, State, LocalState, NewLocalState). | |
| 271 | create_new_local_state2(comprehension_set(IDs, Body), Val, State, LocalState, NewLocalState) :- !, | |
| 272 | % TODO: we could extract values from the context; e.g., if we have x:{y|Pred} we could use value assigned to x for y | |
| 273 | % format(user_output,'Comprehension set ~w, Val=~w~n~n',[IDs,Val]), | |
| 274 | (is_empty_set_value(Val) -> | |
| 275 | get_any_most_satisfier(IDs, Body, State, LocalState, NewLocalState) | |
| 276 | % we could have a symbolic value; which we do not know if it is empty or not; Body could also have a WD error | |
| 277 | ; get_any_if_possible(IDs, Body, State, LocalState, NewLocalState)). | |
| 278 | create_new_local_state2(general_sum(IDs, CondPred, _Expr), _Val, State, LocalState, NewLocalState) :- !, | |
| 279 | get_any_if_possible(IDs, CondPred, State, LocalState, NewLocalState). | |
| 280 | create_new_local_state2(general_product(IDs, CondPred, _Expr), _Val, State, LocalState, NewLocalState) :- !, | |
| 281 | get_any_if_possible(IDs, CondPred, State, LocalState, NewLocalState). | |
| 282 | create_new_local_state2(quantified_union(IDs, CondPred, _Expr), _Val, State, LocalState, NewLocalState) :- !, | |
| 283 | get_any_if_possible(IDs, CondPred, State, LocalState, NewLocalState). | |
| 284 | create_new_local_state2(quantified_intersection(IDs, CondPred, _Expr), _Val, State, LocalState, NewLocalState) :- !, | |
| 285 | get_any_if_possible(IDs, CondPred, State, LocalState, NewLocalState). | |
| 286 | %create_new_local_state2(value(StoredVal), Val, State, LocalState, NewLocalState) :- nonvar(StoredVal), | |
| 287 | % StoredVal = closure(P,T,Body), | |
| 288 | % !, | |
| 289 | % create_typed_ids(P,T,IDs), | |
| 290 | % create_new_local_state2(comprehension_set(IDs, Body), Val, State, LocalState, NewLocalState). | |
| 291 | create_new_local_state2(Expr, Value, _State, _LocalState, _NewLocalState) :- | |
| 292 | add_error(bvisual,'Uncovered Expr in create_new_local_state: ',Expr:Value), | |
| 293 | fail. | |
| 294 | ||
| 295 | % a bit of a hack; it would be better to look at the Prolog version of the value | |
| 296 | is_empty_set_value('{}'). | |
| 297 | is_empty_set_value('[]'). | |
| 298 | is_empty_set_value(X) :- translate:unicode_translation(empty_set,X). | |
| 299 | ||
| 300 | % construct predicate from list of typed ids and expressions | |
| 301 | construct_pred([],[],TailPreds,TailPreds). | |
| 302 | construct_pred([ID|T],[EXPR|ET],TailPreds,[b(equal(ID,EXPR),pred,[])|TR]) :- | |
| 303 | construct_pred(T,ET,TailPreds,TR). | |
| 304 | ||
| 305 | ||
| 306 | :- use_module(probsrc(custom_explicit_sets),[is_interval_closure/3]). | |
| 307 | % rewrite rules to transform some values into a formula tree | |
| 308 | rewrite_expression(b(value(StoredVal),Type,I),b(comprehension_set(IDs, Body),Type,[compiled_closure|I])) :- | |
| 309 | nonvar(StoredVal), | |
| 310 | StoredVal = closure(P,T,Body), | |
| 311 | \+ is_interval_closure(StoredVal,_,_), | |
| 312 | create_typed_ids(P,T,IDs). | |
| 313 | ||
| 314 | ||
| 315 | %% | |
| 316 | % Get subexpressions, merge sub expressions with associative operators, like | |
| 317 | % 'and'. For example the predicate and(A,and(B,C)) has three sub expressions: | |
| 318 | % A, B and C. This behaviour is controlled by x_associative_operator/1. | |
| 319 | % | |
| 320 | ||
| 321 | x_get_sub_expressions(TExpr, Children) :- | |
| 322 | x_get_sub_expressions(TExpr, [], [], Children). | |
| 323 | ||
| 324 | x_get_sub_expressions(TExpr, _, _, Children) :- nonvar(TExpr), | |
| 325 | do_not_expand(TExpr), !, | |
| 326 | Children=[]. | |
| 327 | x_get_sub_expressions(TExpr, State, LocalState, Children) :- | |
| 328 | syntaxtraversion(TExpr,Expr,_,_,Children0,_), | |
| 329 | % add custom explanation rules for certain nodes: | |
| 330 | x_get_custom_explanation_subexpressions(Expr,State, LocalState, Children0,Children). | |
| 331 | ||
| 332 | ||
| 333 | do_not_expand(b(equal(A,B),pred,_)) :- get_preference(pp_propositional_logic_mode,true), | |
| 334 | A = b(identifier(_),boolean,_), % do not expand such nodes | |
| 335 | is_explicit_value(B). | |
| 336 | ||
| 337 | :- use_module(library(lists)). | |
| 338 | % is a value which does not need to be further decomposed | |
| 339 | is_explicit_value(b(Val,_,_)) :- is_explicit_value2(Val). | |
| 340 | is_explicit_value2(integer(_N)). | |
| 341 | is_explicit_value2(string(_S)). | |
| 342 | is_explicit_value2(boolean_true). | |
| 343 | is_explicit_value2(boolean_false). | |
| 344 | is_explicit_value2(identifier(N)) :- b_global_sets:lookup_global_constant(N,_). | |
| 345 | is_explicit_value2(couple(A,B)) :- is_explicit_value(A), is_explicit_value(B). | |
| 346 | is_explicit_value2(value(_)). | |
| 347 | % to do: add further cases | |
| 348 | ||
| 349 | ||
| 350 | :- use_module(library(timeout),[time_out/3]). | |
| 351 | x_get_custom_explanation_subexpressions(function(Fun,Value),State,LocalState,_C0,Children) :- | |
| 352 | % a custom rule for function application of closures | |
| 353 | % print(function(Fun)),nl, print(value(Value)),nl,print(C0),nl, | |
| 354 | simple_value(Fun), | |
| 355 | get_preference(formula_tree_minimal_timeout,TOVAL), | |
| 356 | time_out(b_compute_expression_no_wd(Fun, LocalState, State, FunValue),TOVAL,TRes), TRes \== time_out, | |
| 357 | FunValue = closure(P,T,Pred), | |
| 358 | P = [X,_Res], % TO DO: extend to functions with multiple arguments | |
| 359 | closures:is_lambda_closure(P,T,Pred, _OtherIDs,_OtherTypes, DomainPred, Expr),!, | |
| 360 | bsyntaxtree:replace_id_by_expr(Expr,X,Value,NewExpr), | |
| 361 | bsyntaxtree:replace_id_by_expr(DomainPred,X,Value,NewDomainPred), | |
| 362 | Children = [Fun,Value, NewDomainPred,NewExpr]. | |
| 363 | % we could do similar treatment for member(Tuple,Fun) | |
| 364 | x_get_custom_explanation_subexpressions(Expr,_State,_LocalState, Children0,Children) :- | |
| 365 | x_get_custom_explanation_subexpressions(Expr, Children0,Children). | |
| 366 | ||
| 367 | ||
| 368 | ||
| 369 | simple_value(b(V,_,_)) :- simple_value_aux(V). | |
| 370 | simple_value_aux(identifier(_)). | |
| 371 | simple_value_aux(value(_)). | |
| 372 | ||
| 373 | :- use_module(probsrc(typing_tools),[create_maximal_type_set/2]). | |
| 374 | ||
| 375 | % TO DO: add explanation rules, e.g., f:A --> B ==> f:TA +-> B & dom(f)=A ,... | |
| 376 | x_get_custom_explanation_subexpressions(Expr,_Children0, Children):- | |
| 377 | functor(Expr, Op, _), | |
| 378 | x_operator_with_local_variables(Op),!,Children=[]. | |
| 379 | x_get_custom_explanation_subexpressions(Expr,Children0, Children):- | |
| 380 | functor(Expr, Op, _), Op = set_extension, | |
| 381 | length(Children0,Len), (Len>50 ; maplist(is_explicit_value,Children0)), | |
| 382 | % can be a very large explicit node; no sense in expanding and showing every element | |
| 383 | % print(no_child(Expr)),nl, | |
| 384 | % TO DO: check if simple value | |
| 385 | !,Children=[]. | |
| 386 | x_get_custom_explanation_subexpressions(Expr,Children0, Children):- | |
| 387 | functor(Expr, Op, _), | |
| 388 | x_associative_operator(Op),!, | |
| 389 | maplist(x_collect_exprs_with_op(Op), Children0, Children1), | |
| 390 | scanlist(x_append_reverse, Children1, [], Children). | |
| 391 | x_get_custom_explanation_subexpressions(member(X,b(partial_function(Dom,Range),T,Info)),_,Children) :- | |
| 392 | T=set(set(couple(DomT,RanT))), | |
| 393 | %print(pf(Dom,Range)),nl, | |
| 394 | (\+ bsyntaxtree:is_just_type(Dom) ; \+ bsyntaxtree:is_just_type(Range)), | |
| 395 | nonmember(already_expanded,Info), | |
| 396 | % otherwise we may have an infinite recursion; maybe better to add other operator | |
| 397 | %print(not_type),nl,fail, | |
| 398 | !, | |
| 399 | Children = [A|TB], | |
| 400 | create_maximal_type_set(DomT,DomType), | |
| 401 | create_maximal_type_set(RanT,RanType), | |
| 402 | A = b(member(X,b(partial_function(DomType,RanType),T,[already_expanded|Info])),pred,[]), | |
| 403 | add_subset_check(b(domain(X),set(DomT),[]), Dom, TB,TC), | |
| 404 | add_subset_check(b(range(X),set(RanT),[]), Range, TC,[]), | |
| 405 | TB \= [],!. | |
| 406 | x_get_custom_explanation_subexpressions(member(X,b(total_function(Dom,Range),T,Info)),_,Children) :- | |
| 407 | T=set(set(couple(DomT,RanT))), !, | |
| 408 | Children = [A,B|TC], | |
| 409 | %print(total_fun_check(DomT,RanT)),nl, | |
| 410 | create_maximal_type_set(DomT,DomType), | |
| 411 | create_maximal_type_set(RanT,RanType), | |
| 412 | A = b(member(X,b(partial_function(DomType,RanType),T,Info)),pred,[]), | |
| 413 | B = b(equal(b(domain(X),set(DomT),[]), Dom),pred,[]), | |
| 414 | add_subset_check(b(range(X),set(RanT),[]), Range, TC,[]). | |
| 415 | x_get_custom_explanation_subexpressions(member(X,b(total_surjection(Dom,Range),T,Info)),_,Children) :- | |
| 416 | T=set(set(couple(DomT,RanT))), !, | |
| 417 | Children = [A,B,C], | |
| 418 | create_maximal_type_set(DomT,DomType), | |
| 419 | create_maximal_type_set(RanT,RanType), | |
| 420 | A = b(member(X,b(partial_function(DomType,RanType),T,Info)),pred,[]), | |
| 421 | B = b(equal(b(domain(X),set(DomT),[]), Dom),pred,[]), | |
| 422 | C = b(equal(b(range(X),set(RanT),[]), Range),pred,[]). | |
| 423 | x_get_custom_explanation_subexpressions(member(X,b(partial_surjection(Dom,Range),T,Info)),_,Children) :- | |
| 424 | T=set(set(couple(DomT,RanT))), !, | |
| 425 | Children = [A|TB], | |
| 426 | create_maximal_type_set(DomT,DomType), | |
| 427 | create_maximal_type_set(RanT,RanType), | |
| 428 | A = b(member(X,b(partial_function(DomType,RanType),T,Info)),pred,[]), | |
| 429 | add_subset_check(b(domain(X),set(DomT),[]), Dom, TB,[C]), | |
| 430 | C = b(equal(b(range(X),set(RanT),[]), Range),pred,[]). | |
| 431 | x_get_custom_explanation_subexpressions(Expr,Children, Res) :- | |
| 432 | functor(Expr,Functor,_), | |
| 433 | (filter_trivial_children_for(Functor) -> exclude(is_explicit_value,Children,Res) ; Res=Children). | |
| 434 | ||
| 435 | filter_trivial_children_for(let_predicate). % avoid we see trivial values twice | |
| 436 | ||
| 437 | ||
| 438 | ||
| 439 | add_subset_check(LHS,RHS,Result,EndOfList) :- | |
| 440 | % add subset check, unless "obvious" | |
| 441 | (bsyntaxtree:is_just_type(RHS) -> Result=EndOfList | |
| 442 | ; Result = [b(subset(LHS, RHS),pred,[])|EndOfList] | |
| 443 | ). | |
| 444 | ||
| 445 | x_collect_exprs_with_op(Op, TExpr, Children):- | |
| 446 | syntaxtraversion(TExpr,Expr,_,_,_,_), | |
| 447 | ( functor(Expr, Op, _), | |
| 448 | \+ get_texpr_label(TExpr,_) % do not expand out if there is a separate label for the sub-conjunct/disjunct | |
| 449 | -> x_get_sub_expressions(TExpr, Children) | |
| 450 | ; Children = [TExpr] | |
| 451 | ). | |
| 452 | ||
| 453 | x_append_reverse(X,Y,L):- append(Y,X,L). | |
| 454 | ||
| 455 | ||
| 456 | ||
| 457 | %% | |
| 458 | % Get label for an expression | |
| 459 | % | |
| 460 | ||
| 461 | x_get_label(b(Expr,_,Infos), Label) :- | |
| 462 | x_get_label_aux(Expr,Infos,Label). | |
| 463 | x_get_label_aux(comprehension_set(_,_),[compiled_closure|_],Label) :- !, % generated by rewrite_expression | |
| 464 | Label= 'comprehension_set (compiled closure)'. | |
| 465 | x_get_label_aux(value(V), _, Label) :- !, get_value_label(V,Label). | |
| 466 | x_get_label_aux(external_function_call(Name,_),_Infos,Label) :- !, | |
| 467 | Label=Name. | |
| 468 | x_get_label_aux(external_pred_call(Name,_),_Infos,Label) :- !, | |
| 469 | Label=Name. | |
| 470 | x_get_label_aux(Expr, _, Label) :- | |
| 471 | %Expr =.. [Op, Label], | |
| 472 | functor(Expr,Op,1), arg(1,Expr,Label), | |
| 473 | x_literal_value_operator(Op). | |
| 474 | x_get_label_aux(Expr, _ , Label):- | |
| 475 | functor(Expr, Fkt, _), | |
| 476 | x_functor_to_label(Fkt, Label). | |
| 477 | ||
| 478 | get_value_label(V,Label) :- var(V),!, Label='VAR-VALUE'. | |
| 479 | %get_value_label(global_set(GS),Label) :- !, Label=GS. % should we pretty print the value | |
| 480 | %get_value_label(V,stored_value(F)) :- functor(V,F,_N). | |
| 481 | get_value_label(V,Label) :- translate_bvalue_with_limit(V,40,Label). | |
| 482 | %% | |
| 483 | % Helpers | |
| 484 | % | |
| 485 | ||
| 486 | %% | |
| 487 | % Maps functor of prolog term to correct label | |
| 488 | % | |
| 489 | :- use_module(probsrc(translate),[translate_prolog_constructor_in_mode/2]). | |
| 490 | x_functor_to_label(F,R) :- translate_prolog_constructor_in_mode(F,PPVersion), !, R=PPVersion. | |
| 491 | %x_functor_to_label(conjunct, conjunction):-!. | |
| 492 | %x_functor_to_label(disjunct, disjunction):-!. | |
| 493 | x_functor_to_label(function, R):-!, R='func.applic.'. | |
| 494 | x_functor_to_label(X, X). | |
| 495 | ||
| 496 | %% | |
| 497 | % List of operators which may generate a new local state, | |
| 498 | % like a forall predicate. | |
| 499 | % | |
| 500 | x_is_quantifier_like(comprehension_set). | |
| 501 | x_is_quantifier_like(general_sum). | |
| 502 | x_is_quantifier_like(general_product). | |
| 503 | x_is_quantifier_like(quantified_union). | |
| 504 | x_is_quantifier_like(quantified_intersection). | |
| 505 | x_is_quantifier_like(X) :- x_is_quantifier(X). | |
| 506 | ||
| 507 | x_is_quantifier(forall). | |
| 508 | x_is_quantifier(exists). | |
| 509 | x_is_quantifier(let_predicate). | |
| 510 | x_is_quantifier(let_expression). | |
| 511 | ||
| 512 | x_is_quantifier(true_guard). | |
| 513 | ||
| 514 | %% | |
| 515 | % List of associative operators | |
| 516 | % | |
| 517 | ||
| 518 | x_associative_operator(conjunct). | |
| 519 | x_associative_operator(disjunct). | |
| 520 | x_associative_operator(add). | |
| 521 | x_associative_operator(mul). | |
| 522 | ||
| 523 | ||
| 524 | %% | |
| 525 | % List of operators with local variables | |
| 526 | % | |
| 527 | ||
| 528 | % x_operator_with_local_variables(comprehension_set). is now supported in create_new_local_state2 | |
| 529 | % x_operator_with_local_variables(let_predicate). ditto, and for let_expression | |
| 530 | %x_operator_with_local_variables(general_sum). | |
| 531 | %x_operator_with_local_variables(general_product). | |
| 532 | %x_operator_with_local_variables(quantified_union). | |
| 533 | %x_operator_with_local_variables(quantified_intersection). | |
| 534 | x_operator_with_local_variables(lambda). | |
| 535 | x_operator_with_local_variables(lazy_let_expr). | |
| 536 | x_operator_with_local_variables(lazy_let_pred). | |
| 537 | x_operator_with_local_variables(lazy_let_subst). | |
| 538 | ||
| 539 | ||
| 540 | %% | |
| 541 | % List of literal values | |
| 542 | % | |
| 543 | x_literal_value_operator(identifier). | |
| 544 | x_literal_value_operator(value). | |
| 545 | x_literal_value_operator(integer). | |
| 546 | x_literal_value_operator(string). | |
| 547 | ||
| 548 | ||
| 549 | ||
| 550 | ||
| 551 | ||
| 552 | ||
| 553 | %% | |
| 554 | % Try to execute an 'Any' statement on LocalState and State. | |
| 555 | % New state with the values is returned by NewLocalState. | |
| 556 | % Compare to b_interpreter:b_execute_statement2(any(Parameters,PreCond,Body),...) | |
| 557 | % | |
| 558 | ||
| 559 | :- use_module(probsrc(b_enumerate), [b_tighter_enumerate_values_in_ctxt/3]). | |
| 560 | :- use_module(probsrc(b_interpreter), [set_up_typed_localstate/6]). | |
| 561 | :- use_module(probsrc(error_manager), [add_message/4, time_out_with_enum_warning_one_solution/3, is_time_out_result/1]). | |
| 562 | ||
| 563 | ||
| 564 | ||
| 565 | get_any(IDs, Condition, State, LocalState, NewLocalState) :- | |
| 566 | catch(get_any1(IDs, Condition, State, LocalState, NewLocalState), E, | |
| 567 | (add_warning(bvisual,'Exception in bvisual while treating quantfier predicate: ',E),fail)). | |
| 568 | get_any1(IDs, C, S, LS, NewLS) :- | |
| 569 | % used to be: on_enumeration_warning(bvisual:get_any2(IDs, C, S, LS, NewLS), fail). | |
| 570 | % call below does not force throwing enum warnings, which is ok as we just want one solution: | |
| 571 | %BackupCall = fail, AddAsEventError = false, | |
| 572 | %catch_enumeration_warning_exceptions(bvisual:get_any2(IDs, C, S, LS, NewLS),BackupCall, AddAsEventError, C), | |
| 573 | get_preference(formula_tree_maximal_timeout,MaxTO), | |
| 574 | time_out_with_enum_warning_one_solution(bvisual:get_any2(IDs, C, S, LS, NewLS),MaxTO,Res), | |
| 575 | \+ is_time_out_result(Res). | |
| 576 | get_any2(IDs, Condition, State, LocalState, NewLocalState) :- | |
| 577 | set_up_typed_localstate(IDs, _FreshVars, TypedVals, LocalState, NewLocalState,positive), | |
| 578 | init_wait_flags(WF,[get_any2]), | |
| 579 | b_interpreter:b_test_boolean_expression(Condition,NewLocalState, State, WF), /* check WHERE */ | |
| 580 | b_tighter_enumerate_values_in_ctxt(TypedVals,Condition,WF), | |
| 581 | ground_wait_flags(WF). | |
| 582 | ||
| 583 | ||
| 584 | get_any_most_satisfier(IDs, Condition, State, LocalState, NewLocalState):- | |
| 585 | ( any_maxsolver(IDs, Condition, State, LocalState, NewLocalState) -> | |
| 586 | true | |
| 587 | ; get_typing_solution(IDs, State, LocalState, NewLocalState) | |
| 588 | ). | |
| 589 | ||
| 590 | % just get a solution for truth: i.e., find any value for the types | |
| 591 | get_typing_solution(IDs, State, LocalState, NewLocalState) :- | |
| 592 | conjunct_predicates([], Truth), | |
| 593 | get_any(IDs, Truth, State, LocalState, NewLocalState). | |
| 594 | ||
| 595 | % get some successful solution; or if this does not work: just call get_any_most_satisfier | |
| 596 | get_any_if_possible(IDs, Condition, State, LocalState, NewLocalState) :- | |
| 597 | if(get_any(IDs, Condition, State, LocalState, NewLocalState),true, | |
| 598 | get_any_most_satisfier(IDs, Condition, State, LocalState, NewLocalState)). | |
| 599 | ||
| 600 | ||
| 601 | ||
| 602 | %% | |
| 603 | % Main predicate for writing dot graph | |
| 604 | % Use it to start. | |
| 605 | % | |
| 606 | % FileSpec is used by tell/1 to open a stream. | |
| 607 | % The graph is printed by print_dot_graph_for_tree/1. | |
| 608 | % | |
| 609 | ||
| 610 | :- use_module(probsrc(tools),[catch_call/1]). | |
| 611 | :- use_module(probsrc(tools_io),[safe_intelligent_open_file/3]). | |
| 612 | ||
| 613 | write_dot_graph_to_file(Tree, FileSpec) :- | |
| 614 | catch_call(write_dot_graph_to_file_aux(Tree, FileSpec)). | |
| 615 | write_dot_graph_to_file_aux(Tree, FileSpec) :- | |
| 616 | temp_set_prefs(CHNG) , | |
| 617 | safe_intelligent_open_file(FileSpec,write,Stream), | |
| 618 | call_cleanup(print_dot_graph(Stream,Tree), | |
| 619 | (close(Stream),reset_prefs(CHNG))). | |
| 620 | ||
| 621 | print_dot_graph(Stream,Tree):- | |
| 622 | html_formula_tree,!, | |
| 623 | % HTML-table nodes; root on the left, sub-formulas to the right | |
| 624 | format(Stream,'digraph formula_tree {~n',[]), | |
| 625 | format(Stream,' graph [rankdir=LR, ranksep=0.35, nodesep=0.12, ordering=out, fontname="Helvetica"];~n',[]), | |
| 626 | format(Stream,' node [shape=plain, fontname="Helvetica", fontsize=10];~n',[]), | |
| 627 | format(Stream,' edge [color="#8C99A6", arrowhead=none, tailport=e, headport=w];~n',[]), | |
| 628 | visit_tree(x_print_dot_graph_visitor(Stream), x_print_dot_graph_node(Stream), Tree, [],_), | |
| 629 | format(Stream,'}~n',[]). | |
| 630 | print_dot_graph(Stream,Tree):- | |
| 631 | format(Stream,'digraph g {~nrankdir=RL;~n',[]), | |
| 632 | visit_tree(x_print_dot_graph_visitor(Stream), x_print_dot_graph_node(Stream), Tree, [],_), | |
| 633 | format(Stream,'}~n',[]). | |
| 634 | ||
| 635 | % the HTML rendering is the default; the classic rendering uses rect and record nodes | |
| 636 | html_formula_tree :- get_preference(dot_formula_tree_html,true). | |
| 637 | ||
| 638 | ||
| 639 | %% | |
| 640 | % Helpers | |
| 641 | % | |
| 642 | :- public x_print_dot_graph_visitor/6. | |
| 643 | x_print_dot_graph_visitor(Stream,enter, Node, _Children, [], [1,root]) :- | |
| 644 | x_print_dot_graph_node_only(Stream,Node, root),!. | |
| 645 | x_print_dot_graph_visitor(Stream,enter, Node, _Children, In, [NewID|In]):- | |
| 646 | x_print_dot_graph_node(Stream,Node, In, [NewID|_]). | |
| 647 | x_print_dot_graph_visitor(_Stream,leave, _Node, _Children, [NewID,_ID|Out], [NewID|Out]). | |
| 648 | ||
| 649 | /* for 3 args : */ | |
| 650 | x_print_dot_graph_node(Stream,Node, [], []) :- | |
| 651 | x_print_dot_graph_node_only(Stream,Node, root), | |
| 652 | !. | |
| 653 | x_print_dot_graph_node(Stream,Node, [ID|Rest], [NewID|Rest]) :- | |
| 654 | Rest = [PID|_], | |
| 655 | NewID is ID + 1, | |
| 656 | x_print_dot_graph_node_only(Stream,Node, ID), | |
| 657 | (html_formula_tree | |
| 658 | -> format(Stream,'~8|Node~p -> Node~p;~N', [PID, ID]) | |
| 659 | ; format(Stream,'~8|Node~p -> Node~p;~N', [ID, PID])), | |
| 660 | !. | |
| 661 | ||
| 662 | ||
| 663 | ||
| 664 | ||
| 665 | shorten_atom(MaxLengthPerLine, Atom, NewAtom) :- | |
| 666 | max_lines(MaxLines), | |
| 667 | shorten_atom(MaxLengthPerLine, Atom, NewAtom,MaxLines). | |
| 668 | shorten_atom(MaxLengthPerLine, Atom, NewAtom,MaxLines) :- | |
| 669 | ( atom(Atom) -> | |
| 670 | atom_chars(Atom, AtomChars), | |
| 671 | shorten_atom_list(MaxLengthPerLine, AtomChars, NewAtom,MaxLines) | |
| 672 | ; | |
| 673 | NewAtom = Atom | |
| 674 | ). | |
| 675 | ||
| 676 | shorten_atom_list(MaxLengthPerLine, Chars, Atom,MaxLines):- | |
| 677 | %length(Chars, Length), | |
| 678 | split_chars(Chars,MaxLengthPerLine, Prefix,Suffix), | |
| 679 | ( Suffix=[] -> | |
| 680 | atom_chars(Atom, Chars) | |
| 681 | ; | |
| 682 | %lists:append_length(Prefix, Suffix, Chars, MaxLengthPerLine), | |
| 683 | %print( split_chars(Chars,MaxLengthPerLine, Prefix,Suffix) ),nl, | |
| 684 | (MaxLines>1 -> ML1 is MaxLines-1, | |
| 685 | shorten_atom_list(MaxLengthPerLine, Suffix, SuffixAtom,ML1) | |
| 686 | ; SuffixAtom = '...'), | |
| 687 | atom_chars(PrefixAtom, Prefix), | |
| 688 | atom_concat(PrefixAtom, '\\n', PrefixAtomNewLine), | |
| 689 | atom_concat(PrefixAtomNewLine, SuffixAtom, Atom) | |
| 690 | ). | |
| 691 | ||
| 692 | % bvisual:split_chars([a,b,d,e,f],2,P,S), P==[a,b], S==[d,e,f] | |
| 693 | ||
| 694 | % a predicate to split List if it is longer than MaxLen, but only outside of single and double quotes | |
| 695 | % List is suppoed to be already encoded using string_escape | |
| 696 | split_chars(List,MaxLen,Prefix,Suffix) :- | |
| 697 | split_chars(List,MaxLen,Prefix,Suffix,q(outside,outside)). | |
| 698 | ||
| 699 | split_chars(List,MaxL,R,Suffix,Q) :- MaxL < 1, Q=q(outside,outside), !, | |
| 700 | % only add newline if we are outside of quotes | |
| 701 | R=[], Suffix=List. | |
| 702 | split_chars([],_MaxL,[],[],_). | |
| 703 | split_chars(L,MaxL,LR,LeftOver,Q) :- | |
| 704 | is_a_unit(L,T,LR,TR),!, | |
| 705 | M1 is MaxL-1, split_chars(T,M1,TR,LeftOver,Q). | |
| 706 | split_chars([Slash,Quote|T],MaxL,[Slash,Quote|TR],LeftOver,Q) :- Slash = ('\\'), | |
| 707 | !, %(Quote=34;Quote=39), !, % \" or \' | |
| 708 | toggle_quotes(Quote,Q,TQ), | |
| 709 | M1 is MaxL-1, split_chars(T,M1,TR,LeftOver,TQ). | |
| 710 | split_chars([H|T],MaxL,[H|TR],LeftOver,Q) :- | |
| 711 | M1 is MaxL-1, split_chars(T,M1,TR,LeftOver,Q). | |
| 712 | ||
| 713 | toggle_quotes('\'',q(outside,Q2),q(inside,Q2)) :- !. | |
| 714 | toggle_quotes('\'',q(inside,Q2),q(outside,Q2)) :- !. | |
| 715 | toggle_quotes('\"',q(Q1,outside),q(Q1,inside)) :- !. | |
| 716 | toggle_quotes('\"',q(Q1,inside),q(Q1,outside)) :- !. | |
| 717 | toggle_quotes(_,Q,Q). | |
| 718 | ||
| 719 | % check for HTML encodings like à Ë α ... | |
| 720 | % these should be counted as a single character and not be split | |
| 721 | is_a_unit(['&'|T1],T,['&'|T1R],TR) :- get_lexeme(T1,10,LexUnit,T), | |
| 722 | append(LexUnit,TR,T1R). | |
| 723 | ||
| 724 | get_lexeme([';'|T],_,[';'],TRes) :- !, TRes=T. | |
| 725 | get_lexeme([H|T],L,[H|LexT],TRes) :- L>1, L1 is L-1, get_lexeme(T,L1,LexT,TRes). | |
| 726 | ||
| 727 | :- use_module(probsrc(tools_strings),[ajoin/2]). | |
| 728 | get_long_label(Node,MaxPerLine,LongLabel) :- | |
| 729 | long_desc(Node, LongDesc), | |
| 730 | %tools:print_wtime(start_translate(ID)), | |
| 731 | LongDesc = b(P,T,Infos), | |
| 732 | (select_info_labels(Labels,Infos,I2) -> true ; Labels=[],I2=Infos), | |
| 733 | translate_bexpression_with_limit(b(P,T,I2), 250, Pretty0), | |
| 734 | %tools:print_wtime(translated(ID)), | |
| 735 | string_escape(Pretty0, Pretty1), | |
| 736 | shorten_atom(MaxPerLine, Pretty1, Pretty1h), % maximally 10 lines with 40 chars | |
| 737 | gen_labels(Labels,[Pretty1h],Atoms), % print labels separately one per line | |
| 738 | ajoin(Atoms,LongLabel). | |
| 739 | ||
| 740 | gen_labels([],Res,Res). | |
| 741 | gen_labels([Label|T],Tail,['/*@ ',Label,' */\\n' | Res]) :- gen_labels(T,Tail,Res). | |
| 742 | ||
| 743 | :- use_module(probsrc(tools),[string_escape/2]). | |
| 744 | x_print_dot_graph_node_only(Stream,Node, ID) :- html_formula_tree, !, | |
| 745 | html_node_label(Node,ID,HtmlLabel), | |
| 746 | format(Stream,'~4|Node~p [label=<~w>]~N',[ID,HtmlLabel]). | |
| 747 | x_print_dot_graph_node_only(Stream,Node, ID) :- %tools:print_wtime(node(ID,Node)), | |
| 748 | label(Node, Label0), | |
| 749 | max_chars_per_line(MaxPerLine), | |
| 750 | string_escape(Label0, Label1), %tools:print_wtime(escaped(ID)), | |
| 751 | ( ID \= root, is_boolean(Node) | |
| 752 | -> | |
| 753 | Label=Label1, | |
| 754 | get_long_label(Node,MaxPerLine,Pretty1h), | |
| 755 | value(Node, TruthValue), | |
| 756 | string_escape(TruthValue, TVEsc), | |
| 757 | get_predicate_field_seperator(Sep), | |
| 758 | atom_concat(TVEsc, Sep, Pretty4), | |
| 759 | atom_concat(Pretty4, Pretty1h, Pretty) | |
| 760 | ; | |
| 761 | value(Node, Val), | |
| 762 | string_escape(Val, Pretty1), % TO DO: check that we also escape | in case of records! | |
| 763 | (is_boolean(Node) -> get_predicate_field_seperator(Sep) | |
| 764 | ; get_expression_field_seperator(Sep)), | |
| 765 | ((Pretty1=Label1 ; is_explicit_value_node(Node)) | |
| 766 | -> Pretty='', Label=Pretty1 % we have a simple value node like integer(1),... | |
| 767 | ; has_no_children(Node) | |
| 768 | -> get_long_label(Node,MaxPerLine,Label), | |
| 769 | shorten_atom(MaxPerLine, Pretty1, Pretty) | |
| 770 | ; Label=Label1, | |
| 771 | shorten_atom(MaxPerLine, Pretty1, Pretty) | |
| 772 | ) | |
| 773 | ), | |
| 774 | %tools:print_wtime(node_style(ID)), | |
| 775 | x_print_dot_graph_node_style(Node, Shape, Color,Style), | |
| 776 | %tools:print_wtime(printing(ID)), | |
| 777 | (Pretty='' | |
| 778 | -> format(Stream,'~4|Node~p [label="~p", shape="~p", fillcolor="~p", style="~p"]~N', | |
| 779 | [ID, Label, Shape, Color,Style]) | |
| 780 | ; format(Stream,'~4|Node~p [label="~p~p~p", shape="~p", fillcolor="~p", style="~p"]~N', | |
| 781 | [ID, Label, Sep, Pretty, Shape, Color,Style])). % ,tools:print_wtime(finished(ID)). | |
| 782 | ||
| 783 | ||
| 784 | % ------------------------------------------------------------ | |
| 785 | % HTML-table rendering of formula tree nodes | |
| 786 | ||
| 787 | :- use_module(probsrc(tools),[html_escape/2]). | |
| 788 | :- use_module(probsrc(tools_strings),[ajoin_with_sep/3, convert_to_atom/2]). | |
| 789 | ||
| 790 | html_node_label(Node,ID,Html) :- | |
| 791 | is_boolean(Node),!, | |
| 792 | value(Node,Val0), convert_to_atom(Val0,Val), | |
| 793 | pred_value_style(Val,Fill,Border,BadgeCol,Badge), | |
| 794 | long_desc_text(Node,Labels,Text), | |
| 795 | (ID==root, child_nodes(Node,[_|_]), atom_length(Text,TLen), TLen > 100 | |
| 796 | -> % the root formula is usually long (e.g., the full invariant): just show the operator | |
| 797 | label(Node,Op0), convert_to_atom(Op0,Op), html_escape(Op,EOp), | |
| 798 | child_nodes(Node,Ch), length(Ch,NrCh), | |
| 799 | ajoin(['<FONT POINT-SIZE="16">',EOp,'</FONT> <FONT POINT-SIZE="9" COLOR="#666666">', | |
| 800 | NrCh,' sub-formulas</FONT>'],Body), | |
| 801 | LabelCell='', Span='1' | |
| 802 | ; formula_line_width(W), max_lines(ML), | |
| 803 | wrap_to_html_lines(Text,W,ML,Body), | |
| 804 | labels_cell(Labels,LabelCell), | |
| 805 | (LabelCell='' -> Span='1' ; Span='2') | |
| 806 | ), | |
| 807 | ajoin(['<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="3" STYLE="rounded" COLOR="',Border, | |
| 808 | '" BGCOLOR="',Fill,'"><TR><TD ALIGN="LEFT"><FONT POINT-SIZE="8" COLOR="',BadgeCol,'"><B>',Badge, | |
| 809 | '</B></FONT></TD>',LabelCell,'</TR><TR><TD COLSPAN="',Span,'" ALIGN="LEFT" BALIGN="LEFT">',Body, | |
| 810 | '</TD></TR></TABLE>'],Html). | |
| 811 | html_node_label(Node,_ID,Html) :- | |
| 812 | value(Node,Val0), convert_to_atom(Val0,Val), | |
| 813 | label(Node,Label0), convert_to_atom(Label0,Label), | |
| 814 | value_line_width(W), max_value_lines(ML), | |
| 815 | ( (Val=Label ; is_explicit_value_node(Node)) | |
| 816 | -> % a simple value node like an integer, or a constant like a deferred set | |
| 817 | wrap_to_html_lines(Val,W,ML,HVal), | |
| 818 | ajoin(['<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="3" COLOR="#A9B4C0" BGCOLOR="#EDF1F5">', | |
| 819 | '<TR><TD ALIGN="LEFT" BALIGN="LEFT">',HVal,'</TD></TR></TABLE>'],Html) | |
| 820 | ; expression_title(Node,Label,Title), | |
| 821 | wrap_to_html_lines(Title,W,3,HTitle), | |
| 822 | (Val=unknown | |
| 823 | -> Border='#E0A100', TitleBg='#FFF1C7', | |
| 824 | HVal='<FONT COLOR="#9A6700"><I>unknown</I></FONT>' | |
| 825 | ; Border='#A9B4C0', TitleBg='#EDF1F5', | |
| 826 | wrap_to_html_lines(Val,W,ML,HVal0), | |
| 827 | ajoin(['<FONT FACE="Courier" POINT-SIZE="9">',HVal0,'</FONT>'],HVal) | |
| 828 | ), | |
| 829 | ajoin(['<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="3" COLOR="',Border,'" BGCOLOR="white">', | |
| 830 | '<TR><TD ALIGN="LEFT" BALIGN="LEFT" BGCOLOR="',TitleBg,'"><B>',HTitle,'</B></TD></TR>', | |
| 831 | '<TR><TD ALIGN="LEFT" BALIGN="LEFT">',HVal,'</TD></TR></TABLE>'],Html) | |
| 832 | ). | |
| 833 | ||
| 834 | formula_line_width(48). | |
| 835 | value_line_width(48). | |
| 836 | max_value_lines(8). | |
| 837 | ||
| 838 | % colours: fill, border, badge colour and badge text for predicate nodes | |
| 839 | pred_value_style(true, '#E7F4E1','#4E9A3A','#2E6B1F','✓ TRUE') :- !. | |
| 840 | pred_value_style(false,'#FBE3E1','#C23B2E','#9E2A20','✗ FALSE') :- !. | |
| 841 | pred_value_style(Val, '#FFF1D6','#E08A00','#9A5B00',Badge) :- | |
| 842 | upcase_atom_safe(Val,UV), html_escape(UV,EUV), | |
| 843 | ajoin(['? ',EUV],Badge). | |
| 844 | ||
| 845 | upcase_atom_safe(A,U) :- atom_codes(A,C), maplist(upcase_code,C,UC), atom_codes(U,UC). | |
| 846 | upcase_code(C,U) :- C >= 0'a, C =< 0'z, !, U is C-32. | |
| 847 | upcase_code(C,C). | |
| 848 | ||
| 849 | labels_cell([],'') :- !. | |
| 850 | labels_cell(Labels,Cell) :- | |
| 851 | findall(L,(member(L0,Labels),convert_to_atom(L0,L1),atom_concat('@',L1,L)),Ls), | |
| 852 | ajoin_with_sep(Ls,', ',LA), html_escape(LA,ELA), | |
| 853 | ajoin(['<TD ALIGN="RIGHT"><FONT POINT-SIZE="8" COLOR="#555555"><I>',ELA,'</I></FONT></TD>'],Cell). | |
| 854 | ||
| 855 | % pretty-printed formula of a node, with Rodin labels removed | |
| 856 | long_desc_text(Node,Labels,Text) :- | |
| 857 | long_desc(Node, b(P,T,Infos)), | |
| 858 | (select_info_labels(Labels,Infos,I2) -> true ; Labels=[],I2=Infos), | |
| 859 | translate_bexpression_with_limit(b(P,T,I2), 500, Text). | |
| 860 | ||
| 861 | % for an expression node: show the sub-expression itself if it is short, otherwise the operator | |
| 862 | expression_title(Node,_Label,Title) :- | |
| 863 | long_desc(Node,LD), LD = b(_,_,_), | |
| 864 | translate_bexpression_with_limit(LD, 100, Text), | |
| 865 | (has_no_children(Node) -> true ; atom_length(Text,Len), Len =< 40), | |
| 866 | !, Title=Text. | |
| 867 | expression_title(_,Label,Label). | |
| 868 | ||
| 869 | % wrap a text into lines of at most Width characters (breaking preferably after a comma or space), | |
| 870 | % keep at most MaxLines lines and HTML escape the result, separating lines by <BR/> | |
| 871 | wrap_to_html_lines(Text,Width,MaxLines,Html) :- | |
| 872 | atom_chars(Text,Chars), | |
| 873 | wrap_chars(Chars,Width,Lines0), | |
| 874 | truncate_lines(Lines0,MaxLines,Lines), | |
| 875 | maplist(html_line,Lines,HLines), | |
| 876 | ajoin_with_sep(HLines,'<BR/>',Html). | |
| 877 | ||
| 878 | html_line(Chars,H) :- atom_chars(A,Chars), html_escape(A,H). | |
| 879 | ||
| 880 | wrap_chars([],_,[]) :- !. | |
| 881 | wrap_chars(Chars,Max,[Chars]) :- length(Chars,L), L =< Max, !. | |
| 882 | wrap_chars(Chars,Max,[Line|Rest]) :- | |
| 883 | length(Prefix,Max), append(Prefix,Suffix0,Chars), | |
| 884 | (last_break_pos(Prefix,Pos) | |
| 885 | -> length(Line,Pos), append(Line,Tail,Prefix), append(Tail,Suffix0,Suffix) | |
| 886 | ; Line=Prefix, Suffix=Suffix0), | |
| 887 | wrap_chars(Suffix,Max,Rest). | |
| 888 | ||
| 889 | % last position in the second half of a line after which we can break | |
| 890 | last_break_pos(Prefix,Pos) :- | |
| 891 | length(Prefix,Len), Min is Len//2, | |
| 892 | reverse(Prefix,Rev), last_break_pos(Rev,Len,Min,Pos). | |
| 893 | last_break_pos([C|_],P,Min,P) :- P >= Min, break_after_char(C), !. | |
| 894 | last_break_pos([_|T],P,Min,Pos) :- P > Min, P1 is P-1, last_break_pos(T,P1,Min,Pos). | |
| 895 | ||
| 896 | break_after_char(','). | |
| 897 | break_after_char(' '). | |
| 898 | ||
| 899 | truncate_lines(Lines,Max,Lines) :- length(Lines,L), L =< Max, !. | |
| 900 | truncate_lines(Lines,Max,Res) :- | |
| 901 | length(Res0,Max), append(Res0,_,Lines), | |
| 902 | append(Front,[Last],Res0), append(Last,['\x2026\'],Last2), append(Front,[Last2],Res). | |
| 903 | ||
| 904 | ||
| 905 | x_print_dot_graph_node_style(Node, Shape, 'tomato','filled,rounded') :- | |
| 906 | is_boolean(Node), value(Node, false),!, boolean_shape(Shape). | |
| 907 | x_print_dot_graph_node_style(Node, Shape, 'olivedrab2','filled,rounded') :- | |
| 908 | is_boolean(Node), value(Node, true),!, boolean_shape(Shape). | |
| 909 | x_print_dot_graph_node_style(Node, Shape, 'orange','filled,rounded') :- % styles could be dashed,dotted, rounded, ... | |
| 910 | is_boolean(Node),!, boolean_shape(Shape). | |
| 911 | x_print_dot_graph_node_style(Node, Shape, 'yellow','filled') :- | |
| 912 | value(Node, unknown),!, expression_shape(Shape). | |
| 913 | x_print_dot_graph_node_style(_, Shape, 'white',filled) :- | |
| 914 | expression_shape(Shape). | |
| 915 | ||
| 916 | ||
| 917 | % node shapes of the classic rendering | |
| 918 | boolean_shape(rect). | |
| 919 | expression_shape(record). | |
| 920 | ||
| 921 | % in a Dot record we can use | to separate fields | |
| 922 | get_predicate_field_seperator('\\n'). % predicates use rect nodes | |
| 923 | get_expression_field_seperator('|'). % expressions use record nodes | |
| 924 | ||
| 925 | ||
| 926 | ||
| 927 | %% | |
| 928 | % Hierarchical Visitor Pattern for "trees" | |
| 929 | % used to be in gtree module | |
| 930 | % | |
| 931 | % For this implementation a tree is a (root) node and a functor, which | |
| 932 | % maps a node to it's child nodes. For example a Prolog term can be | |
| 933 | % understand as a tree by the functor: | |
| 934 | % children_fkt(Term, Subterms):- Term=..[_|Subterms]. | |
| 935 | % | |
| 936 | % Important: Do not use trees with cycles. This implementation will not | |
| 937 | % detect them and will not terminate. | |
| 938 | % | |
| 939 | % The indented calling is: | |
| 940 | % visit_tree(+VisitorFkt5, +VisitorFkt3, +Node, +EnvIn, -EnvOut) | |
| 941 | % | |
| 942 | % If no functor is specified, the former mentioned one is used. | |
| 943 | % EnvIn and EnvOut are Variables for intermediate values or states, | |
| 944 | % which you might need to maintain between calls to VisitorFkt. You | |
| 945 | % might omit them, if you do not need them. | |
| 946 | % | |
| 947 | % During the visiting process the VisitorFkt functor is called for each | |
| 948 | % node starting with the "root". Calls are made in following order: | |
| 949 | % - VisitorFkt(Node, EnvIn, EnvOut), if Node is a leaf, | |
| 950 | % i.e. ChildrenFkt(Node, []). | |
| 951 | % - VisitorFkt(enter, Node, Children, EnvIn, EnvOut), before branching, | |
| 952 | % - VisitorFkt(leave, Node, Children, EnvIn, EnvOut), after branching | |
| 953 | % to Node's child nodes. | |
| 954 | % !- Only if VisitorFkt(enter, ...) succeeds, child nodes will be | |
| 955 | % visited and VisitorFkt(leave, ...) will be called. | |
| 956 | % | |
| 957 | ||
| 958 | :- use_module(library(lists), [scanlist/4, maplist/2]). | |
| 959 | ||
| 960 | %:- meta_predicate visit_tree(5,3,-,-,-). | |
| 961 | ||
| 962 | visit_tree(VisitorFkt5, VisitorFkt3, Node, EnvIn, EnvOut):- | |
| 963 | child_nodes(Node, Children), | |
| 964 | ( Children == [] -> | |
| 965 | call(VisitorFkt3, Node, EnvIn, EnvOut) | |
| 966 | ; | |
| 967 | ( call(VisitorFkt5, enter, Node, Children, EnvIn, Env1) -> | |
| 968 | scanlist(visit_tree(VisitorFkt5, VisitorFkt3), Children, Env1, Env2), | |
| 969 | call(VisitorFkt5, leave, Node, Children, Env2, EnvOut) | |
| 970 | ; | |
| 971 | EnvOut = EnvIn | |
| 972 | ) | |
| 973 | ), | |
| 974 | !. | |
| 975 | ||
| 976 | ||
| 977 | ||
| 978 |