revision.pl 9.67 KB
Newer Older
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
1 2 3 4 5 6 7
:- module(
  revision,
  [
    revise_model/1
  ]
).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
8 9
:- use_module(ctl).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
10 11 12 13 14 15 16 17 18 19
% 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
20
    argument. cf. \\texttt{CCFS05tcsb}.'
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
21
  ),
22 23 24 25 26 27 28 29
  revise_model(Query, Added, Removed),
  print_message(informational, added(Added)),
  print_message(informational, removed(Removed)).


revise_model(Query, Added, Removed) :-
  findall(Item, item([kind: reaction, item: Item]), Reactions),
  nb_setval(need_recheck, false),
30
  normalize_query(Query, NQuery, _),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
31
  categorize_query(NQuery, ECTL, UCTL, ACTL),
32 33 34 35
  revise_model(ECTL, UCTL, ACTL, [], [], []),
  findall(Item, item([kind: reaction, item: Item]), NewReactions),
  subtract(NewReactions, Reactions, Added),
  subtract(Reactions, NewReactions, Removed).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
36 37 38 39


% Everything copied as much as possible from CCFS05tcsb

40 41 42 43 44 45 46 47
revise_model(ECTL, UCTL, ACTL, Et, Ut, At) :-
  debug(
    revision,
    'calling revise_model(~w, ~w, ~w, ~w, ~w, ~w)',
    [ECTL, UCTL, ACTL, Et, Ut, At]
  ),
  fail.

48
revise_model([E | ECTL], UCTL, ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
49 50 51
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
52
    check_ctl_impl(E, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
53
  ->
54
    debug(revision, 'Entering step w', ['E'])
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
55
  ;
56
    debug(revision, 'Entering step w', ['E''']),
57
    find_and_add_rule,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
58
    % FIXME check Et too since self-loops can be broken by adding a reaction
59 60 61 62
    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
63

64
revise_model([], [U | UCTL], ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
65 66 67
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
68
    check_ctl_impl(U, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
69
  ->
70
    debug(revision, 'Entering step w', ['U'])
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
71
  ;
72
    (
73
      debug(revision, 'Entering step w', ['U''']),
74 75 76 77
      find_and_add_rule,
      % FIXME check Et too since self-loops can be broken by adding a reaction
      append([U | Ut], At, AllSpec)
    ;
78
      debug(revision, 'Entering step w', ['U''''']),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
79
      delete_rules,
80 81 82 83 84 85
      % 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
86 87 88 89

revise_model([], [], [A | ACTL], Et, Ut, At) :-
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
90
  check_ctl_impl(A, Init, silent, Bool, Result),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
91
  (
92
    Result = true
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
93
  ->
94
    debug(revision, 'Entering step w', ['A']),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
95 96 97 98 99
    % we only consider adding U' and E' when A is fully satisfied, thus moving
    % a step from the A' step of the algorithm to the A step
    % FIXME check At too since self-loops can be added by removing a reaction
    check_and_split(Et, Ut, [], Ett, Utt, [], Eprime, Uprime, [], Init, Bool),
    revise_model(Eprime, Uprime, ACTL, Ett, Utt, [A | At])
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
100
  ;
101
    debug(revision, 'Entering step w', ['A''']),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
102 103 104
    (
      nusmv:trace(_)
    ->
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
105
      delete_rules_from_trace
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
106 107
    ;
      delete_rules,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
108 109 110
      % we delete blindly, so we need to check A now to avoid useless
      % recursion
      check_ctl_impl(A, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
111
    ),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
112
    nb_setval(need_recheck, true),
113 114
    revise_model([], [], [A | ACTL], Et, Ut, At)
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
115 116


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
117
revise_model([], [], [], _, _, _) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
118
  findall(Item, item([kind: reaction, item: Item]), Reactions),
119
  debug(revision, 'Success!~nThe new model is: ~w', [Reactions]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
120 121


122 123 124
recheck_spec([], _, _) :-
  !.

125 126
recheck_spec(Specs, Init, Bool) :-
  join_op('/\\', Specs, Spec),
127
  debug(revision, 'Checking consolidated query ~w', [Spec]),
128 129 130
  check_ctl_impl(Spec, Init, no, Bool, true).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
131 132
check_and_split(E, U, A, Et, Ut, At, Ef, Uf, Af, Init, Bool) :-
  (
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
133
    nb_getval(need_recheck, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
134
  ->
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
135
    nb_setval(need_recheck, false),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
136
    % TODO use a single NuSMV process, with a single model loading step
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    check_and_split(E, Et, Ef, Init, Bool),
    check_and_split(U, Ut, Uf, Init, Bool),
    check_and_split(A, At, Af, Init, Bool)
  ;
    Et = E,
    Ut = U,
    At = A,
    Ef = [],
    Uf = [],
    Af = []
  ).


check_and_split([], [], [], _Init, _Bool).

check_and_split([F | Formulae], Ft, Ff, Init, Bool) :-
  (
    check_ctl_impl(F, Init, no, Bool, true)
  ->
    Ft = [F | FFt],
    Ff = FFf
  ;
    Ft = FFt,
    Ff = [F | FFf]
  ),
  check_and_split(Formulae, FFt, FFf, Init, Bool).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
165 166 167 168 169 170 171 172 173 174
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) :-
  !,
175 176 177 178
  categorize_query(Q, NACTL, NUCTL, NECTL),
  maplist(negate_query, NACTL, ACTL),
  maplist(negate_query, NUCTL, UCTL),
  maplist(negate_query, NECTL, ECTL).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

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 = []
  ).


204 205 206
negate_query(Y, not(Y)).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
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)
  ).


237 238
find_and_add_rule :-
  find_rule(R),
239
  debug(revision, 'Considering adding reaction ~w', [R]),
240 241
  add_reaction_backtracking(R).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
242 243
find_rule(R) :-
  enumerate_all_molecules(Molecules),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
244
  AllMolecules = ['_' | Molecules],
245 246
  member(M1, AllMolecules),
  member(M2, AllMolecules),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
247
  (
248
    M1 \= '_'
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
249
  ->
250
    M1 @< M2
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
251
  ;
252
    M1 @=< M2
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
253
  ),
254 255 256 257 258 259 260 261
  (
    % both '_'
    M1 = M2
  ->
    M3 = '_'
  ;
    true
  ),
262 263 264 265 266 267 268 269 270 271 272
  member(M3, AllMolecules),
  member(M4, AllMolecules),
  memberchk('_', [M1, M2, M3, M4]),
  (
    M3 \= '_'
  ->
    M3 @< M4
  ;
    M3 @=< M4
  ),
  (M1, M2) \= (M3, M4),
273 274 275 276 277 278 279 280
  (
    % both '_'
    M3 = M4
  ->
    M1 = '_'
  ;
    true
  ),
281
  reaction_editor:simplify_reaction('=>'(M1 + M2, M3 + M4), R),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
282
  \+ item([kind: reaction, item: R]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
283 284 285 286 287


add_reaction_backtracking(R) :-
  (
    add_item([kind: reaction, item: R]),
288
    debug(revision, 'Adding reaction ~w', [R])
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
289 290
  ;
    delete_item([kind: reaction, item: R]),
291
    debug(revision, 'Failure, removing added reaction ~w', [R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
292
    fail
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
293
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
294

295

296
delete_rules_from_trace :-
297 298 299 300 301
  enumerate_all_molecules(Molecules),
  findall(State, nusmv:trace(State), States),
  rules_from_trace(States, Molecules, Rules),
  % We cut a single state-transition, if necessary by removing several rules
  member(ToBeDeleted, Rules),
302
  ToBeDeleted \= [],
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  maplist(del_reaction_backtracking, ToBeDeleted).


rules_from_trace([_], _, []).

rules_from_trace([S1, S2 | States], Molecules, [R | Rules]) :-
  rules_from_states(S1, S2, Molecules, R),
  rules_from_trace([S2 | States], Molecules, Rules).


rules_from_states(S1, S2, Molecules, R) :-
  rules_from_states(S1, S2, Molecules, [], [], [], R).


rules_from_states([], [], [], Inc, Dec, Pres, Reactions) :-
  msort(Inc, SInc),
  msort(Dec, SDec),
  msort(Pres, SPres),
  findall(Reaction, compatible(SInc, SDec, SPres, Reaction), Reactions).

rules_from_states(["TRUE" | S1], ["TRUE" | S2], [M | Molecules], I, D, P, R) :-
  rules_from_states(S1, S2, Molecules, I, D, [M | P], R).

rules_from_states(["TRUE" | S1], ["FALSE" | S2], [M | Molecules], I, D, P, R) :-
  rules_from_states(S1, S2, Molecules, I, [M | D], [M | P], R).

rules_from_states(["FALSE" | S1], ["TRUE" | S2], [M | Molecules], I, D, P, R) :-
  rules_from_states(S1, S2, Molecules, [M | I], D, P, R).

rules_from_states(["FALSE" | S1], ["FALSE" | S2], [_ | Molecules], I, D, P, R) :-
  rules_from_states(S1, S2, Molecules, I, D, P, R).
334 335


336 337 338 339 340 341 342 343 344 345
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]),
346
    debug(revision, 'Removing reaction ~w', [R])
347 348
  ;
    add_item([kind: reaction, item: R]),
349
    debug(revision, 'Failure, adding back reaction ~w', [R]),
350 351 352 353
    fail
  ).


354 355 356 357
compatible(SInc, SDec, SPres, Reaction) :-
  item([kind: reaction, item: Reaction]),
  reaction(Reaction, _, Reactants, Products, false),
  remove_stoichiometry_and_sort(Reactants, Products, UReac, UProd, UCata),
358 359 360
  % products are true at the end
  ord_union(SInc, SPres, SAfter),
  ord_subset(UProd, SAfter),
361 362
  % only products can increase
  ord_subset(SInc, UProd),
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  % only reactants can decrease
  ord_subset(SDec, UReac),
  % all reactants and catalyst had to be present
  ord_subset(UReac, SPres),
  ord_subset(UCata, SPres).


% Based on reaction/5 implementation, we know catalysts come first
remove_stoichiometry_and_sort([_*M | Reac], [_*M | Prod], UReac, UProd, UUCata) :-
  !,
  remove_stoichiometry_and_sort(Reac, Prod, UReac, UProd, UCata),
  ord_add_element(UCata, M, UUCata).

remove_stoichiometry_and_sort(Reactants, Products, UReac, UProd, []) :-
  maplist(remove_stoich, Reactants, Reac),
  msort(Reac, UReac),
  maplist(remove_stoich, Products, Prod),
  msort(Prod, UProd).


remove_stoich(_*X, X).


386 387 388 389
prolog:message(added(Added)) -->
  { maplist(term_to_atom, Added, AddedAtoms),
    atomic_list_concat(AddedAtoms, ', ', AddedAtom) },
  ['Added reactions: ~w'-[AddedAtom]].
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
390

391 392 393 394
prolog:message(removed(Removed)) -->
  { maplist(term_to_atom, Removed, RemovedAtoms),
    atomic_list_concat(RemovedAtoms, ', ', RemovedAtom) },
  ['Removed reactions: ~w'-[RemovedAtom]].