Commit 3f81cc05 authored by Thierry Martinez's avatar Thierry Martinez

grammar_map

parent e31f2d30
......@@ -179,6 +179,13 @@ gather_initial_parameter_values(InitialParameterValues) :-
).
gather_events(Events) :-
% findall(
% Event,
% (
% item([kind: event; item: event(Condition, Parameter = Value)]),
Events = [].
:- dynamic(variable/2).
......@@ -202,6 +209,8 @@ enumerate_variables :-
convert_ode :-
print(convert_ode),
nl,
retractall(equation(_, _)),
set_counter(parameter_counter, 0),
\+ (
......@@ -211,18 +220,28 @@ convert_ode :-
convert_expression(Expression, IndexedExpression),
assertz(equation(VariableIndex, IndexedExpression))
)
),
print(end_convert_ode),
nl.
convert_condition(Expression, IndexedExpression) :-
grammar_map(
condition,
[
condition: (numerical_simulation:convert_condition),
arithmetic_expression: (numerical_simulation:convert_expression)
],
Expression,
IndexedExpression
).
convert_expression(Value, Value) :-
number(Value),
!.
convert_expression(Molecule, [VariableIndex]) :-
convert_name(Molecule, [VariableIndex]) :-
variable(Molecule, VariableIndex),
!.
convert_expression(Parameter, p(ParameterIndex)) :-
convert_name(Parameter, p(ParameterIndex)) :-
atom(Parameter),
!,
(
......@@ -234,58 +253,15 @@ convert_expression(Parameter, p(ParameterIndex)) :-
assertz(parameter(Parameter, ParameterIndex))
).
convert_expression(- A, - AIndexed) :-
!,
convert_expression(A, AIndexed).
convert_expression([A], - AIndexed) :-
!,
convert_expression(A, AIndexed).
convert_expression(A + B, AIndexed + BIndexed) :-
!,
convert_expression(A, AIndexed),
convert_expression(B, BIndexed).
convert_expression(A - B, AIndexed - BIndexed) :-
!,
convert_expression(A, AIndexed),
convert_expression(B, BIndexed).
convert_expression(A * B, AIndexed * BIndexed) :-
!,
convert_expression(A, AIndexed),
convert_expression(B, BIndexed).
convert_expression(A / B, AIndexed / BIndexed) :-
!,
convert_expression(A, AIndexed),
convert_expression(B, BIndexed).
convert_expression(A ^ B, AIndexed ^ BIndexed) :-
!,
convert_expression(A, AIndexed),
convert_expression(B, BIndexed).
convert_expression(sqrt(A), sqrt(AIndexed)) :-
!,
convert_expression(A, AIndexed).
convert_expression(exp(A), exp(AIndexed)) :-
!,
convert_expression(A, AIndexed).
convert_expression(log(A), log(AIndexed)) :-
!,
convert_expression(A, AIndexed).
convert_expression(cos(A), cos(AIndexed)) :-
!,
convert_expression(A, AIndexed).
convert_expression(sin(A), sin(AIndexed)) :-
!,
convert_expression(A, AIndexed).
convert_expression(E, _Result) :-
type_error(arithmetic_expression, E).
convert_expression(Expression, IndexedExpression) :-
grammar_map(
arithmetic_expression,
[
number: (=),
name: (numerical_simulation:convert_name),
arithmetic_expression: (numerical_simulation:convert_expression)
],
Expression,
IndexedExpression
).
......@@ -172,6 +172,11 @@ condition(not Condition) :-
arithmetic_expression([A]) :-
arithmetic_expression(A).
arithmetic_expression(+ ArithmeticExpression0) :-
arithmetic_expression(ArithmeticExpression0).
arithmetic_expression(- ArithmeticExpression0) :-
arithmetic_expression(ArithmeticExpression0).
arithmetic_expression(ArithmeticExpression0 + ArithmeticExpression1) :-
arithmetic_expression(ArithmeticExpression0),
......
......@@ -21,6 +21,7 @@
term_morphism/3,
substitute/4,
rewrite/3,
grammar_map/4,
call_subprocess/3,
clean/1,
check_cleaned/1
......@@ -243,6 +244,44 @@ rewrite(System, In, Out) :-
).
grammar_map(Grammar, Rules, In, Out) :-
ProtoHead =.. [Grammar, ProtoArg],
(
clause(ProtoHead, ProtoBody),
copy_term((ProtoArg, ProtoBody), (In, BodyIn)),
BodyIn
->
true
;
throw(error(unexpected_term(In, Grammar), grammar_map))
),
!,
Out = ProtoArg,
BodyOut = ProtoBody,
grammar_map_aux(BodyIn, BodyOut, Rules).
grammar_map_aux(true, true, _Rules) :-
!.
grammar_map_aux((AIn, BIn), (AOut, BOut), Rules) :-
!,
grammar_map_aux(AIn, AOut, Rules),
grammar_map_aux(BIn, BOut, Rules).
grammar_map_aux(PIn, POut, Rules) :-
PIn =.. [F, ArgumentIn],
POut =.. [F, ArgumentOut],
(
memberchk((F: P), Rules)
->
true
;
throw(error(unexpected_predicate(F, ArgumentIn), grammar_map))
),
call(P, ArgumentIn, ArgumentOut).
call_subprocess(ExecutableFilename, Arguments, Options) :-
absolute_file_name(ExecutableFilename, Absolute),
process_create(Absolute, Arguments, [process(Pid) | Options]),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment