revision.pl 5.56 KB
Newer Older
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
:- module(
  revision,
  [
    revise_model/1
  ]
).

% Only for separate compilation/linting
:- use_module(nusmv).
:- use_module(util).


revise_model(Query) :-
  biocham_command,
  type(Query, ctl),
  doc(
    'Use theory-revision on the current model to satisfy the query given as
    argument. cf. \\cite{CCFS05tcsb}.'
  ),
  categorize_query(Query, ECTL, UCTL, ACTL),
  revise_model(ECTL, UCTL, ACTL, [], [], []).


% Everything copied as much as possible from CCFS05tcsb

26
revise_model([E | ECTL], UCTL, ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
27 28 29
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
30
    check_ctl_impl(E, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
31
  ->
32
    print_message(informational, step('E'))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
33
  ;
34 35
    print_message(informational, step('E''')),
    find_and_add_rule,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
36
    % FIXME check Et too since self-loops can be broken by adding a reaction
37 38 39 40
    append([E | Ut], At, AllSpec),
    recheck_spec(AllSpec, Init, Bool)
  ),
  revise_model(ECTL, UCTL, ACTL, [E | Et], Ut, At).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
41

42
revise_model([], [U | UCTL], ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
43 44 45
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
46
    check_ctl_impl(U, Init, silent, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
47
  ->
48
    print_message(informational, step('U'))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
49
  ;
50 51 52 53 54 55 56 57 58 59 60 61 62 63
    (
      print_message(informational, step('U''')),
      find_and_add_rule,
      % FIXME check Et too since self-loops can be broken by adding a reaction
      append([U | Ut], At, AllSpec)
    ;
      print_message(informational, step('U''''')),
      find_and_delete_rules,
      % FIXME check At too since self-loops can be added by removing a reaction
      append([U | Ut], Et, AllSpec)
    ),
    recheck_spec(AllSpec, Init, Bool)
  ),
  revise_model([], UCTL, ACTL, Et, [U | Ut], At).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
64 65 66 67 68

revise_model([], [], [A | ACTL], Et, Ut, At) :-
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
69
    check_ctl_impl(A, Init, silent, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
70
  ->
71
    print_message(informational, step('A'))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
72
  ;
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
73
    print_message(informational, step('A''')),
74 75 76
    find_and_delete_rules,
    % FIXME check At too since self-loops can be added by removing a reaction
    append([A | Ut], Et, AllSpec),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
77 78
    % FIXME the published algorithm actually allows to not re-check, but
    % simply split and add again to the formulae to be verified
79 80 81
    recheck_spec(AllSpec, Init, Bool)
  ),
  revise_model([], [], ACTL, Et, Ut, [A | At]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
82 83


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
84
revise_model([], [], [], _, _, _) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
85 86
  findall(Item, item([kind: reaction, item: Item]), Reactions),
  print_message(informational, success(Reactions)).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
87 88


89 90 91 92 93 94
recheck_spec(Specs, Init, Bool) :-
  join_op('/\\', Specs, Spec),
  print_message(informational, checking(Spec)),
  check_ctl_impl(Spec, Init, no, Bool, true).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
categorize_query(Q1 /\ Q2, ECTL, UCTL, ACTL) :-
  !,
  categorize_query(Q1, E1, U1, A1),
  categorize_query(Q2, E2, U2, A2),
  append(E1, E2, ECTL),
  append(U1, U2, UCTL),
  append(A1, A2, ACTL).

categorize_query(not(Q), ECTL, UCTL, ACTL) :-
  !,
  categorize_query(Q, ACTL, UCTL, ECTL).

categorize_query(Query, [], [Query], []) :-
  object(Query),
  !.

categorize_query(Q, ECTL, UCTL, ACTL) :-
  (
    is_ectl(Q)
  ->
    ECTL = [Q],
    UCTL = [],
    ACTL = []
  ;
    is_actl(Q)
  ->
    ECTL = [],
    UCTL = [],
    ACTL = [Q]
  ;
    ECTL = [],
    UCTL = [Q],
    ACTL = []
  ).


is_actl(not(Q)) :-
  !,
  is_ectl(Q).

is_actl(Q) :-
  Q =.. [Functor | QQ],
  (
    memberchk(Functor, ['EX', 'EF', 'EG', 'EU'])
  ->
    fail
  ;
    maplist(is_actl, QQ)
  ).


is_ectl(not(Q)) :-
  !,
  is_actl(Q).

is_ectl(Q) :-
  Q =.. [Functor | QQ],
  (
    memberchk(Functor, ['AX', 'AF', 'AG', 'AU'])
  ->
    fail
  ;
    maplist(is_ectl, QQ)
  ).


161 162 163 164 165
find_and_add_rule :-
  find_rule(R),
  print_message(informational, trying(R)),
  add_reaction_backtracking(R).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
166
% TODO use a pattern instead of fixing S =[E]=> P
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
167 168
find_rule(R) :-
  enumerate_all_molecules(Molecules),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  AllMolecules = ['_' | Molecules],
  member(S, AllMolecules),
  member(P, AllMolecules),
  member(E, AllMolecules),
  S \= P,
  (
    E \= '_'
  ->
    S \= E,
    P \= E,
    R = '=>'(S = [E], P)
  ;
    R = '=>'(S, P)
  ),
  \+ item([kind: reaction, item: R]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
184 185 186 187 188


add_reaction_backtracking(R) :-
  (
    add_item([kind: reaction, item: R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
189 190
    % list_reactions,nl,
    print_message(informational, added(R))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
191 192
  ;
    delete_item([kind: reaction, item: R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
193
    print_message(informational, failure),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
194
    print_message(informational, removed(R)),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
195
    fail
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
196
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
197

198 199

find_and_delete_rules :-
200 201 202 203 204 205 206 207 208 209
  (
    nusmv:trace(_)
  ->
    delete_rules_from_trace
  ;
    delete_rules
  ).


delete_rules_from_trace :-
210 211 212
  fail.


213 214 215 216 217 218 219 220 221 222 223 224 225
delete_rules :-
  findall(Item, item([kind: reaction, item: Item]), Reactions),
  sublist(ToBeDeleted, Reactions),
  ToBeDeleted \= [],
  maplist(del_reaction_backtracking, ToBeDeleted).


del_reaction_backtracking(R) :-
  (
    delete_item([kind: reaction, item: R]),
    print_message(informational, removed(R))
  ;
    add_item([kind: reaction, item: R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
226
    print_message(informational, failure),
227 228 229 230 231
    print_message(informational, added(R)),
    fail
  ).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
232 233 234 235 236 237 238
prolog:message(step(Step)) -->
  ['Entering step ~w'-[Step]].

prolog:message(added(Reaction)) -->
  ['Adding reaction ~w'-[Reaction]].

prolog:message(removed(Reaction)) -->
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
239
  ['Removing reaction ~w'-[Reaction]].
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
240 241 242 243 244 245 246 247 248

prolog:message(trying(Reaction)) -->
  ['Considering reaction ~w'-[Reaction]].

prolog:message(checking(Query)) -->
  ['Checking consolidated query ~w'-[Query]].

prolog:message(success(Reactions)) -->
  ['Success!', nl, 'The new model is: ~w'-[Reactions]].
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
249 250 251

prolog:message(failure) -->
  ['Failed'].