1 % Heinrich Heine Universitaet Duesseldorf
2 % (c) 2021-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(visb_json_utils,[get_attr/3, get_attr_with_pos/5,
6 del_attr/4, del_attr_with_pos/6, non_det_del_attr_with_pos/6,
7 force_del_attr/5, force_del_attr_with_pos/6,
8 get_attr_true/3, is_optional_json_item/1, is_optional_json_item/3,
9 json_true_value/1, json_false_value/1,
10 check_json_string/4,
11 get_pos_from_list/3, construct_prob_pos_term/3,
12 replace_in_json/4, multi_replace_in_json/4,
13 check_all_same_length_json/3]).
14
15 :- use_module(probsrc(module_information),[module_info/2]).
16 :- module_info(group,visualization).
17 :- module_info(description,'This module provides utilities to access and transform JSON terms for VisB.').
18
19 :- set_prolog_flag(double_quotes, codes).
20
21 % Note: error messages use visb_visualiser as source, as they relate to VisB JSON files
22
23 :- use_module(probsrc(error_manager)).
24 :- use_module(probsrc(self_check)).
25 :- use_module(probsrc(tools_strings), [ajoin/2]).
26 :- use_module(library(lists)).
27
28 % JSON terms as produced by the JSON parser with position_infos(true):
29 % json(List) where List contains '='(Attr,Value,From-To) entries
30
31 % ----------------
32 % accessing attributes
33
34 get_attr(Attr,List,Val) :- member('='(Attr,Val,_),List).
35 get_attr_with_pos(Attr,List,Val,File,Pos) :-
36 El = '='(Attr,Val,_),
37 member(El,List),
38 get_pos_from_list([El],File,Pos).
39
40 del_attr(Attr,List,Val,Rest) :- select('='(Attr,Val,_Pos),List,Rest).
41 %del_attr_with_pos(Attr,List,Val,Rest,Pos) :- select('='(Attr,Val,Pos),List,Rest).
42 del_attr_with_pos(Attr,List,Val,Rest,File,PosTerm) :- select('='(Attr,Val,From-To),List,Rest),!,
43 PosTerm = src_position_with_filename_and_ec(From,1,To,1,File).
44 non_det_del_attr_with_pos(Attr,List,Val,Rest,File,PosTerm) :- select('='(Attr,Val,From-To),List,Rest),
45 PosTerm = src_position_with_filename_and_ec(From,1,To,1,File).
46
47 force_del_attr(Attr,List,Val,Rest,File) :- force_del_attr_with_pos(Attr,List,Val,Rest,File,_).
48
49 force_del_attr_with_pos(Attr,List,Val,Rest,File,PosTerm) :- select('='(Attr,Val,From-To),List,Rest),!,
50 %TODO: we would ideally want to extract the exact column position of Val, for this we need to extend the JSON parser first
51 PosTerm = src_position_with_filename_and_ec(From,1,To,1,File).
52 force_del_attr_with_pos(Attr,List,_,_,File,_) :- get_pos_from_list(List,File,Pos),!,
53 add_error(visb_visualiser,'The JSON object has no attribute:',Attr,Pos),fail.
54
55 construct_prob_pos_term(From-To,File,PosTerm) :- !,
56 PosTerm = src_position_with_filename_and_ec(From,1,To,1,File).
57 construct_prob_pos_term(Pos,_,Pos).
58
59 get_pos_from_list(json(List),File,Pos) :- !, get_pos_from_list(List,File,Pos).
60 get_pos_from_list([H|T],File,RPos) :-
61 H='='(_,_,From-_),
62 last([H|T],
63 '='(_,_,_-To)),
64 !,
65 RPos=src_position_with_filename_and_ec(From,1,To,1,File).
66 get_pos_from_list(_,File,File). % should we provide another term?
67
68 check_json_string(Term,_,_,Atom) :- Term = string(Atom), atom(Atom), !.
69 check_json_string(Term,Attr,Pos,_) :- !,
70 ajoin(['Illegal JSON attribute ',Attr,', expected a JSON string:'],Msg),
71 add_error(visb_visualiser,Msg,Term,Pos),fail.
72
73 % ----------------
74 % JSON boolean values and flags
75
76 get_attr_true(Attr,JsonList,File) :-
77 get_attr_with_pos(Attr,JsonList,TRUE,File,Pos),
78 (json_true_value(TRUE) -> true
79 ; json_false_value(TRUE) -> fail
80 ; ajoin(['Value for attribute ',Attr,' is not true or false: '],Msg),
81 add_warning(visb_visualiser,Msg,TRUE,Pos)
82 ).
83
84 % check if a JSON item has been marked as optional, e.g., "optional":true
85 is_optional_json_item(JsonList) :-
86 get_attr(optional,JsonList,Val),
87 \+ json_false_value(Val). % optional not marked as false
88 is_optional_json_item(JsonList,File,Pos) :-
89 get_attr_with_pos(optional,JsonList,Val,File,Pos), \+ json_false_value(Val).
90
91 json_true_value(@(true)).
92 json_true_value(string(true)).
93 json_true_value(number(1)).
94
95 json_false_value(@(false)).
96 json_false_value(string(false)).
97 json_false_value(number(0)).
98
99 % ----------------
100 % replacing %Nr patterns, used for for-loops and repeat in VisB JSON files
101
102 check_all_same_length_json(List,ExpectedLen,Pos) :-
103 nth1(Nr,List,Json),
104 (Json=array(SubList),length(SubList,Len) -> Len \= ExpectedLen ; Len='not a list'),
105 !,
106 ajoin(['The element number ',Nr,' of the repeat list has not the expected length of ',ExpectedLen,', length is: '],Msg),
107 add_warning(visb_visualiser,Msg,Len,Pos).
108 check_all_same_length_json(_,_,_).
109
110 json_value_to_codes(@(Literal), C) :- atom(Literal), !, atom_codes(Literal, C).
111 json_value_to_codes(number(Number), C) :- number(Number), !, number_codes(Number, C).
112 json_value_to_codes(string(String), C) :- atom(String), !, atom_codes(String, C).
113 json_value_to_codes(Json, _) :- !, add_error(json_value_to_codes,'Cannot convert json term to codes:', Json), fail.
114
115 % replace (possibly) multiple patterns at once
116 multi_replace_in_json(array([]),_RepCount,JsonIn,JsonOut) :- !, JsonOut=JsonIn.
117 multi_replace_in_json(array([IterElem|TIT]),RepCount,JsonIn,JsonOut) :-
118 !,
119 number_codes(RepCount,Pat),
120 json_value_to_codes(IterElem,IterElemC),
121 %format('Replacing in JSON: ~s -> ~s~n',[Pat,IterElemC]),
122 replace_in_json(Pat,IterElemC,JsonIn,Json),
123 R1 is RepCount+1,
124 multi_replace_in_json(array(TIT),R1,Json,JsonOut).
125 multi_replace_in_json(IterElem,RepCount,JsonIn,JsonOut) :- % a value, not a list
126 !,
127 number_codes(RepCount,Pat),
128 json_value_to_codes(IterElem,IterElemC),
129 %format('Replacing in JSON: ~s -> ~s~n',[Pat,IterElemC]),
130 replace_in_json(Pat,IterElemC,JsonIn,JsonOut).
131
132 % replace a pattern code prefixed by % by another code list
133 replace_in_json(Pattern,RepStr,string(Val),string(NewVal)) :- !, replace_atom(Pattern,RepStr,Val,NewVal).
134 replace_in_json(Pattern,RepStr,array(Vals),array(NewVals)) :- !, maplist(replace_in_json(Pattern,RepStr),Vals,NewVals).
135 replace_in_json(Pattern,RepStr,json(Vals),json(NewVals)) :- !, maplist(replace_in_json_pair(Pattern,RepStr),Vals,NewVals).
136 replace_in_json(_,_,Val,Val). % number/literal (no replacement necessary)
137
138 replace_in_json_pair(Pattern,RepStr,'='(KIn,VIn,Pos),'='(KOut,VOut,Pos)) :-
139 replace_atom(Pattern,RepStr,KIn,KOut),
140 replace_in_json(Pattern,RepStr,VIn,VOut).
141
142 replace_atom(Pattern,RepStr,Val,NewVal) :-
143 atom_codes(Val,Codes),
144 replace_pat(Pattern,RepStr,NewCodes,Codes,[]),
145 atom_codes(NewVal,NewCodes). % TO DO: keep as codes for efficiency
146
147 :- assert_must_succeed((visb_json_utils: replace_pat("0","_1_",Res,"ab%0cd",[]), Res == "ab_1_cd")).
148 :- assert_must_succeed((visb_json_utils: replace_pat("0","",Res,"ab%0%0cd%0",[]), Res == "abcd")).
149 :- assert_must_succeed((visb_json_utils: replace_pat("1","x",Res,"%1 %10 %1",[]), Res == "x %10 x")).
150 % dcg utility to replace %Pat by NewStr constructing Res
151 replace_pat(Pat,NewStr,Res) --> "%", Pat, \+ digit_follows_number(Pat), !,
152 {append(NewStr,TR,Res)}, replace_pat(Pat,NewStr,TR).
153 replace_pat(Pat,RepStr,[H|T]) --> [H],!, replace_pat(Pat,RepStr,T).
154 replace_pat(_,_,[]) --> [].
155
156 % check that a numeric pattern like %1 is not the prefix of a longer one like %10
157 digit_follows_number(Pat,S,S) :- last(Pat,C), is_digit_code(C), S=[D|_], is_digit_code(D).
158 is_digit_code(C) :- C >= 0'0, C =< 0'9.