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

Thierry Martinez's avatar
Thierry Martinez committed
51

Thierry Martinez's avatar
Thierry Martinez committed
52
:- devdoc('\\section{Commands}').
Thierry Martinez's avatar
Thierry Martinez committed
53 54


Thierry Martinez's avatar
Thierry Martinez committed
55 56 57 58 59 60 61 62 63
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
64
  load_all(_Suffix, InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
65 66 67 68 69 70 71 72 73 74 75


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
76
  add_all(_Suffix, InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
77 78 79 80 81 82 83 84 85 86 87


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
88
  load_all('bc', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
89 90 91 92 93 94 95 96 97 98


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
99
  add_all('bc', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
100 101


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
new_model :-
  biocham_command,
  doc('opens a new fresh model.'),
  new_model(_Id).


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


list_models :-
  biocham_command,
138
  doc('lists all open models.'),
Thierry Martinez's avatar
Thierry Martinez committed
139 140 141 142 143
  list_items([parent: top, kind: model]).


list_current_models :-
  biocham_command,
144
  doc('lists current models.'),
Thierry Martinez's avatar
Thierry Martinez committed
145 146 147 148
  current_models(CurrentModels),
  list_ids(CurrentModels).


149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
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
  ),
  list_model_parameters,
  list_model_functions.

Thierry Martinez's avatar
Thierry Martinez committed
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
:- grammar(range).


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

range(Integer) :-
  integer(Integer).


:- grammar(ref).


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

ref(Name) :-
  name(Name).


select_model(RefSet) :-
  biocham_command,
  type(RefSet, {ref}),
  doc('selects some models.'),
  findall(
    Id,
    (
      member(Ref, RefSet),
      (
        list(Ref)
      ->
        ranges_ids(Ref, Ids),
        member(Id, Ids)
      ;
        find_item([parent: top, key: Ref, id: Id])
Thierry Martinez's avatar
Thierry Martinez committed
200 201 202 203 204 205 206 207
      ),
      item([id: Id, kind: Kind]),
      (
        Kind = model
      ->
        true
      ;
        throw(error(not_a_model(Kind)))
Thierry Martinez's avatar
Thierry Martinez committed
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 237 238 239 240 241 242 243 244 245 246 247
      )
    ),
    CurrentModels
  ),
  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)
    )
  ).


:- dynamic(fresh/0).


get_model_name(Name) :-
  single_model(Id),
  get_model_name(Id, Name).


get_model_name(Id, Name) :-
  find_item([parent: top, id: Id, item: Name]).


Thierry Martinez's avatar
Thierry Martinez committed
248
load_all(Suffix, InputFile) :-
Thierry Martinez's avatar
Thierry Martinez committed
249
  current_models(OldCurrentModels),
Thierry Martinez's avatar
Thierry Martinez committed
250 251 252 253
  findall(
    Id,
    (
      filename(InputFile, Filename),
Thierry Martinez's avatar
Thierry Martinez committed
254 255 256 257 258 259 260
      (
        var(Suffix)
      ->
        file_name_extension(_, Suffix, Filename)
      ;
        true
      ),
Thierry Martinez's avatar
Thierry Martinez committed
261 262 263 264
      load(Suffix, Filename),
      current_models(Ids),
      member(Id, Ids)
    ),
Thierry Martinez's avatar
Thierry Martinez committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    NewCurrentModels
  ),
  (
    NewCurrentModels = []
  ->
    restore_current_models(OldCurrentModels)
  ;
    set_current_models(NewCurrentModels)
  ).


restore_current_models(OldCurrentModels) :-
  findall(
    Model,
    (
      member(Model, OldCurrentModels),
      once(item([id: Model]))
    ),
Thierry Martinez's avatar
Thierry Martinez committed
283 284
    CurrentModels
  ),
Thierry Martinez's avatar
Thierry Martinez committed
285 286 287 288 289 290 291
  (
    CurrentModels = []
  ->
    new_model
  ;
    set_current_models(CurrentModels)
  ).
Thierry Martinez's avatar
Thierry Martinez committed
292 293 294


add_all(Suffix, InputFile) :-
Thierry Martinez's avatar
Thierry Martinez committed
295 296 297
  \+ (
    filename(InputFile, Filename),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
298 299 300
      (
        var(Suffix)
      ->
Thierry Martinez's avatar
Thierry Martinez committed
301
        file_name_extension(_, Suffix, Filename)
Thierry Martinez's avatar
Thierry Martinez committed
302 303 304 305
      ;
        true
      ),
      add(Suffix, Filename)
Thierry Martinez's avatar
Thierry Martinez committed
306 307 308 309
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
310
load(Suffix, Filename) :-
Thierry Martinez's avatar
Thierry Martinez committed
311
  new_model,
Thierry Martinez's avatar
Thierry Martinez committed
312 313 314 315 316 317 318 319 320
  file_base_name(Filename, FileBaseName),
  (
    file_name_extension(Base, _, FileBaseName)
  ->
    true
  ;
    Base = FileBaseName
  ),
  set_model_name(Base),
Thierry Martinez's avatar
Thierry Martinez committed
321
  add(Suffix, Filename).
Thierry Martinez's avatar
Thierry Martinez committed
322 323


Thierry Martinez's avatar
Thierry Martinez committed
324
add(Suffix, Filename) :-
Thierry Martinez's avatar
Thierry Martinez committed
325
  (
Thierry Martinez's avatar
Thierry Martinez committed
326 327 328
    add_file_suffix(Suffix, Functor)
  ->
    call(Functor, Filename)
Thierry Martinez's avatar
Thierry Martinez committed
329
  ;
Thierry Martinez's avatar
Thierry Martinez committed
330 331 332 333 334
    throw(error(unknown_suffix(Suffix)))
  ).


:- multifile(add_file_suffix/2).
Thierry Martinez's avatar
Thierry Martinez committed
335

Thierry Martinez's avatar
Thierry Martinez committed
336 337 338 339

add_file_suffix('', add_biocham_file).


Thierry Martinez's avatar
Thierry Martinez committed
340
add_file_suffix('bc', add_biocham_file).
Thierry Martinez's avatar
Thierry Martinez committed
341 342


Thierry Martinez's avatar
Thierry Martinez committed
343
add_biocham_file(Filename) :-
Thierry Martinez's avatar
NuSMV  
Thierry Martinez committed
344
  automatic_suffix(Filename, '.bc', read, FilenameBc),
Thierry Martinez's avatar
Thierry Martinez committed
345 346 347 348 349 350 351 352 353 354
  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)
Thierry Martinez's avatar
Thierry Martinez committed
355
    )
Thierry Martinez's avatar
Thierry Martinez committed
356 357 358 359 360 361 362 363 364 365
  ).


load_biocham_stream(Stream) :-
  \+ (
    repeat,
    read_term(Stream, Command, [variable_names(VariableNames)]),
    (
      Command = end_of_file
    ->
Thierry Martinez's avatar
Thierry Martinez committed
366
      !
Thierry Martinez's avatar
Thierry Martinez committed
367
    ;
Thierry Martinez's avatar
Thierry Martinez committed
368
      name_variables_and_anonymous(Command, VariableNames),
369
      command(Command)
Thierry Martinez's avatar
Thierry Martinez committed
370 371
    ),
    fail
Thierry Martinez's avatar
Thierry Martinez committed
372 373 374
  ).


Thierry Martinez's avatar
Thierry Martinez committed
375
current_models(Models) :-
Thierry Martinez's avatar
Thierry Martinez committed
376
  get_selection(top, current_models, Models).
Thierry Martinez's avatar
Thierry Martinez committed
377 378


Thierry Martinez's avatar
Thierry Martinez committed
379
set_current_models(Models) :-
Thierry Martinez's avatar
Thierry Martinez committed
380 381 382 383 384
  (
    Models = []
  ->
    throw(error(cannot_select_no_models))
  ;
Thierry Martinez's avatar
Thierry Martinez committed
385
    set_selection(top, current_models, Models)
Thierry Martinez's avatar
Thierry Martinez committed
386
  ).
Thierry Martinez's avatar
Thierry Martinez committed
387 388


Thierry Martinez's avatar
Thierry Martinez committed
389
new_model(Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
390
  (
Thierry Martinez's avatar
Thierry Martinez committed
391
    fresh,
Thierry Martinez's avatar
Thierry Martinez committed
392
    current_models([Id])
Thierry Martinez's avatar
Thierry Martinez committed
393
  ->
Thierry Martinez's avatar
Thierry Martinez committed
394
    true
Thierry Martinez's avatar
Thierry Martinez committed
395
  ;
Thierry Martinez's avatar
Thierry Martinez committed
396
    add_item([parent: top, kind: model, key: new_model, id: Id]),
Thierry Martinez's avatar
Thierry Martinez committed
397
    (
Thierry Martinez's avatar
Thierry Martinez committed
398
      item([parent: top, kind: model, key: initial, id: InitialId])
Thierry Martinez's avatar
Thierry Martinez committed
399 400 401 402 403
    ->
      inherits(Id, InitialId)
    ;
      true
    ),
Thierry Martinez's avatar
Thierry Martinez committed
404
    set_current_models([Id]),
Thierry Martinez's avatar
Thierry Martinez committed
405 406 407 408 409 410 411 412
    assertz(fresh)
  ).


not_fresh :-
  retractall(fresh).


Thierry Martinez's avatar
Thierry Martinez committed
413 414 415 416 417 418
:- dynamic(item/4).


:- dynamic(key/2).


Thierry Martinez's avatar
Thierry Martinez committed
419 420 421 422 423 424 425
:- dynamic(annotation/3).


set_annotation(Id, Kind, Annotation) :-
  retractall(annotation(Id, Kind, _)),
  assertz(annotation(Id, Kind, Annotation)).

Thierry Martinez's avatar
Thierry Martinez committed
426

Thierry Martinez's avatar
Thierry Martinez committed
427 428 429 430
get_annotation(Id, Kind, Annotation) :-
  annotation(Id, Kind, Annotation).


Thierry Martinez's avatar
Thierry Martinez committed
431 432
delete_annotation(Id, Kind) :-
  retract(annotation(Id, Kind, _)).
Thierry Martinez's avatar
Thierry Martinez committed
433

Thierry Martinez's avatar
Aliases  
Thierry Martinez committed
434

Thierry Martinez's avatar
Thierry Martinez committed
435 436 437 438 439 440 441 442
default(Variable, Value) :-
  (
    var(Variable)
  ->
    Variable = Value
  ;
    true
  ).
Thierry Martinez's avatar
Aliases  
Thierry Martinez committed
443

Thierry Martinez's avatar
Thierry Martinez committed
444

Thierry Martinez's avatar
Thierry Martinez committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
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
473
  not_fresh,
Thierry Martinez's avatar
Thierry Martinez committed
474
  create_item_id(Id),
Thierry Martinez's avatar
Thierry Martinez committed
475 476
  put_item(Parent0, Kind, Key, Item, Id).

Thierry Martinez's avatar
Thierry Martinez committed
477

Thierry Martinez's avatar
Thierry Martinez committed
478
put_item(Parent, Kind, Key, Item, Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
479 480 481 482 483 484 485 486 487 488 489 490
  (
    list(Key)
  ->
    \+ (
      member(K, Key),
      \+ (
        assertz(key(K, Id))
      )
    )
  ;
    assertz(key(Key, Id))
  ),
Thierry Martinez's avatar
Thierry Martinez committed
491
  assertz(item(Id, Parent, Kind, Item)).
Thierry Martinez's avatar
Thierry Martinez committed
492 493 494 495


single_model(Model) :-
  (
Thierry Martinez's avatar
Thierry Martinez committed
496
    current_models([Model])
Thierry Martinez's avatar
Thierry Martinez committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510
  ->
    true
  ;
    throw(error(need_single_model))
  ).

single_model(Model, ModelId) :-
  (
    Model = current_model
  ->
    single_model(ModelId)
  ;
    ModelId = Model
  ).
Thierry Martinez's avatar
Thierry Martinez committed
511 512


Thierry Martinez's avatar
Thierry Martinez committed
513
item_parent_option(Options, ChosenParent) :-
Thierry Martinez's avatar
Thierry Martinez committed
514
  optional(parent: Parent, Options),
Thierry Martinez's avatar
Thierry Martinez committed
515
  optional(id: Id, Options),
Thierry Martinez's avatar
Thierry Martinez committed
516
  (
Thierry Martinez's avatar
Thierry Martinez committed
517
    var(Id)
Thierry Martinez's avatar
Thierry Martinez committed
518
  ->
Thierry Martinez's avatar
Thierry Martinez committed
519 520 521 522 523 524 525 526 527 528 529 530 531
    default(Parent, current_model),
    (
      Parent == current_model
    ->
      single_model(ChosenParent)
    ;
      Parent == current_models
    ->
      current_models(Models),
      member(ChosenParent, Models)
    ;
      ChosenParent = Parent
    )
Thierry Martinez's avatar
Thierry Martinez committed
532
  ;
Thierry Martinez's avatar
Thierry Martinez committed
533 534 535 536 537 538 539 540 541 542
    ChosenParent = Parent
  ).


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
543
  (
Thierry Martinez's avatar
Thierry Martinez committed
544
    var(Id)
Thierry Martinez's avatar
Thierry Martinez committed
545
  ->
Thierry Martinez's avatar
Thierry Martinez committed
546 547 548 549 550 551
    (
      Parent0 = Parent
    ;
      \+ member(no_inheritance, Options),
      inherits_from(Parent, Parent0)
    )
Thierry Martinez's avatar
Thierry Martinez committed
552
  ;
Thierry Martinez's avatar
Thierry Martinez committed
553
    Parent0 = Parent
Thierry Martinez's avatar
Thierry Martinez committed
554
  ),
Thierry Martinez's avatar
Thierry Martinez committed
555 556 557 558 559 560 561
  (
    list(Kind)
  ->
    member(Kind0, Kind)
  ;
    Kind0 = Kind
  ),
Thierry Martinez's avatar
Thierry Martinez committed
562 563 564 565
  (
    var(Key)
  ->
    true
Thierry Martinez's avatar
Thierry Martinez committed
566 567 568 569 570
  ;
    list(Key)
  ->
    indexes_ids(Key, Ids),
    member(Id, Ids)
Thierry Martinez's avatar
Thierry Martinez committed
571 572 573
  ;
    key(Key, Id)
  ),
Thierry Martinez's avatar
Thierry Martinez committed
574
  item(Id, Parent0, Kind0, Item).
Thierry Martinez's avatar
Thierry Martinez committed
575

Thierry Martinez's avatar
Thierry Martinez committed
576

Thierry Martinez's avatar
Thierry Martinez committed
577 578 579 580 581 582 583 584
find_item(Options) :-
  (
    item(Options)
  ->
    true
  ;
    throw(error(unknown_item))
  ).
Thierry Martinez's avatar
Thierry Martinez committed
585

Thierry Martinez's avatar
Thierry Martinez committed
586

Thierry Martinez's avatar
Aliases  
Thierry Martinez committed
587 588 589 590 591 592 593
all_items(Options, Items) :-
  findall(
    Item,
    item([item: Item | Options]),
    Items
  ).

Thierry Martinez's avatar
Thierry Martinez committed
594 595 596 597 598 599 600 601 602

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


Thierry Martinez's avatar
Thierry Martinez committed
603 604
optional(Item, List) :-
  (
Thierry Martinez's avatar
Thierry Martinez committed
605
    member(Item, List)
Thierry Martinez's avatar
Thierry Martinez committed
606 607 608 609 610 611 612
  ->
    true
  ;
    true
  ).


Thierry Martinez's avatar
Thierry Martinez committed
613 614 615
:- dynamic(listed_item/2).


Thierry Martinez's avatar
Thierry Martinez committed
616 617 618 619
begin_command :-
  set_counter(list_item_counter, 0).


Thierry Martinez's avatar
Thierry Martinez committed
620
list_items(Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
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
  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
649 650 651


list_ids(Ids) :-
Thierry Martinez's avatar
Thierry Martinez committed
652 653 654 655
  list_ids([], Ids).


list_ids(Options, Ids) :-
Thierry Martinez's avatar
Thierry Martinez committed
656 657 658 659 660 661 662
  (
    peek_count(list_item_counter, 0)
  ->
    retractall(listed_item(_, _))
  ;
    true
  ),
Thierry Martinez's avatar
Thierry Martinez committed
663 664 665 666
  list_ids_aux(Options, Ids).


list_ids_aux(Options, Ids) :-
Thierry Martinez's avatar
Thierry Martinez committed
667 668 669 670 671 672 673 674 675
  optional(indent: Indent, Options),
  default(Indent, 0),
  (
    member(recursive, Options)
  ->
    Recursive = yes
  ;
    Recursive = no
  ),
Thierry Martinez's avatar
Thierry Martinez committed
676
  \+ (
Thierry Martinez's avatar
Thierry Martinez committed
677
    member(Id, Ids),
Thierry Martinez's avatar
Thierry Martinez committed
678
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
679
      count(list_item_counter, Counter),
Thierry Martinez's avatar
Thierry Martinez committed
680
      assertz(listed_item(Counter, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
681
      item(Id, _, _, Item),
Thierry Martinez's avatar
Thierry Martinez committed
682
      indent(Indent),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
683
      (
Thierry Martinez's avatar
Thierry Martinez committed
684
        selection(_, _, Id)
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
685 686 687 688 689 690
      ->
        Selected = '*'
      ;
        Selected = ' '
      ),
      format('[~d]~a~w\n', [Counter, Selected, Item]),
Thierry Martinez's avatar
Thierry Martinez committed
691 692 693 694
      (
        Recursive = yes
      ->
        SubIndent is Indent + 1,
Thierry Martinez's avatar
Thierry Martinez committed
695 696
        all_ids([parent: Id | Options], SubIds),
        list_ids_aux([indent: SubIndent, recursive | Options], SubIds)
Thierry Martinez's avatar
Thierry Martinez committed
697 698 699
      ;
        true
      )
Thierry Martinez's avatar
Thierry Martinez committed
700 701 702 703
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
704 705 706 707 708 709 710
delete_item(Options) :-
  list(Options),
  !,
  find_item([id: Id | Options]),
  delete_item(Id).


Thierry Martinez's avatar
Thierry Martinez committed
711
delete_item(Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
712 713
  \+ (
    at_delete_goal(Id, Goal),
714
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
715 716 717
      Goal
    )
  ),
Thierry Martinez's avatar
Thierry Martinez committed
718 719 720 721
  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
722
  retractall(dependency(Id, _Master)),
Thierry Martinez's avatar
Tables  
Thierry Martinez committed
723
  retractall(at_delete_goal(Id, _Goal)),
Thierry Martinez's avatar
Thierry Martinez committed
724 725 726
  \+ (
    item(SubId, Id, _SubKind, _SubItem),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
727 728 729 730 731 732 733
      (
        delete_item(SubId)
      ->
        true
      ;
        true
      )
Thierry Martinez's avatar
Thierry Martinez committed
734 735 736 737 738
    )
  ),
  \+ (
    dependency(SubId, Id),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
739 740 741 742 743 744 745
      (
        delete_item(SubId)
      ->
        true
      ;
        true
      )
Thierry Martinez's avatar
Thierry Martinez committed
746 747
    )
  ),
Thierry Martinez's avatar
Thierry Martinez committed
748
  retractall(selection(_, _, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
749 750 751 752
  (
    KindItem = model
  ->
    (
Thierry Martinez's avatar
Thierry Martinez committed
753
      selection(top, current_models, _)
Thierry Martinez's avatar
Thierry Martinez committed
754 755
    ->
      true
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
756 757
    ;
      new_model
Thierry Martinez's avatar
Thierry Martinez committed
758 759 760 761
    )
  ;
    true
  ).
Thierry Martinez's avatar
Thierry Martinez committed
762 763


Thierry Martinez's avatar
Thierry Martinez committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
delete_items(Options) :-
  \+ (
    item([id: Id | Options]),
    \+ (
      delete_item(Id)
    )
  ).


indexes_ids(Indexes, Ids) :-
  findall(
    Id,
    (
      member(Ranges, Indexes),
      ranges_ids(Ranges, RangeIds),
      member(Id, RangeIds)
    ),
    Ids
  ).

Thierry Martinez's avatar
Thierry Martinez committed
784

Thierry Martinez's avatar
Thierry Martinez committed
785 786 787 788 789 790 791 792
ranges_ids(Ranges, Ids) :-
  findall(
    Id,
    (
      member(Range, Ranges),
      (
        Range = Min - Max
      ->
Thierry Martinez's avatar
Thierry Martinez committed
793 794 795 796 797 798 799
        (
          Min =< Max
        ->
          between(Min, Max, Range)
        ;
          throw(error(invalid_range(Min, Max)))
        )
Thierry Martinez's avatar
Thierry Martinez committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
      ;
        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
817

Thierry Martinez's avatar
Thierry Martinez committed
818

Thierry Martinez's avatar
Thierry Martinez committed
819
replace_item(Id, Kind, Key, Item) :-
Thierry Martinez's avatar
Thierry Martinez committed
820
  retract(item(Id, Parent, _Kind, _Item)),
Thierry Martinez's avatar
Thierry Martinez committed
821
  retractall(key(_Key, Id)),
Thierry Martinez's avatar
Thierry Martinez committed
822
  put_item(Parent, Kind, Key, Item, Id).
Thierry Martinez's avatar
Thierry Martinez committed
823

Thierry Martinez's avatar
Thierry Martinez committed
824

Thierry Martinez's avatar
Thierry Martinez committed
825 826 827 828 829 830 831 832 833 834 835
change_item(Options, Kind, Key, Item) :-
  catch(
    (
      find_item([kind: Kind, key: Key, id: Id | Options]),
      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
836 837
create_item_id(Id) :-
  count(item_id, Id).
Thierry Martinez's avatar
Thierry Martinez committed
838 839 840 841 842 843 844


:- dynamic(dependency/2).


add_dependency(IdSub, IdMaster) :-
  assertz(dependency(IdSub, IdMaster)).
Thierry Martinez's avatar
Thierry Martinez committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875


inherits(Ancestors) :-
  biocham_command(*),
  type(Ancestors, '*'(name)),
  doc('makes the current model inherit from the given ancestor models.'),
  single_model(Id),
  \+ (
    member(Ancestor, Ancestors),
    \+ (
      find_item([kind: model, key: Ancestor, id: AncestorId]),
      (
        inherits(Id, AncestorId)
      )
    )
  ).


:- dynamic(directly_inherits_from/2).


inherits(Id, AncestorId) :-
  assertz(directly_inherits_from(Id, AncestorId)).


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

inherits_from(Id, AncestorId) :-
  directly_inherits_from(Id, IntermediateAncestorId),
  inherits_from(IntermediateAncestorId, AncestorId).
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
876 877


Thierry Martinez's avatar
Thierry Martinez committed
878
:- dynamic(selection/3).
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
879 880


Thierry Martinez's avatar
Thierry Martinez committed
881 882
get_selection(ParentOrCurrentModel, SelectionName, Ids) :-
  get_selection_parent(ParentOrCurrentModel, Parent),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
883 884
  findall(
    Id,
Thierry Martinez's avatar
Thierry Martinez committed
885 886 887
    selection(Parent, SelectionName, Id),
    Ids
  ).
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
888 889


Thierry Martinez's avatar
Thierry Martinez committed
890 891 892
set_selection(ParentOrCurrentModel, SelectionName, IdsOrOptions) :-
  get_selection_parent(ParentOrCurrentModel, Parent),
  retractall(selection(Parent, SelectionName, _)),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
893 894 895 896 897 898 899 900 901
  \+ (
    (
      IdsOrOptions = [_: _| _]
    ->
      item([id: Id | IdsOrOptions])
    ;
      member(Id, IdsOrOptions)
    ),
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
902
      assertz(selection(Parent, SelectionName, Id))
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
903 904
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
905 906


Thierry Martinez's avatar
Thierry Martinez committed
907 908 909 910 911 912 913 914 915 916
get_selection_parent(ParentOrCurrentModel, Parent) :-
  (
    ParentOrCurrentModel = current_model
  ->
    single_model(Parent)
  ;
    Parent = ParentOrCurrentModel
  ).


Thierry Martinez's avatar
Thierry Martinez committed
917 918
:- dynamic(at_delete_goal/2).

Thierry Martinez's avatar
Thierry Martinez committed
919

Thierry Martinez's avatar
Thierry Martinez committed
920 921
at_delete(Id, Goal) :-
  assertz(at_delete_goal(Id, Goal)).
Thierry Martinez's avatar
ploum  
Thierry Martinez committed
922 923 924 925


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