Commit b26c067a authored by Thierry Martinez's avatar Thierry Martinez

Multi-models

parent 28387295
......@@ -3,7 +3,8 @@
[
start/0,
initialize/0,
biocham_command/0
biocham_command/0,
biocham_command/1
]).
......@@ -20,3 +21,6 @@ initialize :-
biocham_command.
biocham_command(_).
......@@ -424,6 +424,8 @@ generate_body_item_clause(Clause, Stream, Type) :-
(
(
BiochamCommand = yes
;
BiochamCommand = variantargs
;
Type = devdoc,
Doc \= []
......@@ -440,10 +442,15 @@ generate_body_item_clause(Clause, Stream, Type) :-
assertz(argument_type(Argument, ArgumentType))
)
),
make_id(Command, CommandId),
length(Arguments, Arity),
format(atom(Key), '~a/~d', [Command, Arity]),
format(atom(Id), '~a-~d', [CommandId, Arity]),
(
BiochamCommand = variantargs
->
format(atom(Key), '~a/*', [Command])
;
length(Arguments, Arity),
format(atom(Key), '~a/~w', [Command, Arity])
),
make_id(Key, Id),
index_use_id(Key, Id),
format(Stream, '<h5 id="~a""><code>~a', [Id, Command]),
write_arguments(Arguments, Stream),
......@@ -498,6 +505,19 @@ write_argument(Argument, Doc) :-
argument_type(Argument, Type)
->
(
Type = '*'(Name = Value)
->
make_id(Name, NameId),
make_id(Value, ValueId),
format(
Doc,
'<a href="#~a">~a</a><sub>1</sub> = <a href="#~a">~a</a><sub>1</sub>,
...,
<a href="#~a">~a</a><sub><var>n</var></sub> =
<a href="#~a">~a</a><sub><var>n</var></sub>,',
[NameId, Name, ValueId, Value, NameId, Name, ValueId, Value]
)
;
Type = '='(ItemType)
->
make_id(ItemType, Id),
......
:- module(
filename,
[
input_file/1,
output_file/1,
filename/2,
chop_suffix/2,
automatic_suffix/3
......@@ -8,11 +10,27 @@
).
:- grammar(input_file).
input_file(Name) :-
atom(Name).
:- grammar(output_file).
output_file(Name) :-
atom(Name).
filename(Filename, ExpandedFilename) :-
expand_file_name(Filename, List),
member(ExpandedFilename, List).
path_delimiter('/').
chop_suffix(Filename, Suffix) :-
atom_chars(Filename, Chars),
(
......
:- module(
macros,
[
set_macro/2,
set_macro/1,
show_macro/1,
list_macros/0,
delete_macro/1
]
).
set_macro(Macro, Value) :-
biocham_command,
type(Macro, macro),
doc('sets the value of \\argument{macro} to \\argument{Value}.'),
catch(
delete_macro(Macro),
error(undefined_macro(_)),
true
),
add_item(macro, Macro, macro(Macro, Value)).
set_macro(MacroList) :-
biocham_command(*),
type(MacroList, '*'(macro = term)),
doc('sets the value of macros.'),
\+ (
member(Macro = Value, MacroList),
\+ (
catch(
(
find_item([kind: macro, key: Macro, id: Id]),
replace_item(Id, macro, Macro, macro(Macro = Value))
),
error(unknown_item),
add_item(macro, Macro, macro(Macro = Value))
)
)
).
set_macro(Macro) :-
show_macro(Macro) :-
biocham_command,
type(Macro, macro),
doc('shows the expression associated to the given macro.'),
find_macro(Macro, _Id, Expression),
find_item(
[model: current_model, kind: macro, key: Macro, item: macro(_ = Expression)]
),
write(Expression),
nl.
find_macro(Macro, Id, Expression) :-
(
item([model: current_model, kind: macro, id: Id], macro(Macro, Expression))
->
true
;
throw(error(undefined_macro(Macro)))
).
list_macros :-
biocham_command,
doc('lists all known macros.'),
list_items([model: current_model, kind: macro]).
delete_macro(Macro) :-
delete_macro(MacroSet) :-
biocham_command,
type(Macro, macro),
doc('deletes a macro.'),
find_macro(Macro, Id, _Expression),
delete_item(Id).
type(MacroSet, {macro}),
doc('deletes some macros.'),
\+ (
member(Macro, MacroSet),
\+ (
find_item([model: current_model, kind: macro, key: Macro, id: Id]),
delete_item(Id)
)
).
:- use_module(library(plunit)).
:- begin_tests(macros).
test(
'set_macro',
[true(Macros == [macro(a = 1), macro(b = 3)])]
) :-
new_model,
command(set_macro(a = 1, b = 2)),
command(set_macro(b = 3)),
all_items([model: current_model, kind: macro], Macros).
:- end_tests(macros).
......@@ -4,9 +4,15 @@
load/1,
load_biocham/1,
new_model/0,
set_model_name/1,
list_models/0,
select_model/1,
add_item/2,
add_item/3,
add_item/4,
replace_item/4,
item/1,
find_item/1,
all_items/2,
list_items/1,
delete_item/1
......@@ -74,9 +80,17 @@ load_biocham(InputFile) :-
contained in the given Biocham \\texttt{.bc} file.
The suffix \\texttt{.bc} is automatically added to the name if such a
file exists.'),
new_model,
set_model_name(InputFile),
add_biocham(InputFile).
findall(
Id,
(
filename(InputFile, Filename),
new_model(Id),
set_model_name(Filename),
add_biocham_file(Filename)
),
CurrentModels
),
set_current_models(CurrentModels).
add_biocham(InputFile) :-
......@@ -90,19 +104,23 @@ add_biocham(InputFile) :-
\+ (
filename(InputFile, Filename),
\+ (
automatic_suffix(Filename, '.bc', 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)
)
)
add_biocham_file(Filename)
)
).
add_biocham_file(Filename) :-
automatic_suffix(Filename, '.bc', 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)
)
).
......@@ -130,17 +148,42 @@ set_current_models(Models) :-
new_model :-
biocham_command,
doc('opens a new fresh model.'),
new_model(_Id).
new_model(Id) :-
(
fresh
fresh,
nb_getval(current_models, [Id])
->
true
;
create_item_id(NewModel),
set_current_models([NewModel]),
create_item_id(Id),
add_item(top, model, new_model, new_model, Id),
set_current_models([Id]),
assertz(fresh)
).
list_models :-
list_items([model: top, kind: model]).
select_model(NameSet) :-
biocham_command,
type(NameSet, {name}),
doc('selects some models.'),
findall(
Id,
(
member(Name, NameSet),
find_item([model: top, key: Name, id: Id])
),
CurrentModels
),
set_current_models(CurrentModels).
clear_model :-
biocham_command,
doc('clears the current model.'),
......@@ -153,8 +196,7 @@ set_model_name(Name) :-
type(Name, name),
doc('changes the current model name.'),
single_model(Model),
retractall(name(Model, _)),
assertz(name(Model, Name)).
replace_item(Model, model, Name, Name).
not_fresh :-
......@@ -172,9 +214,15 @@ add_item(Kind, Item) :-
add_item(Kind, Key, Item) :-
add_item(Kind, Key, Item, _Id).
add_item(Kind, Key, Item, Id) :-
single_model(Model),
not_fresh,
create_item_id(Id),
add_item(Model, Kind, Key, Item, Id).
add_item(Model, Kind, Key, Item, Id) :-
(
list(Key)
->
......@@ -223,14 +271,8 @@ item(Options) :-
(
var(Models)
->
(
var(Model)
->
nb_getval(current_models, Models),
member(Model, Models)
;
Models = [Model]
)
nb_getval(current_models, Models),
member(Model, Models)
;
Models = current_models
->
......@@ -251,6 +293,14 @@ item(Options) :-
),
item(Id, ModelId, Kind, Item).
find_item(Options) :-
(
item(Options)
->
true
;
throw(error(unknown_item))
).
all_items(Options, Items) :-
findall(
......@@ -281,9 +331,17 @@ list_items(Options) :-
delete_item(Id) :-
retractall(key(_Key, Id)),
retract(item(Id, _Model, _Kind, _Item)).
delete_item(Id, _Model).
delete_item(Id, Model) :-
retract(item(Id, Model, _Kind, _Item)),
retractall(key(_Key, Id)).
replace_item(Id, Kind, Key, Item) :-
delete_item(Id, Model),
add_item(Model, Kind, Key, Item, Id).
create_item_id(Id) :-
count(item_id, Id).
......@@ -9,7 +9,7 @@
numerical_simulation :-
biocham_command,
doc('performs a numerical simulation up to 20s.'),
numerical_simulation(20 * s).
numerical_simulation(20).
numerical_simulation(Time) :-
biocham_command,
......@@ -41,7 +41,7 @@ solve :-
time_final: 100
],
solve(Options, Table),
add_trace(Table).
add_trace('numerical_simulation', Table).
gather_equations(Equations) :-
......
:- module(
objects,
[
input_file/1,
concentration/1,
name/1,
parameter/1,
......@@ -12,13 +11,6 @@
).
:- grammar(input_file).
input_file(Name) :-
atom(Name).
:- grammar(concentration).
......
:- module(
parameters,
[
set_parameter/2,
set_parameter/1,
get_parameter_value/2,
show_parameter/1,
list_parameters/0,
delete_parameter/1
]
).
set_parameter(Parameter, Value) :-
biocham_command,
type(Parameter, parameter),
type(Value, float),
doc('sets the value of \\argument{Parameter} to \\argument{Value}.'),
(
find_parameter(Parameter, Id, _Value)
->
delete_item(Id)
;
true
),
add_item(parameter, parameter(Parameter, Value)).
set_parameter(Parameter) :-
set_parameter(ParameterList) :-
biocham_command(*),
type(ParameterList, '*'(parameter = number)),
doc('sets the value of parameters.'),
\+ (
member(Parameter = Value, ParameterList),
\+ (
catch(
(
find_item([kind: parameter, key: Parameter, id: Id]),
replace_item(Id, parameter, Parameter, parameter(Parameter = Value))
),
error(unknown_item),
add_item(parameter, Parameter, parameter(Parameter = Value))
)
)
).
get_parameter_value(Parameter, Value) :-
find_item([
model: current_model,
kind: parameter,
key: Parameter,
item: parameter(_, Value)
]).
show_parameter(Parameter) :-
biocham_command,
type(Parameter, parameter),
doc('shows the value of \\argument{Parameter}.'),
find_parameter(Parameter, _Id, Value),
get_parameter_value(Parameter, Value),
write(Value),
nl.
find_parameter(Parameter, Id, Value) :-
(
item(
[model: current_model, kind: macro, id: Id],
parameter(Parameter, Value)
)
->
true
;
throw(error(undefined_parameter(Parameter)))
).
list_parameters :-
biocham_command,
doc('shows the values of all known parameters.'),
list_items([model: current_model, kind: parameter]).
delete_parameter(Parameter) :-
delete_parameter(ParameterSet) :-
biocham_command,
type(Parameter, parameter),
doc('deletes a parameter'),
find_parameter(Parameter, Id, _Value),
delete_item(Id).
type(ParameterSet, {parameter}),
doc('deletes some parameters'),
\+ (
member(Parameter, ParameterSet),
\+ (
find_item([
model: current_model,
kind: parameter,
key: Parameter,
id: Id
]),
delete_item(Id)
)
).
:- use_module(library(plunit)).
:- begin_tests(parameters).
test(
'set_parameter',
[true(Parameters == [parameter(a = 1), parameter(b = 3)])]
) :-
new_model,
command(set_parameter(a = 1, b = 2)),
command(set_parameter(b = 3)),
all_items([model: current_model, kind: parameter], Parameters).
:- end_tests(parameters).
......@@ -12,9 +12,9 @@
- gsl.pl
- gsl.plt
- counters.pl
- filename.pl
** About this manual
- doc.pl
- filename.pl
* Syntax of Biocham Reaction Systems
** Biochemical objects
- objects.pl
......@@ -47,8 +47,10 @@
** Listing and declaring parameters, macros, aliases and invariants
*** Parameters
- parameters.pl
- parameters.plt
*** Macros
- macros.pl
- macros.plt
*** Aliases
- aliases.pl
- aliases.plt
......@@ -58,6 +60,9 @@
*** ODE and stochastic simulations
- numerical_simulation.pl
- numerical_simulation.plt
*** Traces
- traces.pl
- traces.plt
* Index
+ index
* Bibliography
......
......@@ -113,9 +113,18 @@ check_types(Command, ArgumentTypes, NewCommand) :-
command(Command) :-
functor(Command, Functor, Arity),
(
predicate_info(Functor/Arity, ArgumentTypes, yes, _)
(
predicate_info(Functor/Arity, ArgumentTypes, yes, _)
->
Command0 = Command
;
predicate_info(Functor/1, ArgumentTypes, variantargs, _)
->
Command =.. [Functor | Arguments],
Command0 =.. [Functor, Arguments]
)
->
check_types(Command, ArgumentTypes, NewCommand),
check_types(Command0, ArgumentTypes, NewCommand),
NewCommand
;
rule(Command)
......
:- module(
traces,
[
add_trace/2,
load_trace/1,
save_trace/1,
select_trace/1,
rename_trace/1,
list_traces/0,
delete_trace/1,
get_trace_data/1
]
).
add_trace(Name, Data) :-
add_item(trace, Name, Name, Id),
add_item(tracedata, Id, Data),
nb_setval(current_trace, Id).
load_trace(InputFile) :-
biocham_command,
type(InputFile, input_file),
doc('
loads the given \\texttt{.csv} file as a trace.'),
\+ (
filename(InputFile, Filename),
\+ (
automatic_suffix(Filename, '.csv', FilenameCsv),
csv_read_file(FilenameCsv, Data),
add_trace(FilenameCsv, Data)
)
).
save_trace(OutputFile) :-
biocham_command,
type(OutputFile, output_file),
doc('
saves the current trace into a \\texttt{.csv} file.'),
nb_getval(current_trace, Id),
find_item([key: Id, item: Data]),
once(filename(OutputFile, Filename)),
automatic_suffix(Filename, '.csv', FilenameCsv),
csv_write_file(FilenameCsv, Data).
list_traces :-
biocham_command,
doc('lists the current set of traces.'),
list_items([model: current_model, kind: trace]).
select_trace(Trace) :-
biocham_command,
type(Trace, trace),
doc('selects \\argument{Trace} to be the current trace.'),
find_item([key: Trace, id: Id]),
nb_setval(current_trace, Id).
rename_trace(Name) :-
biocham_command,
type(Name, name),
doc('renames the current trace.'),
nb_getval(current_trace, Id),
replace_item(Id, trace, Name, Name).
delete_trace(TraceSet) :-
biocham_command,
type(TraceSet, {trace}),
doc('deletes some traces.'),
\+ (
member(Trace, TraceSet),
\+ (
find_item([kind: trace, key: Trace, id: Id]),
delete_item(Id),
find_item([kind: tracedata, key: Id, id: TraceId]),
delete_item(TraceId)
)
).
get_trace_data(Data) :-
nb_getval(current_trace, Id),
find_item([kind: tracedata, key: Id, item: Data]).
:- use_module(library(plunit)).
:- begin_tests(traces).
test(
'add_trace',
[true(Traces == [trace])]
) :-
new_model,
add_trace(trace, [row(50, 50)]),
all_items([model: current_model, kind: trace], Traces).
test(
'get_trace_data',
[true(Data == [row(50, 50)])]
) :-
new_model,
add_trace(trace, [row(50, 50)]),
get_trace_data(Data).
:- end_tests(traces).
......@@ -29,6 +29,13 @@ collect_info(
BiochamCommand = yes,
collect_info(Body, Arguments, ArgumentTypes, _, Doc).
collect_info(
(biocham_command(*), Body), Arguments, ArgumentTypes, BiochamCommand, Doc
) :-
!,
BiochamCommand = variantargs,
collect_info(Body, Arguments, ArgumentTypes, _, Doc).
collect_info(
(type(Argument, Type), Body), Arguments, ArgumentTypes, BiochamCommand, Doc
) :-
......@@ -56,6 +63,9 @@ check_type(number, Number, NewNumber) :-
!,
check_number(Number, NewNumber).
check_type(term, Term, Term) :-
!.
check_type(integer, Number, NewNumber) :-
!,
check_number(Number, NewNumber),
......@@ -66,18 +76,34 @@ check_type('='(SubType), Equals, NewList) :-
equals_to_list(Equals, List),
check_type([SubType], List, NewList).
check_type('='(LeftType, RightType), Equal, NewLeft = NewRight) :-
!,