wgpac.pl 4.9 KB
Newer Older
1
:- module(
2 3 4 5 6 7 8 9 10 11 12 13 14
  wgpac,
  [
    % Grammars
    wgpac/1,
    wgpac_box/1,
    wgpac_named/1,
    wgpac_name/2,
    op(600, xfy, ::),
    op(550, fy, integral),
    % Commands
    compile_wgpac/2

  ]).
15 16 17 18

:- grammar(wgpac).

wgpac(B) :-
19
  wgpac_box(B).
20 21

wgpac(Y) :-
22
  wgpac_named(Y).
23 24 25 26

:- grammar(wgpac_box).

wgpac_box(K) :-
27
  concentration(K).
28 29

wgpac_box(W1 + W2) :-
30 31
  wgpac(W1),
  wgpac(W2).
32 33

wgpac_box(W1 * W2) :-
34 35
  wgpac(W1),
  wgpac(W2).
36 37

wgpac_box(integral W) :-
38 39 40 41 42 43 44 45 46 47 48
  wgpac(W).

:- grammar(wgpac_named).

wgpac_named(Y :: B) :-
  object(Y),
  wgpac_box(B).

wgpac_named(Y) :-
  object(Y).

49 50

wgpac_name(Y :: _, Y) :-
51
  object(Y).
52 53

wgpac_name(Y, Y) :-
54
  object(Y).
55 56

wgpac_free(Y, _) :-
57 58
  object(Y),
  !.
59 60

wgpac_free(Y :: B, Z) :-
61 62 63 64 65 66
  !,
  Y \== Z,
  wgpac_box_free(B, Z).

wgpac_free(B, Z) :-
  wgpac_box_free(B, Z).
67 68

wgpac_box_free(K, _) :-
69 70
  concentration(K),
  !.
71 72

wgpac_box_free(W1 + W2, Z) :-
73 74
  wgpac_free(W1, Z),
  wgpac_free(W2, Z).
75 76

wgpac_box_free(W1 * W2, Z) :-
77 78
  wgpac_free(W1, Z),
  wgpac_free(W2, Z).
79 80

wgpac_box_free(integral W, Z) :-
81
  wgpac_free(W, Z).
82 83

wgpac_well_formed(Y) :-
84
  object(Y).
85 86

wgpac_well_formed(Y :: B) :-
87 88
  object(Y),
  wgpac_box_free(B, Y).
89 90 91


% global counter, useful for anonymous species
92
:- dynamic(fresh_index/1).
93 94

get_fresh(N) :-
95 96 97
  retract(fresh_index(N)),
  M is N + 1,
  assert(fresh_index(M)).
98 99 100


compile_wgpac(Wgpac, Rate) :-
101 102 103
  biocham_command,
  doc('compiles a weak-gpac into a biochemical system.'),
  type(Wgpac, wgpac),
Sylvain Soliman's avatar
Sylvain Soliman committed
104
  !,
105 106 107 108 109
  retractall(fresh_index(_)),
  assert(fresh_index(0)),
  type(Rate, number),
  compile_wgpac_impl(Wgpac, Rate).

110 111 112 113 114 115 116 117 118 119 120
compile_wgpac(WgpacSet, Rate) :-
  biocham_command,
  type(WgpacSet, {wgpac}),
  doc('compiles a set of w-gpac.'),
  \+ (
    member(Wgpac, WgpacSet),
    \+ (
      compile_wgpac(Wgpac, Rate)
    )
  ).

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

compile_wgpac_impl(Y, _) :-
  object(Y),
  !,
  print_message(informational, start(object, Y)).

compile_wgpac_impl(Wgpac, Rate) :-
  Wgpac = Y :: B,
  wgpac_well_formed(Wgpac),
  !,
  print_message(informational, start(named, Wgpac)),
  (
    B = K,
    concentration(K)
  ->
    present([Y], K)
  ;
    B = W1 + W2
  ->
    print_message(informational, found('Sum box')),
    (
      wgpac_named(W1)
    ->
      print_message(informational, found(1, named, W1)),
      wgpac_name(W1, X1),
      compile_wgpac_impl(W1, Rate)
    ;
      print_message(informational, found(1, anonymous, W1)),
      get_fresh(N1),
      atom_concat('x_auto_', N1, X1),
      compile_wgpac_impl(X1 :: W1, Rate)
    ),
153
    (
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      wgpac_named(W2)
    ->
      print_message(informational, found(2, named, W2)),
      wgpac_name(W2, X2),
      compile_wgpac_impl(W2, Rate)
    ;
      print_message(informational, found(2, anonymous, W2)),
      get_fresh(N2),
      atom_concat('x_auto_', N2, X2),
      compile_wgpac_impl(X2 :: W2, Rate)
    ),

    add_reaction(Rate * [X1] for _ =[X1]=> Y),
    add_reaction(Rate * [X2] for _ =[X2]=> Y),
    add_reaction(Rate * [Y] for Y => _)
  ;
    B = W1 * W2
  ->
    print_message(informational, found('Product box')),
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 213 214 215 216 217 218 219 220 221
      wgpac_named(W1)
    ->
      print_message(informational, found(1, named, W1)),
      wgpac_name(W1, X1),
      compile_wgpac_impl(W1, Rate)
    ;
      print_message(informational, found(1, anonymous, W1)),
      get_fresh(N1),
      atom_concat('x_auto_', N1, X1),
      compile_wgpac_impl(X1 :: W1, Rate)
    ),
    (
      wgpac_named(W2)
    ->
      print_message(informational, found(2, named, W2)),
      wgpac_name(W2, X2),
      compile_wgpac_impl(W2, Rate)
    ;
      print_message(informational, found(2, anonymous, W2)),
      get_fresh(N2),
      atom_concat('x_auto_', N2, X2),
      compile_wgpac_impl(X2 :: W2, Rate)
    ),

    add_reaction(Rate * [X1] * [X2] for _ =[X1 + X2]=> Y),
    add_reaction(Rate * [Y] for Y => _)
  ;
    B = integral W1
  ->
    (
      wgpac_named(W1)
    ->
      wgpac_name(W1, X1),
      compile_wgpac_impl(W1, Rate)
    ;
      get_fresh(N),
      atom_concat('x_auto_', N, X1),
      compile_wgpac_impl(X1 :: W1, Rate)
    ),
    add_reaction(_ =[X1]=> Y)
  ).

compile_wgpac_impl(Wgpac, Rate) :-
  print_message(informational, start(anonymous, Wgpac)),
  get_fresh(N),
  atom_concat('x_auto_', N, Y),
  wgpac_well_formed(Y :: Wgpac),
  compile_wgpac_impl(Y :: Wgpac, Rate).
222 223

:- doc('\\begin{example}\n
224 225
  Cosine is generable:\n
  ').
226 227 228 229 230 231
:- biocham_silent(clear_model).
:- biocham(compile_wgpac(a :: integral integral (-1 * a), 100)).
:- biocham(present(a)).
:- biocham(numerical_simulation(10, method:msbdf)).
:- biocham(plot).
:- doc('
232 233
  \\end{example}
  ').
234 235

debug_iswgpac(W) :-
236 237 238
  biocham_command,
  (wgpac(W) -> write('WGPAC') ; write('not WGPAC')),
  nl.
239 240

debug_iswgpacbox(B) :-
241 242 243 244 245 246 247 248 249 250 251 252 253
  biocham_command,
  (wgpacbox(B) -> write('WGPAC BOX') ; write('not WGPAC BOX')),
  nl.


prolog:message(start(Type, Object)) -->
  ['Start ~w compilation of: ~w'-[Type, Object]].

prolog:message(found(Position, Type, Object)) -->
  ['Found ~w at position ~w: ~w'-[Type, Position, Object]].

prolog:message(found(Object)) -->
  ['Found ~w'-[Object]].