revision.pl 9.06 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
  normalize_query(Query, NQuery, _),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
23 24
  categorize_query(NQuery, ECTL, UCTL, ACTL),
  nb_setval(need_recheck, false),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
25 26 27 28 29
  revise_model(ECTL, UCTL, ACTL, [], [], []).


% Everything copied as much as possible from CCFS05tcsb

30
revise_model([E | ECTL], UCTL, ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
31 32 33
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
34
    check_ctl_impl(E, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
35
  ->
36
    print_message(informational, step('E'))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
37
  ;
38 39
    print_message(informational, step('E''')),
    find_and_add_rule,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
40
    % FIXME check Et too since self-loops can be broken by adding a reaction
41 42 43 44
    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
45

46
revise_model([], [U | UCTL], ACTL, Et, Ut, At) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
47 48 49
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
  (
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
50
    check_ctl_impl(U, Init, no, Bool, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
51
  ->
52
    print_message(informational, step('U'))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
53
  ;
54 55 56 57 58 59 60
    (
      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''''')),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
61
      delete_rules,
62 63 64 65 66 67
      % 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
68 69 70 71

revise_model([], [], [A | ACTL], Et, Ut, At) :-
  item([kind: option, item: option(nusmv_initial_states: Init)]),
  item([kind: option, item: option(boolean_semantics: Bool)]),
72
  check_ctl_impl(A, Init, silent, Bool, Result),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
73
  (
74
    Result = true
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
75
  ->
76
    print_message(informational, step('A')),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
77 78 79 80 81
    % 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
82
  ;
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
83
    print_message(informational, step('A''')),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
84 85 86
    (
      nusmv:trace(_)
    ->
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
87
      delete_rules_from_trace
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
88 89
    ;
      delete_rules,
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
90 91 92
      % 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
93
    ),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
94
    nb_setval(need_recheck, true),
95 96
    revise_model([], [], [A | ACTL], Et, Ut, At)
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
97 98


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
99
revise_model([], [], [], _, _, _) :-
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
100 101
  findall(Item, item([kind: reaction, item: Item]), Reactions),
  print_message(informational, success(Reactions)).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
102 103


104 105 106
recheck_spec([], _, _) :-
  !.

107 108 109 110 111 112
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
113 114
check_and_split(E, U, A, Et, Ut, At, Ef, Uf, Af, Init, Bool) :-
  (
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
115
    nb_getval(need_recheck, true)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
116
  ->
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
117
    nb_setval(need_recheck, false),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
118
    % TODO use a single NuSMV process, with a single model loading step
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
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
    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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 204 205 206 207 208 209 210 211 212
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)
  ).


213 214 215 216 217
find_and_add_rule :-
  find_rule(R),
  print_message(informational, trying(R)),
  add_reaction_backtracking(R).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
218 219
find_rule(R) :-
  enumerate_all_molecules(Molecules),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
220
  AllMolecules = ['_' | Molecules],
221 222
  member(M1, AllMolecules),
  member(M2, AllMolecules),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
223
  (
224
    M1 \= '_'
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
225
  ->
226
    M1 @< M2
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
227
  ;
228
    M1 @=< M2
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
229
  ),
230 231 232 233 234 235 236 237
  (
    % both '_'
    M1 = M2
  ->
    M3 = '_'
  ;
    true
  ),
238 239 240 241 242 243 244 245 246 247 248
  member(M3, AllMolecules),
  member(M4, AllMolecules),
  memberchk('_', [M1, M2, M3, M4]),
  (
    M3 \= '_'
  ->
    M3 @< M4
  ;
    M3 @=< M4
  ),
  (M1, M2) \= (M3, M4),
249 250 251 252 253 254 255 256
  (
    % both '_'
    M3 = M4
  ->
    M1 = '_'
  ;
    true
  ),
257
  reaction_editor:simplify_reaction('=>'(M1 + M2, M3 + M4), R),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
258
  \+ item([kind: reaction, item: R]).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
259 260 261 262 263


add_reaction_backtracking(R) :-
  (
    add_item([kind: reaction, item: R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
264
    print_message(informational, added(R))
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
265 266
  ;
    delete_item([kind: reaction, item: R]),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
267
    print_message(informational, failure),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
268
    print_message(informational, removed(R)),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
269
    fail
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
270
  ).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
271

272

273
delete_rules_from_trace :-
274 275 276 277 278
  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),
279
  ToBeDeleted \= [],
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  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).
311 312


313 314 315 316 317 318 319 320 321 322 323 324 325
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
326
    print_message(informational, failure),
327 328 329 330 331
    print_message(informational, added(R)),
    fail
  ).


332 333 334 335
compatible(SInc, SDec, SPres, Reaction) :-
  item([kind: reaction, item: Reaction]),
  reaction(Reaction, _, Reactants, Products, false),
  remove_stoichiometry_and_sort(Reactants, Products, UReac, UProd, UCata),
336 337 338
  % products are true at the end
  ord_union(SInc, SPres, SAfter),
  ord_subset(UProd, SAfter),
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  % 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).


SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
362 363 364 365 366 367 368
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
369
  ['Removing reaction ~w'-[Reaction]].
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
370 371 372 373 374 375 376 377 378

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
379 380 381

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