| 1 | % evaluation of ProB AST set expressions | |
| 2 | ||
| 3 | :- use_module(library(sets)). | |
| 4 | :- use_module(library(lists),[is_list/1,maplist/3]). | |
| 5 | :- use_module(library(avl)). | |
| 6 | ||
| 7 | % returns avl_set | |
| 8 | sint_avl(Expression,avl_set(AVL)) :- | |
| 9 | sint(Expression,Value) , | |
| 10 | findall(Key-true,member(Key,Value),AVLList) , | |
| 11 | list_to_avl(AVLList,AVL). | |
| 12 | ||
| 13 | % ast record | |
| 14 | sint(b(rec(Record),_,_),rec(NewRecord)) :- | |
| 15 | maplist(sint,Record,NewRecord). | |
| 16 | % value field | |
| 17 | sint(field(Name,Value),field(Name,NewValue)) :- | |
| 18 | sint(Value,Temp) , | |
| 19 | ( number(Temp) -> | |
| 20 | NewValue = int(Temp) | |
| 21 | ; Temp = true -> | |
| 22 | NewValue = pred_true | |
| 23 | ; Temp = false -> | |
| 24 | NewValue = pred_false | |
| 25 | ; NewValue = string(Temp)). | |
| 26 | ||
| 27 | % ast set expressions | |
| 28 | sint(b(set_extension([]),_,_),[]). | |
| 29 | ||
| 30 | sint(b(boolean_true,_,_),true). | |
| 31 | sint(b(boolean_false,_,_),false). | |
| 32 | sint(b(string(String),_,_),String). | |
| 33 | sint(b(integer(Integer),_,_),Integer). | |
| 34 | ||
| 35 | sint(b(value(Set),_,_),Set) :- | |
| 36 | is_list(Set). | |
| 37 | sint(b(value(avl_set(AvlSet)),_,_),Set) :- | |
| 38 | avl_to_list(AvlSet,AVLList) , | |
| 39 | findall(Key,member(Key-_,AVLList),Set). | |
| 40 | ||
| 41 | sint(b(interval(b(integer(A),integer,_),b(integer(B),integer,_)),set(integer),_),Value) :- | |
| 42 | interval(A,B,Value). | |
| 43 | ||
| 44 | sint(b(union(Set1,Set2),_,_),Value) :- | |
| 45 | sint(Set1,ValueSet1) , | |
| 46 | sint(Set2,ValueSet2) , | |
| 47 | union(ValueSet1,ValueSet2,Value). | |
| 48 | ||
| 49 | sint(b(general_union(Set),_,_),Value) :- | |
| 50 | sint(Set,ValueSet) , | |
| 51 | flatten(ValueSet,Value). | |
| 52 | ||
| 53 | sint(b(intersection(Set1,Set2),_,_),Value) :- | |
| 54 | sint(Set1,ValueSet1) , | |
| 55 | sint(Set2,ValueSet2) , | |
| 56 | intersection(ValueSet1,ValueSet2,Value). | |
| 57 | ||
| 58 | sint(b(general_intersection(Set),_,_),Value) :- | |
| 59 | sint(Set,ValueSet) , | |
| 60 | flatten(ValueSet,Value). | |
| 61 | ||
| 62 | sint(b(set_subtraction(Set1,Set2),_,_),Value) :- | |
| 63 | sint(Set1,ValueSet1) , | |
| 64 | sint(Set2,ValueSet2) , | |
| 65 | subtract(ValueSet1,ValueSet2,Value). | |
| 66 | ||
| 67 | sint(b(set_extension(Set),set(_),_),Value) :- | |
| 68 | is_list(Set) , | |
| 69 | % convert to prob value list set | |
| 70 | maplist(ast_to_prob_value,Set,Value). | |
| 71 | ||
| 72 | sint(b(set_extension(Temp),_,_),[Set]) :- | |
| 73 | sint(Temp,Set). | |
| 74 | ||
| 75 | % create list from an interval | |
| 76 | interval(A,A,[A]) :- !. | |
| 77 | interval(A,B,[A|T]) :- | |
| 78 | A1 is A + 1 , | |
| 79 | interval(A1,B,T). | |
| 80 | ||
| 81 | % convert to prob value | |
| 82 | ast_to_prob_value(b(integer(Value),_,_),int(Value)). | |
| 83 | ast_to_prob_value(b(string(Value),_,_),string(Value)). | |
| 84 | ast_to_prob_value(b(Bool,_,_),Value) :- | |
| 85 | (Bool = boolean_true -> Value = pred_true ; Bool = boolean_false -> Value = pred_false). | |
| 86 | % true for prob value | |
| 87 | ast_to_prob_value(_,_). | |
| 88 | % convert to prolog value | |
| 89 | ast_to_value(int(Value),Value). | |
| 90 | ast_to_value(string(Value),Value). | |
| 91 | ast_to_value(Bool,Value) :- | |
| 92 | (Bool = pred_true -> Value = true ; Bool = pred_false -> Value = false). | |
| 93 | ast_to_value(b(integer(Value),_,_),Value). | |
| 94 | ast_to_value(b(string(Value),_,_),Value). | |
| 95 | ast_to_value(b(Bool,_,_),Value) :- | |
| 96 | (Bool = boolean_true -> Value = true ; Bool = boolean_false -> Value = false). |