models.pl 18.2 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  models,
  [
4 5 6
    % Grammars
    range/1,
    ref/1,
7
    % Commands
Thierry Martinez's avatar
Thierry Martinez committed
8
    load/1,
Thierry Martinez's avatar
Thierry Martinez committed
9
    add/1,
Thierry Martinez's avatar
Thierry Martinez committed
10
    load_biocham/1,
Thierry Martinez's avatar
Thierry Martinez committed
11
    add_biocham/1,
12
    export_biocham/1,
Thierry Martinez's avatar
Thierry Martinez committed
13
    new_model/0,
Thierry Martinez's avatar
Thierry Martinez committed
14
    clear_model/0,
Thierry Martinez's avatar
Thierry Martinez committed
15
    list_models/0,
Thierry Martinez's avatar
Thierry Martinez committed
16
    list_current_models/0,
17
    list_model/0,
Thierry Martinez's avatar
Thierry Martinez committed
18
    select_model/1,
Thierry Martinez's avatar
Thierry Martinez committed
19 20
    set_model_name/1,
    delete/1,
21 22
    inherits/1,
    % Public API
Thierry Martinez's avatar
Thierry Martinez committed
23 24
    get_model_name/1,
    get_model_name/2,
Thierry Martinez's avatar
Thierry Martinez committed
25 26
    current_models/1,
    single_model/1,
27
    new_model/1,
Thierry Martinez's avatar
Thierry Martinez committed
28
    set_current_models/1,
Thierry Martinez's avatar
Thierry Martinez committed
29
    add_item/1,
Thierry Martinez's avatar
Thierry Martinez committed
30
    replace_item/4,
Thierry Martinez's avatar
Thierry Martinez committed
31
    change_item/4,
Thierry Martinez's avatar
Thierry Martinez committed
32 33 34
    set_annotation/3,
    get_annotation/3,
    delete_annotation/2,
Thierry Martinez's avatar
Thierry Martinez committed
35
    item/1,
Thierry Martinez's avatar
Thierry Martinez committed
36
    find_item/1,
Thierry Martinez's avatar
Thierry Martinez committed
37
    all_items/2,
Thierry Martinez's avatar
Thierry Martinez committed
38
    all_ids/2,
Thierry Martinez's avatar
Thierry Martinez committed
39
    list_items/1,
Thierry Martinez's avatar
Thierry Martinez committed
40
    list_ids/1,
Thierry Martinez's avatar
Thierry Martinez committed
41
    list_ids/2,
42
    print_item/1,
Thierry Martinez's avatar
Thierry Martinez committed
43
    deleting/1,
Thierry Martinez's avatar
Thierry Martinez committed
44 45
    delete_item/1,
    delete_items/1,
Thierry Martinez's avatar
Thierry Martinez committed
46
    add_dependency/2,
Thierry Martinez's avatar
Thierry Martinez committed
47 48
    add_file_suffix/2,
    inherits/2,
Thierry Martinez's avatar
Thierry Martinez committed
49 50
    inherits_from/2,
    directly_inherits_from/2,
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
51
    begin_command/0,
Thierry Martinez's avatar
Thierry Martinez committed
52 53
    get_selection/3,
    set_selection/3,
Thierry Martinez's avatar
ploum  
Thierry Martinez committed
54
    at_delete/2,
Thierry Martinez's avatar
Thierry Martinez committed
55 56
    get_parent/2,
    get_model/2
Thierry Martinez's avatar
Thierry Martinez committed
57 58 59
  ]
).

Thierry Martinez's avatar
Thierry Martinez committed
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
:- devdoc('\\section{Grammars}').


:- grammar(range).


range(Integer0 - Integer1) :-
  integer(Integer0),
  integer(Integer1).

range(Integer) :-
  integer(Integer).


:- grammar(ref).


ref(Range) :-
  list(range, Range).

ref(Name) :-
  name(Name).


Thierry Martinez's avatar
Thierry Martinez committed
85
:- devdoc('\\section{Commands}').
Thierry Martinez's avatar
Thierry Martinez committed
86 87


Thierry Martinez's avatar
Thierry Martinez committed
88 89 90 91 92 93 94 95 96
load(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as the corresponding \\command{load_biocham/1} / \\command{load_sbml/1}
    / \\command{load_ode/1} / \\command{load_trace/1},
    depending on the file extension
    (respectively \\texttt{.bc}, \\texttt{.xml}, \\texttt{.ode}, \\texttt{.csv}
    -- assuming no extension is \\texttt{.bc}).'),
Thierry Martinez's avatar
Thierry Martinez committed
97
  load_all(_Suffix, InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
98 99 100 101 102 103 104 105 106 107 108


add(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as the corresponding \\command{add_biocham/1} / \\command{add_sbml/1}
    / \\command{add_ode/1} / \\command{add_trace/1},
    depending on the file extension
    (respectively \\texttt{.bc}, \\texttt{.xml}, \\texttt{.ode}, \\texttt{.csv}
    -- assuming no extension is \\texttt{.bc}).'),
Thierry Martinez's avatar
Thierry Martinez committed
109
  add_all(_Suffix, InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
110 111 112 113 114 115 116 117 118 119 120


load_biocham(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    opens a new model, loads the reaction rules and executes the commands
    (with the file directory as current directory)
    contained in the given Biocham \\texttt{.bc} file.
    The suffix \\texttt{.bc} is automatically added to the name if such a
    file exists.'),
Thierry Martinez's avatar
Thierry Martinez committed
121
  load_all('bc', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
122 123 124 125 126 127 128 129 130 131


add_biocham(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    the rules of the given \\texttt{.bc} file are loaded and
    \\emph{added} to the current set of rules.
    The commands contained in the file are executed
    (with the file directory as current directory).'),
Thierry Martinez's avatar
Thierry Martinez committed
132
  add_all('bc', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
133 134


135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
export_biocham(OutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  doc('
    exports the current model into a \\texttt{.bc} file.
  '),
  setup_call_cleanup(
    open(OutputFile, write, Stream),
    with_output_to(
      Stream,
      list_model
    ),
    close(Stream)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
151 152 153
new_model :-
  biocham_command,
  doc('opens a new fresh model.'),
154 155 156 157 158 159 160 161 162 163
  (
    fresh,
    current_models([_Id])
  ->
    true
  ;
    new_model(Id),
    set_current_models([Id]),
    assertz(fresh)
  ).
Thierry Martinez's avatar
Thierry Martinez committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179


clear_model :-
  biocham_command,
  doc('clears the current model.'),
  current_models(CurrentModels),
  \+ (
    member(Id, CurrentModels),
    \+ (
      delete_item(Id)
    )
  ).


list_models :-
  biocham_command,
180
  doc('lists all open models.'),
Thierry Martinez's avatar
Thierry Martinez committed
181 182 183 184 185
  list_items([parent: top, kind: model]).


list_current_models :-
  biocham_command,
186
  doc('lists current models.'),
Thierry Martinez's avatar
Thierry Martinez committed
187 188 189 190
  current_models(CurrentModels),
  list_ids(CurrentModels).


191 192 193 194 195 196 197 198 199 200 201 202
list_model :-
  biocham_command,
  doc('lists the contents of the current Biocham model.'),
  (
    is_reaction_model
  ->
    list_model_reactions
  ;
    is_influence_model
  ->
    list_model_influences
  ),
203
  list_model_initial_state,
204
  list_model_parameters,
Thierry Martinez's avatar
Thierry Martinez committed
205
  list_model_events,
206 207
  list_model_functions,
  list_model_options.
208

Thierry Martinez's avatar
Thierry Martinez committed
209 210 211 212 213

select_model(RefSet) :-
  biocham_command,
  type(RefSet, {ref}),
  doc('selects some models.'),
Thierry Martinez's avatar
Thierry Martinez committed
214
  find_model_refs(RefSet, CurrentModels),
Thierry Martinez's avatar
Thierry Martinez committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  set_current_models(CurrentModels).


set_model_name(Name) :-
  biocham_command,
  type(Name, name),
  doc('changes the current model name.'),
  single_model(Id),
  replace_item(Id, model, Name, Name).


delete(Indexes) :-
  biocham_command(*),
  type(Indexes, '*'([range])),
  doc('deletes the listed elements from the model.'),
  indexes_ids(Indexes, Ids),
  \+ (
    member(Id, Ids),
    \+ (
      delete_item(Id)
    )
  ).


239 240
inherits(Ancestors) :-
  biocham_command(*),
Thierry Martinez's avatar
Thierry Martinez committed
241
  type(Ancestors, '*'(ref)),
242
  doc('makes the current model inherit from the given ancestor models.'),
Thierry Martinez's avatar
Thierry Martinez committed
243
  single_model(Id),
Thierry Martinez's avatar
Thierry Martinez committed
244
  find_model_refs(Ancestors, Models),
Thierry Martinez's avatar
Thierry Martinez committed
245
  \+ (
Thierry Martinez's avatar
Thierry Martinez committed
246
    member(AncestorId, Models),
Thierry Martinez's avatar
Thierry Martinez committed
247
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
248
      inherits(Id, AncestorId)
Thierry Martinez's avatar
Thierry Martinez committed
249 250 251 252
    )
  ).


253
:- devdoc('\\section{Public API}').
Thierry Martinez's avatar
Thierry Martinez committed
254 255


256 257 258
get_model_name(Name) :-
  single_model(Id),
  get_model_name(Id, Name).
Thierry Martinez's avatar
Thierry Martinez committed
259 260


261 262
get_model_name(Id, Name) :-
  find_item([parent: top, id: Id, item: Name]).
Thierry Martinez's avatar
Thierry Martinez committed
263 264


Thierry Martinez's avatar
Thierry Martinez committed
265
current_models(Models) :-
Thierry Martinez's avatar
Thierry Martinez committed
266
  get_selection(top, current_models, Models).
Thierry Martinez's avatar
Thierry Martinez committed
267 268


269
single_model(Model) :-
Thierry Martinez's avatar
Thierry Martinez committed
270
  (
271
    current_models([Model])
Thierry Martinez's avatar
Thierry Martinez committed
272
  ->
Thierry Martinez's avatar
Thierry Martinez committed
273
    true
Thierry Martinez's avatar
Thierry Martinez committed
274
  ;
275
    throw(error(need_single_model))
Thierry Martinez's avatar
Thierry Martinez committed
276 277 278
  ).


279 280
new_model(Id) :-
  add_item([parent: top, kind: model, key: new_model, id: Id]),
Thierry Martinez's avatar
Thierry Martinez committed
281
  (
282
    item([parent: top, kind: model, key: initial, id: InitialId])
Thierry Martinez's avatar
Thierry Martinez committed
283
  ->
284
    inherits(Id, InitialId)
Thierry Martinez's avatar
Thierry Martinez committed
285 286 287
  ;
    true
  ).
Thierry Martinez's avatar
Thierry Martinez committed
288

Thierry Martinez's avatar
Thierry Martinez committed
289

Thierry Martinez's avatar
Thierry Martinez committed
290 291 292 293 294 295 296 297 298 299
set_current_models(Models) :-
  (
    Models = []
  ->
    throw(error(cannot_select_no_models))
  ;
    set_selection(top, current_models, Models)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
300 301 302 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
add_item(Options) :-
  memberchk(kind: Kind, Options),
  optional(item: Item, Options),
  optional(parent: Parent, Options),
  optional(key: Key, Options),
  optional(id: Id, Options),
  default(Parent, current_model),
  default(Key, []),
  (
    Parent = current_model
  ->
    single_model(Parent0)
  ;
    Parent0 = Parent
  ),
  (
    var(Item)
  ->
    (
      list(Key)
    ->
      throw(error(no_item_given))
    ;
      Item = Key
    )
  ;
    true
  ),
Thierry Martinez's avatar
Thierry Martinez committed
328
  not_fresh,
Thierry Martinez's avatar
Thierry Martinez committed
329
  create_item_id(Id),
Thierry Martinez's avatar
Thierry Martinez committed
330 331
  put_item(Parent0, Kind, Key, Item, Id),
  update_parent_model_identifier_kinds(Parent0).
Thierry Martinez's avatar
Thierry Martinez committed
332

Thierry Martinez's avatar
Thierry Martinez committed
333

334 335 336
replace_item(Id, Kind, Key, Item) :-
  retract(item(Id, Parent, _Kind, _Item)),
  retractall(key(_Key, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
337 338
  put_item(Parent, Kind, Key, Item, Id),
  update_parent_model_identifier_kinds(Parent).
Thierry Martinez's avatar
Thierry Martinez committed
339 340


341 342 343
change_item(Options, Kind, Key, Item) :-
  catch(
    (
344
      find_item([noinheritance, kind: Kind, key: Key, id: Id | Options]),
345 346 347 348
      replace_item(Id, Kind, Key, Item)
    ),
    error(unknown_item),
    add_item([kind: Kind, key: Key, item: Item | Options])
Thierry Martinez's avatar
Thierry Martinez committed
349 350
  ).

Thierry Martinez's avatar
Thierry Martinez committed
351

352 353 354
set_annotation(Id, Kind, Annotation) :-
  retractall(annotation(Id, Kind, _)),
  assertz(annotation(Id, Kind, Annotation)).
Thierry Martinez's avatar
Thierry Martinez committed
355

356 357 358 359 360 361 362

get_annotation(Id, Kind, Annotation) :-
  annotation(Id, Kind, Annotation).


delete_annotation(Id, Kind) :-
  retract(annotation(Id, Kind, _)).
Thierry Martinez's avatar
Thierry Martinez committed
363 364 365 366 367 368 369 370


item(Options) :-
  optional(kind: Kind, Options),
  optional(id: Id, Options),
  optional(key: Key, Options),
  optional(item: Item, Options),
  item_parent_option(Options, Parent),
Thierry Martinez's avatar
Thierry Martinez committed
371
  (
Thierry Martinez's avatar
Thierry Martinez committed
372
    var(Id)
Thierry Martinez's avatar
Thierry Martinez committed
373
  ->
Thierry Martinez's avatar
Thierry Martinez committed
374 375 376 377 378 379
    (
      Parent0 = Parent
    ;
      \+ member(no_inheritance, Options),
      inherits_from(Parent, Parent0)
    )
Thierry Martinez's avatar
Thierry Martinez committed
380
  ;
Thierry Martinez's avatar
Thierry Martinez committed
381
    Parent0 = Parent
Thierry Martinez's avatar
Thierry Martinez committed
382
  ),
Thierry Martinez's avatar
Thierry Martinez committed
383 384 385 386 387 388 389
  (
    list(Kind)
  ->
    member(Kind0, Kind)
  ;
    Kind0 = Kind
  ),
Thierry Martinez's avatar
Thierry Martinez committed
390 391 392 393
  (
    var(Key)
  ->
    true
Thierry Martinez's avatar
Thierry Martinez committed
394 395 396 397 398
  ;
    list(Key)
  ->
    indexes_ids(Key, Ids),
    member(Id, Ids)
Thierry Martinez's avatar
Thierry Martinez committed
399 400 401
  ;
    key(Key, Id)
  ),
Thierry Martinez's avatar
Thierry Martinez committed
402
  item(Id, Parent0, Kind0, Item).
Thierry Martinez's avatar
Thierry Martinez committed
403

Thierry Martinez's avatar
Thierry Martinez committed
404

Thierry Martinez's avatar
Thierry Martinez committed
405 406 407 408 409 410 411 412
find_item(Options) :-
  (
    item(Options)
  ->
    true
  ;
    throw(error(unknown_item))
  ).
Thierry Martinez's avatar
Thierry Martinez committed
413

Thierry Martinez's avatar
Thierry Martinez committed
414

Thierry Martinez's avatar
Thierry Martinez committed
415 416 417 418 419 420 421
all_items(Options, Items) :-
  findall(
    Item,
    item([item: Item | Options]),
    Items
  ).

Thierry Martinez's avatar
Thierry Martinez committed
422 423 424 425 426 427 428 429 430

all_ids(Options, Ids) :-
  findall(
    Id,
    item([id: Id | Options]),
    Ids
  ).


Thierry Martinez's avatar
Thierry Martinez committed
431
list_items(Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  all_ids([no_inheritance | Options], Ids),
  list_ids(Options, Ids),
  (
    member(no_inheritance, Options)
  ->
    true
  ;
    item_parent_option(Options, Parent),
    (
      var(Parent)
    ->
      true
    ;
      optional(indent: Indent, Options),
      default(Indent, 0),
      \+ (
        inherits_from(Parent, Parent0),
        all_ids([parent: Parent0, no_inheritance | Options], InheritedIds),
        InheritedIds \= [],
        \+ (
          indent(Indent),
          find_item([id: Parent0, item: ParentName]),
          format('From inherited \'~w\':\n', [ParentName]),
          list_ids(Options, InheritedIds)
        )
      )
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
460 461 462


list_ids(Ids) :-
Thierry Martinez's avatar
Thierry Martinez committed
463 464 465 466
  list_ids([], Ids).


list_ids(Options, Ids) :-
Thierry Martinez's avatar
Thierry Martinez committed
467
  (
468 469
    peek_count(list_item_counter, 0),
    \+ memberchk(no_numbering, Options)
Thierry Martinez's avatar
Thierry Martinez committed
470 471 472 473 474
  ->
    retractall(listed_item(_, _))
  ;
    true
  ),
Thierry Martinez's avatar
Thierry Martinez committed
475 476 477
  list_ids_aux(Options, Ids).


478 479 480 481 482 483 484 485 486 487 488 489 490
print_item(Id) :-
  item(Id, _Parent, Kind, Item),
  (
    atom_concat('print_', Kind, F),
    G =.. [F, Id, Item],
    predicate_property(G, visible)
  ->
    G
  ;
    write(Item)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
491 492 493
:- dynamic(deleting/1).


Thierry Martinez's avatar
Thierry Martinez committed
494 495 496 497 498 499 500
delete_item(Options) :-
  list(Options),
  !,
  find_item([id: Id | Options]),
  delete_item(Id).


Thierry Martinez's avatar
Thierry Martinez committed
501
delete_item(Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
502 503 504 505 506 507 508
  (
    deleting(Id)
 ->
    throw(error(circular_delete(Id), delete_item))
  ;
    assertz(deleting(Id))
  ),
Thierry Martinez's avatar
Thierry Martinez committed
509 510
  \+ (
    at_delete_goal(Id, Goal),
511
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
512
      Goal
Thierry Martinez's avatar
Thierry Martinez committed
513 514 515 516
    ->
      true
    ;
      throw(error(delete_goal_failure(Id), delete_item))
Thierry Martinez's avatar
Thierry Martinez committed
517 518
    )
  ),
Thierry Martinez's avatar
Thierry Martinez committed
519 520 521 522
  retract(item(Id, _Model, KindItem, _Item)),
  retractall(key(_Key, Id)),
  retractall(annotation(Id, _KindAnnotation, _Annotation)),
  retractall(listed_item(_Index, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
523
  retractall(dependency(Id, _Master)),
Thierry Martinez's avatar
Thierry Martinez committed
524
  retractall(at_delete_goal(Id, _Goal)),
Thierry Martinez's avatar
Thierry Martinez committed
525 526 527
  \+ (
    item(SubId, Id, _SubKind, _SubItem),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
528 529 530 531 532 533 534
      (
        delete_item(SubId)
      ->
        true
      ;
        true
      )
Thierry Martinez's avatar
Thierry Martinez committed
535 536 537 538 539
    )
  ),
  \+ (
    dependency(SubId, Id),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
540 541 542 543 544 545 546
      (
        delete_item(SubId)
      ->
        true
      ;
        true
      )
Thierry Martinez's avatar
Thierry Martinez committed
547 548
    )
  ),
Thierry Martinez's avatar
Thierry Martinez committed
549
  retractall(selection(_, _, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
550 551 552 553
  (
    KindItem = model
  ->
    (
Thierry Martinez's avatar
Thierry Martinez committed
554
      selection(top, current_models, _)
Thierry Martinez's avatar
Thierry Martinez committed
555 556
    ->
      true
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
557 558
    ;
      new_model
Thierry Martinez's avatar
Thierry Martinez committed
559 560 561
    )
  ;
    true
Thierry Martinez's avatar
Thierry Martinez committed
562 563
  ),
  retract(deleting(Id)).
Thierry Martinez's avatar
Thierry Martinez committed
564 565


Thierry Martinez's avatar
Thierry Martinez committed
566 567 568 569 570 571 572 573 574
delete_items(Options) :-
  \+ (
    item([id: Id | Options]),
    \+ (
      delete_item(Id)
    )
  ).


575 576 577 578 579 580 581 582 583 584 585 586 587 588
add_dependency(IdSub, IdMaster) :-
  assertz(dependency(IdSub, IdMaster)).


:- multifile(add_file_suffix/2).


add_file_suffix('', add_biocham_file).


add_file_suffix('bc', add_biocham_file).


inherits(Id, AncestorId) :-
Thierry Martinez's avatar
Thierry Martinez committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
  (
    Id = AncestorId
  ->
    throw(error(cannot_inherit_from_itself, inherits))
  ;
    self_or_inherits_from(Child, Id),
    self_or_inherits_from(AncestorId, Parent),
    inherits_from(Parent, Child)
  ->
    throw(error(cannot_inherit_from_descendant, inherits))
  ;
    (
      item(Id, top, model, _Item0),
      item(AncestorId, top, model, _Item1),
      self_or_inherits_from(Child, Id),
      identifier_kind(Child, Ident, Kind0),
      Kind0 \= free,
      identifier_kind(AncestorId, Ident, Kind1),
      Kind1 \= free,
      Kind0 \= Kind1
    ->
      throw(error(kind_mismatch(Ident, Kind0, Kind1), inherits))
    ;
      true
    ),
    assertz(directly_inherits_from(Id, AncestorId))
  ).


prolog:error_message(cannot_inherit_from_itself) -->
  [
    'Cannot inherit from itself.'
  ].


prolog:error_message(already_inherits) -->
  [
    'There is already such an inheritance relation.'
  ].


prolog:error_message(cannot_inherit_from_descendant) -->
  [
    'Cannot inherit from descendant.'
  ].

prolog:error_message(
  kind_mismatch(Ident, Kind, NeededKind)
) -->
  {
    format(
      atom(Message),
      'There is a mismatch between the uses of ~a: ~a and ~a.',
      [Ident, Kind, NeededKind]
    )
  },
  [Message].


inherits_from(Id, AncestorId) :-
  directly_inherits_from(Id, AncestorId).

inherits_from(Id, AncestorId) :-
  directly_inherits_from(Id, IntermediateAncestorId),
  inherits_from(IntermediateAncestorId, AncestorId).


self_or_inherits_from(Id, Id).

self_or_inherits_from(Id, AncestorId) :-
    inherits_from(Id, AncestorId).


:- dynamic(directly_inherits_from/2).
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702


begin_command :-
  set_counter(list_item_counter, 0).


get_selection(ParentOrCurrentModel, SelectionName, Ids) :-
  get_selection_parent(ParentOrCurrentModel, Parent),
  findall(
    Id,
    selection(Parent, SelectionName, Id),
    Ids
  ).


set_selection(ParentOrCurrentModel, SelectionName, IdsOrOptions) :-
  get_selection_parent(ParentOrCurrentModel, Parent),
  retractall(selection(Parent, SelectionName, _)),
  \+ (
    (
      IdsOrOptions = [_: _| _]
    ->
      item([id: Id | IdsOrOptions])
    ;
      member(Id, IdsOrOptions)
    ),
    \+ (
      assertz(selection(Parent, SelectionName, Id))
    )
  ).


at_delete(Id, Goal) :-
  assertz(at_delete_goal(Id, Goal)).


get_parent(Id, ParentId) :-
  item(Id, ParentId, _, _).


Thierry Martinez's avatar
Thierry Martinez committed
703 704 705 706 707 708 709 710 711 712 713
get_model(Id, ModelId) :-
  item(Id, ParentId, Kind, _),
  (
    Kind = model
  ->
    ModelId = Id
  ;
    get_model(ParentId, ModelId)
  ).


714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
:- devdoc('\\section{Private predicates}').


default(Variable, Value) :-
  (
    var(Variable)
  ->
    Variable = Value
  ;
    true
  ).


put_item(Parent, Kind, Key, Item, Id) :-
  (
    list(Key)
  ->
    \+ (
      member(K, Key),
      \+ (
        assertz(key(K, Id))
      )
    )
  ;
    assertz(key(Key, Id))
  ),
  assertz(item(Id, Parent, Kind, Item)).


single_model(Model, ModelId) :-
  (
    Model = current_model
  ->
    single_model(ModelId)
  ;
    ModelId = Model
  ).


item_parent_option(Options, ChosenParent) :-
  optional(parent: Parent, Options),
  optional(id: Id, Options),
  (
    var(Id)
  ->
    default(Parent, current_model),
    (
      Parent == current_model
    ->
      single_model(ChosenParent)
    ;
      Parent == current_models
    ->
      current_models(Models),
      member(ChosenParent, Models)
    ;
      ChosenParent = Parent
    )
  ;
    ChosenParent = Parent
  ).


optional(Item, List) :-
  (
    member(Item, List)
  ->
    true
  ;
    true
  ).


:- dynamic(listed_item/2).


list_ids_aux(Options, Ids) :-
  optional(indent: Indent, Options),
  default(Indent, 0),
  (
    member(recursive, Options)
  ->
    Recursive = yes
  ;
    Recursive = no
  ),
  \+ (
    member(Id, Ids),
    \+ (
      indent(Indent),
      (
        selection(_, _, Id)
      ->
        Selected = '*'
      ;
        Selected = ' '
      ),
      (
        memberchk(no_numbering, Options)
      ->
        true
      ;
        count(list_item_counter, Counter),
        assertz(listed_item(Counter, Id)),
        format('[~d]', [Counter])
      ),
820 821 822
      write(Selected),
      print_item(Id),
      nl,
823 824 825 826 827 828 829 830 831 832 833 834 835
      (
        Recursive = yes
      ->
        SubIndent is Indent + 1,
        all_ids([parent: Id | Options], SubIds),
        list_ids_aux([indent: SubIndent, recursive | Options], SubIds)
      ;
        true
      )
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
836 837 838 839 840 841 842 843 844 845 846
indexes_ids(Indexes, Ids) :-
  findall(
    Id,
    (
      member(Ranges, Indexes),
      ranges_ids(Ranges, RangeIds),
      member(Id, RangeIds)
    ),
    Ids
  ).

Thierry Martinez's avatar
Thierry Martinez committed
847

Thierry Martinez's avatar
Thierry Martinez committed
848 849 850 851 852 853 854 855
ranges_ids(Ranges, Ids) :-
  findall(
    Id,
    (
      member(Range, Ranges),
      (
        Range = Min - Max
      ->
Thierry Martinez's avatar
Thierry Martinez committed
856 857 858 859 860 861 862
        (
          Min =< Max
        ->
          between(Min, Max, Range)
        ;
          throw(error(invalid_range(Min, Max)))
        )
Thierry Martinez's avatar
Thierry Martinez committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
      ;
        Index = Range
      ),
      resolve_index(Index, Id)
    ),
    Ids
  ).


resolve_index(Index, Id) :-
  (
    listed_item(Index, Id)
  ->
    true
  ;
    throw(error(no_such_element, Index))
  ).
Thierry Martinez's avatar
Thierry Martinez committed
880

Thierry Martinez's avatar
Thierry Martinez committed
881 882 883

create_item_id(Id) :-
  count(item_id, Id).
Thierry Martinez's avatar
Thierry Martinez committed
884 885 886 887 888


:- dynamic(dependency/2).


889
:- dynamic(fresh/0).
Thierry Martinez's avatar
Thierry Martinez committed
890 891


892 893 894 895
:- dynamic(at_delete_goal/2).


:- dynamic(selection/3).
Thierry Martinez's avatar
Thierry Martinez committed
896 897


898 899 900 901 902 903 904 905
get_selection_parent(ParentOrCurrentModel, Parent) :-
  (
    ParentOrCurrentModel = current_model
  ->
    single_model(Parent)
  ;
    Parent = ParentOrCurrentModel
  ).
Thierry Martinez's avatar
Thierry Martinez committed
906 907


908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
load_all(Suffix, InputFile) :-
  current_models(OldCurrentModels),
  findall(
    Id,
    (
      filename(InputFile, Filename),
      (
        var(Suffix)
      ->
        file_name_extension(_, Suffix, Filename)
      ;
        true
      ),
      load(Suffix, Filename),
      current_models(Ids),
      member(Id, Ids)
    ),
    NewCurrentModels
  ),
  (
    NewCurrentModels = []
  ->
    restore_current_models(OldCurrentModels)
  ;
    set_current_models(NewCurrentModels)
  ).
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
934 935


936
restore_current_models(OldCurrentModels) :-
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
937
  findall(
938 939 940 941 942 943 944 945 946 947 948 949 950
    Model,
    (
      member(Model, OldCurrentModels),
      once(item([id: Model]))
    ),
    CurrentModels
  ),
  (
    CurrentModels = []
  ->
    new_model
  ;
    set_current_models(CurrentModels)
Thierry Martinez's avatar
Thierry Martinez committed
951
  ).
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
952 953


954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
add_all(Suffix, InputFile) :-
  \+ (
    filename(InputFile, Filename),
    \+ (
      (
        var(Suffix)
      ->
        file_name_extension(_, Suffix, Filename)
      ;
        true
      ),
      add(Suffix, Filename)
    )
  ).


load(Suffix, Filename) :-
  new_model,
  file_base_name(Filename, FileBaseName),
  (
    file_name_extension(Base, _, FileBaseName)
  ->
    true
  ;
    Base = FileBaseName
  ),
  set_model_name(Base),
  add(Suffix, Filename).


add(Suffix, Filename) :-
  (
    add_file_suffix(Suffix, Functor)
  ->
    call(Functor, Filename)
  ;
    throw(error(unknown_suffix(Suffix)))
  ).


add_biocham_file(Filename) :-
  automatic_suffix(Filename, '.bc', read, FilenameBc),
  file_directory_name(FilenameBc, FileDirectory),
  setup_call_cleanup(
    (
      open(FilenameBc, read, Stream),
      working_directory(PreviousDirectory, FileDirectory)
    ),
    load_biocham_stream(Stream),
    (
      close(Stream),
      working_directory(_, PreviousDirectory)
    )
  ).


load_biocham_stream(Stream) :-
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1011
  \+ (
1012 1013
    repeat,
    read_term(Stream, Command, [variable_names(VariableNames)]),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1014
    (
1015
      Command = end_of_file
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1016
    ->
1017
      !
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1018
    ;
1019 1020
      name_variables_and_anonymous(Command, VariableNames),
      command(Command)
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1021
    ),
1022
    fail
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
1023
  ).
Thierry Martinez's avatar
Thierry Martinez committed
1024 1025


1026 1027
not_fresh :-
  retractall(fresh).
Thierry Martinez's avatar
Thierry Martinez committed
1028

Thierry Martinez's avatar
Thierry Martinez committed
1029

1030
:- dynamic(item/4).
Thierry Martinez's avatar
ploum  
Thierry Martinez committed
1031 1032


1033 1034
:- dynamic(key/2).

1035

1036
:- dynamic(annotation/3).
Thierry Martinez's avatar
Thierry Martinez committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073


update_parent_model_identifier_kinds(Parent) :-
  (
    Parent = top
  ->
    true
  ;
    get_model(Parent, ModelId),
    update_identifier_kinds(ModelId)
  ).


find_model_refs(RefSet, Models) :-
  findall(
    Id,
    (
      member(Ref, RefSet),
      (
        list(Ref)
      ->
        ranges_ids(Ref, Ids),
        member(Id, Ids)
      ;
        find_item([parent: top, key: Ref, id: Id])
      ),
      item([id: Id, kind: Kind]),
      (
        Kind = model
      ->
        true
      ;
        throw(error(not_a_model(Kind)))
      )
    ),
    Models
  ).